BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlma_deleter.h
Go to the documentation of this file.
1/// @file bdlma_deleter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlma_deleter.h -*-C++-*-
8#ifndef INCLUDED_BDLMA_DELETER
9#define INCLUDED_BDLMA_DELETER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlma_deleter bdlma_deleter
15/// @brief Provide a protocol for deleting objects of parameterized type.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlma
19/// @{
20/// @addtogroup bdlma_deleter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlma_deleter-purpose"> Purpose</a>
25/// * <a href="#bdlma_deleter-classes"> Classes </a>
26/// * <a href="#bdlma_deleter-description"> Description </a>
27/// * <a href="#bdlma_deleter-usage"> Usage </a>
28/// * <a href="#bdlma_deleter-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdlma_deleter-purpose}
31/// Provide a protocol for deleting objects of parameterized type.
32///
33/// # Classes {#bdlma_deleter-classes}
34///
35/// - bdlma::Deleter: protocol class for deleting objects
36///
37/// @see bslma_allocator
38///
39/// # Description {#bdlma_deleter-description}
40/// This component defines the base-level protocol,
41/// `bdlma::Deleter`, for a thread-safe and type-safe deleter of objects of
42/// arbitrary type. This class is extremely useful for transferring the
43/// ownership of objects between different entities. The following usage
44/// example demonstrates this point.
45///
46/// ## Usage {#bdlma_deleter-usage}
47///
48///
49/// This section illustrates intended use of this component.
50///
51/// ### Example 1: Basic Usage {#bdlma_deleter-example-1-basic-usage}
52///
53///
54/// Suppose that we would like to transfer ownership of an object between
55/// threads using `bsl::shared_ptr`. For the sake of discussion, the type of
56/// this object is `my_Obj`, we will suppose that it is created using a given
57/// `basicAllocator`, and that a concrete implementation of `bdlma::Deleter`,
58/// say `my_Deleter`, is to be used. Note that we assume that `my_Obj` does not
59/// require an allocator for any of its members:
60/// @code
61/// bslma::NewDeleteAllocator basicAllocator;
62/// my_Obj *object = new(basicAllocator) my_Obj;
63/// @endcode
64/// Next, create a concrete deleter for `object` using the same allocator as was
65/// used to allocate its footprint:
66/// @code
67/// my_Deleter deleter(&basicAllocator);
68/// @endcode
69/// Finally, create a shared pointer passing to it `object` and the address of
70/// `deleter`:
71/// @code
72/// bsl::shared_ptr<my_Obj> handle(object, &deleter, &basicAllocator);
73/// @endcode
74/// Now the `handle` can be passed to another thread or enqueued efficiently.
75/// When the reference count of `handle` goes to 0, `object` is automatically
76/// deleted via the `deleteObject` method of `deleter`, which in turn will
77/// invoke the destructor of `object`. Note that since the type of the deleter
78/// used to instantiate `handle` is `bdlma::Deleter<my_Obj>`, any kind of
79/// deleter that implements this protocol can be passed. Also note, on the
80/// downside, that the lifetime of `deleter` must be longer than the lifetime of
81/// all associated instances.
82/// @}
83/** @} */
84/** @} */
85
86/** @addtogroup bdl
87 * @{
88 */
89/** @addtogroup bdlma
90 * @{
91 */
92/** @addtogroup bdlma_deleter
93 * @{
94 */
95
96#include <bdlscm_version.h>
97
98
99namespace bdlma {
100
101 // =============
102 // class Deleter
103 // =============
104
105/// Provide a protocol (or pure interface) for a thread-safe object deleter.
106///
107/// See @ref bdlma_deleter
108template <class TYPE>
109class Deleter {
110
111 public:
112 // CREATORS
113
114 /// Destroy this object deleter.
115 virtual ~Deleter();
116
117 // MANIPULATORS
118
119 /// Destroy the specified `instance` based on its static type and return
120 /// its memory footprint to the appropriate memory manager. Note that a
121 /// particular implementation is allowed to destroy this deleter itself.
122 virtual void deleteObject(TYPE *instance) = 0;
123};
124
125// ============================================================================
126// INLINE DEFINITIONS
127// ============================================================================
128
129// CREATORS
130template <class TYPE>
134
135} // close package namespace
136
137
138#endif
139
140// ----------------------------------------------------------------------------
141// Copyright 2015 Bloomberg Finance L.P.
142//
143// Licensed under the Apache License, Version 2.0 (the "License");
144// you may not use this file except in compliance with the License.
145// You may obtain a copy of the License at
146//
147// http://www.apache.org/licenses/LICENSE-2.0
148//
149// Unless required by applicable law or agreed to in writing, software
150// distributed under the License is distributed on an "AS IS" BASIS,
151// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
152// See the License for the specific language governing permissions and
153// limitations under the License.
154// ----------------------------- END-OF-FILE ----------------------------------
155
156/** @} */
157/** @} */
158/** @} */
Definition bdlma_deleter.h:109
virtual ~Deleter()
Destroy this object deleter.
Definition bdlma_deleter.h:131
virtual void deleteObject(TYPE *instance)=0
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlma_alignedallocator.h:276