3 //==========================================================================
7 * @author Craig L. Ching <cching@mqsoftware.com>
8 * @author Gonzalo Diethelm <gonzalo.diethelm@aditiva.com>
10 //==========================================================================
12 #ifndef ACE_VECTOR_T_H
13 #define ACE_VECTOR_T_H
15 #include /**/ "ace/pre.h"
17 #include "ace/Containers_T.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
26 * Default size for an ACE_Vector.
28 static constexpr size_t ACE_VECTOR_DEFAULT_SIZE
= 32;
30 // Forward declaration.
31 template <class T
, size_t DEFAULT_SIZE
> class ACE_Vector_Iterator
;
36 * @brief Defines an STL-like vector container.
38 * This is an STL-like template vector container, a wrapper around
39 * ACE_Array. It provides at least the basic std::vector look and
40 * feel: push_back(), clear(), resize(), capacity(). This template
41 * class uses the copy semantic paradigm, though it is okay to use
42 * reference counted smart pointers (see ACE_Ptr<T>) with this
45 * <b> Requirements and Performance Characteristics</b>
46 * - Internal Structure
48 * - Duplicates allowed?
50 * - Random access allowed?
54 * - Insert/replace speed
56 * - Iterator still valid after change to container?
58 * - Frees memory for removed elements?
62 * - Requirements for contained type
63 * -# Default constructor
67 template<class T
, size_t DEFAULT_SIZE
= ACE_VECTOR_DEFAULT_SIZE
>
68 class ACE_Vector
: public ACE_Array
<T
>
72 * A short name for iterator for ACE_Vector.
74 typedef ACE_Vector_Iterator
<T
, DEFAULT_SIZE
> Iterator
;
78 * General constructor.
80 * @param init_size Initial size of the vector with the default
81 * value of DEFAULT_SIZE
82 * @param alloc Pointer to an ACE allocator. If it is NULL then the
83 * default ACE allocator is used
85 ACE_Vector (const size_t init_size
= DEFAULT_SIZE
,
86 ACE_Allocator
* alloc
= 0);
91 ~ACE_Vector () = default;
93 /// Declare the dynamic allocation hooks.
94 ACE_ALLOC_HOOK_DECLARE
;
97 * Returns the current vector capacity, that is, the currently
98 * allocated buffer size.
100 * @return Current buffer size of the vector
102 size_t capacity () const;
105 * Returns the vector's dynamic size / actual current size of the
106 * vector. Do not confuse it with ACE_Array::size(), which returns
107 * the array's capacity. Unfortunately, ACE is not very consistent
108 * with the function names.
110 * @return Dynamic size / actual current size of the vector.
112 size_t size () const;
115 * Clears out the vector. It does not reallocate the vector's
116 * buffer, it is just sets the vector's dynamic size to 0.
121 * Resizes the vector to the new capacity. If the vector's current
122 * capacity is smaller than the size to be specified, then the
123 * buffer gets reallocated. If the new capacity is less than the
124 * current capacity of the vector, the buffer size stays the same.
126 * @param new_size New capacity of the vector
127 * @param t A filler value (of the class T) for initializing the
128 * elements of the vector with. By default, if this
129 * parameter is not specified, the default value of the
130 * class T will be used (for more detail, see the
131 * initialization clause for this parameter).
133 void resize (const size_t new_size
,
137 * Appends a new element to the vector ("push back"). If the
138 * dynamic size of the vector is equal to the capacity of the vector
139 * (vector is at capacity), the vector automatically doubles its
142 * @param elem A reference to the new element to be appended. By
143 * default, this parameters gets initialized with the
144 * default value of the class T.
146 void push_back (const T
& elem
);
149 * Deletes the last element from the vector ("pop back"). What this
150 * function really does is decrement the dynamic size of the
151 * vector. The vector's buffer does not get reallocated for
157 * This function dumps the content of the vector. TO BE MOVED out
158 * of this class. It needs to be implemented as a global template
159 * function that accepts a const ACE_Vector<T>, in order to
160 * make instances of this class compile on Linux. G++ and xlC
161 * have template instantiation algoriths, which are different from
162 * the one in Visual C++. The algorithms try to instantiate ALL
163 * methods declared in the template class, regardless of whether the
164 * functions are used or not. That is, all of the classes, that are
165 * used as elements in ACE_Vector's, have to have the dump() methods
166 * defined in them (seems to be overkill).
168 * This function calls T::dump() for each element of the vector.
172 // = Compare operators
174 /// Equality comparison operator.
176 * Compare this vector with @arg s for equality. Two vectors are equal
177 * if their sizes are equal and all the elements are equal.
179 bool operator== (const ACE_Vector
<T
, DEFAULT_SIZE
> &s
) const;
181 /// Inequality comparison operator.
183 * Compare this vector with @arg s for inequality such that @c *this !=
184 * @arg s is always the complement of the boolean return value of
185 * @c *this == @arg s.
187 bool operator!= (const ACE_Vector
<T
, DEFAULT_SIZE
> &s
) const;
189 void swap (ACE_Vector
&rhs
);
192 * Implement our own end functions because Array_Base's end functions use the
193 * current capacity, not the Vector's actual element count!
195 /// C++ Standard End Iterator
197 typename ACE_Array_Base
<T
>::iterator
end ();
198 typename ACE_Array_Base
<T
>::const_iterator
end () const;
203 * Dynamic size (length) of the vector.
208 * Current capacity (buffer size) of the vector.
210 size_t curr_max_size_
;
212 friend class ACE_Vector_Iterator
<T
, DEFAULT_SIZE
>;
216 * @class ACE_Vector_Iterator
218 * @brief Implement an iterator over an ACE_Vector.
220 * This iterator is safe in the face of vector element deletions.
221 * But it is NOT safe if the vector is resized via the assignment
222 * operator during iteration. That would be very odd, and dangerous.
224 template <class T
, size_t DEFAULT_SIZE
= ACE_VECTOR_DEFAULT_SIZE
>
225 class ACE_Vector_Iterator
228 ACE_Vector_Iterator (ACE_Vector
<T
, DEFAULT_SIZE
> &);
230 // = Iteration methods.
232 /// Pass back the @a next_item that hasn't been seen in the vector.
233 /// Returns 0 when all items have been seen, else 1.
234 int next (T
*&next_item
);
236 /// Move forward by one element in the vector. Returns 0 when all the
237 /// items in the vector have been seen, else 1.
240 /// Returns 1 when all items have been seen, else 0.
243 /// Dump the state of an object.
246 /// Declare the dynamic allocation hooks.
247 ACE_ALLOC_HOOK_DECLARE
;
250 /// Pointer to the current item in the iteration.
253 /// Reference to the vector we're iterating over.
254 ACE_Vector
<T
, DEFAULT_SIZE
> &vector_
;
257 ACE_END_VERSIONED_NAMESPACE_DECL
259 #if defined (__ACE_INLINE__)
260 #include "ace/Vector_T.inl"
261 #endif /* __ACE_INLINE__ */
263 #include "ace/Vector_T.cpp"
265 #include /**/ "ace/post.h"
267 #endif /* ACE_VECTOR_T_H */