![]() |
PEARL
Parallel Event Access and Replay Library
|
Class template providing a very basic "resource acquisition is initialization" facility. More...
#include <pearl/ScopedPtr.h>
Public Types | |
typedef BaseT | ElementType |
Base type of the managed pointer. More... | |
Public Member Functions | |
Constructors & destructor | |
ScopedPtr (BaseT *ptr=0) | |
Constructs a scoped pointer. More... | |
~ScopedPtr () | |
Destructor. More... | |
Value access | |
BaseT * | get () const |
Returns the managed pointer. More... | |
BaseT * | release () |
Releases ownership of the managed pointer. More... | |
BaseT & | operator* () const |
Indirection operator. More... | |
BaseT * | operator-> () const |
Member-of-pointer operator. More... | |
Value modification | |
void | reset (BaseT *ptr=0) |
Replaces the managed pointer. More... | |
void | swap (ScopedPtr &rhs) |
Swaps the managed pointers. More... | |
Type conversion | |
operator BoolType () const | |
Checks if not empty. More... | |
Related Functions | |
(Note that these are not member functions.) | |
Swap algorithm overload | |
template<typename BaseT > | |
void | swap (ScopedPtr< BaseT > &lhs, ScopedPtr< BaseT > &rhs) |
Overloads the std::swap algorithm for ScopedPtr instances. More... | |
The class template ScopedPtr provides a very basic and lightweight smart pointer to implement the "resource acquisition is initialization" idiom. A ScopedPtr instance manages a pointer to a dynamically allocated object of type BaseT
. It guarantees that the object is deleted by calling its delete
operator when the ScopedPtr instance goes out of scope.
Using a ScopedPtr incurs no space and typically also no time overhead compared to using a plain pointer (assuming a decent optimizing compiler). In contrast to std::auto_ptr
(deprecated), ScopedPtr instances cannot be copied, and thus only allow the transfer of ownership by explicitly releasing the managed pointer from the source instance. For example:
To entirely prohibit transfer of ownership, a const ScopedPtr
can be used.
A ScopedPtr instance may be constructed from an incomplete type BaseT
. BaseT
must be complete at the point where its destructor is invoked, which happens in the destructor and the reset() member function.
BaseT | Base type of the managed pointer, i.e., the type of the pointee. The behavior of this class is undefined if either the destructor or the delete operator for objects of type BaseT throws exceptions. |
typedef BaseT pearl::ScopedPtr< BaseT >::ElementType |
|
explicit |
Constructs a new instance which takes ownership of the provided pointer ptr
. ptr
must have been allocated via a C++ new
expression or be nullptr
.
ptr | Pointer to be managed \n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\n |
pearl::ScopedPtr< BaseT >::~ScopedPtr |
Deletes the object pointed to by the managed pointer by calling its delete
operator.
\n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\n Depends on the requirement that the destructor and the `delete` operator for objects of type @p BaseT do not throw exceptions.
BaseT * pearl::ScopedPtr< BaseT >::get |
Returns the managed pointer without releasing ownership.
\n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\n
pearl::ScopedPtr< BaseT >::operator BoolType |
Conversion to a value of unspecified type which, when used in a boolean context, indicates whether the instance currently manages a pointer (i.e., if the stored pointer is not nullptr
).
bool
; true if not empty, false otherwise \n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\n
BaseT & pearl::ScopedPtr< BaseT >::operator* |
Returns a reference to the object pointed to by the managed pointer. The behavior of this operator is undefined if the managed pointer is nullptr
.
\n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\n
BaseT * pearl::ScopedPtr< BaseT >::operator-> |
Returns the managed pointer. The behavior of this operator is undefined if the managed pointer is nullptr
.
\n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\n
BaseT * pearl::ScopedPtr< BaseT >::release |
Returns the managed pointer and releases ownership. After the call, the instance holds nullptr
.
\n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\n
void pearl::ScopedPtr< BaseT >::reset | ( | BaseT * | ptr = 0 | ) |
Deletes the object pointed to by the current managed pointer by calling its delete
operator, and takes ownership of the provided pointer ptr
. ptr
must have been allocated via a C++ new
expression or be nullptr
.
ptr | New pointer to be managed \n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\nDepends on the requirement that the destructor and the delete operator for objects of type BaseT do not throw exceptions. |
void pearl::ScopedPtr< BaseT >::swap | ( | ScopedPtr< BaseT > & | rhs | ) |
Exchanges the managed pointer of the instance with the managed pointer of rhs
, transferring ownership between the two ScopedPtr instances without destroying either pointee.
rhs | Another ScopedPtr of the same type \n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\n |
Overloads the std::swap
algorithm for ScopedPtr instances. Exchanges the managed pointers of the two instances, transferring ownership between them without destroying either pointee.
BaseT | Base type of the pointers managed by lhs and rhs |
lhs | Left-hand side: Scoped pointer whose contents to swap |
rhs | Right-hand side: Scoped pointer whose contents to swap \n@par Exception safety\n <b class="paramname">No-throw guarantee:</b> never throws exceptions. <br>\n |
![]() |
Copyright © 1998–2019 Forschungszentrum Jülich GmbH,
Jülich Supercomputing Centre
Copyright © 2009–2015 German Research School for Simulation Sciences GmbH, Laboratory for Parallel Programming |