8#ifndef HPP_ALIB_CONTAINERS_SHARED_PTR
9#define HPP_ALIB_CONTAINERS_SHARED_PTR 1
19namespace alib {
namespace containers {
61template<
typename T,
typename TAllocator= HeapAllocator>
88 FieldMembers( TAllocator& pAllocator, T* pCustom,
size_t pAllocSize)
121 return reinterpret_cast<T&
>(
members->
u);
150 :
members(other.members) { other.members=
nullptr; }
161 if (
this == &other || this->members == other.members)
172 if((
members= other.members) !=
nullptr)
197 other.members=
nullptr;
212 template<
typename... TArgs>
215 template<
typename... TArgs,
216 typename TEnableIf= TAllocator,
217 ATMP_IF(!std::is_default_constructible<TEnableIf>::value) >
218 SharedPtr( TAllocator& allocator, TArgs&&... args )
220 auto* mem= allocator().template Alloc<FieldMembers<T>>();
223 new (&mem->u) T(std::forward<TArgs>(args)...);
241 template<
typename... TArgs>
244 template <
typename... TArgs,
typename TEnableIf= TAllocator,
245 ATMP_IF( std::is_default_constructible_v<TEnableIf>
247 && (
sizeof...(TArgs) > 0) ) >
253 new (&mem->u) T(std::forward<TArgs>(args)...);
268 template<
typename TDerived,
typename... TArgs>
271 template <
typename TDerived,
typename TEnableIf= TAllocator,
typename... TArgs,
272 ATMP_IF( std::is_default_constructible_v<TEnableIf> ) >
282 new (&mem->u) TDerived(std::forward<TArgs>(args)...);
297 template<
typename TDerived,
typename... TArgs>
300 template <
typename TDerived,
typename TEnableIf= TAllocator,
typename... TArgs,
301 ATMP_IF( !std::is_default_constructible_v<TEnableIf> ) >
307 auto* mem= allocator().template Alloc<FieldMembers<TDerived>>();
312 new (&mem->u) TDerived(std::forward<TArgs>(args)...);
330 template<
typename TStored= T>
378 operator bool() const noexcept {
return members !=
nullptr; }
397template<
typename T,
typename TAllocator= lang::HeapAllocator>
void InsertDerived(TAllocator &allocator, TArgs &&... args)
lang::AllocatorInterface< TAllocator > AI() const noexcept
void InsertDerived(TArgs &&... args)
AllocatorType & GetAllocator() const
TAllocator AllocatorType
Exposes the allocator as given with template parameter TAllocator.
static constexpr size_t SizeOfAllocation() noexcept
bool Unique() const noexcept
SharedPtr(std::nullptr_t) noexcept
T StoredType
Exposes the stored type specified with template parameter T.
void operator=(std::nullptr_t)
Assignment of nullptr. Same as SetNulled.
bool operator==(std::nullptr_t) const noexcept
FieldMembers< void * > * members
The allocated stored data. Note that T can be a derived type.
bool IsNulled() const noexcept
SharedPtr & operator=(SharedPtr &&other) noexcept
unsigned int UseCount() const noexcept
SharedPtr(TAllocator &allocator, TArgs &&... args)
SharedPtr(TArgs &&... args)
bool operator!=(std::nullptr_t) const noexcept
SharedPtr() noexcept
Default Constructor. Leaves this object nulled.
T & getR() const noexcept
SharedPtr(const SharedPtr &other) noexcept
SharedPtr(SharedPtr &&other) noexcept
T * getP() const noexcept
SharedPtr & operator=(const SharedPtr &other) noexcept
#define ATMP_EQ( T, TEqual)
#define ALIB_ASSERT_ERROR(cond,...)
static ALIB_FORCE_INLINE void Destruct(T &object)
TTo * SafeCast(TFrom *derived)
U u
The instance, either derived or T.
size_t allocSize
The size of the allocated pair of these fields and the custom type.
FieldMembers(T *pCustom, size_t pAllocSize)
std::atomic< unsigned int > refCount
The reference counter used to implement the std::shared_ptr behavior.
T * custom
The duly cast pointer to the custom type behind us.
FieldMembers(TAllocator &pAllocator, T *pCustom, size_t pAllocSize)
AllocatorMember()=delete
Deleted default constructor. (The allocator has to be given with construction)
TAllocator & GetAllocator() const noexcept