smgl 0.11.0
Structured Metadata Engine and Graph Objects Library
Loading...
Searching...
No Matches
Singleton.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <cassert>
6#include <exception>
7#include <functional>
8
9namespace smgl
10{
11
12namespace policy
13{
15using AtExitFunc = void (*)();
16
18template <typename T>
21 static T* Create() { return new T; }
23 static void Destroy(T* obj) { delete obj; }
24};
25
27template <typename T>
30 static T* Create()
31 {
32 // Create aligned static buffer
33 alignas(alignof(T)) static char buf[sizeof(T)];
34 return new (&buf) T;
35 }
37 static void Destroy(T* obj) { obj->~T(); }
38};
39
41template <typename T>
44 static void ScheduleDestruction(AtExitFunc destructionFn)
45 {
46 std::atexit(destructionFn);
47 }
52 static void OnDeadReference()
53 {
54 throw std::logic_error{"Dead reference detected"};
55 }
56};
57
64template <typename T>
66{
67public:
69 static void ScheduleDestruction(AtExitFunc destructionFn)
70 {
71 if (not destroyed_) {
72 std::atexit(destructionFn);
73 }
74 }
75
77 static void OnDeadReference() { destroyed_ = true; }
78
79private:
81 static bool destroyed_;
82};
83
84template <typename T>
86
88template <typename T>
91 using VolatileType = T;
93 struct Lock {
95 Lock() = default;
97 explicit Lock(T& /* unused */) {}
98 };
99};
100} // namespace policy
101
102namespace detail
103{
116// clang-format off
117template <
118 class T,
119 template <class> class CreationPolicy = policy::CreateStatic,
120 template <class> class LifetimePolicy = policy::DefaultLifetime,
121 template <class> class ThreadingModel = policy::SingleThreaded
122>
123// clang-format on
125{
126public:
131 using InstanceType = typename ThreadingModel<T>::VolatileType;
132
134 SingletonHolder() = delete;
135
137 static T& Instance();
138
139private:
141 static void DestroySingleton();
145 static bool destroyed_;
146};
147
148} // namespace detail
149} // namespace smgl
150
151#include "smgl/SingletonImpl.hpp"
T atexit(T... args)
Class for constructing and managing the lifetime of a singleton object.
static InstanceType * instance_
typename ThreadingModel< T >::VolatileType InstanceType
static T & Instance()
Access the singleton instance.
Lifetime policy which reconstructs a destroyed singleton if reaccessed.
Definition Singleton.hpp:66
static void ScheduleDestruction(AtExitFunc destructionFn)
Definition Singleton.hpp:69
void(*)() AtExitFunc
Alias for a function type that can be passed to std::atexit.
Definition Singleton.hpp:15
Project top-level namespace.
Creation policy which allocates a static object.
Definition Singleton.hpp:28
static void Destroy(T *obj)
Definition Singleton.hpp:37
Creation policy that uses new to allocate an object on the stack.
Definition Singleton.hpp:19
static void Destroy(T *obj)
Definition Singleton.hpp:23
Lifetime policy which schedules destruction using std::atexit.
Definition Singleton.hpp:42
static void ScheduleDestruction(AtExitFunc destructionFn)
Definition Singleton.hpp:44
Single-threaded lock type which does nothing.
Definition Singleton.hpp:93
Threading policy for single-threaded applications.
Definition Singleton.hpp:89