Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACE_Array_Base Class Template Reference

Implement a simple dynamic array. More...

#include <Array_Base.h>

Inheritance diagram for ACE_Array_Base:

Inheritance graph
[legend]
Collaboration diagram for ACE_Array_Base:

Collaboration graph
[legend]
List of all members.

Public Types

typedef T TYPE
typedef ACE_Array_Iterator<
T > 
ITERATOR

Public Methods

 ACE_Array_Base (size_t size=0, ACE_Allocator *alloc=0)
 Dynamically create an uninitialized array. More...

 ACE_Array_Base (size_t size, const T &default_value, ACE_Allocator *alloc=0)
 Dynamically initialize the entire array to the <default_value>. More...

 ACE_Array_Base (const ACE_Array_Base< T > &s)
void operator= (const ACE_Array_Base< T > &s)
 ~ACE_Array_Base (void)
 Clean up the array (e.g., delete dynamically allocated memory). More...

T & operator[] (size_t slot)
 Set item in the array at location <slot>. Doesn't perform range checking. More...

const T & operator[] (size_t slot) const
 Get item in the array at location <slot>. Doesn't perform range checking. More...

int set (const T &new_item, size_t slot)
 Set an item in the array at location <slot>. Returns -1 if <slot> is not in range, else returns 0. More...

int get (T &item, size_t slot) const
size_t size (void) const
 Returns the <cur_size_> of the array. More...

int size (size_t new_size)
size_t max_size (void) const
 Returns the <max_size_> of the array. More...

int max_size (size_t new_size)

Private Methods

int in_range (size_t slot) const
 Returns 1 if <slot> is within range, i.e., 0 >= <slot> < <cur_size_>, else returns 0. More...


Private Attributes

size_t max_size_
 Maximum size of the array, i.e., the total number of <T> elements in <array_>. More...

size_t cur_size_
T * array_
 Pointer to the array's storage buffer. More...

ACE_Allocatorallocator_
 Allocation strategy of the ACE_Array_Base. More...


Friends

class ACE_Array_Iterator< T >

Detailed Description

template<class T>
class ACE_Array_Base< T >

Implement a simple dynamic array.

This parametric class implements a simple dynamic array; resizing must be controlled by the user. No comparison or find operations are implemented.

Definition at line 41 of file Array_Base.h.


Member Typedef Documentation

template<class T>
typedef ACE_Array_Iterator<T> ACE_Array_Base::ITERATOR
 

Reimplemented in ACE_Array.

Definition at line 47 of file Array_Base.h.

template<class T>
typedef T ACE_Array_Base::TYPE
 

Reimplemented in ACE_Array.

Definition at line 46 of file Array_Base.h.


Constructor & Destructor Documentation

template<class T>
ACE_Array_Base< T >::ACE_Array_Base size_t    size = 0,
ACE_Allocator   alloc = 0
 

Dynamically create an uninitialized array.

Definition at line 23 of file Array_Base.cpp.

References ACE_ALLOCATOR, allocator_, array_, ACE_Allocator::instance, and size.

00025   : max_size_ (size),
00026     cur_size_ (size),
00027     allocator_ (alloc)
00028 {
00029   if (this->allocator_ == 0)
00030     this->allocator_ = ACE_Allocator::instance ();
00031 
00032   if (size != 0)
00033     {
00034       ACE_ALLOCATOR (this->array_,
00035                      (T *) this->allocator_->malloc (size * sizeof (T)));
00036       for (size_t i = 0; i < size; ++i)
00037         new (&array_[i]) T;
00038     }
00039   else
00040     this->array_ = 0;
00041 }

template<class T>
ACE_Array_Base< T >::ACE_Array_Base size_t    size,
const T &    default_value,
ACE_Allocator   alloc = 0
 

Dynamically initialize the entire array to the <default_value>.

Definition at line 44 of file Array_Base.cpp.

References ACE_ALLOCATOR, allocator_, array_, ACE_Allocator::instance, and size.

00047   : max_size_ (size),
00048     cur_size_ (size),
00049     allocator_ (alloc)
00050 {
00051   if (this->allocator_ == 0)
00052     this->allocator_ = ACE_Allocator::instance ();
00053 
00054   if (size != 0)
00055     {
00056       ACE_ALLOCATOR (this->array_,
00057                      (T *) this->allocator_->malloc (size * sizeof (T)));
00058       for (size_t i = 0; i < size; ++i)
00059         new (&array_[i]) T (default_value);
00060     }
00061   else
00062     this->array_ = 0;
00063 }

