ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
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:

  • 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 than that of std::shared_ptr.
  • 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. Allocators can be of "heavy" weight and are never copied by value.
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 62 of file sharedptr.hpp.

#include <sharedptr.hpp>

Collaboration diagram for SharedPtr< T, TAllocator >:
[legend]

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>
 SharedPtr (TAllocator &allocator, TArgs &&... args)
 
template<typename... TArgs>
 SharedPtr (TArgs &&... args)
 
 ~SharedPtr ()
 
lang::AllocatorInterface< TAllocator > AI () const noexcept
 
T * Get () const
 
AllocatorTypeGetAllocator () const
 
template<typename TDerived , typename... TArgs>
void InsertDerived (TAllocator &allocator, TArgs &&... args)
 
template<typename TDerived , typename... TArgs>
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 AllocatorType = TAllocator

Exposes the allocator as given with template parameter TAllocator.

Definition at line 127 of file sharedptr.hpp.

◆ StoredType

template<typename T , typename TAllocator = HeapAllocator>
using StoredType = T

Exposes the stored type specified with template parameter T.

Definition at line 130 of file sharedptr.hpp.

Field Details:

◆ members

template<typename T , typename TAllocator = HeapAllocator>
FieldMembers<void*>* members
protected

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

Definition at line 105 of file sharedptr.hpp.

Constructor(s) / Destructor Details:

◆ SharedPtr() [1/6]

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

Default Constructor. Leaves this object nulled.

Definition at line 133 of file sharedptr.hpp.

◆ SharedPtr() [2/6]

template<typename T , typename TAllocator = HeapAllocator>
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 138 of file sharedptr.hpp.

◆ SharedPtr() [3/6]

template<typename T , typename TAllocator = HeapAllocator>
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 143 of file sharedptr.hpp.

◆ SharedPtr() [4/6]

template<typename T , typename TAllocator = HeapAllocator>
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 149 of file sharedptr.hpp.

◆ SharedPtr() [5/6]

template<typename T , typename TAllocator = HeapAllocator>
template<typename... TArgs>
SharedPtr ( TAllocator & allocator,
TArgs &&... args )

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.
Parameters
allocatorThe allocator used to allocate and free needed storage.
Template Parameters
TArgsThe argument types used for constructing T.
Parameters
argsThe arguments for constructing T.

◆ SharedPtr() [6/6]

template<typename T , typename TAllocator = HeapAllocator>
template<typename... TArgs>
SharedPtr ( TArgs &&... args)

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
TArgsThe argument types used for constructing T.
Parameters
argsThe arguments for constructing T.

◆ ~SharedPtr()

template<typename T , typename TAllocator = HeapAllocator>
~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 318 of file sharedptr.hpp.

Here is the call graph for this function:

Method Details:

◆ AI()

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

Definition at line 342 of file sharedptr.hpp.

Here is the call graph for this function:

◆ Get()

template<typename T , typename TAllocator = HeapAllocator>
T * Get ( ) const
inline

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

Returns
A pointer to T.

Definition at line 382 of file sharedptr.hpp.

Here is the call graph for this function:

◆ GetAllocator()

template<typename T , typename TAllocator = HeapAllocator>
AllocatorType & 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 335 of file sharedptr.hpp.

Here is the call graph for this function:

◆ getP()

template<typename T , typename TAllocator = HeapAllocator>
T * 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 109 of file sharedptr.hpp.

◆ getR()

template<typename T , typename TAllocator = HeapAllocator>
T & 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 118 of file sharedptr.hpp.

◆ InsertDerived() [1/2]

template<typename T , typename TAllocator = HeapAllocator>
template<typename TDerived , typename... TArgs>
void InsertDerived ( TAllocator & allocator,
TArgs &&... args )

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
TArgsThe argument types used for constructing T.
Parameters
allocatorThe allocator to use.
argsThe arguments for constructing T.

◆ InsertDerived() [2/2]

template<typename T , typename TAllocator = HeapAllocator>
template<typename TDerived , typename... TArgs>
void InsertDerived ( TArgs &&... args)

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
TArgsThe argument types used for constructing T.
Parameters
argsThe arguments for constructing T.

◆ IsNulled()

template<typename T , typename TAllocator = HeapAllocator>
bool IsNulled ( ) const
inlinenoexcept

Returns true if this is an empty instance.

Returns
true if UseCount is 0, false otherwise.

Definition at line 364 of file sharedptr.hpp.

◆ operator bool()

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

Definition at line 378 of file sharedptr.hpp.

◆ operator!=()

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

Comparison with nullptr.

Returns
false if UseCount is greater than 0.

Definition at line 375 of file sharedptr.hpp.

◆ operator*()

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

Overloaded operator to access members of custom type T

Returns
A pointer to T.

Definition at line 390 of file sharedptr.hpp.

Here is the call graph for this function:

◆ operator->()

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

Overloaded operator to access members of custom type T

Returns
A pointer to T.

Definition at line 386 of file sharedptr.hpp.

Here is the call graph for this function:

◆ operator=() [1/3]

template<typename T , typename TAllocator = HeapAllocator>
SharedPtr & 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 158 of file sharedptr.hpp.

Here is the call graph for this function:

◆ operator=() [2/3]

template<typename T , typename TAllocator = HeapAllocator>
SharedPtr & 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 182 of file sharedptr.hpp.

Here is the call graph for this function:

◆ operator=() [3/3]

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

Assignment of nullptr. Same as SetNulled.

Definition at line 367 of file sharedptr.hpp.

Here is the call graph for this function:

◆ operator==()

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

Comparison with nullptr.

Returns
true if UseCount is greater than 0.

Definition at line 371 of file sharedptr.hpp.

◆ SetNulled()

template<typename T , typename TAllocator = HeapAllocator>
void 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 360 of file sharedptr.hpp.

Here is the call graph for this function:

◆ SizeOfAllocation()

template<typename T , typename TAllocator = HeapAllocator>
template<typename TStored = T>
static constexpr size_t 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 331 of file sharedptr.hpp.

◆ Unique()

template<typename T , typename TAllocator = HeapAllocator>
bool Unique ( ) const
inlinenoexcept

Returns true if the UseCount is 1.

Returns
true if this instance is set but not shared.

Definition at line 354 of file sharedptr.hpp.

◆ UseCount()

template<typename T , typename TAllocator = HeapAllocator>
unsigned int 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 349 of file sharedptr.hpp.


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