ACE_Array_Base< T > Class Template Reference

Implement a simple dynamic array. More...

#include <Array_Base.h>

Inheritance diagram for ACE_Array_Base< T >:

Inheritance graph
[legend]
Collaboration diagram for ACE_Array_Base< T >:

Collaboration graph
[legend]
List of all members.

Public Types

typedef T TYPE
typedef ACE_Array_Iterator<
T > 
ITERATOR
typedef T value_type
typedef value_typeiterator
typedef value_type const * const_iterator
typedef value_typereference
typedef value_type const & const_reference
typedef value_typepointer
typedef value_type const * const_pointer
typedef ptrdiff_t difference_type
typedef ACE_Allocator::size_type size_type

Public Member Functions

ACE_DECLARE_STL_REVERSE_ITERATORS ACE_Array_Base (size_type size=0, ACE_Allocator *the_allocator=0)
 Dynamically create an uninitialized array.

 ACE_Array_Base (size_type size, T const &default_value, ACE_Allocator *the_allocator=0)
 Dynamically initialize the entire array to the .

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

T & operator[] (size_type slot)
T const & operator[] (size_type slot) const
int set (T const &new_item, size_type slot)
int get (T &item, size_type slot) const
size_type size (void) const
 Returns the of the array.

int size (size_type new_size)
size_type max_size (void) const
 Returns the of the array.

int max_size (size_type new_size)
void swap (ACE_Array_Base< T > &array)
Forward Iterator Accessors
Forward iterator accessors.

iterator begin (void)
iterator end (void)
const_iterator begin (void) const
const_iterator end (void) const
Reverse Iterator Accessors
Reverse iterator accessors.

reverse_iterator rbegin (void)
reverse_iterator rend (void)
const_reverse_iterator rbegin (void) const
const_reverse_iterator rend (void) const

Protected Member Functions

bool in_range (size_type slot) const

Protected Attributes

size_type max_size_
size_type cur_size_
value_typearray_
 Pointer to the array's storage buffer.

ACE_Allocatorallocator_
 Allocation strategy of the ACE_Array_Base.


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 43 of file Array_Base.h.


Member Typedef Documentation

template<class T>
typedef value_type const* ACE_Array_Base< T >::const_iterator
 

Definition at line 54 of file Array_Base.h.

template<class T>
typedef value_type const* ACE_Array_Base< T >::const_pointer
 

Definition at line 58 of file Array_Base.h.

template<class T>
typedef value_type const& ACE_Array_Base< T >::const_reference
 

Definition at line 56 of file Array_Base.h.

template<class T>
typedef ptrdiff_t ACE_Array_Base< T >::difference_type
 

Definition at line 59 of file Array_Base.h.

template<class T>
typedef value_type* ACE_Array_Base< T >::iterator
 

Definition at line 53 of file Array_Base.h.

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

Reimplemented in ACE_Array< T >, ACE_Array< ACE_Get_Opt_Long_Option * >, and ACE_Array< ACE_INET_Addr >.

Definition at line 49 of file Array_Base.h.

template<class T>
typedef value_type* ACE_Array_Base< T >::pointer
 

Definition at line 57 of file Array_Base.h.

template<class T>
typedef value_type& ACE_Array_Base< T >::reference
 

Definition at line 55 of file Array_Base.h.

template<class T>
typedef ACE_Allocator::size_type ACE_Array_Base< T >::size_type
 

Definition at line 60 of file Array_Base.h.

Referenced by ACE_Array_Base< T >::ACE_Array_Base(), ACE_SString::ACE_SString(), ACE_SString::find(), ACE_Array_Base< T >::get(), ACE_Array_Base< T >::in_range(), ACE_Array_Base< T >::max_size(), ACE_Array_Base< T >::operator=(), ACE_SString::operator[](), ACE_Array_Base< T >::operator[](), ACE_SString::rfind(), ACE_Array_Base< T >::set(), ACE_Array_Base< T >::size(), ACE_SString::substr(), and ACE_SString::substring().

template<class T>
typedef T ACE_Array_Base< T >::TYPE
 

Reimplemented in ACE_Array< T >, ACE_Array< ACE_Get_Opt_Long_Option * >, and ACE_Array< ACE_INET_Addr >.

