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 compared to std::shared_ptr
:
- 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.
- 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. (In this case, of course, the footprint increases to two times the
sizeof(void*)
.)
- 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
-
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 51 of file sharedptr.inl.
template<typename T, typename TAllocator = HeapAllocator>
template<typename... TArgs, typename TRequires = TAllocator>
requires ( !std::default_initializable<TRequires> )
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 203 of file sharedptr.inl.
template<typename T, typename TAllocator = HeapAllocator>
template<typename TDerived, typename TRequires = TAllocator, typename... TArgs>
requires ( !std::is_default_constructible_v<TRequires> )
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
-
TRequires | Defaulted template parameter. Must not be specified. |
TArgs | The argument types used for constructing T. |
- Parameters
-
allocator | The allocator to use. |
args | The arguments for constructing T. |
Definition at line 274 of file sharedptr.inl.
template<typename T, typename TAllocator = HeapAllocator>
template<typename TDerived, typename TRequires = TAllocator, typename... TArgs>
requires std::is_default_constructible_v<TRequires>
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
-
TRequires | Defaulted template parameter. Must not be specified. |
TArgs | The argument types used for constructing T. |
- Parameters
-
args | The arguments for constructing T. |
Definition at line 249 of file sharedptr.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 147 of file sharedptr.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 333 of file sharedptr.inl.