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 54 of file sharedval.inl.
template<typename T, typename TAllocator = HeapAllocator>
template<typename... TArgs, typename TRequires = TAllocator>
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
-
| TArgs | The argument types used for constructing T. |
| TRequires | Defaulted template parameter. Must not be specified. |
- Parameters
-
| allocator | The allocator used to allocate and free needed storage. |
| args | The arguments for constructing T. |
Definition at line 216 of file sharedval.inl.
template<typename T, typename TAllocator = HeapAllocator>
template<typename... TArgs, typename TRequires = TAllocator>
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.
-
If this was not done, this constructor was chosen for copy constructors of this type. In this case, the compilation of this constructor would fail when initializing field member.
- Template Parameters
-
| TArgs | The argument types used for constructing T. |
| TRequires | Defaulted template parameter. Must not be specified. |
- Parameters
-
| args | The arguments for constructing T. |
Definition at line 246 of file sharedval.inl.
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 166 of file sharedval.inl.
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 286 of file sharedval.inl.