eLifeSpan_Shortest = -20000,
77eLifeSpan_Short = -10000,
79eLifeSpan_Long = 10000,
80eLifeSpan_Longest = 20000
119 typedefvoid (*FUserCleanup)(
void* ptr);
146FUserCleanup user_cleanup = 0,
147 TLifeSpanlife_span = TLifeSpan::GetDefault())
148: m_SelfCleanup(self_cleanup),
149m_UserCleanup(user_cleanup),
150m_LifeSpan(life_span),
151m_CreationOrder(x_GetCreationOrder()),
163 returnm_Ptr.load(memory_order_acquire);
166m_Ptr.store(ptr, memory_order_release);
169 returnx_GetPtr() !=
nullptr;
172 if(
autoptr = x_GetPtr() ) {
188 if(!m_InstanceMutex || !m_MutexRefCount) {
189m_InstanceMutex =
new CMutex;
194m_InstanceMutex->Lock();
199m_InstanceMutex->Unlock();
200x_ReleaseInstanceMutex();
206 if(--m_MutexRefCount > 0)
return;
226 static intx_GetCreationOrder(
void);
241 if( m_SelfCleanup ) {
243m_SelfCleanup(
this, guard);
246x_ReleaseInstanceMutex();
305 if(stack) stack->insert(ptr);
312 static voidDisableChildThreadsCheck();
329 const size_tindex = level;
331 _ASSERT(index < stacks.size());
332 returnstacks[index];
336 static voidx_Cleanup(
CMutexGuard& guard, TStack*& stack);
370 if(ptr)
delete static_cast<T*
>(ptr);
374 if(ptr)
delete const_cast<T*
>(
static_cast<const T*
>(ptr));
399 typedef T* (*FCreate)(void);
461 return static_cast<T*
>(
const_cast<void*
>(ptr));
550 "CSafeStatic::Init: Register() failed");
574 template<
classT,
classV, V value>
585 #define SAFE_CONST_STATIC(type, init_value) \ 586 SAFE_CONST_STATIC_EX(type, type, init_value) 590 #define SAFE_CONST_STATIC_EX(type, init_value_type, init_value) \ 591 CSafeStatic< const type, \ 592 CSafeStaticInit_Callbacks<const type, init_value_type, init_value> > 595 #define SAFE_CONST_STATIC_STRING(var, value) \ 596 char SAFE_CONST_STATIC_STRING_##var[] = value; \ 597 SAFE_CONST_STATIC_EX(std::string, const char*, \ 598 SAFE_CONST_STATIC_STRING_##var) var 617 return static_cast<T*
>(
const_cast<void*
>(ptr));
645 template<
classFUserCreate>
647 T&
Get(FUserCreate user_create)
662 void Set(
T*
object);
668 template<
classFUserCreate>
669 void x_Init(FUserCreate user_create);
679 if( user_cleanup ) {
703 return static_cast<T*
>(
const_cast<void*
>(ptr));
731 template<
classFUserCreate>
733 T&
Get(FUserCreate user_create)
753 template<
classFUserCreate>
764 if( user_cleanup ) {
767ptr->RemoveReference();
791 if( !x_IsSetPtr() ) {
806 if( !x_IsSetPtr() ) {
815 template<
classFUserCreate>
820 if( !x_IsSetPtr() ) {
822 if(
autoptr = user_create() ) {
835 if( !x_IsSetPtr() ) {
838 object->AddReference();
851 if( !x_IsSetPtr() ) {
862 template<
classFUserCreate>
867 if( !x_IsSetPtr() ) {
869 T* ptr = user_create();
void Release()
Manually force the resource to be released.
static void Register(CSafeStaticPtr_Base *ptr)
Add new on-demand variable to the cleanup stack.
static TStack *& x_GetStack(CSafeStaticLifeSpan::ELifeLevel level)
static bool sm_ChildThreadsCheck
multiset< CSafeStaticPtr_Base *, CSafeStatic_Less > TStack
Safe static callbacks version allowing initial value of type V.
ELifeLevel GetLifeLevel() const
Get life level value.
static CSafeStaticLifeSpan & GetDefault(void)
Get default life span (set to eLifeSpan_Min).
int GetLifeSpan(void) const
Get life span value.
ELifeSpan
Predefined life spans for the safe static objects.
@ eLifeSpan_Min
std static, not adjustable
ELifeLevel
Predefined life levels for the safe static objects.
DECLARE_CLASS_STATIC_MUTEX(sm_ClassMutex)
void(* FUserCleanup)(void *ptr)
User cleanup function type.
const void * x_GetPtr() const
static int x_GetCreationOrder(void)
CGuard< CSafeStaticPtr_Base > TInstanceMutexGuard
CSafeStaticPtr_Base(FSelfCleanup self_cleanup, FUserCleanup user_cleanup=0, TLifeSpan life_span=TLifeSpan::GetDefault())
Constructor.
FUserCleanup m_UserCleanup
CSafeStaticLifeSpan TLifeSpan
Life span.
void x_ReleaseInstanceMutex(void)
bool x_IsStdStatic(void) const
atomic< const void * > m_Ptr
Pointer to the data.
FSelfCleanup m_SelfCleanup
void x_SetPtr(const void *ptr)
const void * x_ReleasePtr()
static void sx_SelfCleanup(CSafeStaticPtr_Base *safe_static, TInstanceMutexGuard &guard)
CSafeStaticLifeSpan TLifeSpan
static T * x_CastPtr(const void *ptr)
T & Get(FUserCreate user_create)
Get the existing object or create a new one using the provided FUserCreate object.
void Set(T *object)
Initialize with an existing object.
CSafeStaticPtr(FUserCleanup user_cleanup=0, TLifeSpan life_span=TLifeSpan::GetDefault())
Constructor.
T & Get(void)
Create the variable if not created yet, return the reference.
T & Get(void)
Create the variable if not created yet, return the reference.
static void sx_SelfCleanup(CSafeStaticPtr_Base *safe_static, TInstanceMutexGuard &guard)
void x_Init(FUserCreate user_create)
static T * x_CastPtr(const void *ptr)
void Set(T *object)
Initialize with an existing object.
T & Get(FUserCreate user_create)
Get the existing object or create a new one using the provided FUserCreate object.
CSafeStaticLifeSpan TLifeSpan
CSafeStaticRef(FUserCleanup user_cleanup=0, TLifeSpan life_span=TLifeSpan::GetDefault())
Constructor.
Helper class for object allocation/deallocation.
static void s_RemoveReference(CObject *ptr)
static void s_RemoveReference(const CObject *ptr)
static void s_RemoveReference(const void *ptr)
static void s_AddReference(const void *)
static void s_AddReference(void *)
static void s_RemoveReference(void *ptr)
static void s_AddReference(CObject *ptr)
static T * s_Create(void)
Create a new class instance.
Initialization and cleanup of a safe-static object.
CSafeStatic_Callbacks(FCreate create=0, FCleanup cleanup=0)
The constructor allows to use CSafeStatic_Callbacks as a simple wrapper for static functions.
void Cleanup(T &value)
Perform cleanup before destruction.
T * Create(void)
Create new object.
void(* FCleanup)(T &value)
T *(* FCreate)(void)
The default implementation allows to use callback functions rather than a new class.
CSafeStatic_Allocator< T > TAllocator
Comparison for safe static ptrs.
bool operator()(const TPtr &ptr1, const TPtr &ptr2) const
CSafeStaticPtr_Base * TPtr
CSafeStatic_Callbacks< T >::FCreate FCreate
Callback function types.
CSafeStatic_Allocator< T > TAllocator
CSafeStatic & operator=(const CSafeStatic &)=delete
CSafeStatic(FCreate create, FCleanup cleanup, TLifeSpan life_span=TLifeSpan::GetDefault())
Constructor.
static void sx_SelfCleanup(CSafeStaticPtr_Base *safe_static, TInstanceMutexGuard &guard)
CSafeStaticLifeSpan TLifeSpan
T & Get(void)
Create the variable if not created yet, return the reference.
CSafeStatic(const CSafeStatic &)=delete
CSafeStatic(TLifeSpan life_span=TLifeSpan::GetDefault())
Constructor.
CSafeStatic< T, TCallbacks > TThisType
CSafeStatic_Callbacks< T >::FCleanup FCleanup
static T * x_CastPtr(const void *ptr)
CSafeStatic(TCallbacks callbacks, TLifeSpan life_span=TLifeSpan::GetDefault())
Constructor.
static void cleanup(void)
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
#define NCBI_RETHROW_SAME(prev_exception, message)
Generic macro to re-throw the same exception.
void AddReference(void) const
Add reference to object.
void RemoveReference(void) const
Remove reference to object.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
void Cleanup(CTls< TValue > &value)
#define NCBI_XNCBI_EXPORT
unsigned int
A callback function used to compare two keys in a database.
const GenericPointer< typename T::ValueType > T2 value
static CSafeStaticGuard s_CleanupGuard
This static variable must be present in all modules using on-demand static variables.
Multi-threading â mutexes; rw-locks; semaphore.
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
SSimpleLock is a functor to wrap calling Lock().
SSimpleLock is a functor to wrap calling Unlock().
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4