Definition at line 48 of file Array_Base.h.

template<class T>
typedef T ACE_Array_Base< T >::value_type
 

Definition at line 52 of file Array_Base.h.


Constructor & Destructor Documentation

template<class T>
ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_Array_Base< T >::ACE_Array_Base size_type  size = 0,
ACE_Allocator the_allocator = 0
 

Dynamically create an uninitialized array.

Definition at line 25 of file Array_Base.cpp.

References ACE_ALLOCATOR, ACE_Array_Base< T >::array_, ACE_Allocator::instance(), and ACE_Array_Base< T >::size_type.

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

template<class T>
ACE_Array_Base< T >::ACE_Array_Base size_type  size,
T const &  default_value,
ACE_Allocator the_allocator = 0
 

Dynamically initialize the entire array to the .

Definition at line 46 of file Array_Base.cpp.

References ACE_ALLOCATOR, ACE_Array_Base< T >::array_, ACE_Allocator::instance(), and ACE_Array_Base< T >::size_type.

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

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

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

Definition at line 70 of file Array_Base.cpp.

References ACE_ALLOCATOR, ACE_Array_Base< T >::array_, ACE_Allocator::instance(), ACE_Array_Base< T >::size(), and ACE_Array_Base< T >::size_type.

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

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

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

Definition at line 9 of file Array_Base.inl.

References ACE_DES_ARRAY_FREE.

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


Member Function Documentation

template<class T>
ACE_INLINE ACE_Array_Base< T >::const_iterator ACE_Array_Base< T >::begin void   )  const
 

Definition at line 33 of file Array_Base.inl.

References ACE_Array_Base< T >::array_.

00034 {
00035   return this->array_;
00036 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::iterator ACE_Array_Base< T >::begin void   ) 
 

Definition at line 19 of file Array_Base.inl.

References ACE_Array_Base< T >::array_.

00020 {
00021   return this->array_;
00022 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::const_iterator ACE_Array_Base< T >::end void   )  const
 

Definition at line 40 of file Array_Base.inl.

References ACE_Array_Base< T >::array_, and ACE_Array_Base< T >::cur_size_.

00041 {
00042   return this->array_ + this->cur_size_;
00043 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::iterator ACE_Array_Base< T >::end void   ) 
 

Definition at line 26 of file Array_Base.inl.

References ACE_Array_Base< T >::array_, and ACE_Array_Base< T >::cur_size_.

00027 {
00028   return this->array_ + this->cur_size_;
00029 }

template<class T>
int ACE_Array_Base< T >::get T &  item,
size_type  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 149 of file Array_Base.cpp.

References ACE_Array_Base< T >::array_, ACE_Array_Base< T >::in_range(), and ACE_Array_Base< T >::size_type.

00151 {
00152   if (this->in_range (slot))
00153     {
00154       // Copies the item.  If you don't want to copy, use operator []
00155       // instead (but then you'll be responsible for range checking).
00156       item = this->array_[slot];
00157       return 0;
00158     }
00159   else
00160     return -1;
00161 }

template<class T>
ACE_INLINE bool ACE_Array_Base< T >::in_range size_type  slot  )  const [protected]
 

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

Definition at line 86 of file Array_Base.inl.

References ACE_Array_Base< T >::cur_size_, and ACE_Array_Base< T >::size_type.

Referenced by ACE_Array_Base< T >::get(), and ACE_Array_Base< T >::set().

00087 {
00088   return index < this->cur_size_;
00089 }

template<class T>
int ACE_Array_Base< T >::max_size size_type  new_size  ) 
 

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

Definition at line 164 of file Array_Base.cpp.

References ACE_ALLOCATOR_RETURN, ACE_DES_ARRAY_FREE, ACE_Array_Base< T >::array_, ACE_Array_Base< T >::cur_size_, ACE_Array_Base< T >::max_size_, and ACE_Array_Base< T >::size_type.

