BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma_managedptrdeleter.h
Go to the documentation of this file.
1/// @file bslma_managedptrdeleter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_managedptrdeleter.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_MANAGEDPTRDELETER
9#define INCLUDED_BSLMA_MANAGEDPTRDELETER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$ $CSID$")
13
14/// @defgroup bslma_managedptrdeleter bslma_managedptrdeleter
15/// @brief Provide an in-core value-semantic class to call a delete function.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_managedptrdeleter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_managedptrdeleter-purpose"> Purpose</a>
25/// * <a href="#bslma_managedptrdeleter-classes"> Classes </a>
26/// * <a href="#bslma_managedptrdeleter-description"> Description </a>
27/// * <a href="#bslma_managedptrdeleter-attributes"> Attributes </a>
28///
29/// # Purpose {#bslma_managedptrdeleter-purpose}
30/// Provide an in-core value-semantic class to call a delete function.
31///
32/// # Classes {#bslma_managedptrdeleter-classes}
33///
34/// - bslma::ManagedPtrDeleter: in-core value-semantic class storing a deleter
35///
36/// @see bslma_managedptr
37///
38/// # Description {#bslma_managedptrdeleter-description}
39/// This component provides a single, complex-constrained in-core
40/// value-semantic attribute class, `bslma::ManagedPtrDeleter`, that is used to
41/// store a bound function call for a "factory" to destroy an object.
42///
43/// ## Attributes {#bslma_managedptrdeleter-attributes}
44///
45///
46/// @code
47/// Name Type Default
48/// ---------------- ------------------------ -------
49/// object void * 0
50/// factory void * 0
51/// deleter void (*)(void *, void *) 0
52///
53/// Complex Constraints
54/// ------------------------------------------------------------------
55/// '0 == deleter' or 'deleter(object, factory)' has defined behavior.
56/// @endcode
57/// * object Address of the object to be destroyed by the factory.
58/// * factory Address of the factory object that is responsible for destroying
59/// `object`.
60/// * deleter Address of the function that restores the erased types of
61/// `object` and `factory`, and invokes the `factory` method to
62/// destroy `object`.
63/// @}
64/** @} */
65/** @} */
66
67/** @addtogroup bsl
68 * @{
69 */
70/** @addtogroup bslma
71 * @{
72 */
73/** @addtogroup bslma_managedptrdeleter
74 * @{
75 */
76
77#include <bslscm_version.h>
78
80
81#include <bsls_assert.h>
82
83
84namespace bslma {
85
86 // =======================
87 // class ManagedPtrDeleter
88 // =======================
89
90/// This complex constrained in-core value-semantic class holds the
91/// information necessary for `ManagedPtr` to correctly manage its
92/// underlying object, namely the addresses of `object` and `factory`, and
93/// the `deleter` function, optionally supplied through the constructors or
94/// through the `set` method. This information is stored in a sub-structure
95/// to allow the compiler to copy it more efficiently.
96///
97/// See the {Attributes} section in the component-level documentation. Note
98/// that the class invariants are identically the constraints on the
99/// individual attributes.
100///
101/// This class:
102/// * supports a complete set of *value-semantic* operations
103/// * is *exception-safe*
104/// * is *alias-safe*
105/// * is `const` *thread-safe*
106/// For terminology see @ref bsldoc_glossary .
107///
108/// See @ref bslma_managedptrdeleter
110
111 public:
112 // PUBLIC TYPES
113
114 /// Deleter function prototype used to destroy the managed pointer.
115 typedef void (*Deleter)(void *managedObject, void *cookie);
116
117 private:
118 // DATA
119 void *d_object_p; // pointer to the actual managed object (i.e., not
120 // an alias)
121
122 void *d_factory_p; // optional factory to be specified to the deleter
123
124 Deleter d_deleter; // deleter used to destroy the managed object
125
126 public:
127 // CREATORS
128
129 /// Create a default `ManagedPtrDeleter` object that does not refer to
130 /// any object or factory instance.
132
133 /// Create a `ManagedPtrDeleter` object that refers to the object and
134 /// factory instances located at the specified `object` and `factory`
135 /// memory locations, and the specified `deleter`. The behavior is
136 /// undefined unless `deleter` is either 0, or points to a function
137 /// whose behavior is defined if called once with `object` and `factory`
138 /// as arguments.
139 ManagedPtrDeleter(void *object, void *factory, Deleter deleter);
140
141 /// Create a `ManagedPtrDeleter` object having the same value as the
142 /// specified `original` object. Note that this trivial copy
143 /// constructor's definition is compiler generated.
145
146 /// Destroy this object. Note that this trivial destructor's definition
147 /// is compiler generated.
149
150 // MANIPULATORS
151
152 /// Assign to this object the value of the specified `rhs` object, and
153 /// return a reference providing modifiable access to this object. Note
154 /// that this trivial copy-assignment operator's definition is compiler
155 /// generated.
157
158 /// Reset this `ManagedPtrDeleter` to its default-constructed state.
159 void clear();
160
161 /// Set this `ManagedPtrDeleter` to refer to the object and factory
162 /// instances located at the specified `object` and `factory` memory
163 /// locations, and the specified `deleter`. The behavior is undefined
164 /// unless `deleter` is either 0, or points to a function whose behavior
165 /// is defined if called once with `object` and `factory` as arguments.
166 void set(void *object, void *factory, Deleter deleter);
167
168 // ACCESSORS
169
170 /// Invoke the deleter object. The behavior is undefined unless
171 /// `deleter` is not 0 and has not already been called on the managed
172 /// object associated with this deleter.
173 void deleteManagedObject() const;
174
175 /// Return the deleter function associated with this deleter.
176 Deleter deleter() const;
177
178 /// Return a pointer to the factory instance associated with this
179 /// deleter.
180 void *factory() const;
181
182 /// Return a pointer to the managed object associated with this deleter.
183 void *object() const;
184};
185
186// FREE OPERATORS
187
188/// Return `true` if the specified `lhs` and `rhs` objects have the same
189/// value, and `false` otherwise. Two `ManagedPtrDeleter` objects have the
190/// same value if the corresponding values of their `object`, `factory`, and
191/// `deleter` attributes are the same.
192bool operator==(const ManagedPtrDeleter& lhs, const ManagedPtrDeleter& rhs);
193
194/// Return `true` if the specified `lhs` and `rhs` objects do not have the
195/// same value, and `false` otherwise. Two `ManagedPtrDeleter` objects do
196/// not have the same value if any of the corresponding values of their
197/// `object`, `factory`, and `deleter` attributes are not the same.
198bool operator!=(const ManagedPtrDeleter& lhs, const ManagedPtrDeleter& rhs);
199
200// ============================================================================
201// INLINE DEFINITIONS
202// ============================================================================
203
204 // -----------------------
205 // class ManagedPtrDeleter
206 // -----------------------
207
208// CREATORS
209inline
211: d_object_p(0)
212, d_factory_p(0)
213, d_deleter(0)
214{
215}
216
217inline
219 void *factory,
220 Deleter deleter)
221: d_object_p(object)
222, d_factory_p(factory)
223, d_deleter(deleter)
224{
225}
226
227// MANIPULATORS
228inline
230{
231 d_object_p = 0;
232 d_factory_p = 0;
233 d_deleter = 0;
234}
235
236inline
237void ManagedPtrDeleter::set(void *object, void *factory, Deleter deleter)
238{
239 d_object_p = object;
240 d_factory_p = factory;
241 d_deleter = deleter;
242}
243
244// ACCESSORS
245inline
247{
248 BSLS_ASSERT_SAFE(0 != d_deleter);
249
250 d_deleter(d_object_p, d_factory_p);
251}
252
253inline
256{
257 return d_deleter;
258}
259
260inline
262{
263 return d_factory_p;
264}
265
266inline
268{
269 return d_object_p;
270}
271
272} // close package namespace
273
274// FREE OPERATORS
275inline
276bool bslma::operator==(const ManagedPtrDeleter& lhs,
277 const ManagedPtrDeleter& rhs)
278{
279 return lhs.object() == rhs.object()
280 && lhs.factory() == rhs.factory()
281 && lhs.deleter() == rhs.deleter();
282}
283
284inline
285bool bslma::operator!=(const ManagedPtrDeleter& lhs,
286 const ManagedPtrDeleter& rhs)
287{
288 return lhs.object() != rhs.object()
289 || lhs.factory() != rhs.factory()
290 || lhs.deleter() != rhs.deleter();
291}
292
293// TYPE TRAITS
294namespace bslmf {
295
296template <>
297struct IsBitwiseMoveable<bslma::ManagedPtrDeleter>
298 : bsl::integral_constant<bool, true>
299{
300};
301
302} // close namespace bslmf
303
304
305#endif
306
307// ----------------------------------------------------------------------------
308// Copyright 2016 Bloomberg Finance L.P.
309//
310// Licensed under the Apache License, Version 2.0 (the "License");
311// you may not use this file except in compliance with the License.
312// You may obtain a copy of the License at
313//
314// http://www.apache.org/licenses/LICENSE-2.0
315//
316// Unless required by applicable law or agreed to in writing, software
317// distributed under the License is distributed on an "AS IS" BASIS,
318// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
319// See the License for the specific language governing permissions and
320// limitations under the License.
321// ----------------------------- END-OF-FILE ----------------------------------
322
323/** @} */
324/** @} */
325/** @} */
Definition bslma_managedptrdeleter.h:109
void(* Deleter)(void *managedObject, void *cookie)
Deleter function prototype used to destroy the managed pointer.
Definition bslma_managedptrdeleter.h:115
void set(void *object, void *factory, Deleter deleter)
Definition bslma_managedptrdeleter.h:237
void clear()
Reset this ManagedPtrDeleter to its default-constructed state.
Definition bslma_managedptrdeleter.h:229
ManagedPtrDeleter()
Definition bslma_managedptrdeleter.h:210
ManagedPtrDeleter & operator=(const ManagedPtrDeleter &rhs)
void * factory() const
Definition bslma_managedptrdeleter.h:261
void * object() const
Return a pointer to the managed object associated with this deleter.
Definition bslma_managedptrdeleter.h:267
Deleter deleter() const
Return the deleter function associated with this deleter.
Definition bslma_managedptrdeleter.h:255
void deleteManagedObject() const
Definition bslma_managedptrdeleter.h:246
ManagedPtrDeleter(const ManagedPtrDeleter &original)
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_encoderoptions.h:68
bool operator==(const ManagedPtrDeleter &lhs, const ManagedPtrDeleter &rhs)
bool operator!=(const ManagedPtrDeleter &lhs, const ManagedPtrDeleter &rhs)
Definition bdlbb_blob.h:576
Definition bslmf_integralconstant.h:244
Definition bslmf_isbitwisemoveable.h:718