PEARL
Parallel Event Access and Replay Library
Classes | Public Types | List of all members
pearl::ScopedPtr< BaseT > Class Template Reference

Class template providing a very basic "resource acquisition is initialization" facility. More...

#include <pearl/ScopedPtr.h>

Inheritance diagram for pearl::ScopedPtr< BaseT >:
pearl::uncopyable::Uncopyable

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...
 

Detailed Description

template<typename BaseT>
class pearl::ScopedPtr< BaseT >

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:

destination.reset(source.release());

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.

Template Parameters
BaseTBase 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.

Member Typedef Documentation

◆ ElementType

template<typename BaseT >
typedef BaseT pearl::ScopedPtr< BaseT >::ElementType

Constructor & Destructor Documentation

◆ ScopedPtr()

template<typename BaseT >
pearl::ScopedPtr< BaseT >::ScopedPtr ( BaseT *  ptr = 0)
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.

Parameters
ptrPointer to be managed
        \n@par Exception safety\n        <b class="paramname">No-throw guarantee:</b>         never throws exceptions.        <br>\n

◆ ~ScopedPtr()

template<typename BaseT >
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.

Member Function Documentation

◆ get()

template<typename BaseT >
BaseT * pearl::ScopedPtr< BaseT >::get

Returns the managed pointer without releasing ownership.

Returns
Managed pointer
        \n@par Exception safety\n        <b class="paramname">No-throw guarantee:</b>         never throws exceptions.        <br>\n

◆ operator BoolType()

template<typename BaseT >
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).

Returns
Value which implicitly converts to bool; true if not empty, false otherwise
        \n@par Exception safety\n        <b class="paramname">No-throw guarantee:</b>         never throws exceptions.        <br>\n

◆ operator*()

template<typename BaseT >
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.

Returns
Reference to pointee
        \n@par Exception safety\n        <b class="paramname">No-throw guarantee:</b>         never throws exceptions.        <br>\n

◆ operator->()

template<typename BaseT >
BaseT * pearl::ScopedPtr< BaseT >::operator->

Returns the managed pointer. The behavior of this operator is undefined if the managed pointer is nullptr.

Returns
Managed pointer
        \n@par Exception safety\n        <b class="paramname">No-throw guarantee:</b>         never throws exceptions.        <br>\n

◆ release()

template<typename BaseT >
BaseT * pearl::ScopedPtr< BaseT >::release

Returns the managed pointer and releases ownership. After the call, the instance holds nullptr.

Returns
Managed pointer
        \n@par Exception safety\n        <b class="paramname">No-throw guarantee:</b>         never throws exceptions.        <br>\n

◆ reset()

template<typename BaseT >
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.

Parameters
ptrNew pointer to be managed
        \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 BaseT do not throw exceptions.

◆ swap()

template<typename BaseT >
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.

Parameters
rhsAnother ScopedPtr of the same type
        \n@par Exception safety\n        <b class="paramname">No-throw guarantee:</b>         never throws exceptions.        <br>\n

Friends And Related Function Documentation

◆ swap()

template<typename BaseT >
void swap ( ScopedPtr< BaseT > &  lhs,
ScopedPtr< BaseT > &  rhs 
)
related

Overloads the std::swap algorithm for ScopedPtr instances. Exchanges the managed pointers of the two instances, transferring ownership between them without destroying either pointee.

Template Parameters
BaseTBase type of the pointers managed by lhs and rhs
Parameters
lhsLeft-hand side: Scoped pointer whose contents to swap
rhsRight-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

The documentation for this class was generated from the following files:

Scalasca    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