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:
- 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 than that of
std::shared_ptr
.
- 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. Allocators can be of "heavy" weight and are never copied by value.
- 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 62 of file sharedptr.hpp.
template<typename T , typename TAllocator = HeapAllocator>
template<typename... TArgs>
SharedPtr |
( |
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>
template<typename TDerived , typename... TArgs>
void InsertDerived |
( |
TAllocator & | allocator, |
|
|
TArgs &&... | args ) |
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
-
TArgs | The argument types used for constructing T. |
- Parameters
-
allocator | The allocator to use. |
args | The arguments for constructing T. |
template<typename T , typename TAllocator = HeapAllocator>
template<typename TDerived , typename... TArgs>
void InsertDerived |
( |
TArgs &&... | args | ) |
|
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
-
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 158 of file sharedptr.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 360 of file sharedptr.hpp.