BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslma_managedptr_members.h
Go to the documentation of this file.
1/// @file bslma_managedptr_members.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslma_managedptr_members.h -*-C++-*-
8#ifndef INCLUDED_BSLMA_MANAGEDPTR_MEMBERS
9#define INCLUDED_BSLMA_MANAGEDPTR_MEMBERS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslma_managedptr_members bslma_managedptr_members
15/// @brief Provide the internal state of a managed pointer class.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslma
19/// @{
20/// @addtogroup bslma_managedptr_members
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslma_managedptr_members-purpose"> Purpose</a>
25/// * <a href="#bslma_managedptr_members-classes"> Classes </a>
26/// * <a href="#bslma_managedptr_members-description"> Description </a>
27///
28/// # Purpose {#bslma_managedptr_members-purpose}
29/// Provide the internal state of a managed pointer class.
30///
31/// # Classes {#bslma_managedptr_members-classes}
32///
33/// - bslma::ManagedPtr_Members: internal state of a `bslma::ManagedPtr` object
34///
35/// @see bslma_managedptr
36///
37/// # Description {#bslma_managedptr_members-description}
38/// This component provides a class, `bslma::ManagedPtr_Members`,
39/// to store and manage the internal state of a `bslma::ManagedPtr` object. It
40/// enforces the rules for correct transfer of ownership from one
41/// `bslma::ManagedPtr` object to another. A `bslma::ManagedPtr_Members` object
42/// has two attributes:
43/// * pointer - address of the object being managed, or an alias to the object
44/// * deleter - address of a function used to destroy the managed object
45/// @}
46/** @} */
47/** @} */
48
49/** @addtogroup bsl
50 * @{
51 */
52/** @addtogroup bslma
53 * @{
54 */
55/** @addtogroup bslma_managedptr_members
56 * @{
57 */
58
59#include <bslscm_version.h>
60
62
64
65#include <bsls_assert.h>
66
67
68namespace bslma {
69
70 // ========================
71 // class ManagedPtr_Members
72 // ========================
73
74/// This class provides a type-agnostic container for managed pointer data
75/// members. This type exists so that a `ManagedPtr_Ref` (see
76/// @ref bslma_managedptr ) can point to the representation of a `ManagedPtr`
77/// even if the `ManagedPtr_Ref` object is instantiated on a different type
78/// than the managed pointer type (e.g., in the case of conversions or
79/// aliasing). `ManagedPtr_Members` also "erases" the type of each member,
80/// so that the same object code can be shared between all instantiations of
81/// the `ManagedPtr` class template, reducing template bloat. Note that
82/// objects of this type have an "unset" state, where the pointer member has
83/// a null value and the deleter member has an unspecified value.
84///
85/// See @ref bslma_managedptr_members
87
88 private:
89 // PRIVATE TYPES
90 typedef ManagedPtrDeleter::Deleter DeleterFunc;
91
92 // DATA
93 void *d_obj_p; // pointer to the managed object, or an
94 // alias to the object; 'void *' is used so
95 // that this single (non-template) class may
96 // be used for any instantiation of
97 // 'bslma::ManagedPtr'
98
99 ManagedPtrDeleter d_deleter; // deleter-related information
100
101 private:
102 // NOT IMPLEMENTED
103 ManagedPtr_Members& operator=(const ManagedPtr_Members&);
104
105 public:
106 // CREATORS
107
108 /// Create a `ManagedPtr_Members` object in an unset state, i.e., where
109 /// `pointer` is null and `deleter` has an unspecified value.
111
112 /// Create a `ManagedPtr_Members` object having the same `pointer` as
113 /// the specified `other` object, and, if `0 != other.pointer()`, the
114 /// same deleter as `other`, then put `other` into an unset state.
115 explicit ManagedPtr_Members(ManagedPtr_Members& other);
116
117 /// Create a `ManagedPtr_Members` object having the specified `object`,
118 /// `factory`, and `deleter`. The newly created object does not manage
119 /// a pointer if `0 == object`. The behavior is undefined unless
120 /// `object` is null if `deleter` is null.
121 ManagedPtr_Members(void *object, void *factory, DeleterFunc deleter);
122
123 /// Create a `ManagedPtr_Members` object having the specified `object`,
124 /// `factory`, `deleter`, and `alias`. The newly created object does
125 /// not manage a pointer if `0 == object`. The behavior is undefined
126 /// unless `object` is null if `deleter` is null, and `0 == object` if
127 /// and only if `0 == alias`. Note that this constructor is important
128 /// for managed pointers pointing to one of multiple base classes of a
129 /// class using multiple inheritance.
130 ManagedPtr_Members(void *object,
131 void *factory,
132 DeleterFunc deleter,
133 void *alias);
134
136 // Destroy this object. Note that this trivial destructor's definition
137 // is compiler generated.
138
139 // MANIPULATORS
140
141 /// Reset this object to an unset state with no effect on the previously
142 /// managed object (if any).
143 void clear();
144
145 /// Reset this object to have the same `pointer` as the specified
146 /// `other` object, and, if `0 != other->pointer()`, the same deleter as
147 /// `other`, then put `other` into an unset state. This method has no
148 /// effect on the previously managed object (if any). The behavior is
149 /// undefined unless `this != other`.
150 void move(ManagedPtr_Members *other);
151
152 /// Destroy the currently managed object (if any), reset this object to
153 /// have the same `pointer` as the specified `other` object, and, if
154 /// `0 != other->pointer()`, the same deleter as `other`, then put
155 /// `other` into an unset state. This method has no effect if
156 /// `this == other`.
157 void moveAssign(ManagedPtr_Members *other);
158
159 /// Reset this object to have the specified `object`, `factory`, and
160 /// `deleter` with no effect on the previously managed object (if any).
161 /// The object does not manage a pointer if `0 == object`. The behavior
162 /// is undefined unless `object` is null if `deleter` is null.
163 void set(void *object, void *factory, DeleterFunc deleter);
164
165 /// Set `pointer` to have the specified `alias`. If `0 == alias`, then
166 /// this object will have an unset state with no effect on the
167 /// previously managed object (if any). The behavior is undefined
168 /// unless `0 == pointer()` if and only if `0 == alias`.
169 void setAliasPtr(void *alias);
170
171 /// Efficiently exchange the state of this object with the state of the
172 /// specified `other` object. This method provides the no-throw
173 /// exception-safety guarantee. Note that if either object is in an
174 /// unset state, then the only guarantee about the unset state that may
175 /// be exchanged is that the `pointer` value shall be null.
177
178 // ACCESSORS
179
180 /// Return a reference providing non-modifiable access to the deleter
181 /// used to destroy the currently managed object (if any). The behavior
182 /// is undefined unless `pointer` is not null.
183 const ManagedPtrDeleter& deleter() const;
184
185 /// Return the address providing modifiable access to the currently
186 /// managed object, or 0 if this object is in an unset state. Note that
187 /// the returned pointer may be an alias to the managed object.
188 void *pointer() const;
189
190 /// Destroy the currently managed object (if any). Note that calling
191 /// this method twice on an object that is not in an unset state,
192 /// without assigning a new pointer to manage between the two calls,
193 /// results in undefined behavior unless this object's deleter
194 /// specifically supports such usage.
195 void runDeleter() const;
196};
197
198// ============================================================================
199// INLINE DEFINITIONS
200// ============================================================================
201
202 // ------------------------
203 // class ManagedPtr_Members
204 // ------------------------
205
206// CREATORS
207inline
209: d_obj_p(0)
210{
211}
212
213inline
215: d_obj_p(other.d_obj_p)
216{
217 if (d_obj_p) {
218 d_deleter = other.d_deleter;
219 }
220 other.clear();
221}
222
223inline
225 void *factory,
226 DeleterFunc deleter)
227: d_obj_p(object)
228, d_deleter(object, factory, deleter)
229{
230 BSLS_ASSERT_SAFE(0 != deleter || 0 == object);
231}
232
233inline
235 void *factory,
236 DeleterFunc deleter,
237 void *alias)
238: d_obj_p(alias)
239, d_deleter(object, factory, deleter)
240{
241 BSLS_ASSERT_SAFE(0 != deleter || 0 == object);
242 BSLS_ASSERT_SAFE(!object == !alias); // both are null, or neither is null
243}
244
245// MANIPULATORS
246inline
248{
249 d_obj_p = 0;
250}
251
252inline
254{
255 BSLS_ASSERT_SAFE(other);
256 BSLS_ASSERT_SAFE(this != other);
257
258 // If 'other->d_obj_p' is null, then 'other->d_deleter' has an unspecified
259 // value.
260
261 d_obj_p = other->d_obj_p;
262 if (other->d_obj_p) {
263 d_deleter = other->d_deleter;
264 }
265
266 other->clear();
267}
268
269inline
271{
272 BSLS_ASSERT_SAFE(other);
273
274 // Must protect against self-assignment due to destructive move.
275
276 if (this != other) {
277 runDeleter();
278 move(other);
279 }
280}
281
282inline
283void ManagedPtr_Members::set(void *object, void *factory, DeleterFunc deleter)
284{
285 BSLS_ASSERT_SAFE(0 != deleter || 0 == object);
286
287 // Note that 'factory' may be null if 'deleter' supports it, so that cannot
288 // be asserted here.
289
290 d_obj_p = object;
291 if (object) {
292 d_deleter.set(object, factory, deleter);
293 }
294}
295
296inline
298{
299 BSLS_ASSERT_SAFE(!alias == !d_obj_p); // both are null, or neither is null
300
301 d_obj_p = alias;
302}
303
304// ACCESSORS
305inline
307{
308 BSLS_ASSERT_SAFE(d_obj_p);
309
310 return d_deleter;
311}
312
313inline
315{
316 return d_obj_p;
317}
318
319inline
321{
322 if (d_obj_p) {
323 d_deleter.deleteManagedObject();
324 }
325}
326
327} // close package namespace
328
329// TYPE TRAITS
330namespace bslmf {
331
332template <>
333struct IsBitwiseMoveable<bslma::ManagedPtr_Members> : bsl::true_type
334{
335};
336
337} // close namespace bslmf
338
339
340#endif
341
342// ----------------------------------------------------------------------------
343// Copyright 2016 Bloomberg Finance L.P.
344//
345// Licensed under the Apache License, Version 2.0 (the "License");
346// you may not use this file except in compliance with the License.
347// You may obtain a copy of the License at
348//
349// http://www.apache.org/licenses/LICENSE-2.0
350//
351// Unless required by applicable law or agreed to in writing, software
352// distributed under the License is distributed on an "AS IS" BASIS,
353// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
354// See the License for the specific language governing permissions and
355// limitations under the License.
356// ----------------------------- END-OF-FILE ----------------------------------
357
358/** @} */
359/** @} */
360/** @} */
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 deleteManagedObject() const
Definition bslma_managedptrdeleter.h:246
Definition bslma_managedptr_members.h:86
void moveAssign(ManagedPtr_Members *other)
Definition bslma_managedptr_members.h:270
void clear()
Definition bslma_managedptr_members.h:247
ManagedPtr_Members()
Definition bslma_managedptr_members.h:208
const ManagedPtrDeleter & deleter() const
Definition bslma_managedptr_members.h:306
void swap(ManagedPtr_Members &other)
void move(ManagedPtr_Members *other)
Definition bslma_managedptr_members.h:253
void setAliasPtr(void *alias)
Definition bslma_managedptr_members.h:297
void runDeleter() const
Definition bslma_managedptr_members.h:320
void set(void *object, void *factory, DeleterFunc deleter)
Definition bslma_managedptr_members.h:283
void * pointer() const
Definition bslma_managedptr_members.h:314
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_encoderoptions.h:68
Definition bdlbb_blob.h:576
Definition bslmf_isbitwisemoveable.h:718