BDE 4.39.x 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
58/// > Address of the object to be destroyed by the factory.
59///
60/// * factory
61/// > Address of the factory object that is responsible for destroying
62/// > `object`.
63///
64/// * deleter
65/// > Address of the function that restores the erased types of `object` and
66/// > `factory`, and invokes the `factory` method to destroy `object`.
67/// @}
68/** @} */
69/** @} */
70
71/** @addtogroup bsl
72 * @{
73 */
74/** @addtogroup bslma
75 * @{
76 */
77/** @addtogroup bslma_managedptrdeleter
78 * @{
79 */
80
81#include <bslscm_version.h>
82
84
85#include <bsls_assert.h>
86
87
88namespace bslma {
89
90 // =======================
91 // class ManagedPtrDeleter
92 // =======================
93
94/// This complex constrained in-core value-semantic class holds the information
95/// necessary for `ManagedPtr` to correctly manage its underlying object,
96/// namely the addresses of `object` and `factory`, and the `deleter` function,
97/// optionally supplied through the constructors or through the `set` method.
98/// This information is stored in a sub-structure to allow the compiler to copy
99/// it more efficiently.
100///
101/// See the @ref bslma_managedptrdeleter-attributes for information on the class attributes.
102///
103/// \note Note that the class invariants are identically the constraints on the individual
104/// attributes.
105///
106/// This class:
107/// * supports a complete set of *value-semantic* operations
108/// * is *exception-safe*
109/// * is *alias-safe*
110/// * is `const` *thread-safe*
111/// For terminology see @ref bsldoc_glossary .
112///
113/// See @ref bslma_managedptrdeleter
115
116 public:
117 // PUBLIC TYPES
118
119 /// Deleter function prototype used to destroy the managed pointer.
120 typedef void (*Deleter)(void *managedObject, void *cookie);
121
122 private:
123 // DATA
124 void *d_object_p; // pointer to the actual managed object (i.e., not
125 // an alias)
126
127 void *d_factory_p; // optional factory to be specified to the deleter
128
129 Deleter d_deleter; // deleter used to destroy the managed object
130
131 public:
132 // CREATORS
133
134 /// Create a default `ManagedPtrDeleter` object that does not refer to any
135 /// object or factory instance.
137
138 /// Create a `ManagedPtrDeleter` object that refers to the object and
139 /// factory instances located at the specified `object` and `factory`
140 /// memory locations, and the specified `deleter`.
141 ///
142 /// \pre The behavior is undefined unless `deleter` is either 0, or points to a function whose
143 /// behavior is defined if called once with `object` and `factory` as
144 /// arguments.
145 ManagedPtrDeleter(void *object, void *factory, Deleter deleter);
146
147 /// Create a `ManagedPtrDeleter` object having the same value as the specified `original` object.
148 ///
149 /// \note Note that this trivial copy constructor's
150 /// definition is compiler generated.
152
153 /// Destroy this object.
154 /// \note Note that this trivial destructor's definition is
155 /// compiler generated.
157
158 // MANIPULATORS
159
160 /// Assign to this object the value of the specified `rhs` object, and
161 /// return a reference providing modifiable access to this object.
162 ///
163 /// \note Note that this trivial copy-assignment operator's definition is compiler
164 /// generated.
166
167 /// Reset this `ManagedPtrDeleter` to its default-constructed state.
168 void clear();
169
170 /// Set this `ManagedPtrDeleter` to refer to the object and factory
171 /// instances located at the specified `object` and `factory` memory
172 /// locations, and the specified `deleter`.
173 ///
174 /// \pre The behavior is undefined unless `deleter` is either 0, or points to a function whose behavior is
175 /// defined if called once with `object` and `factory` as arguments.
176 void set(void *object, void *factory, Deleter deleter);
177
178 // ACCESSORS
179
180 /// Invoke the deleter object.
181 /// \pre The behavior is undefined unless `deleter`
182 /// is not 0 and has not already been called on the managed object
183 /// associated with this deleter.
184 void deleteManagedObject() const;
185
186 /// Return the deleter function associated with this deleter.
187 Deleter deleter() const;
188
189 /// Return a pointer to the factory instance associated with this deleter.
190 void *factory() const;
191
192 /// Return a pointer to the managed object associated with this deleter.
193 void *object() const;
194};
195
196// FREE OPERATORS
197
198/// Return `true` if the specified `lhs` and `rhs` objects have the same value,
199/// and `false` otherwise. Two `ManagedPtrDeleter` objects have the same value
200/// if the corresponding values of their `object`, `factory`, and `deleter`
201/// attributes are the same.
202bool operator==(const ManagedPtrDeleter& lhs, const ManagedPtrDeleter& rhs);
203
204/// Return `true` if the specified `lhs` and `rhs` objects do not have the same
205/// value, and `false` otherwise. Two `ManagedPtrDeleter` objects do not have
206/// the same value if any of the corresponding values of their `object`,
207/// `factory`, and `deleter` attributes are not the same.
208bool operator!=(const ManagedPtrDeleter& lhs, const ManagedPtrDeleter& rhs);
209
210// ============================================================================
211// INLINE DEFINITIONS
212// ============================================================================
213
214 // -----------------------
215 // class ManagedPtrDeleter
216 // -----------------------
217
218// CREATORS
219inline
221: d_object_p(0)
222, d_factory_p(0)
223, d_deleter(0)
224{
225}
226
227inline
229 void *factory,
230 Deleter deleter)
231: d_object_p(object)
232, d_factory_p(factory)
233, d_deleter(deleter)
234{
235}
236
237// MANIPULATORS
238inline
240{
241 d_object_p = 0;
242 d_factory_p = 0;
243 d_deleter = 0;
244}
245
246inline
247void ManagedPtrDeleter::set(void *object, void *factory, Deleter deleter)
248{
249 d_object_p = object;
250 d_factory_p = factory;
251 d_deleter = deleter;
252}
253
254// ACCESSORS
255inline
257{
258 BSLS_ASSERT_SAFE(0 != d_deleter);
259
260 d_deleter(d_object_p, d_factory_p);
261}
262
263inline
266{
267 return d_deleter;
268}
269
270inline
272{
273 return d_factory_p;
274}
275
276inline
278{
279 return d_object_p;
280}
281
282} // close package namespace
283
284// FREE OPERATORS
285inline
286bool bslma::operator==(const ManagedPtrDeleter& lhs,
287 const ManagedPtrDeleter& rhs)
288{
289 return lhs.object() == rhs.object()
290 && lhs.factory() == rhs.factory()
291 && lhs.deleter() == rhs.deleter();
292}
293
294inline
295bool bslma::operator!=(const ManagedPtrDeleter& lhs,
296 const ManagedPtrDeleter& rhs)
297{
298 return lhs.object() != rhs.object()
299 || lhs.factory() != rhs.factory()
300 || lhs.deleter() != rhs.deleter();
301}
302
303// TYPE TRAITS
304namespace bslmf {
305
306template <>
307struct IsBitwiseMoveable<bslma::ManagedPtrDeleter>
308 : bsl::integral_constant<bool, true>
309{
310};
311
312} // close namespace bslmf
313
314
315#endif
316
317// ----------------------------------------------------------------------------
318// Copyright 2016 Bloomberg Finance L.P.
319//
320// Licensed under the Apache License, Version 2.0 (the "License");
321// you may not use this file except in compliance with the License.
322// You may obtain a copy of the License at
323//
324// http://www.apache.org/licenses/LICENSE-2.0
325//
326// Unless required by applicable law or agreed to in writing, software
327// distributed under the License is distributed on an "AS IS" BASIS,
328// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
329// See the License for the specific language governing permissions and
330// limitations under the License.
331// ----------------------------- END-OF-FILE ----------------------------------
332
333/** @} */
334/** @} */
335/** @} */
Definition bslma_managedptrdeleter.h:114
void(* Deleter)(void *managedObject, void *cookie)
Deleter function prototype used to destroy the managed pointer.
Definition bslma_managedptrdeleter.h:120
void set(void *object, void *factory, Deleter deleter)
Definition bslma_managedptrdeleter.h:247
void clear()
Reset this ManagedPtrDeleter to its default-constructed state.
Definition bslma_managedptrdeleter.h:239
ManagedPtrDeleter()
Definition bslma_managedptrdeleter.h:220
ManagedPtrDeleter & operator=(const ManagedPtrDeleter &rhs)
void * factory() const
Return a pointer to the factory instance associated with this deleter.
Definition bslma_managedptrdeleter.h:271
void * object() const
Return a pointer to the managed object associated with this deleter.
Definition bslma_managedptrdeleter.h:277
Deleter deleter() const
Return the deleter function associated with this deleter.
Definition bslma_managedptrdeleter.h:265
void deleteManagedObject() const
Definition bslma_managedptrdeleter.h:256
ManagedPtrDeleter(const ManagedPtrDeleter &original)
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Definition baljsn_encoder_testtypes.h:76
bool operator==(const ManagedPtrDeleter &lhs, const ManagedPtrDeleter &rhs)
bool operator!=(const ManagedPtrDeleter &lhs, const ManagedPtrDeleter &rhs)
Definition bdlbb_blob.h:579
Definition bslmf_integralconstant.h:261
Definition bslmf_isbitwisemoveable.h:718