Thrill  0.1
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

Construction, Assignment and Destruction
 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...
 
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...
 
 ~CountingPtr ()
 destructor: decrements reference count in ptr. More...
 
Observers
Type & operator* () const noexcept
 return the enclosed object as reference. More...
 
Type * operator-> () const noexcept
 return the enclosed pointer. More...
 
Type * get () const noexcept
 return the enclosed pointer. More...
 
bool valid () const noexcept
 test for a non-nullptr pointer More...
 
 operator bool () const noexcept
 cast to bool checks for a nullptr pointer More...
 
bool empty () const noexcept
 test for a nullptr pointer More...
 
bool unique () const noexcept
 if the object is referred by this CountingPtr only More...
 
size_t use_count () const noexcept
 
Modifiers
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...
 
Comparison Operators
bool operator== (const CountingPtr &other) const noexcept
 test equality of only the pointer values. More...
 
bool operator!= (const CountingPtr &other) const noexcept
 test inequality of only the pointer values. More...
 
bool operator== (Type *other) const noexcept
 test equality of only the address pointed to More...
 
bool operator!= (Type *other) const noexcept
 test inequality of only the address pointed to More...
 
bool operator< (const CountingPtr &other) const noexcept
 compare the pointer values. More...
 
bool operator<= (const CountingPtr &other) const noexcept
 compare the pointer values. More...
 
bool operator> (const CountingPtr &other) const noexcept
 compare the pointer values. More...
 
bool operator>= (const CountingPtr &other) const noexcept
 compare the pointer values. More...
 
bool operator< (Type *other) const noexcept
 compare the pointer values. More...
 
bool operator<= (Type *other) const noexcept
 compare the pointer values. More...
 
bool operator> (Type *other) const noexcept
 compare the pointer values. More...
 
bool operator>= (Type *other) const noexcept
 compare the pointer values. 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

◆ element_type

using element_type = Type

contained type.

Definition at line 69 of file counting_ptr.hpp.

Constructor & Destructor Documentation

◆ CountingPtr() [1/7]

CountingPtr ( )
inlinenoexcept

default constructor: contains a nullptr pointer.

Definition at line 94 of file counting_ptr.hpp.

◆ CountingPtr() [2/7]

CountingPtr ( std::nullptr_t  )
inlinenoexcept

implicit conversion from nullptr_t: contains a nullptr pointer.

Definition at line 98 of file counting_ptr.hpp.

◆ CountingPtr() [3/7]

CountingPtr ( Type *  ptr)
inlineexplicitnoexcept

constructor from pointer: initializes new reference to ptr.

Definition at line 102 of file counting_ptr.hpp.

◆ CountingPtr() [4/7]

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

copy-constructor: also initializes new reference to ptr.

Definition at line 107 of file counting_ptr.hpp.

◆ CountingPtr() [5/7]

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

copy-constructor: also initializes new reference to ptr.

Definition at line 115 of file counting_ptr.hpp.

◆ CountingPtr() [6/7]

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

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

Definition at line 120 of file counting_ptr.hpp.

◆ CountingPtr() [7/7]

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

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

Definition at line 128 of file counting_ptr.hpp.

◆ ~CountingPtr()

~CountingPtr ( )
inline

destructor: decrements reference count in ptr.

Definition at line 182 of file counting_ptr.hpp.

Member Function Documentation

◆ dec_reference()

void dec_reference ( )
inlineprivatenoexcept

decrement reference count of current object and maybe delete it.

Definition at line 80 of file counting_ptr.hpp.

◆ empty()

bool empty ( ) const
inlinenoexcept

◆ get()

◆ inc_reference()

void inc_reference ( Type *  o)
inlineprivatenoexcept

increment reference count of object.

Definition at line 76 of file counting_ptr.hpp.

◆ operator bool()

operator bool ( ) const
inlinenoexcept

cast to bool checks for a nullptr pointer

Definition at line 209 of file counting_ptr.hpp.

◆ operator!=() [1/2]

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

test inequality of only the pointer values.

Definition at line 257 of file counting_ptr.hpp.

◆ operator!=() [2/2]

bool operator!= ( Type *  other) const
inlinenoexcept

test inequality of only the address pointed to

Definition at line 265 of file counting_ptr.hpp.

◆ operator*()

Type& operator* ( ) const
inlinenoexcept

return the enclosed object as reference.

Definition at line 190 of file counting_ptr.hpp.

◆ operator->()

Type* operator-> ( ) const
inlinenoexcept

return the enclosed pointer.

Definition at line 196 of file counting_ptr.hpp.

◆ operator<() [1/2]

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

compare the pointer values.

Definition at line 269 of file counting_ptr.hpp.

◆ operator<() [2/2]

bool operator< ( Type *  other) const
inlinenoexcept

compare the pointer values.

Definition at line 285 of file counting_ptr.hpp.

◆ operator<=() [1/2]

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

compare the pointer values.

Definition at line 273 of file counting_ptr.hpp.

◆ operator<=() [2/2]

bool operator<= ( Type *  other) const
inlinenoexcept

compare the pointer values.

Definition at line 289 of file counting_ptr.hpp.

◆ operator=() [1/4]

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

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

Definition at line 134 of file counting_ptr.hpp.

Referenced by PinnedByteBlockPtr::operator=().

◆ operator=() [2/4]

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

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

Definition at line 149 of file counting_ptr.hpp.

◆ operator=() [3/4]

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

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

Definition at line 159 of file counting_ptr.hpp.

◆ operator=() [4/4]

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

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

Definition at line 172 of file counting_ptr.hpp.

◆ operator==() [1/2]

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

test equality of only the pointer values.

Definition at line 253 of file counting_ptr.hpp.

◆ operator==() [2/2]

bool operator== ( Type *  other) const
inlinenoexcept

test equality of only the address pointed to

Definition at line 261 of file counting_ptr.hpp.

◆ operator>() [1/2]

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

compare the pointer values.

Definition at line 277 of file counting_ptr.hpp.

◆ operator>() [2/2]

bool operator> ( Type *  other) const
inlinenoexcept

compare the pointer values.

Definition at line 293 of file counting_ptr.hpp.

◆ operator>=() [1/2]

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

compare the pointer values.

Definition at line 281 of file counting_ptr.hpp.

◆ operator>=() [2/2]

bool operator>= ( Type *  other) const
inlinenoexcept

compare the pointer values.

Definition at line 297 of file counting_ptr.hpp.

◆ reset()

◆ swap()

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

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

Definition at line 238 of file counting_ptr.hpp.

◆ unify()

void unify ( )
inline

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

Definition at line 242 of file counting_ptr.hpp.

◆ unique()

bool unique ( ) const
inlinenoexcept

if the object is referred by this CountingPtr only

Definition at line 217 of file counting_ptr.hpp.

◆ use_count()

size_t use_count ( ) const
inlinenoexcept

Returns the number of different shared_ptr instances managing the current object.

Definition at line 222 of file counting_ptr.hpp.

◆ valid()

Member Data Documentation

◆ ptr_

Type* ptr_
private

the pointer to the currently referenced object.

Definition at line 73 of file counting_ptr.hpp.


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