BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslma_destructionutil.h
Go to the documentation of this file.
1/// @file bslma_destructionutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_destructionutil.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_DESTRUCTIONUTIL
9#define INCLUDED_BSLMA_DESTRUCTIONUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_destructionutil bslma_destructionutil
15/// @brief Provide routines that destroy objects efficiently.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_destructionutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_destructionutil-purpose"> Purpose</a>
25/// * <a href="#bslma_destructionutil-classes"> Classes </a>
26/// * <a href="#bslma_destructionutil-description"> Description </a>
27/// * <a href="#bslma_destructionutil-usage"> Usage </a>
28/// * <a href="#bslma_destructionutil-example-1-destroy-int-and-an-integer-wrapper"> Example 1: Destroy int and an Integer Wrapper </a>
29///
30/// # Purpose {#bslma_destructionutil-purpose}
31/// Provide routines that destroy objects efficiently.
32///
33/// # Classes {#bslma_destructionutil-classes}
34///
35/// - bslma::DestructionUtil: namespace for routines that destroy objects
36///
37/// @see bslma_constructionutil
38///
39/// # Description {#bslma_destructionutil-description}
40/// This component provides utilities to destroy scalars with a
41/// uniform interface, but select a different implementation according to the
42/// traits possessed by the underlying type.
43///
44/// The trait under consideration by this component is:
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 {#bslma_destructionutil-usage}
58///
59///
60/// In this section we show intended use of this component. Note that this
61/// component is for use by the `bslstl` package. Other clients should use the
62/// STL algorithms (in header `<algorithm>` and `<memory>`).
63///
64/// ### Example 1: Destroy int and an Integer Wrapper {#bslma_destructionutil-example-1-destroy-int-and-an-integer-wrapper}
65///
66///
67/// In this example, we will use `bslma::DestructionUtil` to destroy both a
68/// scalar integer and a `MyInteger` type object. Calling the `destroy` method
69/// on a scalar integer is a no-op while calling the `destroy` method on an
70/// object of `MyInteger` class invokes the destructor of the object.
71///
72/// First, we define a `MyInteger` class that represents an integer value:
73/// @code
74/// /// This class represents an integer value.
75/// class MyInteger {
76///
77/// // DATA
78/// int d_intValue; // integer value
79///
80/// public:
81/// // CREATORS
82///
83/// /// Create a `MyInteger` object having integer value `0`.
84/// MyInteger();
85///
86/// /// Create a `MyInteger` object having the specified `value`.
87/// explicit MyInteger(int value);
88///
89/// /// Destroy this object.
90/// ~MyInteger();
91///
92/// // ACCESSORS
93/// int getValue() const;
94/// };
95/// @endcode
96/// Then, we create an object, `myInteger`, of type `MyInteger`:
97/// @code
98/// bsls::ObjectBuffer<MyInteger> buffer;
99/// MyInteger *myInteger = &buffer.object();
100/// new (myInteger) MyInteger(1);
101/// @endcode
102/// Notice that we use an `ObjectBuffer` to allow us to safely invoke the
103/// destructor explicitly.
104///
105/// Now, we define a primitive integer:
106/// @code
107/// int scalarInteger = 2;
108/// @endcode
109/// Finally, we use the uniform `bslma::DestructionUtil::destroy`
110/// method to destroy both `myInteger` and `scalarInteger`:
111/// @code
112/// bslma::DestructionUtil::destroy(myInteger);
113/// bslma::DestructionUtil::destroy(&scalarInteger);
114/// @endcode
115/// @}
116/** @} */
117/** @} */
118
119/** @addtogroup bsl
120 * @{
121 */
122/** @addtogroup bslma
123 * @{
124 */
125/** @addtogroup bslma_destructionutil
126 * @{
127 */
128
129#include <bslscm_version.h>
130
133#include <bslmf_removecv.h>
134
135#include <bsls_assert.h>
136#include <bsls_platform.h>
137
138#include <stddef.h> // 'size_t'
139#include <string.h> // 'memset'
140
141
142
143namespace bslma {
144
145 // ======================
146 // struct DestructionUtil
147 // ======================
148
149/// This `struct` provides a namespace for a suite of utility functions that
150/// destroy elements of the parameterized type `TYPE`. Depending on the
151/// traits of `TYPE`, the destructor may be invoked or not (i.e., optimized
152/// away as a no-op).
153///
154/// See @ref bslma_destructionutil
156
157 private:
158 // PRIVATE CLASS METHODS
159
160 /// Destroy the object of the parameterized `TYPE` at the specified
161 /// `address` if the second argument is of type `bsl::false_type`, and
162 /// do nothing otherwise. This method is a no-op if the second argument
163 /// is of type `bsl::true_type`, indicating that the object at `address` is bit-wise copyable.
164 ///
165 /// \note Note that the second argument is for overload
166 /// resolution only and its value is ignored.
167 template <class TYPE>
168 static void destroy(TYPE *address, bsl::true_type);
169 template <class TYPE>
170 static void destroy(const TYPE *address, bsl::true_type);
171 template <class TYPE>
172 static void destroy(TYPE *address, bsl::false_type);
173
174 /// Write the specified `numBytes` bytes of arbitary values at the specified `address`.
175 ///
176 /// \note Note that this function is deliberately out
177 /// of line to avoid compiler warnings, most importantly a spurious
178 /// gcc-11 warning https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101854.
179 static void scribbleOverMemory(void *address, size_t numBytes);
180
181 public:
182 // CLASS METHODS
183
184 /// Destroy the specified `object` of the parameterized `TYPE`, as if by
185 /// calling the `TYPE` destructor, but do not deallocate the memory occupied by `object`.
186 ///
187 /// \note Note that the destructor may deallocate other
188 /// memory owned by `object`. Also note that this function is a no-op
189 /// if the `TYPE` has the trivial destructor trait.
190 template <class TYPE>
191 static void destroy(TYPE *object);
192
193};
194
195// ============================================================================
196// INLINE AND TEMPLATE FUNCTION DEFINITIONS
197// ============================================================================
198
199 // ----------------------
200 // struct DestructionUtil
201 // ----------------------
202
203// PRIVATE CLASS METHODS
204template <class TYPE>
205inline
206void DestructionUtil::destroy(TYPE *address, bsl::true_type)
207{
208#ifdef BSLS_ASSERT_SAFE_IS_ACTIVE
209 scribbleOverMemory(address, sizeof(TYPE));
210#endif
211 (void)address;
212}
213
214template <class TYPE>
215inline
216void DestructionUtil::destroy(const TYPE *address, bsl::true_type)
217{
218 // No-op.
219
220 (void) address;
221}
222
223#ifdef BSLS_PLATFORM_CMP_MSVC
224#pragma warning( push ) // For some reason, VC2008 does not detect
225#pragma warning( disable : 4100 ) // that 'address' is used.
226#endif
227
228template <class TYPE>
229inline
230void DestructionUtil::destroy(TYPE *address, bsl::false_type)
231{
232#ifndef BSLS_PLATFORM_CMP_SUN
233 address->~TYPE();
234#else
235 // Workaround for a bug in Sun's CC whereby destructors cannot be called on
236 // 'const' objects of polymorphic types.
237
238 typedef bsl::remove_cv<TYPE>::type NoCvType;
239 const_cast<NoCvType *>(address)->~NoCvType();
240#endif
241}
242
243#ifdef BSLS_PLATFORM_CMP_MSVC
244#pragma warning( pop )
245#endif
246
247// CLASS METHODS
248template <class TYPE>
249inline
250void DestructionUtil::destroy(TYPE *object)
251{
252 BSLS_ASSERT_SAFE(object);
253
254 destroy(object, typename bslmf::IsBitwiseCopyable<TYPE>::type());
255}
256
257} // close package namespace
258
259
260#endif
261
262// ----------------------------------------------------------------------------
263// Copyright 2013 Bloomberg Finance L.P.
264//
265// Licensed under the Apache License, Version 2.0 (the "License");
266// you may not use this file except in compliance with the License.
267// You may obtain a copy of the License at
268//
269// http://www.apache.org/licenses/LICENSE-2.0
270//
271// Unless required by applicable law or agreed to in writing, software
272// distributed under the License is distributed on an "AS IS" BASIS,
273// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
274// See the License for the specific language governing permissions and
275// limitations under the License.
276// ----------------------------- END-OF-FILE ----------------------------------
277
278/** @} */
279/** @} */
280/** @} */
#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 baljsn_encoder_testtypes.h:76
remove_const< typenameremove_volatile< t_TYPE >::type >::type type
Definition bslmf_removecv.h:128
Definition bslma_destructionutil.h:155