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
-
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 65 of file sharedval.hpp.
template<typename T , typename TAllocator = HeapAllocator>
Initial value: ATMP_IF_T_F( std::is_default_constructible<TAllocator>::value,
FieldMembersNoTA,
FieldMembersWithAllocator)
#define ATMP_IF_T_F( Cond, T, F)
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.
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
-
allocator | The allocator used to allocate and free needed storage. |
- Template Parameters
-
TArgs | The argument types used for constructing T. |
- Parameters
-
args | The arguments for constructing T. |
template<typename T , typename TAllocator = HeapAllocator>
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
-
other | The object to copy into this one. |
- Returns
- A reference to
this
.
Definition at line 176 of file sharedval.hpp.
template<typename T , typename TAllocator = HeapAllocator>
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.