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

Description:

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

This templated class is an alternative for C++ standard library type std::shared_ptr with important restrictions:

  • Instead of managing a pointer, a value is managed.
  • It is not possible to store derived types within this type, which is a common use case with std::shared_ptr, especially in consideration with dynamic (virtual) types.
    This implies that no abstract types can be stored.
  • 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).

The advantages are:

  • The type has a footprint of only sizeof(void*), where the standard's type has a size of two pointers.
  • An internal second pointer-dereferencing is avoided when accessing the shared value.
  • 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.

Note that despite being named SharedVal, which is in contrast to sibling type SharedPtr, the type still behaves like a pointer. It can be nulled, value nullptr can be assigned, and member access is performed with the operator->. And of-course the object is destructed, and the memory is freed in case the last copy of an instance is nulled or gets out of scope. A different naming proposal could have been SharedStaticPtr to indicate that no dynamic conversions and abstract types are applicable.

See also
  • Class SharedPtr which allows storing derived, dynamic types.
  • 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 65 of file sharedval.hpp.

#include <sharedval.hpp>

Inner Type Index:

struct  FieldMembersNoTA
 Fields if TAllocator is default constructible (e.g., HeapAllocator). More...
 
struct  FieldMembersWithAllocator
 Fields if TAllocator is not default constructible (not HeapAllocator). More...
 

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:

static constexpr size_t SizeOfAllocation ()
 

Public Method Index:

 SharedVal () noexcept
 Default Constructor. Leaves this object nulled.
 
 SharedVal (const SharedVal &other) noexcept
 
 SharedVal (SharedVal &&other) noexcept
 
 SharedVal (std::nullptr_t) noexcept
 
template<typename... TArgs>
 SharedVal (TAllocator &allocator, TArgs &&... args)
 
template<typename... TArgs>
 SharedVal (TArgs &&... args)
 
 ~SharedVal ()
 
T * Get () const
 
AllocatorTypeGetAllocator () const
 
bool IsNulled () const noexcept
 
 operator bool () const noexcept
 
bool operator!= (std::nullptr_t) const noexcept
 
T & operator* () const
 
T * operator-> () const
 
SharedValoperator= (const SharedVal &other) noexcept
 
SharedValoperator= (SharedVal &&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 Type Index:

using FieldMembers
 

Protected Field Index:

FieldMembersmembers
 The stored data.
 

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 145 of file sharedval.hpp.

◆ FieldMembers

template<typename T , typename TAllocator = HeapAllocator>
using FieldMembers
protected
Initial value:
ATMP_IF_T_F( std::is_default_constructible<TAllocator>::value,
FieldMembersNoTA,
FieldMembersWithAllocator)
#define ATMP_IF_T_F( Cond, T, F)
Definition tmp.hpp:50

The type of the stored data. Note that we cannot use helper AllocatorMember, because type T could be derived from the same base class and this would break EBO rules, that force the compiler to give two distinguishable members, inherited or not, a different address.

Definition at line 123 of file sharedval.hpp.

◆ StoredType

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

Exposes the stored type specified with template parameter T.

Definition at line 148 of file sharedval.hpp.

Field Details:

◆ members

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

The stored data.

Definition at line 128 of file sharedval.hpp.

Constructor(s) / Destructor Details:

◆ SharedVal() [1/6]

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

Default Constructor. Leaves this object nulled.

Definition at line 151 of file sharedval.hpp.

◆ SharedVal() [2/6]

template<typename T , typename TAllocator = HeapAllocator>
SharedVal ( 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 156 of file sharedval.hpp.

◆ SharedVal() [3/6]

template<typename T , typename TAllocator = HeapAllocator>
SharedVal ( const SharedVal< 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 161 of file sharedval.hpp.

◆ SharedVal() [4/6]

template<typename T , typename TAllocator = HeapAllocator>
SharedVal ( SharedVal< T, TAllocator > && other)
inlinenoexcept

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

Parameters
otherThe object to copy.

Definition at line 167 of file sharedval.hpp.

◆ SharedVal() [5/6]

template<typename T , typename TAllocator = HeapAllocator>
template<typename... TArgs>
SharedVal ( 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.

◆ SharedVal() [6/6]

template<typename T , typename TAllocator = HeapAllocator>
template<typename... TArgs>
SharedVal ( 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.

◆ ~SharedVal()

template<typename T , typename TAllocator = HeapAllocator>
~SharedVal ( )
inline

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

Definition at line 267 of file sharedval.hpp.

Method Details:

◆ 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 324 of file sharedval.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 280 of file sharedval.hpp.

◆ 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 132 of file sharedval.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 138 of file sharedval.hpp.

◆ 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 306 of file sharedval.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 320 of file sharedval.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, false otherwise.

Definition at line 317 of file sharedval.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 332 of file sharedval.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 328 of file sharedval.hpp.

Here is the call graph for this function:

◆ operator=() [1/3]

template<typename T , typename TAllocator = HeapAllocator>
SharedVal & operator= ( const SharedVal< 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 176 of file sharedval.hpp.

◆ operator=() [2/3]

template<typename T , typename TAllocator = HeapAllocator>
SharedVal & operator= ( SharedVal< 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 197 of file sharedval.hpp.

◆ operator=() [3/3]

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

Assignment of nullptr. Same as SetNulled.

Definition at line 309 of file sharedval.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, false otherwise.

Definition at line 313 of file sharedval.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 302 of file sharedval.hpp.

Here is the call graph for this function:

◆ SizeOfAllocation()

template<typename T , typename TAllocator = HeapAllocator>
static constexpr size_t SizeOfAllocation ( )
inlinestaticconstexpr
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 276 of file sharedval.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 296 of file sharedval.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 291 of file sharedval.hpp.


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