template<class T>
ACE_Array_Base< T >::ACE_Array_Base const ACE_Array_Base< T > &    s
 

The copy constructor performs initialization by making an exact copy of the contents of parameter <s>, i.e., *this == s will return true.

Definition at line 68 of file Array_Base.cpp.

References ACE_ALLOCATOR, allocator_, array_, ACE_Allocator::instance, and size.

00069   : max_size_ (s.size ()),
00070     cur_size_ (s.size ()),
00071     allocator_ (s.allocator_)
00072 {
00073   if (this->allocator_ == 0)
00074     this->allocator_ = ACE_Allocator::instance ();
00075 
00076   ACE_ALLOCATOR (this->array_,
00077                  (T *) this->allocator_->malloc (s.size () * sizeof (T)));
00078   for (size_t i = 0; i < this->size (); i++)
00079     new (&this->array_[i]) T (s.array_[i]);
00080 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::~ACE_Array_Base void   
 

Clean up the array (e.g., delete dynamically allocated memory).

Definition at line 7 of file Array_Base.inl.

References ACE_DES_ARRAY_FREE.

00008 {
00009   ACE_DES_ARRAY_FREE (this->array_,
00010                       this->max_size_,
00011                       this->allocator_->free,
00012                       T);
00013 }


Member Function Documentation

template<class T>
int ACE_Array_Base< T >::get T &    item,
size_t    slot
const
 

Get an item in the array at location <slot>. Returns -1 if <slot> is not in range, else returns 0. Note that this function copies the item. If you want to avoid the copy, you can use the const operator[], but then you'll be responsible for range checking.

Definition at line 132 of file Array_Base.cpp.

References array_, and in_range.

00133 {
00134   if (this->in_range (slot))
00135     {
00136       // Copies the item.  If you don't want to copy, use operator []
00137       // instead (but then you'll be responsible for range checking).
00138       item = this->array_[slot];
00139       return 0;
00140     }
00141   else
00142     return -1;
00143 }

template<class T>
ACE_INLINE int ACE_Array_Base< T >::in_range size_t    slot const [private]
 

Returns 1 if <slot> is within range, i.e., 0 >= <slot> < <cur_size_>, else returns 0.

Definition at line 28 of file Array_Base.inl.

References cur_size_.

Referenced by get, and set.

00029 {
00030   return index < this->cur_size_;
00031 }

template<class T>
int ACE_Array_Base< T >::max_size size_t    new_size
 

Changes the size of the array to match <new_size>. It copies the old contents into the new array. Return -1 on failure. It does not affect new_size

Definition at line 146 of file Array_Base.cpp.

References ACE_ALLOCATOR_RETURN, ACE_DES_ARRAY_FREE, array_, cur_size_, and max_size_.

00147 {
00148   if (new_size > this->max_size_)
00149     {
00150       T *tmp;
00151 
00152       ACE_ALLOCATOR_RETURN (tmp,
00153                             (T *) this->allocator_->malloc (new_size * sizeof (T)),
00154                             -1);
00155       for (size_t i = 0; i < this->cur_size_; ++i)
00156         new (&tmp[i]) T (this->array_[i]);
00157 
00158       // Initialize the new portion of the array that exceeds the
00159       // previously allocated section.
00160       for (size_t j = this->cur_size_; j < new_size; j++)
00161         new (&tmp[j]) T;
00162 
00163       ACE_DES_ARRAY_FREE (this->array_,
00164                           this->max_size_,
00165                           this->allocator_->free,
00166                           T);
00167       this->array_ = tmp;
00168       this->max_size_ = new_size;
00169       this->cur_size_ = new_size;
00170     }
00171 
00172   return 0;
00173 }

template<class T>
ACE_INLINE size_t ACE_Array_Base< T >::max_size void    const
 

Returns the <max_size_> of the array.

Definition at line 22 of file Array_Base.inl.

References max_size_.

Referenced by ACE_Vector::ACE_Vector, ACE_Vector::push_back, ACE_Vector::resize, and size.

00023 {
00024   return this->max_size_;
00025 }

template<class T>
void ACE_Array_Base< T >::operator= const ACE_Array_Base< T > &    s
 

Assignment operator performs an assignment by making an exact copy of the contents of parameter <s>, i.e., *this == s will return true. Note that if the <max_size_> of <array_> is >= than <s.max_size_> we can copy it without reallocating. However, if <max_size_> is < <s.max_size_> we must delete the <array_>, reallocate a new <array_>, and then copy the contents of <s>.

Definition at line 85 of file Array_Base.cpp.

References ACE_ALLOCATOR, ACE_DES_ARRAY_FREE, ACE_DES_ARRAY_NOFREE, array_, cur_size_, max_size_, and size.

Referenced by ACE_Array::operator=.

00086 {
00087   // Check for "self-assignment".
00088 
00089   if (this != &s)
00090     {
00091       if (this->max_size_ < s.size ())
00092         {
00093           ACE_DES_ARRAY_FREE (this->array_,
00094                               this->max_size_,
00095                               this->allocator_->free,
00096                               T);
00097           ACE_ALLOCATOR (this->array_,
00098                          (T *) this->allocator_->malloc (s.size () * sizeof (T)));
00099           this->max_size_ = s.size ();
00100         }
00101       else
00102         {
00103           ACE_DES_ARRAY_NOFREE (this->array_,
00104                                 s.size (),
00105                                 T);
00106         }
00107 
00108       this->cur_size_ = s.size ();
00109 
00110       for (size_t i = 0; i < this->size (); i++)
00111         new (&this->array_[i]) T (s.array_[i]);
00112     }
00113 }

template<class T>
ACE_INLINE const T & ACE_Array_Base< T >::operator[] size_t    slot const
 

Get item in the array at location <slot>. Doesn't perform range checking.

Definition at line 40 of file Array_Base.inl.

References array_.

00041 {
00042   return this->array_[index];
00043 }

template<class T>
ACE_INLINE T & ACE_Array_Base< T >::operator[] size_t    slot
 

Set item in the array at location <slot>. Doesn't perform range checking.

Definition at line 34 of file Array_Base.inl.

References array_.

00035 {
00036   return this->array_[index];
00037 }

template<class T>
int ACE_Array_Base< T >::set const T &    new_item,
size_t    slot
 

Set an item in the array at location <slot>. Returns -1 if <slot> is not in range, else returns 0.

Definition at line 118 of file Array_Base.cpp.

References array_, and in_range.

00119 {
00120   if (this->in_range (slot))
00121     {
00122       this->array_[slot] = new_item;
00123       return 0;
00124     }
00125   else
00126     return -1;
00127 }

template<class T>
int ACE_Array_Base< T >::size size_t    new_size
 

Changes the size of the array to match <new_size>. It copies the old contents into the new array. Return -1 on failure.

Definition at line 176 of file Array_Base.cpp.

References cur_size_, and max_size.

00177 {
00178   int r = this->max_size (new_size);
00179   if (r != 0)
00180     return r;
00181   this->cur_size_ = new_size;
00182   return 0;
00183 }

template<class T>
ACE_INLINE size_t ACE_Array_Base< T >::size void    const
 

Returns the <cur_size_> of the array.

Reimplemented in ACE_Vector.

Definition at line 16 of file Array_Base.inl.

References cur_size_.

Referenced by ACE_Array_Base, operator=, ACE_Array::operator==, ACE_Vector::push_back, and ACE_Vector::resize.

00017 {
00018   return this->cur_size_;
00019 }


Friends And Related Function Documentation

template<class T>
friend class ACE_Array_Iterator< T > [friend]
 

Definition at line 147 of file Array_Base.h.


Member Data Documentation

template<class T>
ACE_Allocator* ACE_Array_Base::allocator_ [private]
 

Allocation strategy of the ACE_Array_Base.

Definition at line 145 of file Array_Base.h.

Referenced by ACE_Array_Base.

template<class T>
T* ACE_Array_Base::array_ [private]
 

Pointer to the array's storage buffer.

Definition at line 142 of file Array_Base.h.

Referenced by ACE_Array_Base, get, max_size, operator=, operator[], and set.

template<class T>
size_t ACE_Array_Base::cur_size_ [private]
 

Current size of the array. This starts out being == to <max_size_>. However, if we are assigned a smaller array, then <cur_size_> will become less than <max_size_>. The purpose of keeping track of both sizes is to avoid reallocating memory if we don't have to.

Definition at line 139 of file Array_Base.h.

Referenced by in_range, max_size, operator=, and size.

template<class T>
size_t ACE_Array_Base::max_size_ [private]
 

Maximum size of the array, i.e., the total number of <T> elements in <array_>.

Definition at line 130 of file Array_Base.h.

Referenced by max_size, and operator=.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:44:56 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002