This templated class is an alternative for C++ standard library type std::shared_ptr with important restrictions:
std::shared_ptr
, especially in consideration with dynamic (virtual) types.std::weak_ptr
and corresponding functionality.owner_before
and corresponding comparison operators.The advantages are:
sizeof(void*)
, where the standard's type has a size of two pointers.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.
T | The custom type that is shared with this pointer. |
TAllocator | The 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 54 of file sharedval.inl.
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, typename TRequires = TAllocator> requires (!std::default_initializable<TRequires>) | |
SharedVal (TAllocator &allocator, TArgs &&... args) | |
template<typename... TArgs, typename TRequires = TAllocator> requires ( std::default_initializable<TRequires> && (sizeof...(TArgs) > 0) && ( sizeof...(TArgs) != 1 || !std::same_as<std::decay_t<std::tuple_element_t<0, std::tuple<TArgs...>>>, SharedVal> ) && std::is_constructible_v<T, TArgs...> ) | |
SharedVal (TArgs &&... args) | |
~SharedVal () | |
T * | Get () const |
AllocatorType & | GetAllocator () const |
bool | IsNulled () const noexcept |
operator bool () const noexcept | |
bool | operator!= (std::nullptr_t) const noexcept |
T & | operator* () const |
T * | operator-> () const |
SharedVal & | operator= (const SharedVal &other) noexcept |
SharedVal & | operator= (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: | |
FieldMembers * | members |
The stored data. | |
Protected Method Index: | |
T * | getP () const noexcept |
T & | getR () const noexcept |
using alib::containers::SharedVal< T, TAllocator >::AllocatorType = TAllocator |
Exposes the allocator as given with template parameter TAllocator.
Definition at line 135 of file sharedval.inl.
|
protected |
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 113 of file sharedval.inl.
using alib::containers::SharedVal< T, TAllocator >::StoredType = T |
Exposes the stored type specified with template parameter T.
Definition at line 138 of file sharedval.inl.
|
protected |
The stored data.
Definition at line 118 of file sharedval.inl.
|
inlinenoexcept |
Default Constructor. Leaves this object nulled.
Definition at line 141 of file sharedval.inl.
|
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 146 of file sharedval.inl.
|
inlinenoexcept |
Copy Constructor. Increases the reference counter of the shared pointer (in case given other is not nulled).
other | The object to copy. |
Definition at line 151 of file sharedval.inl.
|
inlinenoexcept |
Move Constructor. Does not increase the reference counter, instead nulls the other.
other | The object to copy. |
Definition at line 157 of file sharedval.inl.
|
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.
TArgs | The argument types used for constructing T. |
TRequires | Defaulted template parameter. Must not be specified. |
allocator | The allocator used to allocate and free needed storage. |
args | The arguments for constructing T. |
Definition at line 216 of file sharedval.inl.
|
inline |
Constructor missing the allocator instance. To be used only with allocators that are default-constructible (like HeapAllocator is).
TArgs | The argument types used for constructing T. |
TRequires | Defaulted template parameter. Must not be specified. |
args | The arguments for constructing T. |
Definition at line 246 of file sharedval.inl.
|
inline |
Destructor. If this is the last copy, the destructor of T is invoked and the memory is freed to TAllocator.
Definition at line 251 of file sharedval.inl.
|
inline |
Returns a non-constant pointer to the stored object of type T.
Definition at line 308 of file sharedval.inl.
|
inline |
0
. Definition at line 264 of file sharedval.inl.
|
inlineprotectednoexcept |
Internal shortcut to receive the custom member.
nullptr
in case this is empty. Definition at line 122 of file sharedval.inl.
|
inlineprotectednoexcept |
Internal shortcut to receive a reference to the custom member. Asserts in debug-compilations.
Definition at line 128 of file sharedval.inl.
|
inlinenoexcept |
Returns true
if this is an empty instance.
true
if UseCount is 0
, false
otherwise. Definition at line 290 of file sharedval.inl.
|
inlinenoexcept |
true
if this instance is not nulled, false
otherwise. Definition at line 304 of file sharedval.inl.
|
inlinenoexcept |
Comparison with nullptr
.
false
if UseCount is greater than 0
, false
otherwise. Definition at line 301 of file sharedval.inl.
|
inline |
Overloaded operator to access members of custom type T
Definition at line 316 of file sharedval.inl.
|
inline |
Overloaded operator to access members of custom type T
Definition at line 312 of file sharedval.inl.
|
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.
other | The object to copy into this one. |
this
. Definition at line 166 of file sharedval.inl.
|
inlinenoexcept |
Move Assignment Operator. Cares for self-assignment. Otherwise, the object in other is copied to this.
other | The object to move into this one. |
this
. Definition at line 187 of file sharedval.inl.
|
inline |
Assignment of nullptr
. Same as SetNulled.
Definition at line 293 of file sharedval.inl.
|
inlinenoexcept |
Comparison with nullptr
.
true
if UseCount is greater than 0
, false
otherwise. Definition at line 297 of file sharedval.inl.
|
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 286 of file sharedval.inl.
|
inlinestaticconstexpr |
Definition at line 260 of file sharedval.inl.
|
inlinenoexcept |
Returns true
if the UseCount is 1
.
true
if this instance is set but not shared. Definition at line 280 of file sharedval.inl.
|
inlinenoexcept |
Returns the number of shared usages. In a multithreaded environment, the value returned is approximate.
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 275 of file sharedval.inl.