Implement a simple dynamic array. More...
#include <Array_Base.h>


Public Types | |
| typedef T | TYPE |
| typedef ACE_Array_Iterator< T > | ITERATOR |
| typedef T | value_type |
| typedef value_type * | iterator |
| typedef value_type const * | const_iterator |
| typedef value_type & | reference |
| typedef value_type const & | const_reference |
| typedef value_type * | pointer |
| 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 <default_value>. | |
| 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 <cur_size_> of the array. | |
| int | size (size_type new_size) |
| size_type | max_size (void) const |
| Returns the <max_size_> of the array. | |
| int | max_size (size_type new_size) |
| void | swap (ACE_Array_Base< T > &array) |
Forward Iterator Accessors | |
| iterator | begin (void) |
| iterator | end (void) |
| const_iterator | begin (void) const |
| const_iterator | end (void) const |
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_type * | array_ |
| Pointer to the array's storage buffer. | |
| ACE_Allocator * | allocator_ |
| Allocation strategy of the ACE_Array_Base. | |
Friends | |
| class | ACE_Array_Iterator< 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.
| typedef value_type const* ACE_Array_Base< T >::const_iterator |
Definition at line 54 of file Array_Base.h.
| typedef value_type const* ACE_Array_Base< T >::const_pointer |
Definition at line 58 of file Array_Base.h.
| typedef value_type const& ACE_Array_Base< T >::const_reference |
Definition at line 56 of file Array_Base.h.
| typedef ptrdiff_t ACE_Array_Base< T >::difference_type |
Definition at line 59 of file Array_Base.h.
| typedef value_type* ACE_Array_Base< T >::iterator |
Definition at line 53 of file Array_Base.h.
| 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.
| typedef value_type* ACE_Array_Base< T >::pointer |
Definition at line 57 of file Array_Base.h.
| typedef value_type& ACE_Array_Base< T >::reference |
Definition at line 55 of file Array_Base.h.
| typedef ACE_Allocator::size_type ACE_Array_Base< T >::size_type |
Definition at line 60 of file Array_Base.h.
| 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.
| typedef T ACE_Array_Base< T >::value_type |
Definition at line 52 of file Array_Base.h.
| ACE_Array_Base< T >::ACE_Array_Base | ( | typename ACE_Array_Base< T >::size_type | size = 0, |
|
| ACE_Allocator * | the_allocator = 0 | |||
| ) | [inline] |
Dynamically create an uninitialized array.
Definition at line 25 of file Array_Base.cpp.
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 }
| ACE_Array_Base< T >::ACE_Array_Base | ( | typename ACE_Array_Base< T >::size_type | size, | |
| T const & | default_value, | |||
| ACE_Allocator * | the_allocator = 0 | |||
| ) | [inline] |
Dynamically initialize the entire array to the <default_value>.
Definition at line 46 of file Array_Base.cpp.
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 }
| ACE_Array_Base< T >::ACE_Array_Base | ( | ACE_Array_Base< T > const & | s | ) | [inline] |
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 70 of file Array_Base.cpp.
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 }
| ACE_Array_Base< T >::~ACE_Array_Base | ( | void | ) | [inline] |
Clean up the array (e.g., delete dynamically allocated memory).
Definition at line 9 of file Array_Base.inl.
00010 { 00011 ACE_DES_ARRAY_FREE (this->array_, 00012 this->max_size_, 00013 this->allocator_->free, 00014 T); 00015 }
| ACE_Array_Base< T >::const_iterator ACE_Array_Base< T >::begin | ( | void | ) | const [inline] |
Definition at line 33 of file Array_Base.inl.
00034 { 00035 return this->array_; 00036 }
| ACE_Array_Base< T >::iterator ACE_Array_Base< T >::begin | ( | void | ) | [inline] |
Definition at line 19 of file Array_Base.inl.
00020 { 00021 return this->array_; 00022 }
| ACE_Array_Base< T >::const_iterator ACE_Array_Base< T >::end | ( | void | ) | const [inline] |
Definition at line 40 of file Array_Base.inl.
| ACE_Array_Base< T >::iterator ACE_Array_Base< T >::end | ( | void | ) | [inline] |
Definition at line 26 of file Array_Base.inl.
| int ACE_Array_Base< T >::get | ( | T & | item, | |
| typename ACE_Array_Base< T >::size_type | slot | |||
| ) | const [inline] |
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.
| bool ACE_Array_Base< T >::in_range | ( | typename ACE_Array_Base< T >::size_type | index | ) | const [inline, protected] |
Returns 1 if slot is within range, i.e., 0 >= slot < cur_size_, else returns 0.
Definition at line 86 of file Array_Base.inl.
00087 { 00088 return index < this->cur_size_; 00089 }
| int ACE_Array_Base< T >::max_size | ( | typename ACE_Array_Base< T >::size_type | new_size | ) | [inline] |
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 164 of file Array_Base.cpp.
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 }
| ACE_Array_Base< T >::size_type ACE_Array_Base< T >::max_size | ( | void | ) | const [inline] |
Returns the <max_size_> of the array.
Definition at line 80 of file Array_Base.inl.
00081 { 00082 return this->max_size_; 00083 }
| void ACE_Array_Base< T >::operator= | ( | ACE_Array_Base< T > const & | s | ) | [inline] |
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>.
Reimplemented in ACE_Array< T >, ACE_Array< ACE_Get_Opt_Long_Option * >, and ACE_Array< ACE_INET_Addr >.
Definition at line 87 of file Array_Base.cpp.
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 }
| const T & ACE_Array_Base< T >::operator[] | ( | typename ACE_Array_Base< T >::size_type | index | ) | const [inline] |
Get item in the array at location slot. Doesn't perform range checking.
Definition at line 98 of file Array_Base.inl.
00099 { 00100 return this->array_[index]; 00101 }
| T & ACE_Array_Base< T >::operator[] | ( | typename ACE_Array_Base< T >::size_type | index | ) | [inline] |
Set item in the array at location slot. Doesn't perform range checking.
Definition at line 92 of file Array_Base.inl.
00093 { 00094 return this->array_[index]; 00095 }
| ACE_Array_Base< T >::const_reverse_iterator ACE_Array_Base< T >::rbegin | ( | void | ) | const [inline] |
Definition at line 61 of file Array_Base.inl.
00062 { 00063 return const_reverse_iterator (this->end ()); 00064 }
| ACE_Array_Base< T >::reverse_iterator ACE_Array_Base< T >::rbegin | ( | void | ) | [inline] |
Definition at line 47 of file Array_Base.inl.
00048 { 00049 return reverse_iterator (this->end ()); 00050 }
| ACE_Array_Base< T >::const_reverse_iterator ACE_Array_Base< T >::rend | ( | void | ) | const [inline] |
Definition at line 68 of file Array_Base.inl.
00069 { 00070 return const_reverse_iterator (this->begin ()); 00071 }
| ACE_Array_Base< T >::reverse_iterator ACE_Array_Base< T >::rend | ( | void | ) | [inline] |
Definition at line 54 of file Array_Base.inl.
00055 { 00056 return reverse_iterator (this->begin ()); 00057 }
| int ACE_Array_Base< T >::set | ( | T const & | new_item, | |
| typename ACE_Array_Base< T >::size_type | slot | |||
| ) | [inline] |
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.
| int ACE_Array_Base< T >::size | ( | typename ACE_Array_Base< T >::size_type | new_size | ) | [inline] |
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 194 of file Array_Base.cpp.
| ACE_Array_Base< T >::size_type ACE_Array_Base< T >::size | ( | void | ) | const [inline] |
Returns the <cur_size_> of the array.
Reimplemented in ACE_Vector< T, DEFAULT_SIZE >.
Definition at line 74 of file Array_Base.inl.
00075 { 00076 return this->cur_size_; 00077 }
| void ACE_Array_Base< T >::swap | ( | ACE_Array_Base< T > & | array | ) | [inline] |
Swap the contents of this array with the given array in an exception-safe manner.
Reimplemented in ACE_Vector< T, DEFAULT_SIZE >.
Definition at line 206 of file Array_Base.cpp.
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 }
friend class ACE_Array_Iterator< T > [friend] |
Definition at line 191 of file Array_Base.h.
ACE_Allocator* ACE_Array_Base< T >::allocator_ [protected] |
Allocation strategy of the ACE_Array_Base.
Definition at line 189 of file Array_Base.h.
value_type* ACE_Array_Base< T >::array_ [protected] |
Pointer to the array's storage buffer.
Definition at line 186 of file Array_Base.h.
size_type ACE_Array_Base< T >::cur_size_ [protected] |
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 183 of file Array_Base.h.
size_type ACE_Array_Base< T >::max_size_ [protected] |
Maximum size of the array, i.e., the total number of T elements in array_.
Definition at line 174 of file Array_Base.h.
1.6.1