ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
alib::containers::SharedPtr< T, TAllocator > Class Template Reference

Description:

template<typename T, typename TAllocator = HeapAllocator>
class alib::containers::SharedPtr< T, TAllocator >

This templated class lifts restrictions imposed by (otherwise slightly more efficient) class SharedVal and is an almost full-featured alternative for C++ standard library type std::shared_ptr .

What is missing compared to std::shared_ptr:

  • Pointers to externally allocated objects cannot be assigned. The shared objects are always created with construction of this class or with method InsertDerived .
  • Special method InsertDerived is to be used, in case types derived from T are to be placed inside.
  • Overall, the interface is more explicit and seems less convenient.
  • This implementation misses the coexistence of sibling type std::weak_ptr and corresponding functionality.
  • This implementation misses an equivalent to method owner_before and corresponding comparison operators.
  • This implementation misses dedicated array support (at least as of today).

Advantages are:

  • The type has a footprint of only sizeof(void*), where the standard's type has a size of two pointers.
  • The type performs only one allocation. (Common implementations of the standard's type perform two allocations.)
  • The type supports storing references to ALib {lang;Allocator;allocators} which are used for allocation and freeing of memory. (In this case, of course, the footprint increases to two times the sizeof(void*).)
See also
  • Sibling class SharedVal, with is a restricted but slightly more efficient version of this class.
  • Class TSharedMonoVal of module ALib Monomem, which incorporates an own embedded instance of class TMonoAllocator. This allocator can be used for further monotonic allocations by the contained type or other code entities that receive the shared pointer.
Template Parameters
TThe custom type that is shared with this pointer.
TAllocatorThe allocator that is used to allocate an instance of T together with a reference counter and optionally a reference to such allocator if passed with construction.

Definition at line 51 of file sharedptr.inl.

Inner Type Index:

struct  FieldMembers
 

Public Type Index:

using AllocatorType = TAllocator
 Exposes the allocator as given with template parameter TAllocator.
 
using StoredType = T
 Exposes the stored type specified with template parameter T.
 

Public Static Method Index:

template<typename TStored = T>
static constexpr size_t SizeOfAllocation () noexcept
 

Public Method Index:

 SharedPtr () noexcept
 Default Constructor. Leaves this object nulled.
 
 SharedPtr (const SharedPtr &other) noexcept
 
 SharedPtr (SharedPtr &&other) noexcept
 
 SharedPtr (std::nullptr_t) noexcept
 
template<typename... TArgs, typename TRequires = TAllocator>
requires ( !std::default_initializable<TRequires> )
 SharedPtr (TAllocator &allocator, TArgs &&... args)
 
template<typename... TArgs, typename TRequires = TAllocator>
requires ( std::is_default_constructible_v<TRequires> && !std::is_same_v<std::decay_t<TArgs>... , SharedPtr> && (sizeof...(TArgs) > 0) )
 SharedPtr (TArgs &&... args)
 
 ~SharedPtr ()
 
lang::AllocatorInterface< TAllocator > AI () const noexcept
 
T * Get () const
 
AllocatorTypeGetAllocator () const
 
template<typename TDerived, typename TRequires = TAllocator, typename... TArgs>
requires ( !std::is_default_constructible_v<TRequires> )
void InsertDerived (TAllocator &allocator, TArgs &&... args)
 
template<typename TDerived, typename TRequires = TAllocator, typename... TArgs>
requires std::is_default_constructible_v<TRequires>
void InsertDerived (TArgs &&... args)
 
bool IsNulled () const noexcept
 
 operator bool () const noexcept
 
bool operator!= (std::nullptr_t) const noexcept
 
T & operator* () const
 
T * operator-> () const
 
SharedPtroperator= (const SharedPtr &other) noexcept
 
SharedPtroperator= (SharedPtr &&other) noexcept
 
void operator= (std::nullptr_t)
 Assignment of nullptr. Same as SetNulled.
 
bool operator== (std::nullptr_t) const noexcept
 
void SetNulled ()
 
bool Unique () const noexcept
 
unsigned int UseCount () const noexcept
 

Protected Field Index:

FieldMembers< void * > * members
 The allocated stored data. Note that T can be a derived type.
 

Protected Method Index:

T * getP () const noexcept
 
T & getR () const noexcept
 

Type Definition Details:

◆ AllocatorType

template<typename T, typename TAllocator = HeapAllocator>
using alib::containers::SharedPtr< T, TAllocator >::AllocatorType = TAllocator

Exposes the allocator as given with template parameter TAllocator.

Definition at line 116 of file sharedptr.inl.

◆ StoredType

template<typename T, typename TAllocator = HeapAllocator>
using alib::containers::SharedPtr< T, TAllocator >::StoredType = T

Exposes the stored type specified with template parameter T.

Definition at line 119 of file sharedptr.inl.

Field Details:

◆ members

template<typename T, typename TAllocator = HeapAllocator>
FieldMembers<void*>* alib::containers::SharedPtr< T, TAllocator >::members
protected

The allocated stored data. Note that T can be a derived type.

Definition at line 94 of file sharedptr.inl.

Constructor(s) / Destructor Details:

◆ SharedPtr() [1/6]

template<typename T, typename TAllocator = HeapAllocator>
alib::containers::SharedPtr< T, TAllocator >::SharedPtr ( )
inlinenoexcept

Default Constructor. Leaves this object nulled.

Definition at line 122 of file sharedptr.inl.

◆ SharedPtr() [2/6]

template<typename T, typename TAllocator = HeapAllocator>
alib::containers::SharedPtr< T, TAllocator >::SharedPtr ( std::nullptr_t )
inlinenoexcept

Constructs an empty instance from std::nullptr. This constructor is necessary to allow assignment of std::nullptr to values of this type, which clears the automatic pointer.

Definition at line 127 of file sharedptr.inl.

◆ SharedPtr() [3/6]

template<typename T, typename TAllocator = HeapAllocator>
alib::containers::SharedPtr< T, TAllocator >::SharedPtr ( const SharedPtr< T, TAllocator > & other)
inlinenoexcept

Copy Constructor. Increases the reference counter of the shared pointer (in case given other is not nulled).

Parameters
otherThe object to copy.

Definition at line 132 of file sharedptr.inl.

◆ SharedPtr() [4/6]

template<typename T, typename TAllocator = HeapAllocator>
alib::containers::SharedPtr< T, TAllocator >::SharedPtr ( SharedPtr< T, TAllocator > && other)
inlinenoexcept

Move Constructor. Does not increase the reference counter, instead nulls the other.

Parameters
otherThe object to copy.

Definition at line 138 of file sharedptr.inl.

◆ SharedPtr() [5/6]

template<typename T, typename TAllocator = HeapAllocator>
template<typename... TArgs, typename TRequires = TAllocator>
requires ( !std::default_initializable<TRequires> )
alib::containers::SharedPtr< T, TAllocator >::SharedPtr ( TAllocator & allocator,
TArgs &&... args )
inline

Constructor taking an allocator along with the construction parameters for the instance of T. The allocator is used allocate the needed memory (one allocation) and the reference to it is internally stored, to be able to free the memory later.

Note
This constructor is accepted by the compiler only if template type TAllocator is not default-constructible.
Template Parameters
TArgsThe argument types used for constructing T.
TRequiresDefaulted template parameter. Must not be specified.
Parameters
allocatorThe allocator used to allocate and free needed storage.
argsThe arguments for constructing T.

Definition at line 203 of file sharedptr.inl.

◆ SharedPtr() [6/6]

template<typename T, typename TAllocator = HeapAllocator>
template<typename... TArgs, typename TRequires = TAllocator>
requires ( std::is_default_constructible_v<TRequires> && !std::is_same_v<std::decay_t<TArgs>... , SharedPtr> && (sizeof...(TArgs) > 0) )
alib::containers::SharedPtr< T, TAllocator >::SharedPtr ( TArgs &&... args)
inline

Constructor missing the allocator instance. To be used only with allocators that are default-constructible (like HeapAllocator is).

Note
This constructor is accepted by the compiler only if
  • TAllocator is default-constructible, and
  • the variadic argument list is not empty (here default construction is chosen), and
  • the variadic arguments would not match to the copy or move constructor, and
  • the variadic arguments are constructing type T.
Template Parameters
TRequiresDefaulted template parameter. Must not be specified.
TArgsThe argument types used for constructing T.
Parameters
argsThe arguments for constructing T.

Definition at line 229 of file sharedptr.inl.

◆ ~SharedPtr()

template<typename T, typename TAllocator = HeapAllocator>
alib::containers::SharedPtr< T, TAllocator >::~SharedPtr ( )
inline

Destructor. If this is the last copy, the destructor of T is invoked and the memory is freed to TAllocator.

Definition at line 291 of file sharedptr.inl.

Method Details:

◆ AI()

template<typename T, typename TAllocator = HeapAllocator>
lang::AllocatorInterface< TAllocator > alib::containers::SharedPtr< T, TAllocator >::AI ( ) const
inlinenoexcept
Returns
The allocator interface of the allocator received with construction.

Definition at line 315 of file sharedptr.inl.

◆ Get()

template<typename T, typename TAllocator = HeapAllocator>
T * alib::containers::SharedPtr< T, TAllocator >::Get ( ) const
inline

Returns a non-constant pointer to the stored object of type T.

Returns
A pointer to T.

Definition at line 355 of file sharedptr.inl.

◆ GetAllocator()

template<typename T, typename TAllocator = HeapAllocator>
AllocatorType & alib::containers::SharedPtr< T, TAllocator >::GetAllocator ( ) const
inline
Returns
The allocator given with construction that will be used to free the memory that had been allocated, at the moment the use counter becomes 0.

Definition at line 308 of file sharedptr.inl.

◆ getP()

template<typename T, typename TAllocator = HeapAllocator>
T * alib::containers::SharedPtr< T, TAllocator >::getP ( ) const
inlineprotectednoexcept

Internal shortcut to receive the custom member.

Returns
The pointer to the contained type, or nullptr in case this is empty.

Definition at line 98 of file sharedptr.inl.

◆ getR()

template<typename T, typename TAllocator = HeapAllocator>
T & alib::containers::SharedPtr< T, TAllocator >::getR ( ) const
inlineprotectednoexcept

Internal shortcut to receive a reference to the custom member. Asserts in debug-compilations.

Returns
A reference the contained type.

Definition at line 107 of file sharedptr.inl.

◆ InsertDerived() [1/2]

template<typename T, typename TAllocator = HeapAllocator>
template<typename TDerived, typename TRequires = TAllocator, typename... TArgs>
requires ( !std::is_default_constructible_v<TRequires> )
void alib::containers::SharedPtr< T, TAllocator >::InsertDerived ( TAllocator & allocator,
TArgs &&... args )
inline

Disposes any currently held data (in case this was the last user, the current object is deleted and memory freed) and places a new instance of (potentially) derived type TDerived in this object.

This overload of the method is accepted by the compiler only if type TAllocator is not default-constructible.

Template Parameters
TRequiresDefaulted template parameter. Must not be specified.
TArgsThe argument types used for constructing T.
Parameters
allocatorThe allocator to use.
argsThe arguments for constructing T.

Definition at line 274 of file sharedptr.inl.

◆ InsertDerived() [2/2]

template<typename T, typename TAllocator = HeapAllocator>
template<typename TDerived, typename TRequires = TAllocator, typename... TArgs>
requires std::is_default_constructible_v<TRequires>
void alib::containers::SharedPtr< T, TAllocator >::InsertDerived ( TArgs &&... args)
inline

Disposes any currently held data (in case this was the last user, the current object is deleted and memory freed) and places a new instance of (potentially) derived type TDerived in this object.

This overload of the method is accepted by the compiler only if type TAllocator is default-constructible.

Template Parameters
TRequiresDefaulted template parameter. Must not be specified.
TArgsThe argument types used for constructing T.
Parameters
argsThe arguments for constructing T.

Definition at line 249 of file sharedptr.inl.

◆ IsNulled()

template<typename T, typename TAllocator = HeapAllocator>
bool alib::containers::SharedPtr< T, TAllocator >::IsNulled ( ) const
inlinenoexcept

Returns true if this is an empty instance.

Returns
true if UseCount is 0, false otherwise.

Definition at line 337 of file sharedptr.inl.

◆ operator bool()

template<typename T, typename TAllocator = HeapAllocator>
alib::containers::SharedPtr< T, TAllocator >::operator bool ( ) const
inlinenoexcept
Returns
true if this instance is not nulled, false otherwise.

Definition at line 351 of file sharedptr.inl.

◆ operator!=()

template<typename T, typename TAllocator = HeapAllocator>
bool alib::containers::SharedPtr< T, TAllocator >::operator!= ( std::nullptr_t ) const
inlinenoexcept

Comparison with nullptr.

Returns
false if UseCount is greater than 0.

Definition at line 348 of file sharedptr.inl.

◆ operator*()

template<typename T, typename TAllocator = HeapAllocator>
T & alib::containers::SharedPtr< T, TAllocator >::operator* ( ) const
inline

Overloaded operator to access members of custom type T

Returns
A pointer to T.

Definition at line 363 of file sharedptr.inl.

◆ operator->()

template<typename T, typename TAllocator = HeapAllocator>
T * alib::containers::SharedPtr< T, TAllocator >::operator-> ( ) const
inline

Overloaded operator to access members of custom type T

Returns
A pointer to T.

Definition at line 359 of file sharedptr.inl.

◆ operator=() [1/3]

template<typename T, typename TAllocator = HeapAllocator>
SharedPtr & alib::containers::SharedPtr< T, TAllocator >::operator= ( const SharedPtr< T, TAllocator > & other)
inlinenoexcept

Copy Assignment Operator. Cares for self-assignment and assignment of a shared pointer with the same content. Otherwise, the reference counter of the current object is decreased, disposed if necessary, and then the object in other is copied to this object.

Parameters
otherThe object to copy into this one.
Returns
A reference to this.

Definition at line 147 of file sharedptr.inl.

◆ operator=() [2/3]

template<typename T, typename TAllocator = HeapAllocator>
SharedPtr & alib::containers::SharedPtr< T, TAllocator >::operator= ( SharedPtr< T, TAllocator > && other)
inlinenoexcept

Move Assignment Operator. Cares for self-assignment. Otherwise, the object in other is copied to this.

Parameters
otherThe object to move into this one.
Returns
A reference to this.

Definition at line 171 of file sharedptr.inl.

◆ operator=() [3/3]

template<typename T, typename TAllocator = HeapAllocator>
void alib::containers::SharedPtr< T, TAllocator >::operator= ( std::nullptr_t )
inline

Assignment of nullptr. Same as SetNulled.

Definition at line 340 of file sharedptr.inl.

◆ operator==()

template<typename T, typename TAllocator = HeapAllocator>
bool alib::containers::SharedPtr< T, TAllocator >::operator== ( std::nullptr_t ) const
inlinenoexcept

Comparison with nullptr.

Returns
true if UseCount is greater than 0.

Definition at line 344 of file sharedptr.inl.

◆ SetNulled()

template<typename T, typename TAllocator = HeapAllocator>
void alib::containers::SharedPtr< T, TAllocator >::SetNulled ( )
inline

Sets this object to nulled state, as if default constructed or nullptr was assigned. If no shared copy exists, all data is destructed and memory is freed.
As an alternative to this method, nullptr can be assigned.

Definition at line 333 of file sharedptr.inl.

◆ SizeOfAllocation()

template<typename T, typename TAllocator = HeapAllocator>
template<typename TStored = T>
constexpr size_t alib::containers::SharedPtr< T, TAllocator >::SizeOfAllocation ( )
inlinestaticconstexprnoexcept
Returns
The size of the memory that is allocated for the T as well as for the reference counter and the allocator member. (To whom it may concern.)

Definition at line 304 of file sharedptr.inl.

◆ Unique()

template<typename T, typename TAllocator = HeapAllocator>
bool alib::containers::SharedPtr< T, TAllocator >::Unique ( ) const
inlinenoexcept

Returns true if the UseCount is 1.

Returns
true if this instance is set but not shared.

Definition at line 327 of file sharedptr.inl.

◆ UseCount()

template<typename T, typename TAllocator = HeapAllocator>
unsigned int alib::containers::SharedPtr< T, TAllocator >::UseCount ( ) const
inlinenoexcept

Returns the number of shared usages. In a multithreaded environment, the value returned is approximate.

Returns
The number of shared usages. If this instance was default-constructed, moved, method SetNulled was called, or nullptr was assigned, then 0 is returned.

Definition at line 322 of file sharedptr.inl.


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