Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CountingPtr< Type, Deleter > Class Template Reference

Detailed Description

template<typename Type, typename Deleter = CountingPtrDefaultDeleter>
class tlx::CountingPtr< Type, Deleter >

High-performance smart pointer used as a wrapping reference counting pointer.

This smart pointer class requires two functions in the template type: void inc_reference() and void dec_reference(). These must increment and decrement a reference count inside the templated object. When initialized, the type must have reference count zero. Each new object referencing the data calls inc_reference() and each destroying holder calls del_reference(). When the data object determines that it's internal count is zero, then it must destroy itself.

Accompanying the CountingPtr is a class ReferenceCounter, from which reference counted classes may be derive from. The class ReferenceCounter implement all methods required for reference counting.

The whole method is more similar to boost's instrusive_ptr, but also yields something resembling std::shared_ptr. However, compared to std::shared_ptr, this class only contains a single pointer, while shared_ptr contains two which are only related if constructed with std::make_shared.

Another advantage with this method is that no kludges like std::enable_shared_from_this are needed.

Definition at line 65 of file counting_ptr.hpp.

+ Inheritance diagram for CountingPtr< Type, Deleter >:

#include <counting_ptr.hpp>

Public Types

using element_type = Type
 contained type. More...
 

Public Member Functions

 CountingPtr () noexcept
 default constructor: contains a nullptr pointer. More...
 
 CountingPtr (std::nullptr_t) noexcept
 implicit conversion from nullptr_t: contains a nullptr pointer. More...
 
 CountingPtr (Type *ptr) noexcept
 constructor from pointer: initializes new reference to ptr. More...
 
 CountingPtr (const CountingPtr &other) noexcept
 copy-constructor: also initializes new reference to ptr. More...
 
template<typename Subclass , typename = typename std::enable_if< std::is_convertible<Subclass*, Type*>::value, void>::type>
 CountingPtr (const CountingPtr< Subclass, Deleter > &other) noexcept
 copy-constructor: also initializes new reference to ptr. More...
 
 CountingPtr (CountingPtr &&other) noexcept
 move-constructor: just moves pointer, does not change reference counts. More...
 
template<typename Subclass , typename = typename std::enable_if< std::is_convertible<Subclass*, Type*>::value, void>::type>
 CountingPtr (CountingPtr< Subclass, Deleter > &&other) noexcept
 move-constructor: just moves pointer, does not change reference counts. More...
 
 ~CountingPtr ()
 destructor: decrements reference count in ptr. More...
 
bool empty () const noexcept
 test for a nullptr pointer More...
 
Type * get () const noexcept
 return the enclosed pointer. More...
 
 operator bool () const noexcept
 cast to bool checks for a nullptr pointer More...
 
bool operator!= (const CountingPtr &other) const noexcept
 test inequality of only the pointer values. More...
 
bool operator!= (Type *other) const noexcept
 test inequality of only the address pointed to More...
 
Type & operator* () const noexcept
 return the enclosed object as reference. More...
 
Type * operator-> () const noexcept
 return the enclosed pointer. More...
 
CountingPtroperator= (const CountingPtr &other) noexcept
 
template<typename Subclass , typename = typename std::enable_if< std::is_convertible<Subclass*, Type*>::value, void>::type>
CountingPtroperator= (const CountingPtr< Subclass, Deleter > &other) noexcept
 
CountingPtroperator= (CountingPtr &&other) noexcept
 move-assignment operator: move reference of other to current object. More...
 
template<typename Subclass , typename = typename std::enable_if< std::is_convertible<Subclass*, Type*>::value, void>::type>
CountingPtroperator= (CountingPtr< Subclass, Deleter > &&other) noexcept
 move-assignment operator: move reference of other to current object. More...
 
bool operator== (const CountingPtr &other) const noexcept
 test equality of only the pointer values. More...
 
bool operator== (Type *other) const noexcept
 test equality of only the address pointed to More...
 
void reset ()
 release contained pointer, frees object if this is the last reference. More...
 
void swap (CountingPtr &b) noexcept
 
void unify ()
 make and refer a copy if the original object was shared. More...
 
bool unique () const noexcept
 if the object is referred by this CountingPtr only More...
 
bool valid () const noexcept
 test for a non-nullptr pointer More...
 

Private Member Functions

void dec_reference () noexcept
 decrement reference count of current object and maybe delete it. More...
 
void inc_reference (Type *o) noexcept
 increment reference count of object. More...
 

Private Attributes

Type * ptr_
 the pointer to the currently referenced object. More...
 

Member Typedef Documentation

using element_type = Type

contained type.

Definition at line 69 of file counting_ptr.hpp.

Constructor & Destructor Documentation

CountingPtr ( )
inlinenoexcept

default constructor: contains a nullptr pointer.

Definition at line 91 of file counting_ptr.hpp.

Referenced by CountingPtr< WriteStream >::unify().

CountingPtr ( std::nullptr_t  )
inlinenoexcept

implicit conversion from nullptr_t: contains a nullptr pointer.

Definition at line 95 of file counting_ptr.hpp.

CountingPtr ( Type *  ptr)
inlineexplicitnoexcept

constructor from pointer: initializes new reference to ptr.

Definition at line 99 of file counting_ptr.hpp.

CountingPtr ( const CountingPtr< Type, Deleter > &  other)
inlinenoexcept

copy-constructor: also initializes new reference to ptr.

Definition at line 104 of file counting_ptr.hpp.

CountingPtr ( const CountingPtr< Subclass, Deleter > &  other)
inlinenoexcept

copy-constructor: also initializes new reference to ptr.

Definition at line 112 of file counting_ptr.hpp.

CountingPtr ( CountingPtr< Type, Deleter > &&  other)
inlinenoexcept

move-constructor: just moves pointer, does not change reference counts.

Definition at line 117 of file counting_ptr.hpp.

CountingPtr ( CountingPtr< Subclass, Deleter > &&  other)
inlinenoexcept

move-constructor: just moves pointer, does not change reference counts.

Definition at line 125 of file counting_ptr.hpp.

~CountingPtr ( )
inline

destructor: decrements reference count in ptr.

Definition at line 179 of file counting_ptr.hpp.

Member Function Documentation

void dec_reference ( )
inlineprivatenoexcept

decrement reference count of current object and maybe delete it.

Definition at line 80 of file counting_ptr.hpp.

Referenced by CountingPtr< WriteStream >::operator=(), CountingPtr< WriteStream >::reset(), and CountingPtr< WriteStream >::~CountingPtr().

bool empty ( ) const
inlinenoexcept
void inc_reference ( Type *  o)
inlineprivatenoexcept

increment reference count of object.

Definition at line 76 of file counting_ptr.hpp.

Referenced by CountingPtr< WriteStream >::CountingPtr(), and CountingPtr< WriteStream >::operator=().

operator bool ( ) const
inlinenoexcept

cast to bool checks for a nullptr pointer

Definition at line 217 of file counting_ptr.hpp.

bool operator!= ( const CountingPtr< Type, Deleter > &  other) const
inlinenoexcept

test inequality of only the pointer values.

Definition at line 201 of file counting_ptr.hpp.

bool operator!= ( Type *  other) const
inlinenoexcept

test inequality of only the address pointed to

Definition at line 209 of file counting_ptr.hpp.

Type& operator* ( ) const
inlinenoexcept

return the enclosed object as reference.

Definition at line 182 of file counting_ptr.hpp.

Type* operator-> ( ) const
inlinenoexcept

return the enclosed pointer.

Definition at line 188 of file counting_ptr.hpp.

CountingPtr& operator= ( const CountingPtr< Type, Deleter > &  other)
inlinenoexcept

copy-assignment operator: acquire reference on new one and dereference current object.

Definition at line 131 of file counting_ptr.hpp.

Referenced by PinnedByteBlockPtr::operator=().

CountingPtr& operator= ( const CountingPtr< Subclass, Deleter > &  other)
inlinenoexcept

copy-assignment operator: acquire reference on new one and dereference current object.

Definition at line 146 of file counting_ptr.hpp.

CountingPtr& operator= ( CountingPtr< Type, Deleter > &&  other)
inlinenoexcept

move-assignment operator: move reference of other to current object.

Definition at line 156 of file counting_ptr.hpp.

CountingPtr& operator= ( CountingPtr< Subclass, Deleter > &&  other)
inlinenoexcept

move-assignment operator: move reference of other to current object.

Definition at line 169 of file counting_ptr.hpp.

bool operator== ( const CountingPtr< Type, Deleter > &  other) const
inlinenoexcept

test equality of only the pointer values.

Definition at line 197 of file counting_ptr.hpp.

bool operator== ( Type *  other) const
inlinenoexcept

test equality of only the address pointed to

Definition at line 205 of file counting_ptr.hpp.

void swap ( CountingPtr< Type, Deleter > &  b)
inlinenoexcept

swap enclosed object with another counting pointer (no reference counts need change)

Definition at line 242 of file counting_ptr.hpp.

void unify ( )
inline

make and refer a copy if the original object was shared.

Definition at line 229 of file counting_ptr.hpp.

bool unique ( ) const
inlinenoexcept

if the object is referred by this CountingPtr only

Definition at line 225 of file counting_ptr.hpp.

Member Data Documentation


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