8#ifndef HPP_ALIB_MONOMEM_SHARED_MONO_PTR
9#define HPP_ALIB_MONOMEM_SHARED_MONO_PTR 1
24namespace alib {
namespace monomem {
85template<
typename T,
typename TAllocator,
typename TLock>
97 std::atomic<unsigned int> refCount;
100 size_t initialBufferSize,
101 unsigned int bufferGrowthInPercent )
102 : allocator( pAllocator, firstBuffer, initialBufferSize, bufferGrowthInPercent )
104 template<
typename TEnableIf= TAllocator,
105 ATMP_IF(std::is_default_constructible<TEnableIf>::value)>
107 size_t initialBufferSize,
108 unsigned int bufferGrowthInPercent )
109 : allocator(
ALIB_DBG(
"ShardMonoVal",) firstBuffer, initialBufferSize, bufferGrowthInPercent )
141 size_t initialBufferSize,
142 unsigned int bufferGrowthInPercent )
143 :
allocator( pAllocator, firstBuffer, initialBufferSize, bufferGrowthInPercent )
154 template<
typename TEnableIf= TAllocator,
155 ATMP_IF(std::is_default_constructible<TEnableIf>::value)>
157 size_t initialBufferSize,
158 unsigned int bufferGrowthInPercent )
160 firstBuffer, initialBufferSize, bufferGrowthInPercent )
165 #if ALIB_DEBUG && !DOXYGEN
168 void dbgassert()
const {}
211 size_t initialBufferSizeInKB,
unsigned int bufferGrowthInPercent )
213 auto size= initialBufferSizeInKB * 1024;
238 TSharedMonoVal(
size_t initialBufferSizeInKB,
unsigned int bufferGrowthInPercent )
240 auto size= initialBufferSizeInKB * 1024;
241 void* mem= TAllocator().allocate( size,
alignof(
detail::Buffer) );
255 "Template parameter TLock of class TSharedMonoVal must be <void> if module ALib Threads is "
256 "not included in the ALib Distribution." )
279 template<
typename... TArgs>
289 new (&
members->custom) T( std::forward<TArgs>(args)... );
304 template<
typename... TArgs>
305 void ConstructT( TArgs&&... args ) {
members->custom.Construct(std::forward<TArgs>(args)... ); }
325 const T&
Self() const noexcept { dbgassert();
return *
members->custom; }
367 :
members(other.members) { other.members=
nullptr; }
378 if (
this == &other || this->members == other.members)
389 if((
members= other.members) !=
nullptr)
414 other.members=
nullptr;
453 T*
Get() noexcept { dbgassert();
return members->custom.Get(); }
457 const T*
Get() const noexcept { dbgassert();
return members->custom.Get(); }
460 operator bool() const noexcept {
return members !=
nullptr; }
623 #define ATPASS ALIB_STATIC_ASSERT(ForbiddenTemplateParameterGiven, ATMP_EQ(TIf,TLock), "Template parameter of this method is deduced by the compiler and must not be given!");
624 template<
typename TIf= TLock, ATMP_T_IF(
int, !ATMP_EQ(TIf,
void ))= 0>
625 TIf&
GetLock() const noexcept { ATPASS dbgassert();
return members->lock; }
683DOX_MARKER([DOX_MANUAL_ALIASES_SHAREDMONOVAL])
685template<
typename T,
typename TAllocator= lang::HeapAllocator,
typename TLock=
void>
687DOX_MARKER([DOX_MANUAL_ALIASES_SHAREDMONOVAL])
void SetNulled() noexcept
bool TryAcquireTimed(const Ticks::Duration &waitDuration, const CallerInfo &ci) const noexcept
AllocatorType & GetAllocator() noexcept
bool TryAcquireShared(const CallerInfo &ci) const noexcept
void ReleaseRecursive(const CallerInfo &ci) const noexcept
TSharedMonoVal & operator=(const TSharedMonoVal &other) noexcept
TSharedMonoVal & operator=(TSharedMonoVal &&other) noexcept
void Acquire(const CallerInfo &ci) const noexcept
TLock & GetLock() const noexcept
bool Unique() const noexcept
TSharedMonoVal(size_t initialBufferSizeInKB, unsigned int bufferGrowthInPercent)
static constexpr size_t SizeOfAllocation()
void AcquireShared(const CallerInfo &ci) const noexcept
T StoredType
Exposes the stored type specified with template parameter T.
void operator=(std::nullptr_t)
Assignment of nullptr. Same as SetNulled.
const T & operator*() const noexcept
void Release(const CallerInfo &ci) const noexcept
bool operator==(std::nullptr_t) const noexcept
bool TryAcquire(const CallerInfo &ci) const noexcept
const T * operator->() const noexcept
bool IsNulled() const noexcept
TLock LockType
Exposes the lock type specified with template parameter TLock.
TSharedMonoVal(TSharedMonoVal &&other) noexcept
unsigned int UseCount() const noexcept
TSharedMonoVal(TAllocator &allocator, size_t initialBufferSizeInKB, unsigned int bufferGrowthInPercent)
TSharedMonoVal(std::nullptr_t) noexcept
bool operator!=(std::nullptr_t) const noexcept
ATMP_IF_T_F(ATMP_EQ(void, TLock), FieldMembersNoLock, FieldMembersWithLock) FieldMembers
TSharedMonoVal(const TSharedMonoVal &other) noexcept
void ConstructT(TArgs &&... args)
void Reset(TArgs &&... args)
T * operator->() noexcept
const T * Get() const noexcept
TSharedMonoVal() noexcept
Default Constructor. Leaves this object nulled.
void ReleaseShared(const CallerInfo &ci) const noexcept
void AcquireRecursive(const CallerInfo &ci) const noexcept
bool TryAcquireSharedTimed(const Ticks::Duration &waitDuration, const CallerInfo &ci) const noexcept
const T & Self() const noexcept
#define ATMP_IF_T_F( Cond, T, F)
#define ALIB_ASSERT_MODULE(modulename)
#define ATMP_BOOL_IF(Cond)
#define ATMP_VOID_IF(Cond)
#define ALIB_STATIC_ASSERT(CondVariable, Cond, Message)
#define ATMP_EQ( T, TEqual)
#define ATMP_HAS_METHOD(T, Method,...)
#define ALIB_ASSERT_ERROR(cond,...)
static ALIB_FORCE_INLINE void Destruct(T &object)
time::Ticks Ticks
Type alias in namespace alib.
lang::CallerInfo CallerInfo
Type alias in namespace alib.
FieldMembersWithLock(TAllocator &pAllocator, detail::Buffer *firstBuffer, size_t initialBufferSize, unsigned int bufferGrowthInPercent)
std::atomic< unsigned int > refCount
The reference counter used to implement the std::shared_ptr behavior.
TLock lock
The embedded lock.
lang::Placeholder< T > custom
The space for the custom member. The instance will be constructed using placement-new.
FieldMembersWithLock(detail::Buffer *firstBuffer, size_t initialBufferSize, unsigned int bufferGrowthInPercent)
TMonoAllocator< TAllocator > allocator
The allocator that this class is contained in.