BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslalg_arraydestructionprimitives.h
Go to the documentation of this file.
1/// @file bslalg_arraydestructionprimitives.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslalg_arraydestructionprimitives.h -*-C++-*-
8#ifndef INCLUDED_BSLALG_ARRAYDESTRUCTIONPRIMITIVES
9#define INCLUDED_BSLALG_ARRAYDESTRUCTIONPRIMITIVES
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslalg_arraydestructionprimitives bslalg_arraydestructionprimitives
15/// @brief Provide primitive algorithms that destroy arrays.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslalg
19/// @{
20/// @addtogroup bslalg_arraydestructionprimitives
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslalg_arraydestructionprimitives-purpose"> Purpose</a>
25/// * <a href="#bslalg_arraydestructionprimitives-classes"> Classes </a>
26/// * <a href="#bslalg_arraydestructionprimitives-description"> Description </a>
27/// * <a href="#bslalg_arraydestructionprimitives-usage"> Usage </a>
28/// * <a href="#bslalg_arraydestructionprimitives-example-1-destroy-arrays-of-int-and-integer-wrapper-objects"> Example 1: Destroy Arrays of int and Integer Wrapper Objects </a>
29///
30/// # Purpose {#bslalg_arraydestructionprimitives-purpose}
31/// Provide primitive algorithms that destroy arrays.
32///
33/// # Classes {#bslalg_arraydestructionprimitives-classes}
34///
35/// - bslalg::ArrayDestructionPrimitives: namespace for array algorithms
36///
37/// @see bslma_destructionutil, bslma_constructionutil
38///
39/// # Description {#bslalg_arraydestructionprimitives-description}
40/// This component provides utilities to destroy arrays with a
41/// uniform interface, but selecting a different implementation according to the
42/// traits possessed by the underlying type.
43///
44/// The traits under consideration by this component are:
45/// @code
46/// Trait Note
47/// ----- -------------------------------------
48/// bslmf::IsBitwiseCopyable Expressed in English as "TYPE has the
49/// bit-wise copyable trait", or "TYPE is
50/// bit-wise copyable", this trait also
51/// implies that destructor calls can be
52/// elided with no effect on observable
53/// behavior.
54///
55/// @endcode
56///
57/// ## Usage {#bslalg_arraydestructionprimitives-usage}
58///
59///
60/// TBD: maybe fix up usage example to show with allocator
61/// In this section we show intended use of this component. Note that this
62/// component is for use by the `bslstl` package. Other clients should use the
63/// STL algorithms (in header `<algorithm>` and `<memory>`).
64///
65/// ### Example 1: Destroy Arrays of int and Integer Wrapper Objects {#bslalg_arraydestructionprimitives-example-1-destroy-arrays-of-int-and-integer-wrapper-objects}
66///
67///
68/// In this example, we will use `bslalg::ArrayDestructionPrimitives` to destroy
69/// both an array of integer scalars and an array of `MyInteger` objects.
70/// Calling the `destroy` method on an array of integers is a no-op while
71/// calling the `destroy` method on an array of objects of `MyInteger` class
72/// invokes the destructor of each of the objects in the array.
73///
74/// First, we define a `MyInteger` class that contains an integer value:
75/// @code
76/// /// This class represents an integer value.
77/// class MyInteger {
78///
79/// int d_intValue; // integer value
80///
81/// public:
82/// // CREATORS
83///
84/// /// Create a `MyInteger` object having integer value `0`.
85/// MyInteger();
86///
87/// /// Create a `MyInteger` object having the specified `value`.
88/// explicit MyInteger(int value);
89///
90/// /// Destroy this object.
91/// ~MyInteger();
92///
93/// // ACCESSORS
94///
95/// /// Return the integer value contained in this object.
96/// int getValue() const;
97/// };
98/// @endcode
99/// Then, we create an array of objects, `myIntegers`, of type `MyInteger` (note
100/// that we `bsls::ObjectBuffer` to allow us to safely invoke the destructor
101/// explicitly):
102/// @code
103/// bsls::ObjectBuffer<MyInteger> arrayBuffer[5];
104/// MyInteger *myIntegers = &arrayBuffer[0].object();
105/// for (int i = 0;i < 5; ++i) {
106/// new (myIntegers + i) MyInteger(i);
107/// }
108/// @endcode
109/// Now, we define a primitive integer array:
110/// @code
111/// int scalarIntegers[] = { 0, 1, 2, 3, 4 };
112/// @endcode
113/// Finally, we use the uniform `bslalg::ArrayDestructionPrimitives:destroy`
114/// method to destroy both `myIntegers` and `scalarIntegers`:
115/// @code
116/// bslalg::ArrayDestructionPrimitives::destroy(myIntegers, myIntegers + 5);
117/// bslalg::ArrayDestructionPrimitives::destroy(scalarIntegers,
118/// scalarIntegers + 5);
119/// @endcode
120/// @}
121/** @} */
122/** @} */
123
124/** @addtogroup bsl
125 * @{
126 */
127/** @addtogroup bslalg
128 * @{
129 */
130/** @addtogroup bslalg_arraydestructionprimitives
131 * @{
132 */
133
134#include <bslscm_version.h>
135
136#include <bsls_assert.h>
137#include <bsls_types.h>
138
140
142
143#include <stddef.h> // 'size_t'
144#include <cstring> // 'memset', 'memcpy', and 'memmove'
145
146
147
148namespace bslalg {
149
150 // =================================
151 // struct ArrayDestructionPrimitives
152 // =================================
153
154/// This `struct` provides a namespace for a suite of utility functions that
155/// destroy arrays of elements of the parameterized type `TARGET_TYPE`.
156/// Depending on the traits of `TARGET_TYPE`, the destructor may be invoked,
157/// or not (optimized away by no-op).
158///
159/// See @ref bslalg_arraydestructionprimitives
161
162 // PRIVATE CLASS METHODS
163
164 /// Destroy each instance of `TARGET_TYPE` in the array beginning at the
165 /// specified `begin` address and ending immediately before the
166 /// specified `end` address, using the specified `allocator`. Elide the
167 /// use of the destructor entirely if (template parameter) `TARGET_TYPE`
168 /// is bitwise copyable, i.e., in the overload where the last argument
169 /// (used only for overload resolution) is of type `bsl::true_type`.
170 template <class TARGET_TYPE, class ALLOCATOR>
171 static void destroy(TARGET_TYPE *begin,
172 TARGET_TYPE *end,
173 ALLOCATOR allocator,
175 template <class TARGET_TYPE, class ALLOCATOR>
176 static void destroy(TARGET_TYPE *begin,
177 TARGET_TYPE *end,
178 ALLOCATOR allocator,
180
181 /// Destroy each instance of `TARGET_TYPE` in the array beginning at the
182 /// specified `begin` address and ending immediately before the
183 /// specified `end` address. Elide the use of the destructor entirely
184 /// if (template parameter) `TARGET_TYPE` is bitwise copyable, i.e. in
185 /// the overload where the last argument (used only for overload
186 /// resolution) if of type `bsl::true_type`.
187 template <class TARGET_TYPE>
188 static void destroy(TARGET_TYPE *begin, TARGET_TYPE *end, bsl::true_type);
189 template <class TARGET_TYPE>
190 static void destroy(TARGET_TYPE *begin, TARGET_TYPE *end, bsl::false_type);
191
192 /// Overwrite the specified `numBytes` of memory starting at the
193 /// specified `ptr`.
194 static void scribbleOverMemory(void *ptr, size_t numBytes);
195
196 public:
197 // CLASS METHODS
198
199 /// Destroy the elements in the segment of an array of parameterized
200 /// `TARGET_TYPE` beginning at the specified `begin` address and ending
201 /// immediately before the specified `end` address, using the specified
202 /// `allocator`. If `begin == 0` and `end == 0` this function has no effect.
203 ///
204 /// \pre The behavior is undefined unless either (1) `begin <= end`,
205 /// `begin != 0`, and `end != 0`, or (2) `begin == 0 && end == 0`.
206 ///
207 /// \note Note that this method does not deallocate any memory (except memory
208 /// deallocated by the element destructor calls).
209 template <class TARGET_TYPE, class ALLOCATOR>
210 static void
211 destroy(TARGET_TYPE *begin, TARGET_TYPE *end, ALLOCATOR allocator);
212
213 /// Destroy of the elements in the segment of an array of parameterized
214 /// `TARGET_TYPE` beginning at the specified `begin` address and ending
215 /// immediately before the specified `end` address. If `begin == 0` and
216 /// `end == 0` this function has no effect.
217 ///
218 /// \pre The behavior is undefined unless either (1) `begin <= end`, `begin != 0`, and `end != 0`, or (2) `begin == 0 && end == 0`.
219 ///
220 /// \note Note that this method does not
221 /// deallocate any memory (except memory deallocated by the element
222 /// destructor calls).
223 template <class TARGET_TYPE>
224 static void destroy(TARGET_TYPE *begin, TARGET_TYPE *end);
225};
226
227// ============================================================================
228// INLINE FUNCTION DEFINITIONS
229// ============================================================================
230
231 // ---------------------------------
232 // struct ArrayDestructionPrimitives
233 // ---------------------------------
234
235// PRIVATE CLASS METHODS
236template <class TARGET_TYPE, class ALLOCATOR>
237inline
239 TARGET_TYPE *end,
240 ALLOCATOR,
242{
243 // 'bslmf::IsBitwiseCopyable' is a valid surrogate for having a trivial
244 // destructor.
245
246#ifdef BSLS_ASSERT_SAFE_IS_ACTIVE
247 if (begin) {
248 scribbleOverMemory(begin, (end - begin) * sizeof(TARGET_TYPE));
249 }
250#else
251 (void) begin;
252 (void) end;
253#endif
254}
255
256template <class TARGET_TYPE, class ALLOCATOR>
258 TARGET_TYPE *end,
259 ALLOCATOR allocator,
261{
262 for (; begin != end; ++begin) {
264 }
265}
266
267template <class TARGET_TYPE>
268inline
270 TARGET_TYPE *end,
272{
273 // 'bslmf::IsBitwiseCopyable' is a valid surrogate for having a trivial
274 // destructor.
275
276#ifdef BSLS_ASSERT_SAFE_IS_ACTIVE
277 scribbleOverMemory(begin, (end - begin) * sizeof(TARGET_TYPE));
278#else
279 (void) begin;
280 (void) end;
281#endif
282}
283
284template <class TARGET_TYPE>
286 TARGET_TYPE *end,
288{
289 for (; begin != end; ++begin) {
290 begin->~TARGET_TYPE();
291 }
292}
293
294// CLASS METHODS
295template <class TARGET_TYPE, class ALLOCATOR>
296inline
298 TARGET_TYPE *end,
299 ALLOCATOR allocator)
300{
301 BSLS_ASSERT_SAFE(begin || !end);
302 BSLS_ASSERT_SAFE(end || !begin);
303 BSLS_ASSERT_SAFE(begin <= end);
304
305 destroy(begin,
306 end,
307 allocator,
309}
310
311template <class TARGET_TYPE>
312inline
314 TARGET_TYPE *end)
315{
316 BSLS_ASSERT_SAFE(begin || !end);
317 BSLS_ASSERT_SAFE(end || !begin);
318 BSLS_ASSERT_SAFE(begin <= end);
319
320 destroy(begin,
321 end,
323}
324
325} // close package namespace
326
327#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
328// ============================================================================
329// BACKWARD COMPATIBILITY
330// ============================================================================
331
332/// This alias is defined for backward compatibility.
334
335#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
336
337
338
339#endif
340
341// ----------------------------------------------------------------------------
342// Copyright 2013 Bloomberg Finance L.P.
343//
344// Licensed under the Apache License, Version 2.0 (the "License");
345// you may not use this file except in compliance with the License.
346// You may obtain a copy of the License at
347//
348// http://www.apache.org/licenses/LICENSE-2.0
349//
350// Unless required by applicable law or agreed to in writing, software
351// distributed under the License is distributed on an "AS IS" BASIS,
352// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
353// See the License for the specific language governing permissions and
354// limitations under the License.
355// ----------------------------- END-OF-FILE ----------------------------------
356
357/** @} */
358/** @} */
359/** @} */
bslalg::ArrayDestructionPrimitives bslalg_ArrayDestructionPrimitives
This alias is defined for backward compatibility.
Definition bslalg_arraydestructionprimitives.h:333
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlc_flathashmap.h:2218
static void destroy(ALLOCATOR_TYPE &basicAllocator, ELEMENT_TYPE *elementAddr)
Definition bslma_allocatortraits.h:1549
Definition bslalg_arraydestructionprimitives.h:160
static void destroy(TARGET_TYPE *begin, TARGET_TYPE *end, ALLOCATOR allocator, bsl::true_type)
Definition bslalg_arraydestructionprimitives.h:238
static void scribbleOverMemory(void *ptr, size_t numBytes)