-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals.hpp
264 lines (235 loc) · 6.31 KB
/
globals.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* @file globals.hpp
* @author Jens Munk Hansen <[email protected]>
* @date Fri Mar 31 20:45:27 2017
*
* @brief Templated singletons
*
* Copyright 2017 Jens Munk Hansen
*
*/
#pragma once
/*
* This file is part of SOFUS.
*
* SOFUS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SOFUS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SOFUS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sps/cenv.h> // SPS_ATTR_DESTRUCTOR
#include <sps/debug.h>
#ifdef _MSC_VER
# include <cstdlib> // atexit
#endif
#include <atomic>
#include <mutex>
#include <iostream>
namespace sps {
/*
Template <struct V> is not allowed so this gives a warning, when V is a struct
*/
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4099)
#elif __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wmismatched-tags"
#endif
template <typename T, template <typename> class V> class globalstruct {
public:
static std::atomic<class V<T>*> pVar;
private:
globalstruct() = delete;
globalstruct(const globalstruct& rhs) = delete;
void operator=(const globalstruct& rhs) = delete;
};
#ifdef _MSC_VER
# pragma warning(pop)
#elif __clang__
# pragma clang diagnostic pop
#endif
class Identifiable {
private:
static std::uint32_t UIDCreate() {
static std::atomic<std::uint32_t> g_uid = {0};
std::uint32_t retval = std::atomic_fetch_add(&g_uid, 1U);
if (retval + 1 == 0) {
exit(-1);
}
return retval;
}
protected:
Identifiable() {
// A unique is guaranteed, but many temporaries are created
// std::cout << "Identifiable created" << std::endl;
m_Id = UIDCreate();
// std::cout << "m_Id: " << m_Id << std::endl;
}
public:
std::uint32_t IDGet() const {
return m_Id;
}
private:
std::uint32_t m_Id;
};
/*! \brief Singleton base class. Phoenix singleton
*
* @tparam T type of the actual singleton
*
* Usage:
*
* class MySingleton : public sps::Singleton<MySingleton> {
* private:
* MySingleton() {
* }
* friend class sps::Singleton<MySingleton>;
* };
*
* MySingleton *pSingleton = MySingleton::InstanceGet();
*
* The class could have been inherited from sps::Default, but
* then the user must friend sps::Default<MySingleton>, which
* is unfortunate.
*/
template <class T>
class Singleton {
public:
/**
* InstanceGet
*
* Acquire the (non-static) singleton instance
*
* @return Pointer to singleton instance
*/
static T* InstanceGet() {
T* pInstance = g_instance.load(std::memory_order_acquire);
if (!pInstance) {
std::lock_guard<std::recursive_mutex> guard(g_mutex);
pInstance = g_instance.load(std::memory_order_relaxed);
if (!pInstance) {
// Enforce explicit instantiation of
// Singleton<T>::InstanceDestroy() by calling the function
// here once. In this way, the user does not need to remember
// to inistantiate the function. Note, this requires the mutex
// to be recursive
Singleton<T>::InstanceDestroy();
pInstance = new T;
g_instance.store(pInstance, std::memory_order_release);
}
}
return pInstance;
}
static int InstanceDestroy() SPS_ATTR_DESTRUCTOR;
protected:
Singleton() {
// If we never include this in a DLL, which is loaded
// dynamically, we can use the standard atexit handler.
// std::atexit([]()->void { Singleton<T>::InstanceDestroy();});
}
// If we set g_instance = nullptr in this method, the mutex need to be recursive
~Singleton() = default;
/**
* Copy-ctor and assignment could be deleted such that we do not allow copying the singleton
*
*/
Singleton(Singleton const &) = delete;
Singleton& operator=(Singleton const &) = delete;
private:
static std::atomic<T*> g_instance;
// static std::mutex g_mutex;
static std::recursive_mutex g_mutex;
};
/**
* InstanceDestroy
*
* Note this must be explicitly instantiated for all descendants. Introducing
* a member
*
* const int atexit = Singleton<T>::InstanceDestroy()
*
* would enforce this, but this a nasty hack
*
* @return error code
*/
template <class T>
int Singleton<T>::InstanceDestroy() {
int retval = -1;
std::lock_guard<std::recursive_mutex> guard(g_mutex);
if (g_instance) {
debug_print("Singleton destroyed");
delete g_instance;
g_instance = nullptr;
retval = 0;
}
return retval;
}
template <class T>
std::atomic<T*> Singleton<T>::g_instance{nullptr};
template <class T>
std::recursive_mutex Singleton<T>::g_mutex;
// Note this a copy-able version of the Singleton above
template <class T>
class Default {
public:
/**
* InstanceGet
*
* Acquire the (non-static) singleton instance
*
* @return Pointer to singleton instance
*/
static T* InstanceGet() {
T* pInstance = g_instance.load(std::memory_order_acquire);
if (!pInstance) {
std::lock_guard<std::mutex> guard(g_mutex);
pInstance = g_instance.load(std::memory_order_relaxed);
if (!pInstance) {
pInstance = new T;
g_instance.store(pInstance, std::memory_order_release);
}
}
return pInstance;
}
static int InstanceDestroy() SPS_ATTR_DESTRUCTOR;
~Default() {
debug_print("\n");
}
protected:
Default() {
debug_print("\n");
}
private:
static std::atomic<T*> g_instance; // Default instance
static std::mutex g_mutex; // Mutex for accessing default instance
};
template <class T>
int Default<T>::InstanceDestroy() {
int retval = -1;
std::lock_guard<std::mutex> guard(g_mutex);
if (g_instance) {
debug_print("Default destroyed");
delete g_instance;
g_instance = nullptr;
retval = 0;
}
return retval;
}
template <class T>
std::atomic<T*> Default<T>::g_instance{nullptr};
template <class T>
std::mutex Default<T>::g_mutex;
} // namespace sps
/* Local variables: */
/* indent-tabs-mode: nil */
/* tab-width: 2 */
/* c-basic-offset: 2 */
/* End: */