BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslma_deleterhelper.h
Go to the documentation of this file.
1/// @file bslma_deleterhelper.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_deleterhelper.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_DELETERHELPER
9#define INCLUDED_BSLMA_DELETERHELPER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_deleterhelper bslma_deleterhelper
15/// @brief Provide namespace for functions used to delete objects.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_deleterhelper
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_deleterhelper-purpose"> Purpose</a>
25/// * <a href="#bslma_deleterhelper-classes"> Classes </a>
26/// * <a href="#bslma_deleterhelper-description"> Description </a>
27/// * <a href="#bslma_deleterhelper-usage"> Usage </a>
28///
29/// # Purpose {#bslma_deleterhelper-purpose}
30/// Provide namespace for functions used to delete objects.
31///
32/// # Classes {#bslma_deleterhelper-classes}
33///
34/// - bslma::DeleterHelper: non-primitive functions for deleting objects
35///
36/// @see bslma_rawdeleterguard, bslmf_ispolymporphic
37///
38/// # Description {#bslma_deleterhelper-description}
39/// This component provides non-primitive procedures used to delete
40/// objects of parameterized `TYPE` by first calling the destructor of the
41/// object, and then freeing the memory footprint of the object using a
42/// parameterized `ALLOCATOR` (allocator or pool) provided as a second argument.
43/// The "raw" method (`deleteObjectRaw`) should be used only if we are sure that
44/// the supplied object is **not** of a type that is a secondary base class --
45/// i.e., the object's address is (numerically) the same as when it was
46/// originally dispensed by `ALLOCATOR`. The non-"raw" `deleteObject` has no
47/// such restriction. Note that this component will fail to compile when
48/// instantiated for a class that gives a false-positive for the type trait
49/// `bsl::is_polymorphic`. See the @ref bslmf_ispolymporphic component for more
50/// details.
51///
52/// ## Usage {#bslma_deleterhelper-usage}
53///
54///
55/// The following `my_RawDeleterGuard` class defines a guard that
56/// unconditionally deletes a managed object upon destruction. Via the
57/// `deleteObjectRaw` method supplied by this component, the guard's destructor
58/// first destroys the managed object, then deallocates the footprint of the
59/// object. The declaration of `my_RawDeleterGuard` follows:
60/// @code
61/// /// This class implements a guard that unconditionally deletes a managed
62/// /// object upon destruction by first invoking the object's destructor,
63/// /// and then invoking the `deallocate` method of an allocator (or pool)
64/// /// of parameterized `ALLOCATOR` type supplied at construction.
65/// template <class TYPE, class ALLOCATOR>
66/// class my_RawDeleterGuard {
67///
68/// // DATA
69/// TYPE *d_object_p; // managed object
70/// ALLOCATOR *d_allocator_p; // allocator or pool (held, not owned)
71///
72/// private:
73/// // NOT IMPLEMENTED
74/// my_RawDeleterGuard(const my_RawDeleterGuard&);
75/// my_RawDeleterGuard& operator=(const my_RawDeleterGuard&);
76///
77/// public:
78/// // CREATORS
79///
80/// /// Create a raw deleter guard that unconditionally manages the
81/// /// specified `object`, and that uses the specified `allocator` to
82/// /// delete `object` upon the destruction of this guard. The
83/// /// behavior is undefined unless `object` and `allocator` are
84/// /// non-zero, and `allocator` supplied the memory for `object`.
85/// /// Note that `allocator` must remain valid throughout the lifetime
86/// /// of this guard.
87/// my_RawDeleterGuard(TYPE *object, ALLOCATOR *allocator);
88///
89/// /// Destroy this raw deleter guard and delete the object it manages
90/// /// by first invoking the destructor of the (managed) object, and
91/// /// then invoking the `deallocate` method of the allocator (or pool)
92/// /// that was supplied with the object at construction.
93/// ~my_RawDeleterGuard();
94/// };
95/// @endcode
96/// The `deleteObjectRaw` method is used in the destructor as follows:
97/// @code
98/// template <class TYPE, class ALLOCATOR>
99/// inline
100/// my_RawDeleterGuard<TYPE, ALLOCATOR>::~my_RawDeleterGuard()
101/// {
102/// bslma::DeleterHelper::deleteObjectRaw(d_object_p, d_allocator_p);
103/// }
104/// @endcode
105/// Note that we have denoted our guard to be a "raw" guard in keeping with this
106/// use of `deleteObjectRaw` (as opposed to `deleteObject`).
107/// @}
108/** @} */
109/** @} */
110
111/** @addtogroup bsl
112 * @{
113 */
114/** @addtogroup bslma
115 * @{
116 */
117/** @addtogroup bslma_deleterhelper
118 * @{
119 */
120
121#include <bslscm_version.h>
122
123#include <bslma_pointerutil.h>
124
125#include <bslmf_ispolymorphic.h>
126
127#include <bsls_assert.h>
128#include <bsls_platform.h>
129
130
131
132namespace bslma {
133
134 // ====================
135 // struct DeleterHelper
136 // ====================
137
138/// This struct provides a namespace for helper functions used for deleting
139/// objects in various pools and allocators.
140///
141/// See @ref bslma_deleterhelper
143
144 // CLASS METHODS
145
146 /// Destroy the specified `object` based on its dynamic type and then
147 /// use the specified `allocator` to deallocate its memory footprint.
148 /// Do nothing if `object` is a null pointer.
149 ///
150 /// \pre The behavior is undefined unless `allocator` is non-null, and `object`, when cast
151 /// appropriately to `void *`, was allocated using `allocator` and has
152 /// not already been deallocated.
153 ///
154 /// \note Note that `dynamic_cast<void *>(object)` is applied if `TYPE` is polymorphic,
155 /// and `static_cast<void *>(object)` is applied otherwise.
156 template <class TYPE, class ALLOCATOR>
157 static void deleteObject(const TYPE *object, ALLOCATOR *allocator);
158
159 /// Destroy the specified `object` and then use the specified
160 /// `allocator` to deallocate its memory footprint. Do nothing if `object` is a null pointer.
161 ///
162 /// \pre The behavior is undefined unless
163 /// `allocator` is non-null, `object` is **not** a secondary base class
164 /// pointer (i.e., the address is (numerically) the same as when it was
165 /// originally dispensed by `allocator`), and `object` was allocated
166 /// using `allocator` and has not already been deallocated.
167 template <class TYPE, class ALLOCATOR>
168 static void deleteObjectRaw(const TYPE *object, ALLOCATOR *allocator);
169};
170
171// ============================================================================
172// INLINE DEFINITIONS
173// ============================================================================
174
175 // =================================
176 // local struct DeleterHelper_Helper
177 // =================================
178
179template <bool IS_POLYMORPHIC>
181 template <class TYPE>
182 static void *caster(const TYPE *object)
183 {
184 return PointerUtil::voidify(object);
185 }
186};
187
188template <>
190 template <class TYPE>
191 static void *caster(const TYPE *object)
192 {
193 return dynamic_cast<void *>(const_cast<TYPE *>(object));
194 }
195};
196
197 // --------------------
198 // struct DeleterHelper
199 // --------------------
200
201// CLASS METHODS
202template <class TYPE, class ALLOCATOR>
203inline
204void DeleterHelper::deleteObject(const TYPE *object, ALLOCATOR *allocator)
205{
206 BSLS_ASSERT_SAFE(allocator);
207
208 if (0 != object) {
209 void *address = DeleterHelper_Helper<
210 bsl::is_polymorphic<TYPE>::value>::caster(object);
211 BSLS_ASSERT_OPT(address);
212
213#if defined(BSLS_PLATFORM_CMP_SUN) && BSLS_PLATFORM_CMP_VERSION < 0x5130
214 const_cast<TYPE *>(object)->~TYPE();
215#else
216 object->~TYPE();
217#endif
218
219 allocator->deallocate(address);
220 }
221}
222
223template <class TYPE, class ALLOCATOR>
224inline
225void DeleterHelper::deleteObjectRaw(const TYPE *object, ALLOCATOR *allocator)
226{
227 BSLS_ASSERT_SAFE(allocator);
228
229 if (0 != object) {
230 void *address = const_cast<TYPE *>(object);
231
232#if defined(BSLS_PLATFORM_CMP_SUN) && BSLS_PLATFORM_CMP_VERSION < 0x5130
233 const_cast<TYPE *>(object)->~TYPE();
234#else
235 object->~TYPE();
236#endif
237
238 allocator->deallocate(address);
239 }
240}
241
242} // close package namespace
243
244#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
245// ============================================================================
246// BACKWARD COMPATIBILITY
247// ============================================================================
248
249/// This alias is defined for backward compatibility.
251#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
252
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/** @} */
bslma::DeleterHelper bslma_DeleterHelper
This alias is defined for backward compatibility.
Definition bslma_deleterhelper.h:250
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_ASSERT_OPT(X)
Definition bsls_assert.h:2045
#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
Definition bslmf_ispolymorphic.h:254
static void * caster(const TYPE *object)
Definition bslma_deleterhelper.h:191
Definition bslma_deleterhelper.h:180
static void * caster(const TYPE *object)
Definition bslma_deleterhelper.h:182
Definition bslma_deleterhelper.h:142
static void deleteObject(const TYPE *object, ALLOCATOR *allocator)
Definition bslma_deleterhelper.h:204
static void deleteObjectRaw(const TYPE *object, ALLOCATOR *allocator)
Definition bslma_deleterhelper.h:225
static BSLS_KEYWORD_CONSTEXPR void * voidify(TYPE *address) BSLS_KEYWORD_NOEXCEPT
Definition bslma_pointerutil.h:350