00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Array_Base.h 00006 * 00007 * $Id: Array_Base.h 80826 2008-03-04 14:51:23Z wotte $ 00008 * 00009 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_ARRAY_BASE_H 00014 #define ACE_ARRAY_BASE_H 00015 00016 #include /**/ "ace/pre.h" 00017 00018 #include /**/ "ace/config-all.h" 00019 00020 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00021 # pragma once 00022 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00023 00024 #include "ace/Global_Macros.h" 00025 #include "ace/Malloc_Base.h" 00026 #include <iterator> /* For reverse_iterator adapters */ 00027 00028 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00029 00030 // Forward declaration. 00031 template <class T> class ACE_Array_Iterator; 00032 00033 /** 00034 * @class ACE_Array_Base 00035 * 00036 * @brief Implement a simple dynamic array 00037 * 00038 * This parametric class implements a simple dynamic array; 00039 * resizing must be controlled by the user. No comparison or find 00040 * operations are implemented. 00041 */ 00042 template<class T> 00043 class ACE_Array_Base 00044 { 00045 public: 00046 00047 // Old/ACE-style traits. 00048 typedef T TYPE; 00049 typedef ACE_Array_Iterator<T> ITERATOR; 00050 00051 // STL-style typedefs/traits. 00052 typedef T value_type; 00053 typedef value_type * iterator; 00054 typedef value_type const * const_iterator; 00055 typedef value_type & reference; 00056 typedef value_type const & const_reference; 00057 typedef value_type * pointer; 00058 typedef value_type const * const_pointer; 00059 typedef ptrdiff_t difference_type; 00060 typedef ACE_Allocator::size_type size_type; 00061 00062 ACE_DECLARE_STL_REVERSE_ITERATORS 00063 00064 // = Initialization and termination methods. 00065 00066 /// Dynamically create an uninitialized array. 00067 ACE_Array_Base (size_type size = 0, 00068 ACE_Allocator * the_allocator = 0); 00069 00070 /// Dynamically initialize the entire array to the <default_value>. 00071 ACE_Array_Base (size_type size, 00072 T const & default_value, 00073 ACE_Allocator * the_allocator = 0); 00074 00075 /** 00076 * The copy constructor performs initialization by making an exact 00077 * copy of the contents of parameter <s>, i.e., *this == s will 00078 * return true. 00079 */ 00080 ACE_Array_Base (ACE_Array_Base<T> const & s); 00081 00082 /** 00083 * Assignment operator performs an assignment by making an exact 00084 * copy of the contents of parameter <s>, i.e., *this == s will 00085 * return true. Note that if the <max_size_> of <array_> is >= than 00086 * <s.max_size_> we can copy it without reallocating. However, if 00087 * <max_size_> is < <s.max_size_> we must delete the <array_>, 00088 * reallocate a new <array_>, and then copy the contents of <s>. 00089 */ 00090 void operator= (ACE_Array_Base<T> const & s); 00091 00092 /// Clean up the array (e.g., delete dynamically allocated memory). 00093 ~ACE_Array_Base (void); 00094 00095 // = Set/get methods. 00096 00097 /// Set item in the array at location @a slot. Doesn't 00098 /// perform range checking. 00099 T & operator[] (size_type slot); 00100 00101 /// Get item in the array at location @a slot. Doesn't 00102 /// perform range checking. 00103 T const & operator[] (size_type slot) const; 00104 00105 /// Set an item in the array at location @a slot. Returns 00106 /// -1 if @a slot is not in range, else returns 0. 00107 int set (T const & new_item, size_type slot); 00108 00109 /** 00110 * Get an item in the array at location @a slot. Returns -1 if 00111 * @a slot is not in range, else returns 0. Note that this function 00112 * copies the item. If you want to avoid the copy, you can use 00113 * the const operator [], but then you'll be responsible for range checking. 00114 */ 00115 int get (T & item, size_type slot) const; 00116 00117 /// Returns the <cur_size_> of the array. 00118 size_type size (void) const; 00119 00120 /** 00121 * Changes the size of the array to match <new_size>. 00122 * It copies the old contents into the new array. 00123 * Return -1 on failure. 00124 */ 00125 int size (size_type new_size); 00126 00127 /// Returns the <max_size_> of the array. 00128 size_type max_size (void) const; 00129 00130 /** 00131 * Changes the size of the array to match <new_size>. 00132 * It copies the old contents into the new array. 00133 * Return -1 on failure. 00134 * It does not affect new_size 00135 */ 00136 int max_size (size_type new_size); 00137 00138 /** 00139 * @name Forward Iterator Accessors 00140 * 00141 * Forward iterator accessors. 00142 */ 00143 //@{ 00144 iterator begin (void); 00145 iterator end (void); 00146 const_iterator begin (void) const; 00147 const_iterator end (void) const; 00148 //@} 00149 00150 /** 00151 * @name Reverse Iterator Accessors 00152 * 00153 * Reverse iterator accessors. 00154 */ 00155 //@{ 00156 reverse_iterator rbegin (void); 00157 reverse_iterator rend (void); 00158 const_reverse_iterator rbegin (void) const; 00159 const_reverse_iterator rend (void) const; 00160 //@} 00161 00162 /// Swap the contents of this array with the given @a array in 00163 /// an exception-safe manner. 00164 void swap (ACE_Array_Base<T> & array); 00165 00166 protected: 00167 00168 /// Returns 1 if @a slot is within range, i.e., 0 >= @a slot < 00169 /// <cur_size_>, else returns 0. 00170 bool in_range (size_type slot) const; 00171 00172 /// Maximum size of the array, i.e., the total number of <T> elements 00173 /// in <array_>. 00174 size_type max_size_; 00175 00176 /** 00177 * Current size of the array. This starts out being == to 00178 * <max_size_>. However, if we are assigned a smaller array, then 00179 * <cur_size_> will become less than <max_size_>. The purpose of 00180 * keeping track of both sizes is to avoid reallocating memory if we 00181 * don't have to. 00182 */ 00183 size_type cur_size_; 00184 00185 /// Pointer to the array's storage buffer. 00186 value_type * array_; 00187 00188 /// Allocation strategy of the ACE_Array_Base. 00189 ACE_Allocator * allocator_; 00190 00191 friend class ACE_Array_Iterator<T>; 00192 }; 00193 00194 // **************************************************************** 00195 00196 /** 00197 * @class ACE_Array_Iterator 00198 * 00199 * @brief Implement an iterator over an ACE_Array. 00200 * 00201 * This iterator is safe in the face of array element deletions. 00202 * But it is NOT safe if the array is resized (via the ACE_Array 00203 * assignment operator) during iteration. That would be very 00204 * odd, and dangerous. 00205 */ 00206 template <class T> 00207 class ACE_Array_Iterator 00208 { 00209 public: 00210 // = Initialization method. 00211 ACE_Array_Iterator (ACE_Array_Base<T> &); 00212 00213 // = Iteration methods. 00214 00215 /// Pass back the <next_item> that hasn't been seen in the Array. 00216 /// Returns 0 when all items have been seen, else 1. 00217 int next (T *&next_item); 00218 00219 /// Move forward by one element in the Array. Returns 0 when all the 00220 /// items in the Array have been seen, else 1. 00221 int advance (void); 00222 00223 /// Returns 1 when all items have been seen, else 0. 00224 int done (void) const; 00225 00226 /// Dump the state of an object. 00227 void dump (void) const; 00228 00229 /// Declare the dynamic allocation hooks. 00230 ACE_ALLOC_HOOK_DECLARE; 00231 00232 private: 00233 /// Pointer to the current item in the iteration. 00234 size_t current_; 00235 00236 /// Pointer to the Array we're iterating over. 00237 ACE_Array_Base<T> &array_; 00238 }; 00239 00240 ACE_END_VERSIONED_NAMESPACE_DECL 00241 00242 #if defined (__ACE_INLINE__) 00243 #include "ace/Array_Base.inl" 00244 #endif /* __ACE_INLINE__ */ 00245 00246 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00247 #include "ace/Array_Base.cpp" 00248 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00249 00250 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00251 #pragma implementation ("Array_Base.cpp") 00252 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00253 00254 #include /**/ "ace/post.h" 00255 00256 #endif /* ACE_ARRAY_BASE_H */