BDE 4.14.0 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).
159
160 // PRIVATE CLASS METHODS
161
162 /// Destroy each instance of `TARGET_TYPE` in the array beginning at the
163 /// specified `begin` address and ending immediately before the
164 /// specified `end` address, using the specified `allocator`. Elide the
165 /// use of the destructor entirely if (template parameter) `TARGET_TYPE`
166 /// is bitwise copyable, i.e., in the overload where the last argument
167 /// (used only for overload resolution) is of type `bsl::true_type`.
168 template <class TARGET_TYPE, class ALLOCATOR>
169 static void destroy(TARGET_TYPE *begin,
170 TARGET_TYPE *end,
171 ALLOCATOR allocator,
173 template <class TARGET_TYPE, class ALLOCATOR>
174 static void destroy(TARGET_TYPE *begin,
175 TARGET_TYPE *end,
176 ALLOCATOR allocator,
178
179 /// Destroy each instance of `TARGET_TYPE` in the array beginning at the
180 /// specified `begin` address and ending immediately before the
181 /// specified `end` address. Elide the use of the destructor entirely
182 /// if (template parameter) `TARGET_TYPE` is bitwise copyable, i.e. in
183 /// the overload where the last argument (used only for overload
184 /// resolution) if of type `bsl::true_type`.
185 template <class TARGET_TYPE>
186 static void destroy(TARGET_TYPE *begin, TARGET_TYPE *end, bsl::true_type);
187 template <class TARGET_TYPE>
188 static void destroy(TARGET_TYPE *begin, TARGET_TYPE *end, bsl::false_type);
189
190 /// Overwrite the specified `numBytes` of memory starting at the
191 /// specified `ptr`.
192 static void scribbleOverMemory(void *ptr, size_t numBytes);
193
194 public:
195 // CLASS METHODS
196
197 /// Destroy the elements in the segment of an array of parameterized
198 /// `TARGET_TYPE` beginning at the specified `begin` address and ending
199 /// immediately before the specified `end` address, using the specified
200 /// `allocator`. If `begin == 0` and `end == 0` this function has no
201 /// effect. The behavior is undefined unless either (1) `begin <= end`,
202 /// `begin != 0`, and `end != 0`, or (2) `begin == 0 && end == 0`. Note
203 /// that this method does not deallocate any memory (except memory
204 /// deallocated by the element destructor calls).
205 template <class TARGET_TYPE, class ALLOCATOR>
206 static void
207 destroy(TARGET_TYPE *begin, TARGET_TYPE *end, ALLOCATOR allocator);
208
209 /// Destroy of the elements in the segment of an array of parameterized
210 /// `TARGET_TYPE` beginning at the specified `begin` address and ending
211 /// immediately before the specified `end` address. If `begin == 0` and
212 /// `end == 0` this function has no effect. The behavior is undefined
213 /// unless either (1) `begin <= end`, `begin != 0`, and `end != 0`, or
214 /// (2) `begin == 0 && end == 0`. Note that this method does not
215 /// deallocate any memory (except memory deallocated by the element
216 /// destructor calls).
217 template <class TARGET_TYPE>
218 static void destroy(TARGET_TYPE *begin, TARGET_TYPE *end);
219};
220
221// ============================================================================
222// INLINE FUNCTION DEFINITIONS
223// ============================================================================
224
225 // ---------------------------------
226 // struct ArrayDestructionPrimitives
227 // ---------------------------------
228
229// PRIVATE CLASS METHODS
230template <class TARGET_TYPE, class ALLOCATOR>
231inline
233 TARGET_TYPE *end,
234 ALLOCATOR,
236{
237 // 'bslmf::IsBitwiseCopyable' is a valid surrogate for having a trivial
238 // destructor.
239
240#ifdef BSLS_ASSERT_SAFE_IS_ACTIVE
241 if (begin) {
242 scribbleOverMemory(begin, (end - begin) * sizeof(TARGET_TYPE));
243 }
244#else
245 (void) begin;
246 (void) end;
247#endif
248}
249
250template <class TARGET_TYPE, class ALLOCATOR>
252 TARGET_TYPE *end,
253 ALLOCATOR allocator,
255{
256 for (; begin != end; ++begin) {
258 }
259}
260
261template <class TARGET_TYPE>
262inline
264 TARGET_TYPE *end,
266{
267 // 'bslmf::IsBitwiseCopyable' is a valid surrogate for having a trivial
268 // destructor.
269
270#ifdef BSLS_ASSERT_SAFE_IS_ACTIVE
271 scribbleOverMemory(begin, (end - begin) * sizeof(TARGET_TYPE));
272#else
273 (void) begin;
274 (void) end;
275#endif
276}
277
278template <class TARGET_TYPE>
280 TARGET_TYPE *end,
282{
283 for (; begin != end; ++begin) {
284 begin->~TARGET_TYPE();
285 }
286}
287
288// CLASS METHODS
289template <class TARGET_TYPE, class ALLOCATOR>
290inline
292 TARGET_TYPE *end,
293 ALLOCATOR allocator)
294{
295 BSLS_ASSERT_SAFE(begin || !end);
296 BSLS_ASSERT_SAFE(end || !begin);
297 BSLS_ASSERT_SAFE(begin <= end);
298
299 destroy(begin,
300 end,
301 allocator,
303}
304
305template <class TARGET_TYPE>
306inline
308 TARGET_TYPE *end)
309{
310 BSLS_ASSERT_SAFE(begin || !end);
311 BSLS_ASSERT_SAFE(end || !begin);
312 BSLS_ASSERT_SAFE(begin <= end);
313
314 destroy(begin,
315 end,
317}
318
319} // close package namespace
320
321#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
322// ============================================================================
323// BACKWARD COMPATIBILITY
324// ============================================================================
325
326/// This alias is defined for backward compatibility.
328
329#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
330
331
332
333#endif
334
335// ----------------------------------------------------------------------------
336// Copyright 2013 Bloomberg Finance L.P.
337//
338// Licensed under the Apache License, Version 2.0 (the "License");
339// you may not use this file except in compliance with the License.
340// You may obtain a copy of the License at
341//
342// http://www.apache.org/licenses/LICENSE-2.0
343//
344// Unless required by applicable law or agreed to in writing, software
345// distributed under the License is distributed on an "AS IS" BASIS,
346// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
347// See the License for the specific language governing permissions and
348// limitations under the License.
349// ----------------------------- END-OF-FILE ----------------------------------
350
351/** @} */
352/** @} */
353/** @} */
bslalg::ArrayDestructionPrimitives bslalg_ArrayDestructionPrimitives
This alias is defined for backward compatibility.
Definition bslalg_arraydestructionprimitives.h:327
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlc_flathashmap.h:1805
static void destroy(ALLOCATOR_TYPE &basicAllocator, ELEMENT_TYPE *elementAddr)
Definition bslma_allocatortraits.h:1494
Definition bslalg_arraydestructionprimitives.h:158
static void destroy(TARGET_TYPE *begin, TARGET_TYPE *end, ALLOCATOR allocator, bsl::true_type)
Definition bslalg_arraydestructionprimitives.h:232
static void scribbleOverMemory(void *ptr, size_t numBytes)