smgl 0.11.0
Structured Metadata Engine and Graph Objects Library
Loading...
Searching...
No Matches
SingletonImpl.hpp
1namespace smgl
2{
3namespace detail
4{
5
6/* Initialize the instance pointer */
7// clang-format off
8template <
9 class T,
10 template <class> class C,
11 template <class> class L,
12 template <class> class M
13>
14// clang-format on
17
18/* Initialize the destroyed boolean */
19// clang-format off
20template <
21 class T,
22 template <class> class C,
23 template <class> class L,
24 template <class> class M
25>
26// clang-format on
28
29/* Instance retrieval implementation */
30// clang-format off
31template <
32 class T,
33 template <class> class C,
34 template <class> class L,
35 template <class> class M
36>
37// clang-format on
39{
40 // If we don't have an instance
41 if (!instance_) {
42 // Use the threading model to get a lock
43 [[maybe_unused]] typename M<T>::Lock guard;
44 // Double-checked locking pattern
45 if (!instance_) {
46 // Handle the singleton already being destroyed
47 if (destroyed_) {
48 destroyed_ = false;
49 L<T>::OnDeadReference();
50 }
51 // Construct the singleton
52 instance_ = C<T>::Create();
53 L<T>::ScheduleDestruction(&DestroySingleton);
54 }
55 }
56 return *instance_;
57}
58
59/* Instance destruction implementation */
60// clang-format off
61template <
62 class T,
63 template <class> class C,
64 template <class> class L,
65 template <class> class M
66>
67//clang-format on
70{
71 assert(!destroyed_);
72 C<T>::Destroy(instance_);
73 instance_ = nullptr;
74 destroyed_ = true;
75}
76
77}
78}
static InstanceType * instance_
typename ThreadingModel< T >::VolatileType InstanceType
static T & Instance()
Access the singleton instance.
Project top-level namespace.