BDE 4.14.0 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/// // NOT IMPLEMENTED
73/// my_RawDeleterGuard(const my_RawDeleterGuard&);
74/// my_RawDeleterGuard& operator=(const my_RawDeleterGuard&);
75///
76/// public:
77/// // CREATORS
78///
79/// /// Create a raw deleter guard that unconditionally manages the
80/// /// specified `object`, and that uses the specified `allocator` to
81/// /// delete `object` upon the destruction of this guard. The
82/// /// behavior is undefined unless `object` and `allocator` are
83/// /// non-zero, and `allocator` supplied the memory for `object`.
84/// /// Note that `allocator` must remain valid throughout the lifetime
85/// /// of this guard.
86/// my_RawDeleterGuard(TYPE *object, ALLOCATOR *allocator);
87///
88/// /// Destroy this raw deleter guard and delete the object it manages
89/// /// by first invoking the destructor of the (managed) object, and
90/// /// then invoking the `deallocate` method of the allocator (or pool)
91/// /// that was supplied with the object at construction.
92/// ~my_RawDeleterGuard();
93/// };
94/// @endcode
95/// The `deleteObjectRaw` method is used in the destructor as follows:
96/// @code
97/// template <class TYPE, class ALLOCATOR>
98/// inline
99/// my_RawDeleterGuard<TYPE, ALLOCATOR>::~my_RawDeleterGuard()
100/// {
101/// bslma::DeleterHelper::deleteObjectRaw(d_object_p, d_allocator_p);
102/// }
103/// @endcode
104/// Note that we have denoted our guard to be a "raw" guard in keeping with this
105/// use of `deleteObjectRaw` (as opposed to `deleteObject`).
106/// @}
107/** @} */
108/** @} */
109
110/** @addtogroup bsl
111 * @{
112 */
113/** @addtogroup bslma
114 * @{
115 */
116/** @addtogroup bslma_deleterhelper
117 * @{
118 */
119
120#include <bslscm_version.h>
121
122#include <bslmf_ispolymorphic.h>
123
124#include <bsls_assert.h>
125#include <bsls_platform.h>
126
127
128
129namespace bslma {
130
131 // ====================
132 // struct DeleterHelper
133 // ====================
134
135/// This struct provides a namespace for helper functions used for deleting
136/// objects in various pools and allocators.
138
139 // CLASS METHODS
140
141 /// Destroy the specified `object` based on its dynamic type and then
142 /// use the specified `allocator` to deallocate its memory footprint.
143 /// Do nothing if `object` is a null pointer. The behavior is undefined
144 /// unless `allocator` is non-null, and `object`, when cast
145 /// appropriately to `void *`, was allocated using `allocator` and has
146 /// not already been deallocated. Note that
147 /// `dynamic_cast<void *>(object)` is applied if `TYPE` is polymorphic,
148 /// and `static_cast<void *>(object)` is applied otherwise.
149 template <class TYPE, class ALLOCATOR>
150 static void deleteObject(const TYPE *object, ALLOCATOR *allocator);
151
152 /// Destroy the specified `object` and then use the specified
153 /// `allocator` to deallocate its memory footprint. Do nothing if
154 /// `object` is a null pointer. The behavior is undefined unless
155 /// `allocator` is non-null, `object` is **not** a secondary base class
156 /// pointer (i.e., the address is (numerically) the same as when it was
157 /// originally dispensed by `allocator`), and `object` was allocated
158 /// using `allocator` and has not already been deallocated.
159 template <class TYPE, class ALLOCATOR>
160 static void deleteObjectRaw(const TYPE *object, ALLOCATOR *allocator);
161};
162
163// ============================================================================
164// INLINE DEFINITIONS
165// ============================================================================
166
167 // =================================
168 // local struct DeleterHelper_Helper
169 // =================================
170
171template <bool IS_POLYMORPHIC>
173 template <class TYPE>
174 static void *caster(const TYPE *object)
175 {
176 return static_cast<void *>(const_cast<TYPE *>(object));
177 }
178};
179
180template <>
182 template <class TYPE>
183 static void *caster(const TYPE *object)
184 {
185 return dynamic_cast<void *>(const_cast<TYPE *>(object));
186 }
187};
188
189 // --------------------
190 // struct DeleterHelper
191 // --------------------
192
193// CLASS METHODS
194template <class TYPE, class ALLOCATOR>
195inline
196void DeleterHelper::deleteObject(const TYPE *object, ALLOCATOR *allocator)
197{
198 BSLS_ASSERT_SAFE(allocator);
199
200 if (0 != object) {
201 void *address = DeleterHelper_Helper<
202 bsl::is_polymorphic<TYPE>::value>::caster(object);
203 BSLS_ASSERT_OPT(address);
204
205#if defined(BSLS_PLATFORM_CMP_SUN) && BSLS_PLATFORM_CMP_VERSION < 0x5130
206 const_cast<TYPE *>(object)->~TYPE();
207#else
208 object->~TYPE();
209#endif
210
211 allocator->deallocate(address);
212 }
213}
214
215template <class TYPE, class ALLOCATOR>
216inline
217void DeleterHelper::deleteObjectRaw(const TYPE *object, ALLOCATOR *allocator)
218{
219 BSLS_ASSERT_SAFE(allocator);
220
221 if (0 != object) {
222 void *address = const_cast<TYPE *>(object);
223
224#if defined(BSLS_PLATFORM_CMP_SUN) && BSLS_PLATFORM_CMP_VERSION < 0x5130
225 const_cast<TYPE *>(object)->~TYPE();
226#else
227 object->~TYPE();
228#endif
229
230 allocator->deallocate(address);
231 }
232}
233
234} // close package namespace
235
236#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
237// ============================================================================
238// BACKWARD COMPATIBILITY
239// ============================================================================
240
241/// This alias is defined for backward compatibility.
243#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
244
245
246
247#endif
248
249// ----------------------------------------------------------------------------
250// Copyright 2013 Bloomberg Finance L.P.
251//
252// Licensed under the Apache License, Version 2.0 (the "License");
253// you may not use this file except in compliance with the License.
254// You may obtain a copy of the License at
255//
256// http://www.apache.org/licenses/LICENSE-2.0
257//
258// Unless required by applicable law or agreed to in writing, software
259// distributed under the License is distributed on an "AS IS" BASIS,
260// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
261// See the License for the specific language governing permissions and
262// limitations under the License.
263// ----------------------------- END-OF-FILE ----------------------------------
264
265/** @} */
266/** @} */
267/** @} */
bslma::DeleterHelper bslma_DeleterHelper
This alias is defined for backward compatibility.
Definition bslma_deleterhelper.h:242
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_ASSERT_OPT(X)
Definition bsls_assert.h:1856
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_encoderoptions.h:68
Definition bslmf_ispolymorphic.h:254
static void * caster(const TYPE *object)
Definition bslma_deleterhelper.h:183
Definition bslma_deleterhelper.h:172
static void * caster(const TYPE *object)
Definition bslma_deleterhelper.h:174
Definition bslma_deleterhelper.h:137
static void deleteObject(const TYPE *object, ALLOCATOR *allocator)
Definition bslma_deleterhelper.h:196
static void deleteObjectRaw(const TYPE *object, ALLOCATOR *allocator)
Definition bslma_deleterhelper.h:217