Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SharedStaticData.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Common.h"
6
8
9 namespace Private_ {
10 // hack to avoid #include of Thread.h for Thread::SuppressInterruptionInContext
11 bool SharedStaticData_DTORHelper_ (conditional_t<kSpinLock_IsFasterThan_mutex, SpinLock, mutex>* m, unsigned int* cu) noexcept;
12 }
13
14 /*
15 ********************************************************************************
16 ******************************** SharedStaticData<T> ***************************
17 ********************************************************************************
18 */
19 template <typename T>
20 SharedStaticData<T>::SharedStaticData ()
21 {
22 [[maybe_unused]] lock_guard critSec{sMutex_};
23 ++sCountUses_;
24 if (sCountUses_ == 1) {
25 sOnceObj_ = new T{};
26 }
27 }
28 template <typename T>
29 SharedStaticData<T>::~SharedStaticData ()
30 {
31 if (Private_::SharedStaticData_DTORHelper_ (&sMutex_, &sCountUses_)) {
32 Assert (sCountUses_ == 0);
33 delete sOnceObj_;
34 sOnceObj_ = nullptr;
35 }
36 }
37 template <typename T>
39 {
40 // no need to lock since no way to destroy value while 'this' object still exists
41 Ensure (sCountUses_ >= 1);
42 EnsureNotNull (sOnceObj_);
43 return *sOnceObj_;
44 }
45 template <typename T>
46 inline const T& SharedStaticData<T>::Get () const
47 {
48 // no need to lock since no way to destroy value while 'this' object still exists
49 Ensure (sCountUses_ >= 1);
50 EnsureNotNull (sOnceObj_);
51 return *sOnceObj_;
52 }
53
54}
#define EnsureNotNull(p)
Definition Assertions.h:340