00165 {
00166   if (new_size > this->max_size_)
00167     {
00168       T *tmp = 0;
00169 
00170       ACE_ALLOCATOR_RETURN (tmp,
00171                             (T *) this->allocator_->malloc (new_size * sizeof (T)),
00172                             -1);
00173       for (size_type i = 0; i < this->cur_size_; ++i)
00174         new (&tmp[i]) T (this->array_[i]);
00175 
00176       // Initialize the new portion of the array that exceeds the
00177       // previously allocated section.
00178       for (size_type j = this->cur_size_; j < new_size; ++j)
00179         new (&tmp[j]) T;
00180 
00181       ACE_DES_ARRAY_FREE (this->array_,
00182                           this->max_size_,
00183                           this->allocator_->free,
00184                           T);
00185       this->array_ = tmp;
00186       this->max_size_ = new_size;
00187       this->cur_size_ = new_size;
00188     }
00189 
00190   return 0;
00191 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::size_type ACE_Array_Base< T >::max_size void   )  const
 

Returns the of the array.

Definition at line 80 of file Array_Base.inl.

References ACE_Array_Base< T >::max_size_.

Referenced by ACE_Vector< T, DEFAULT_SIZE >::ACE_Vector(), ACE_DLL_Handle::get_dll_names(), ACE_DLL_Handle::open(), ACE_Vector< T, DEFAULT_SIZE >::push_back(), ACE_Vector< T, DEFAULT_SIZE >::resize(), and ACE_Array_Base< T >::size().

00081 {
00082   return this->max_size_;
00083 }

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

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

Definition at line 87 of file Array_Base.cpp.

References ACE_DES_ARRAY_NOFREE, ACE_Array_Base< T >::array_, ACE_Array_Base< T >::cur_size_, ACE_Array_Base< T >::max_size_, ACE_Array_Base< T >::size(), ACE_Array_Base< T >::size_type, and ACE_Array_Base< T >::swap().

Referenced by ACE_Array< T >::operator=().

00088 {
00089   // Check for "self-assignment".
00090 
00091   if (this != &s)
00092     {
00093       if (this->max_size_ < s.size ())
00094         {
00095           // Need to reallocate memory.
00096 
00097           // Strongly exception-safe assignment.
00098           //
00099           // Note that we're swapping the allocators here, too.
00100           // Should we?  Probably.  "*this" should be a duplicate of
00101           // the "right hand side".
00102           ACE_Array_Base<T> tmp (s);
00103           this->swap (tmp);
00104         }
00105       else
00106         {
00107           // Underlying array is large enough.  No need to reallocate
00108           // memory.
00109           //
00110           // "*this" still owns the memory for the underlying array.
00111           // Do not swap out the allocator.
00112           //
00113           // @@ Why don't we just drop the explicit destructor and
00114           //    placement operator new() calls with a straight
00115           //    element-by-element assignment?  Is the existing
00116           //    approach more efficient?
00117           //        -Ossama
00118 
00119           ACE_DES_ARRAY_NOFREE (this->array_,
00120                                 s.size (),
00121                                 T);
00122 
00123           this->cur_size_ = s.size ();
00124 
00125           for (size_type i = 0; i < this->size (); ++i)
00126             new (&this->array_[i]) T (s.array_[i]);
00127         }
00128     }
00129 }

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

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

Definition at line 98 of file Array_Base.inl.

References ACE_Array_Base< T >::array_, and ACE_Array_Base< T >::size_type.

00099 {
00100   return this->array_[index];
00101 }

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

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

Definition at line 92 of file Array_Base.inl.

References ACE_Array_Base< T >::array_, and ACE_Array_Base< T >::size_type.

00093 {
00094   return this->array_[index];
00095 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::const_reverse_iterator ACE_Array_Base< T >::rbegin void   )  const
 

Definition at line 61 of file Array_Base.inl.

00062 {
00063   return const_reverse_iterator (this->end ());
00064 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::reverse_iterator ACE_Array_Base< T >::rbegin void   ) 
 

Definition at line 47 of file Array_Base.inl.

00048 {
00049   return reverse_iterator (this->end ());
00050 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::const_reverse_iterator ACE_Array_Base< T >::rend void   )  const
 

Definition at line 68 of file Array_Base.inl.

00069 {
00070   return const_reverse_iterator (this->begin ());
00071 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::reverse_iterator ACE_Array_Base< T >::rend void   ) 
 

Definition at line 54 of file Array_Base.inl.

00055 {
00056   return reverse_iterator (this->begin ());
00057 }

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

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

Definition at line 134 of file Array_Base.cpp.

