tlx
CountingPtr< Type, Deleter > Class Template Reference

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

#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
 copy-assignment operator: acquire reference on new one and dereference current object. More...
 
template<typename Subclass , typename = typename std::enable_if< std::is_convertible<Subclass*, Type*>::value, void>::type>
CountingPtroperator= (const CountingPtr< Subclass, Deleter > &other) noexcept
 copy-assignment operator: acquire reference on new one and dereference current object. More...
 
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
 Returns the number of different shared_ptr instances managing the current object. More...
 
Modifiers
void reset ()
 release contained pointer, frees object if this is the last reference. More...
 
void swap (CountingPtr &b) noexcept
 swap enclosed object with another counting pointer (no reference counts need change) More...
 
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 inc_reference (Type *o) noexcept
 increment reference count of object. More...
 
void dec_reference () noexcept
 decrement reference count of current object and maybe delete it. More...
 

Private Attributes

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

Friends

template<typename Other , typename OtherDeleter >
class CountingPtr
 all CountingPtr are friends such that they may steal pointers. More...
 

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.

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 94 of file counting_ptr.hpp.

CountingPtr ( std::nullptr_t  )
inlinenoexcept

implicit conversion from nullptr_t: contains a nullptr pointer.

Definition at line 98 of file counting_ptr.hpp.

CountingPtr ( Type *  ptr)
inlineexplicitnoexcept

constructor from pointer: initializes new reference to ptr.

Definition at line 102 of file counting_ptr.hpp.

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 ( const CountingPtr< Subclass, Deleter > &  other)
inlinenoexcept

copy-constructor: also initializes new reference to ptr.

Definition at line 115 of file counting_ptr.hpp.

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 ( 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 ( )
inline

destructor: decrements reference count in ptr.

Definition at line 182 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.

bool empty ( ) const
inlinenoexcept

test for a nullptr pointer

Definition at line 213 of file counting_ptr.hpp.

Type* get ( ) const
inlinenoexcept

return the enclosed pointer.

Definition at line 202 of file counting_ptr.hpp.

void inc_reference ( Type *  o)
inlineprivatenoexcept

increment reference count of object.

Definition at line 76 of file counting_ptr.hpp.

operator bool ( ) const
inlinenoexcept

cast to bool checks for a nullptr pointer

Definition at line 209 of file counting_ptr.hpp.

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.

bool operator!= ( Type *  other) const
inlinenoexcept

test inequality of only the address pointed to

Definition at line 265 of file counting_ptr.hpp.

Type& operator* ( ) const
inlinenoexcept

return the enclosed object as reference.

Definition at line 190 of file counting_ptr.hpp.

Type* operator-> ( ) const
inlinenoexcept

return the enclosed pointer.

Definition at line 196 of file counting_ptr.hpp.

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

compare the pointer values.

Definition at line 269 of file counting_ptr.hpp.

bool operator< ( Type *  other) const
inlinenoexcept

compare the pointer values.

Definition at line 285 of file counting_ptr.hpp.

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

compare the pointer values.

Definition at line 273 of file counting_ptr.hpp.

bool operator<= ( Type *  other) const
inlinenoexcept

compare the pointer values.

Definition at line 289 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 134 of file counting_ptr.hpp.

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.

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.

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.

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.

bool operator== ( Type *  other) const
inlinenoexcept

test equality of only the address pointed to

Definition at line 261 of file counting_ptr.hpp.

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

compare the pointer values.

Definition at line 277 of file counting_ptr.hpp.

bool operator> ( Type *  other) const
inlinenoexcept

compare the pointer values.

Definition at line 293 of file counting_ptr.hpp.

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

compare the pointer values.

Definition at line 281 of file counting_ptr.hpp.

bool operator>= ( Type *  other) const
inlinenoexcept

compare the pointer values.

Definition at line 297 of file counting_ptr.hpp.

void reset ( )
inline

release contained pointer, frees object if this is the last reference.

Definition at line 231 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 238 of file counting_ptr.hpp.

void unify ( )
inline

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

Definition at line 242 of file counting_ptr.hpp.

bool unique ( ) const
inlinenoexcept

if the object is referred by this CountingPtr only

Definition at line 217 of file counting_ptr.hpp.

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.

bool valid ( ) const
inlinenoexcept

test for a non-nullptr pointer

Definition at line 205 of file counting_ptr.hpp.

Friends And Related Function Documentation

friend class CountingPtr
friend

all CountingPtr are friends such that they may steal pointers.

Definition at line 88 of file counting_ptr.hpp.

Member Data Documentation

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: