BDE 4.14.0 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).
154
155 private:
156 // PRIVATE CLASS METHODS
157
158 /// Destroy the object of the parameterized `TYPE` at the specified
159 /// `address` if the second argument is of type `bsl::false_type`, and
160 /// do nothing otherwise. This method is a no-op if the second argument
161 /// is of type `bsl::true_type`, indicating that the object at `address`
162 /// is bit-wise copyable. Note that the second argument is for overload
163 /// resolution only and its value is ignored.
164 template <class TYPE>
165 static void destroy(TYPE *address, bsl::true_type);
166 template <class TYPE>
167 static void destroy(const TYPE *address, bsl::true_type);
168 template <class TYPE>
169 static void destroy(TYPE *address, bsl::false_type);
170
171 /// Write the specified `numBytes` bytes of arbitary values at the
172 /// specified `address`. Note that this function is deliberately out
173 /// of line to avoid compiler warnings, most importantly a spurious
174 /// gcc-11 warning https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101854.
175 static void scribbleOverMemory(void *address, size_t numBytes);
176
177 public:
178 // CLASS METHODS
179
180 /// Destroy the specified `object` of the parameterized `TYPE`, as if by
181 /// calling the `TYPE` destructor, but do not deallocate the memory
182 /// occupied by `object`. Note that the destructor may deallocate other
183 /// memory owned by `object`. Also note that this function is a no-op
184 /// if the `TYPE` has the trivial destructor trait.
185 template <class TYPE>
186 static void destroy(TYPE *object);
187
188};
189
190// ============================================================================
191// INLINE AND TEMPLATE FUNCTION DEFINITIONS
192// ============================================================================
193
194 // ----------------------
195 // struct DestructionUtil
196 // ----------------------
197
198// PRIVATE CLASS METHODS
199template <class TYPE>
200inline
201void DestructionUtil::destroy(TYPE *address, bsl::true_type)
202{
203#ifdef BSLS_ASSERT_SAFE_IS_ACTIVE
204 scribbleOverMemory(address, sizeof(TYPE));
205#endif
206 (void)address;
207}
208
209template <class TYPE>
210inline
211void DestructionUtil::destroy(const TYPE *address, bsl::true_type)
212{
213 // No-op.
214
215 (void) address;
216}
217
218#ifdef BSLS_PLATFORM_CMP_MSVC
219#pragma warning( push ) // For some reason, VC2008 does not detect
220#pragma warning( disable : 4100 ) // that 'address' is used.
221#endif
222
223template <class TYPE>
224inline
225void DestructionUtil::destroy(TYPE *address, bsl::false_type)
226{
227#ifndef BSLS_PLATFORM_CMP_SUN
228 address->~TYPE();
229#else
230 // Workaround for a bug in Sun's CC whereby destructors cannot be called on
231 // 'const' objects of polymorphic types.
232
233 typedef bsl::remove_cv<TYPE>::type NoCvType;
234 const_cast<NoCvType *>(address)->~NoCvType();
235#endif
236}
237
238#ifdef BSLS_PLATFORM_CMP_MSVC
239#pragma warning( pop )
240#endif
241
242// CLASS METHODS
243template <class TYPE>
244inline
245void DestructionUtil::destroy(TYPE *object)
246{
247 BSLS_ASSERT_SAFE(object);
248
249 destroy(object, typename bslmf::IsBitwiseCopyable<TYPE>::type());
250}
251
252} // close package namespace
253
254
255#endif
256
257// ----------------------------------------------------------------------------
258// Copyright 2013 Bloomberg Finance L.P.
259//
260// Licensed under the Apache License, Version 2.0 (the "License");
261// you may not use this file except in compliance with the License.
262// You may obtain a copy of the License at
263//
264// http://www.apache.org/licenses/LICENSE-2.0
265//
266// Unless required by applicable law or agreed to in writing, software
267// distributed under the License is distributed on an "AS IS" BASIS,
268// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
269// See the License for the specific language governing permissions and
270// limitations under the License.
271// ----------------------------- END-OF-FILE ----------------------------------
272
273/** @} */
274/** @} */
275/** @} */
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_encoderoptions.h:68
remove_const< typenameremove_volatile< t_TYPE >::type >::type type
Definition bslmf_removecv.h:126
Definition bslma_destructionutil.h:153