BDE 4.14.0 Production release
|
#include <bslma_managedptr.h>
Public Types | |
typedef ManagedPtrDeleter::Deleter | DeleterFunc |
typedef TARGET_TYPE | element_type |
Alias to the TARGET_TYPE template parameter. | |
Public Member Functions | |
ManagedPtr () | |
Create an empty managed pointer. | |
template<class MANAGED_TYPE > | |
ManagedPtr (MANAGED_TYPE *ptr) | |
ManagedPtr (ManagedPtr_Ref< TARGET_TYPE > ref) BSLS_KEYWORD_NOEXCEPT | |
ManagedPtr (ManagedPtr &original) BSLS_KEYWORD_NOEXCEPT | |
ManagedPtr (bslmf::MovableRef< ManagedPtr > original) BSLS_KEYWORD_NOEXCEPT | |
template<class BDE_OTHER_TYPE > | |
ManagedPtr (bslmf::MovableRef< ManagedPtr< BDE_OTHER_TYPE > > original, typename bsl::enable_if< bsl::is_convertible< BDE_OTHER_TYPE *, TARGET_TYPE * >::value, ManagedPtr_TraitConstraint >::type=ManagedPtr_TraitConstraint()) BSLS_KEYWORD_NOEXCEPT | |
template<class ALIASED_TYPE > | |
ManagedPtr (ManagedPtr< ALIASED_TYPE > &alias, TARGET_TYPE *ptr) | |
template<class ALIASED_TYPE > | |
ManagedPtr (bslmf::MovableRef< ManagedPtr< ALIASED_TYPE > > alias, TARGET_TYPE *ptr) | |
template<class MANAGED_TYPE , class FACTORY_TYPE > | |
ManagedPtr (MANAGED_TYPE *ptr, FACTORY_TYPE *factory) | |
ManagedPtr (bsl::nullptr_t, bsl::nullptr_t=0) | |
template<class FACTORY_TYPE > | |
ManagedPtr (bsl::nullptr_t, FACTORY_TYPE *factory) | |
ManagedPtr (TARGET_TYPE *ptr, void *cookie, DeleterFunc deleter) | |
template<class MANAGED_TYPE > | |
ManagedPtr (MANAGED_TYPE *ptr, void *cookie, DeleterFunc deleter) | |
template<class MANAGED_TYPE , class MANAGED_BASE > | |
ManagedPtr (MANAGED_TYPE *ptr, void *cookie, void(*deleter)(MANAGED_BASE *, void *)) | |
template<class MANAGED_TYPE , class MANAGED_BASE , class COOKIE_TYPE , class COOKIE_BASE > | |
ManagedPtr (MANAGED_TYPE *ptr, COOKIE_TYPE *cookie, void(*deleter)(MANAGED_BASE *, COOKIE_BASE *)) | |
~ManagedPtr () | |
ManagedPtr & | operator= (ManagedPtr &rhs) BSLS_KEYWORD_NOEXCEPT |
ManagedPtr & | operator= (bslmf::MovableRef< ManagedPtr > rhs) BSLS_KEYWORD_NOEXCEPT |
template<class BDE_OTHER_TYPE > | |
bsl::enable_if< bsl::is_convertible< BDE_OTHER_TYPE *, TARGET_TYPE * >::value, ManagedPtr< TARGET_TYPE > >::type & | operator= (bslmf::MovableRef< ManagedPtr< BDE_OTHER_TYPE > > rhs) BSLS_KEYWORD_NOEXCEPT |
ManagedPtr & | operator= (ManagedPtr_Ref< TARGET_TYPE > ref) BSLS_KEYWORD_NOEXCEPT |
ManagedPtr & | operator= (bsl::nullptr_t) |
template<class REFERENCED_TYPE > | |
operator ManagedPtr_Ref< REFERENCED_TYPE > () | |
void | clear () |
template<class MANAGED_TYPE > | |
void | load (MANAGED_TYPE *ptr) |
template<class MANAGED_TYPE , class FACTORY_TYPE > | |
void | load (MANAGED_TYPE *ptr, FACTORY_TYPE *factory) |
template<class MANAGED_TYPE > | |
void | load (MANAGED_TYPE *ptr, void *cookie, DeleterFunc deleter) |
void | load (bsl::nullptr_t=0, void *cookie=0, DeleterFunc deleter=0) |
template<class MANAGED_TYPE , class COOKIE_TYPE > | |
void | load (MANAGED_TYPE *ptr, COOKIE_TYPE *cookie, DeleterFunc deleter) |
template<class MANAGED_TYPE , class MANAGED_BASE > | |
void | load (MANAGED_TYPE *ptr, void *cookie, void(*deleter)(MANAGED_BASE *, void *)) |
template<class MANAGED_TYPE , class MANAGED_BASE , class COOKIE_TYPE , class COOKIE_BASE > | |
void | load (MANAGED_TYPE *ptr, COOKIE_TYPE *cookie, void(*deleter)(MANAGED_BASE *, COOKIE_BASE *)) |
template<class ALIASED_TYPE > | |
void | loadAlias (ManagedPtr< ALIASED_TYPE > &alias, TARGET_TYPE *ptr) |
ManagedPtr_PairProxy< TARGET_TYPE, ManagedPtrDeleter > | release () |
TARGET_TYPE * | release (ManagedPtrDeleter *deleter) |
void | reset () |
void | swap (ManagedPtr &other) |
operator BoolType () const | |
bslmf::AddReference< TARGET_TYPE >::Type | operator* () const |
TARGET_TYPE * | operator-> () const |
const ManagedPtrDeleter & | deleter () const |
TARGET_TYPE * | get () const |
TARGET_TYPE * | ptr () const |
Friends | |
template<class ALIASED_TYPE > | |
class | ManagedPtr |
This class is a "smart pointer" that refers to a target object accessed via a pointer to the specified parameter type, TARGET_TYPE
, and that supports sole ownership of a managed object that is potentially of a different type, and may be an entirely different object from the target object. A managed pointer ensures that the object it manages is destroyed when the managed pointer is destroyed (or re-assigned), using the "deleter" supplied along with the managed object. The target object referenced by a managed pointer may be accessed using either the ->
operator, or the dereference operator (operator *
). The specified TARGET_TYPE
may be const
-qualified, but may not be volatile
-qualified, nor may it be a reference type.
A managed pointer may be empty, in which case it neither refers to a target object nor owns a managed object. An empty managed pointer is the equivalent of a null pointer: Such a managed pointer is not de-referenceable, and tests as false
in boolean expressions.
A managed pointer for which the managed object is not the same object as the target is said to alias the managed object (see the section "Aliasing" in the component-level documentation).
See bslma_managedptr
typedef ManagedPtrDeleter::Deleter bslma::ManagedPtr< TARGET_TYPE >::DeleterFunc |
Alias for a function-pointer type for functions used to destroy the object managed by a ManagedPtr
object.
typedef TARGET_TYPE bslma::ManagedPtr< TARGET_TYPE >::element_type |
|
inline |
|
inlineexplicit |
Create a managed pointer having a target object referenced by the specified ptr
, owning the managed object *ptr
, and having a deleter that will call delete ptr
to destroy the managed object when invoked (e.g., when this managed pointer object is destroyed), unless 0 == ptr
, in which case create an empty managed pointer. The deleter will invoke the destructor of MANAGED_TYPE
rather than the destructor of TARGET_TYPE
. This constructor will not compile unless MANAGED_TYPE *
is convertible to TARGET_TYPE *
. Note that this behavior allows ManagedPtr
to be defined for void
pointers, and to call the correct destructor for the managed object, even if the destructor for TARGET_TYPE
is not declared as virtual
. The behavior is undefined unless the managed object (if any) can be destroyed by delete
, or if the lifetime of the managed object is already managed by another object.
|
inline |
Create a managed pointer having the same target object as the managed pointer referenced by the specified ref
, transfer ownership of the managed object owned by the managed pointer referenced by ref
, and reset the managed pointer referenced by ref
to empty. This constructor is used to create a managed pointer from a managed pointer rvalue, or from a managed pointer to a "compatible" type, where "compatible" means a built-in conversion from COMPATIBLE_TYPE *
to TARGET_TYPE *
is defined, e.g., derived *
to base *
, T *
to const T *
, or T *
to void *
.
|
inline |
|
inline |
Create a managed pointer having the same target object as the specified original
, transfer ownership of the object managed by original
(if any) to this managed pointer, and reset original
to empty.
|
inline |
Create a managed pointer having the same target object as the specified original
, transfer ownership of the object managed by original
(if any) to this managed pointer, and reset original
to empty. TARGET_TYPE
must be an accessible and unambiguous base of BDE_OTHER_TYPE
|
inline |
Create a managed pointer that takes ownership of the object managed by the specified alias
, but which uses the specified ptr
to refer to its target object, unless 0 == ptr
, in which case create an empty managed pointer. Reset alias
to empty if ownership of its managed object is transferred. The behavior is undefined if alias
is empty, but 0 != ptr
. Note that destroying or re-assigning a managed pointer created with this constructor will destroy the object originally managed by alias
(unless release
is called first); the destructor for *ptr
is not called directly.
|
inline |
|
inline |
Create a managed pointer having a target object referenced by the specified ptr
, owning the managed object *ptr
, and having a deleter that will call factory->deleteObject(ptr)
to destroy the managed object when invoked (e.g., when this managed pointer object is destroyed), unless 0 == ptr
, in which case create an empty managed pointer. The deleter will invoke the destructor of MANAGED_TYPE
rather than the destructor of TARGET_TYPE
. This constructor will not compile unless MANAGED_TYPE *
is convertible to TARGET_TYPE *
. The behavior is undefined unless the managed object (if any) can be destroyed by the specified factory
, or if 0 == factory && 0 != ptr
, or if the lifetime of the managed object is already managed by another object. Note that bslma::Allocator
, and any class publicly and unambiguously derived from bslma::Allocator
, meets the requirements for FACTORY_TYPE
.
|
inlineexplicit |
Create an empty managed pointer. Note that this constructor is necessary to match null-pointer literal arguments, in order to break ambiguities and provide valid type deduction with the other constructor templates in this class.
|
inline |
Create an empty managed pointer. Note that the specified factory
is ignored, as an empty managed pointer does not call its deleter.
|
inline |
Create a managed pointer having a target object referenced by the specified ptr
, owning the managed object *ptr
, and having a deleter that will invoke the specified deleter
with the address of the currently managed object, and with the specified cookie
(that the deleter can use for its own purposes), unless 0 == ptr
, in which case create an empty managed pointer. The behavior is undefined if ptr
is already managed by another object, or if 0 == deleter && 0 != ptr
. Note that this constructor is required only because the deprecated overloads cause an ambiguity in its absence; it should be removed when the deprecated overloads are removed.
|
inline |
Create a managed pointer having a target object referenced by the specified ptr
, owning the managed object *ptr
, and having a deleter that will invoke the specified deleter
with the address of the currently managed object, and with the specified cookie
(that the deleter can use for its own purposes), unless 0 == ptr
, in which case create an empty managed pointer. This constructor will not compile unless MANAGED_TYPE *
is convertible to TARGET_TYPE *
. The deleter will invoke the destructor of MANAGED_TYPE
rather than the destructor of TARGET_TYPE
. The behavior is undefined if ptr
is already managed by another object, or if 0 == deleter && 0 != ptr
.
|
inline |
[DEPRECATED]: Instead, use:
Create a managed pointer having a target object referenced by the specified ptr
, owning the managed object *ptr
, and having a deleter that will invoke the specified deleter
with the address of the currently managed object, and with the specified cookie
(that the deleter can use for its own purposes), unless 0 == ptr
, in which case create an empty managed pointer. This constructor will not compile unless MANAGED_TYPE *
is convertible to TARGET_TYPE *
, and MANAGED_TYPE *
is convertible to MANAGED_BASE *
. The deleter will invoke the destructor of MANAGED_TYPE
rather than the destructor of TARGET_TYPE
. The behavior is undefined if ptr
is already managed by another object, or if 0 == deleter && 0 != ptr
. Note that this constructor is needed only to avoid ambiguous type deductions when passing a null pointer literal as the cookie
when the user passes a deleter taking a type other than void *
for its object type. Also note that this function is deprecated as it relies on undefined compiler behavior for its implementation (that luckily performs as required on every platform supported by BDE).
|
inline |
[DEPRECATED]: Instead, use:
Create a managed pointer having a target object referenced by the specified ptr
, owning the managed object *ptr
, and having a deleter that will invoke the specified deleter
with the address of the currently managed object, and with the specified cookie
(that the deleter can use for its own purposes), unless 0 == ptr
, in which case create an empty managed pointer. This constructor will not compile unless MANAGED_TYPE *
is convertible to TARGET_TYPE *
, and MANAGED_TYPE *
is convertible to MANAGED_BASE *
. The deleter will invoke the destructor of MANAGED_TYPE
rather than the destructor of TARGET_TYPE
. The behavior is undefined if ptr
is already managed by another object, or if 0 == deleter && 0 != ptr
. Note that this function is deprecated as it relies on undefined compiler behavior for its implementation (that luckily performs as required on every platform supported by BDE).
|
inline |
Destroy this managed pointer object. Destroy the object managed by this managed pointer by invoking the user-supplied deleter, unless this managed pointer is empty, in which case the deleter will not be called.
|
inline |
[DEPRECATED] Use reset
instead.
Destroy the current managed object (if any) and reset this managed pointer to empty.
|
inline |
Return a reference to the non-modifiable deleter information associated with this managed pointer. The behavior is undefined if this managed pointer is empty.
|
inline |
Return the address of the target object, or 0 if this managed pointer is empty.
|
inline |
Destroy the current managed object (if any) and reset this managed pointer to empty. Note that the optionally specified cookie
and deleter
will be ignored, as empty managed pointers do not invoke a deleter.
|
inline |
Destroy the currently managed object, if any. Then, set the target object of this managed pointer to be that referenced by the specified ptr
, take ownership of *ptr
as the currently managed object, and set a deleter that uses the currently installed default allocator to destroy the managed object when invoked (e.g., when this managed pointer object is destroyed), unless 0 == ptr
, in which case reset this managed pointer to empty. The deleter will invoke the destructor of MANAGED_TYPE
rather than the destructor of TARGET_TYPE
. This function will not compile unless MANAGED_TYPE *
is convertible to TARGET_TYPE *
. The behavior is undefined unless the managed object (if any) can be destroyed by the currently installed default allocator, or if the lifetime of the managed object is already managed by another object.
|
inline |
|
inline |
[DEPRECATED]: Instead, use:
Destroy the currently managed object, if any. Then, set the target object of this managed pointer to be that referenced by the specified ptr
, take ownership of *ptr
as the currently managed object, and set a deleter that will invoke the specified deleter
with the address of the currently managed object, and with the specified cookie
(that the deleter can use for its own purposes), unless 0 == ptr
, in which case reset this managed pointer to empty. The behavior is undefined if ptr
is already managed by another object, or if 0 == deleter && 0 != ptr
. Note that this function is deprecated as it relies on undefined compiler behavior for its implementation, but luckily perform as required for all currently supported platforms; on platforms where the non-deprecated overload is not available (e.g., GCC 3.4) code should be written as if it were available, as an appropriate (deprecated) overload will be selected with the correct (non-deprecated) behavior.
|
inline |
Destroy the currently managed object, if any. Then, set the target object of this managed pointer to be that referenced by the specified ptr
, take ownership of *ptr
as the currently managed object, and set a deleter that calls factory->deleteObject(ptr)
to destroy the managed object when invoked (e.g., when this managed pointer object is destroyed), unless 0 == ptr
, in which case reset this managed pointer to empty. The deleter will invoke the destructor of MANAGED_TYPE
rather than the destructor of TARGET_TYPE
. This function will not compile unless MANAGED_TYPE *
is convertible to TARGET_TYPE *
. The behavior is undefined unless the managed object (if any) can be destroyed by the specified factory
, or if 0 == factory && 0 != ptr
, or if the the lifetime of the managed object is already managed by another object. Note that bslma::Allocator
, and any class publicly and unambiguously derived from bslma::Allocator
, meets the requirements for FACTORY_TYPE
.
|
inline |
Destroy the currently managed object, if any. Then, set the target object of this managed pointer to be that referenced by the specified ptr
, take ownership of *ptr
as the currently managed object, and set a deleter that will invoke the specified deleter
with the address of the currently managed object, and with the specified cookie
(that the deleter can use for its own purposes), unless 0 == ptr
, in which case reset this managed pointer to empty. The behavior is undefined if ptr
is already managed by another object, or if 0 == deleter && 0 != ptr
. Note that GCC 3.4 and earlier versions have a bug in template type deduction/overload resolution that causes ambiguities if this signature is available. This function will be restored on that platform once the deprecated signatures are finally removed.
|
inline |
void bslma::ManagedPtr< TARGET_TYPE >::loadAlias | ( | ManagedPtr< ALIASED_TYPE > & | alias, |
TARGET_TYPE * | ptr | ||
) |
If the specified alias
manages the same object as this managed pointer, set the target object of this managed pointer to be that referenced by the specified ptr
; otherwise, destroy the currently managed object (if any), and if alias
is empty, reset this managed pointer to empty; otherwise, transfer ownership (and the deleter) of the object managed by alias
, and set the target object of this managed pointer to be that referenced by ptr
. The behavior is undefined if 0 == ptr
and alias
is not empty, or if 0 != ptr
and alias
is empty, or if ptr
is already managed by a managed pointer other than alias
. Note that this establishes a managed pointer where ptr
aliases alias
. The managed object for alias
will ultimately be destroyed, and the destructor for ptr
is not called directly.
|
inline |
Return a value of "unspecified bool" type that evaluates to false
if this managed pointer is empty, and true
otherwise. Note that this conversion operator allows a managed pointer to be used within a conditional context, such as within an if
or while
statement, but does not allow managed pointers to be compared (e.g., via <
or >
). Also note that a superior solution is available in C++11 using the explicit operator bool()
syntax, that removes the need for a special boolean-like type and private equality-comparison operators.
|
inline |
Return a managed pointer reference, referring to this object. Note that this conversion operator is used implicitly to allow the construction of managed pointers from rvalues because temporaries cannot be passed by references offering modifiable access.
|
inline |
Return a reference to the target object. The behavior is undefined if this managed pointer is empty, or if TARGET_TYPE
is void
or const void
.
|
inline |
Return the address of the target object, or 0 if this managed pointer is empty.
|
inline |
Destroy the current managed object (if any) and reset this managed pointer to empty.
|
inline |
If this object and the specified rhs
manage the same object, return a reference to this managed pointer; otherwise, destroy the managed object owned by this managed pointer, transfer ownership of the managed object owned by rhs
to this managed pointer, set this managed pointer to point to the target object referenced by rhs
, reset rhs
to empty, and return a reference to this managed pointer.
|
inline |
If this object and the specified rhs
manage the same object, return a reference to this managed pointer; otherwise, destroy the managed object owned by this managed pointer, transfer ownership of the managed object owned by rhs
to this managed pointer, set this managed pointer to point to the target object referenced by rhs
, reset rhs
to empty, and return a reference to this managed pointer. TARGET_TYPE
must be an accessible and unambiguous base of BDE_OTHER_TYPE
|
inline |
|
inline |
If this object and the managed pointer reference by the specified ref
manage the same object, return a reference to this managed pointer; otherwise, destroy the managed object owned by this managed pointer, transfer ownership of the managed object owned by the managed pointer referenced by ref
, and set this managed pointer to point to the target object currently referenced the managed pointer referenced by ref
; then reset the managed pointer referenced by ref
to empty, and return a reference to this managed pointer. This operator is (implicitly) used to assign from a managed pointer rvalue, or from a managed pointer to a "compatible" type, where "compatible" means a built-in conversion from MANAGED_TYPE *
to TARGET_TYPE *
is defined, e.g., derived *
to base *
, T *
to const T *
, or T *
to void *
.
|
inline |
[DEPRECATED]: Use get
instead.
Return the address of the target object, or 0 if this managed pointer is empty.
ManagedPtr_PairProxy< TARGET_TYPE, ManagedPtrDeleter > bslma::ManagedPtr< TARGET_TYPE >::release | ( | ) |
Return a raw pointer to the current target object (if any) and the deleter for the currently managed object, and reset this managed pointer to empty. It is undefined behavior to run the returned deleter unless the returned pointer to target object is not null.
TARGET_TYPE * bslma::ManagedPtr< TARGET_TYPE >::release | ( | ManagedPtrDeleter * | deleter | ) |
Load the specified deleter
for the currently managed object and reset this managed pointer to empty. Return a raw pointer to the target object (if any) managed by this pointer. It is undefined behavior to run the returned deleter unless the returned pointer to target object is not null.
|
inline |
Destroy the current managed object (if any) and reset this managed pointer to empty.
|
inline |
Exchange the value and ownership of this managed pointer with the specified other
managed pointer.