00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Array_Base.h 00006 * 00007 * $Id: Array_Base.h,v 1.1.1.2 2003/02/21 18:36:32 chad Exp $ 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/OS.h" 00026 #include "ace/Malloc_Base.h" 00027 00028 // Forward declaration. 00029 template <class T> class ACE_Array_Iterator; 00030 00031 /** 00032 * @class ACE_Array_Base 00033 * 00034 * @brief Implement a simple dynamic array 00035 * 00036 * This parametric class implements a simple dynamic array; 00037 * resizing must be controlled by the user. No comparison or find 00038 * operations are implemented. 00039 */ 00040 template<class T> 00041 class ACE_Array_Base 00042 { 00043 public: 00044 00045 // Define a "trait" 00046 typedef T TYPE; 00047 typedef ACE_Array_Iterator<T> ITERATOR; 00048 00049 // = Initialization and termination methods. 00050 00051 /// Dynamically create an uninitialized array. 00052 ACE_Array_Base (size_t size = 0, 00053 ACE_Allocator *alloc = 0); 00054 00055 /// Dynamically initialize the entire array to the <default_value>. 00056 ACE_Array_Base (size_t size, 00057 const T &default_value, 00058 ACE_Allocator *alloc = 0); 00059 00060 /** 00061 * The copy constructor performs initialization by making an exact 00062 * copy of the contents of parameter <s>, i.e., *this == s will 00063 * return true. 00064 */ 00065 ACE_Array_Base (const ACE_Array_Base<T> &s); 00066 00067 /** 00068 * Assignment operator performs an assignment by making an exact 00069 * copy of the contents of parameter <s>, i.e., *this == s will 00070 * return true. Note that if the <max_size_> of <array_> is >= than 00071 * <s.max_size_> we can copy it without reallocating. However, if 00072 * <max_size_> is < <s.max_size_> we must delete the <array_>, 00073 * reallocate a new <array_>, and then copy the contents of <s>. 00074 */ 00075 void operator= (const ACE_Array_Base<T> &s); 00076 00077 /// Clean up the array (e.g., delete dynamically allocated memory). 00078 ~ACE_Array_Base (void); 00079 00080 // = Set/get methods. 00081 00082 /// Set item in the array at location <slot>. Doesn't 00083 /// perform range checking. 00084 T &operator [] (size_t slot); 00085 00086 /// Get item in the array at location <slot>. Doesn't 00087 /// perform range checking. 00088 const T &operator [] (size_t slot) const; 00089 00090 /// Set an item in the array at location <slot>. Returns 00091 /// -1 if <slot> is not in range, else returns 0. 00092 int set (const T &new_item, size_t slot); 00093 00094 /** 00095 * Get an item in the array at location <slot>. Returns -1 if 00096 * <slot> is not in range, else returns 0. Note that this function 00097 * copies the item. If you want to avoid the copy, you can use 00098 * the const operator [], but then you'll be responsible for range checking. 00099 */ 00100 int get (T &item, size_t slot) const; 00101 00102 /// Returns the <cur_size_> of the array. 00103 size_t size (void) const; 00104 00105 /** 00106 * Changes the size of the array to match <new_size>. 00107 * It copies the old contents into the new array. 00108 * Return -1 on failure. 00109 */ 00110 int size (size_t new_size); 00111 00112 /// Returns the <max_size_> of the array. 00113 size_t max_size (void) const; 00114 00115 /** 00116 * Changes the size of the array to match <new_size>. 00117 * It copies the old contents into the new array. 00118 * Return -1 on failure. 00119 * It does not affect new_size 00120 */ 00121 int max_size (size_t new_size); 00122 00123 private: 00124 /// Returns 1 if <slot> is within range, i.e., 0 >= <slot> < 00125 /// <cur_size_>, else returns 0. 00126 int in_range (size_t slot) const; 00127 00128 /// Maximum size of the array, i.e., the total number of <T> elements 00129 /// in <array_>. 00130 size_t max_size_; 00131 00132 /** 00133 * Current size of the array. This starts out being == to 00134 * <max_size_>. However, if we are assigned a smaller array, then 00135 * <cur_size_> will become less than <max_size_>. The purpose of 00136 * keeping track of both sizes is to avoid reallocating memory if we 00137 * don't have to. 00138 */ 00139 size_t cur_size_; 00140 00141 /// Pointer to the array's storage buffer. 00142 T *array_; 00143 00144 /// Allocation strategy of the ACE_Array_Base. 00145 ACE_Allocator *allocator_; 00146 00147 friend class ACE_Array_Iterator<T>; 00148 }; 00149 00150 // **************************************************************** 00151 00152 /** 00153 * @class ACE_Array_Iterator 00154 * 00155 * @brief Implement an iterator over an ACE_Array. 00156 * 00157 * This iterator is safe in the face of array element deletions. 00158 * But it is NOT safe if the array is resized (via the ACE_Array 00159 * assignment operator) during iteration. That would be very 00160 * odd, and dangerous. 00161 */ 00162 template <class T> 00163 class ACE_Array_Iterator 00164 { 00165 public: 00166 // = Initialization method. 00167 ACE_Array_Iterator (ACE_Array_Base<T> &); 00168 00169 // = Iteration methods. 00170 00171 /// Pass back the <next_item> that hasn't been seen in the Array. 00172 /// Returns 0 when all items have been seen, else 1. 00173 int next (T *&next_item); 00174 00175 /// Move forward by one element in the Array. Returns 0 when all the 00176 /// items in the Array have been seen, else 1. 00177 int advance (void); 00178 00179 /// Returns 1 when all items have been seen, else 0. 00180 int done (void) const; 00181 00182 /// Dump the state of an object. 00183 void dump (void) const; 00184 00185 /// Declare the dynamic allocation hooks. 00186 ACE_ALLOC_HOOK_DECLARE; 00187 00188 private: 00189 /// Pointer to the current item in the iteration. 00190 size_t current_; 00191 00192 /// Pointer to the Array we're iterating over. 00193 ACE_Array_Base<T> &array_; 00194 }; 00195 00196 #if defined (__ACE_INLINE__) 00197 #include "ace/Array_Base.inl" 00198 #endif /* __ACE_INLINE__ */ 00199 00200 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00201 #include "ace/Array_Base.cpp" 00202 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00203 00204 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00205 #pragma implementation ("Array_Base.cpp") 00206 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00207 00208 #include "ace/post.h" 00209 00210 #endif /* ACE_ARRAY_BASE_H */
1.2.14 written by Dimitri van Heesch,
© 1997-2002