References ACE_Array_Base< T >::array_, ACE_Array_Base< T >::in_range(), and ACE_Array_Base< T >::size_type.

Referenced by ACE_DLL_Handle::get_dll_names(), and ACE_DLL_Handle::open().

00136 {
00137   if (this->in_range (slot))
00138     {
00139       this->array_[slot] = new_item;
00140       return 0;
00141     }
00142   else
00143     return -1;
00144 }

template<class T>
int ACE_Array_Base< T >::size size_type  new_size  ) 
 

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

Definition at line 194 of file Array_Base.cpp.

References ACE_Array_Base< T >::cur_size_, ACE_Array_Base< T >::max_size(), and ACE_Array_Base< T >::size_type.

00195 {
00196   int const r = this->max_size (new_size);
00197 
00198   if (r == 0)
00199     this->cur_size_ = new_size;
00200 
00201   return r;
00202 }

template<class T>
ACE_INLINE ACE_Array_Base< T >::size_type ACE_Array_Base< T >::size void   )  const
 

Returns the of the array.

Reimplemented in ACE_Vector< T, DEFAULT_SIZE >.

Definition at line 74 of file Array_Base.inl.

References ACE_Array_Base< T >::cur_size_.

Referenced by ACE_Array_Base< T >::ACE_Array_Base(), ACE_DLL_Handle::get_dll_names(), ACE_DLL_Handle::open(), ACE_Array_Base< T >::operator=(), ACE_Array< T >::operator==(), ACE_Vector< T, DEFAULT_SIZE >::push_back(), and ACE_Vector< T, DEFAULT_SIZE >::resize().

00075 {
00076   return this->cur_size_;
00077 }

template<class T>
void ACE_Array_Base< T >::swap ACE_Array_Base< T > &  array  ) 
 

Swap the contents of this array with the given array in an exception-safe manner.

Definition at line 206 of file Array_Base.cpp.

References ACE_Array_Base< T >::allocator_, ACE_Array_Base< T >::array_, ACE_Array_Base< T >::cur_size_, and ACE_Array_Base< T >::max_size_.

Referenced by ACE_Array_Base< T >::operator=(), and ACE_Vector< T, DEFAULT_SIZE >::swap().

00207 {
00208   std::swap (this->max_size_ , rhs.max_size_);
00209   std::swap (this->cur_size_ , rhs.cur_size_);
00210   std::swap (this->array_    , rhs.array_);
00211   std::swap (this->allocator_, rhs.allocator_);
00212 }


Friends And Related Function Documentation

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

Definition at line 191 of file Array_Base.h.


Member Data Documentation

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

Allocation strategy of the ACE_Array_Base.

Definition at line 189 of file Array_Base.h.

Referenced by ACE_Array_Base< T >::swap().

template<class T>
value_type* ACE_Array_Base< T >::array_ [protected]
 

Pointer to the array's storage buffer.

Definition at line 186 of file Array_Base.h.

Referenced by ACE_Array_Base< T >::ACE_Array_Base(), ACE_Array_Base< T >::begin(), ACE_Array_Base< T >::end(), ACE_Array_Base< T >::get(), ACE_Array_Base< T >::max_size(), ACE_Array_Base< T >::operator=(), ACE_Array_Base< T >::operator[](), ACE_Array_Base< T >::set(), and ACE_Array_Base< T >::swap().

template<class T>
size_type ACE_Array_Base< T >::cur_size_ [protected]
 

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

Definition at line 183 of file Array_Base.h.

Referenced by ACE_Array_Base< T >::end(), ACE_Array_Base< T >::in_range(), ACE_Array_Base< T >::max_size(), ACE_Array_Base< T >::operator=(), ACE_Array_Base< T >::size(), and ACE_Array_Base< T >::swap().

template<class T>
size_type ACE_Array_Base< T >::max_size_ [protected]
 

Maximum size of the array, i.e., the total number of elements in .

Definition at line 174 of file Array_Base.h.

Referenced by ACE_Array_Base< T >::max_size(), ACE_Array_Base< T >::operator=(), and ACE_Array_Base< T >::swap().


The documentation for this class was generated from the following files:
Generated on Sun Jan 27 12:54:00 2008 for ACE by doxygen 1.3.6