BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlma_defaultdeleter.h
Go to the documentation of this file.
1/// @file bdlma_defaultdeleter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlma_defaultdeleter.h -*-C++-*-
8#ifndef INCLUDED_BDLMA_DEFAULTDELETER
9#define INCLUDED_BDLMA_DEFAULTDELETER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlma_defaultdeleter bdlma_defaultdeleter
15/// @brief Provide a concrete default deleter w/optionally-supplied allocator.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlma
19/// @{
20/// @addtogroup bdlma_defaultdeleter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlma_defaultdeleter-purpose"> Purpose</a>
25/// * <a href="#bdlma_defaultdeleter-classes"> Classes </a>
26/// * <a href="#bdlma_defaultdeleter-description"> Description </a>
27/// * <a href="#bdlma_defaultdeleter-usage"> Usage </a>
28/// * <a href="#bdlma_defaultdeleter-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdlma_defaultdeleter-purpose}
31/// Provide a concrete default deleter w/optionally-supplied allocator.
32///
33/// # Classes {#bdlma_defaultdeleter-classes}
34///
35/// - bdlma::DefaultDeleter: concrete default `bdema`-based deleter
36///
37/// @see bslma_allocator, bdlma_deleter
38///
39/// # Description {#bdlma_defaultdeleter-description}
40/// This component provides a default concrete implementation of
41/// the `bdlma::Deleter` protocol:
42/// @code
43/// ( bdlma::DefaultDeleter )
44/// | ctor
45/// v
46/// ( bdlma::Deleter )
47/// dtor
48/// deleteObject
49/// @endcode
50/// Upon construction, a `bdlma::DefaultDeleter` is optionally supplied with a
51/// `bdema`-style allocator. If an allocator is not specified, the currently
52/// installed default allocator is used instead. The memory footprint of
53/// objects that are subsequently deleted via calls to the `deleteObject` method
54/// of the deleter will be returned to the allocator that was established at
55/// construction. Note that the allocator used to create the footprint of
56/// objects passed to `deleteObject` *must* be the same allocator that is used
57/// by the deleter.
58///
59/// ## Usage {#bdlma_defaultdeleter-usage}
60///
61///
62/// This section illustrates intended use of this component.
63///
64/// ### Example 1: Basic Usage {#bdlma_defaultdeleter-example-1-basic-usage}
65///
66///
67/// Suppose that we would like to transfer ownership of an object between
68/// threads using `bsl::shared_ptr`. For the sake of discussion, the type of
69/// this object is `my_Obj` and we will suppose that it is created using a given
70/// `basicAllocator`. Note that we assume that `my_Obj` does not require an
71/// allocator for any of its members:
72/// @code
73/// bslma::NewDeleteAllocator basicAllocator;
74/// my_Obj *object = new(basicAllocator) my_Obj;
75/// @endcode
76/// Next, create a concrete deleter for `object` using the same allocator as was
77/// used to allocate its footprint:
78/// @code
79/// bdlma::DefaultDeleter<my_Obj> deleter(&basicAllocator);
80/// @endcode
81/// Finally, create a shared pointer passing to it `object` and the address of
82/// `deleter`:
83/// @code
84/// bsl::shared_ptr<my_Obj> handle(object, &deleter, &basicAllocator);
85/// @endcode
86/// Now the `handle` can be passed to another thread or enqueued efficiently.
87/// When the reference count of `handle` goes to 0, `object` is automatically
88/// deleted via the `deleteObject` method of `deleter`, which in turn will
89/// invoke the destructor of `object`. Note that since the type of the deleter
90/// used to instantiate `handle` is `bdlma::Deleter<my_Obj>`, any kind of
91/// deleter that implements this protocol can be passed. Also note, on the
92/// downside, that the lifetime of `deleter` must be longer than the lifetime of
93/// all associated instances.
94/// @}
95/** @} */
96/** @} */
97
98/** @addtogroup bdl
99 * @{
100 */
101/** @addtogroup bdlma
102 * @{
103 */
104/** @addtogroup bdlma_defaultdeleter
105 * @{
106 */
107
108#include <bdlscm_version.h>
109
110#include <bdlma_deleter.h>
111
112#include <bslma_allocator.h>
113#include <bslma_default.h>
115
117
118#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
119#include <bslalg_typetraits.h>
120#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
121
122
123namespace bdlma {
124
125 // ====================
126 // class DefaultDeleter
127 // ====================
128
129/// This `class` provides a default concrete implementation of the `Deleter`
130/// protocol. Instances of `DefaultDeleter<TYPE>` either use an allocator
131/// optionally supplied at construction, or the currently installed default
132/// allocator if an allocator is not provided.
133///
134/// See @ref bdlma_defaultdeleter
135template <class TYPE>
136class DefaultDeleter : public Deleter<TYPE> {
137
138 // DATA
139 bslma::Allocator *d_allocator_p; // memory allocator (held, *not* owned)
140
141 // NOT IMPLEMENTED
144
145 public:
146 // CREATORS
147
148 /// Create a default deleter. Optionally specify a `basicAllocator`
149 /// used to manage the memory footprint of objects passed to the
150 /// `deleteObject` method. If `basicAllocator` is 0, the currently
151 /// installed default allocator is used.
152 DefaultDeleter(bslma::Allocator *basicAllocator = 0);
153
154 /// Destroy this default deleter.
155 virtual ~DefaultDeleter();
156
157 // MANIPULATORS
158
159 /// Destroy the specified `instance` based on its static type and return
160 /// its memory footprint to the appropriate memory manager. The
161 /// behavior is undefined unless the memory for the footprint of
162 /// `instance` was supplied by the same allocator as is used by this
163 /// default deleter. Note that this method does not destroy the deleter
164 /// itself.
165 virtual void deleteObject(TYPE *instance);
166};
167
168// ============================================================================
169// INLINE DEFINITIONS
170// ============================================================================
171
172// CREATORS
173template <class TYPE>
174inline
176: d_allocator_p(bslma::Default::allocator(basicAllocator))
177{
178}
179
180template <class TYPE>
184
185// MANIPULATORS
186template <class TYPE>
187inline
189{
190 d_allocator_p->deleteObject(instance);
191}
192
193} // close package namespace
194
195
196// TRAITS
197
198
199namespace bslma {
200
201template <class TYPE>
202struct UsesBslmaAllocator<bdlma::DefaultDeleter<TYPE> > : bsl::true_type {};
203
204} // close namespace bslma
205
206
207#endif
208
209// ----------------------------------------------------------------------------
210// Copyright 2015 Bloomberg Finance L.P.
211//
212// Licensed under the Apache License, Version 2.0 (the "License");
213// you may not use this file except in compliance with the License.
214// You may obtain a copy of the License at
215//
216// http://www.apache.org/licenses/LICENSE-2.0
217//
218// Unless required by applicable law or agreed to in writing, software
219// distributed under the License is distributed on an "AS IS" BASIS,
220// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
221// See the License for the specific language governing permissions and
222// limitations under the License.
223// ----------------------------- END-OF-FILE ----------------------------------
224
225/** @} */
226/** @} */
227/** @} */
Definition bdlma_defaultdeleter.h:136
virtual void deleteObject(TYPE *instance)
Definition bdlma_defaultdeleter.h:188
virtual ~DefaultDeleter()
Destroy this default deleter.
Definition bdlma_defaultdeleter.h:181
Definition bdlma_deleter.h:109
Definition bslma_allocator.h:457
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlma_alignedallocator.h:276
Definition balxml_encoderoptions.h:68
Definition bslma_usesbslmaallocator.h:343