-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalInstances.h
243 lines (190 loc) · 6.36 KB
/
globalInstances.h
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
#pragma once
#include <cstdlib> //for exit(1);
#include <exception>
#include <functional>
#include <list>
#include <utility>
namespace global {
#ifdef __has_cpp_attribute
#ifndef __cpp_exceptions
#define EXCEPTIONS_DISABLED
#endif
#else
// gcc has its own macro prior to supporting __has_cpp_attribute
#if (!defined(__clang__)) && (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 7) && \
(__GNUC_PATCHLEVEL__ >= 1)
#ifndef __EXCEPTIONS
#define EXCEPTIONS_DISABLED
#endif
#else
#warning Cannot detect if exceptions are enabled, so enabling them by default. They can be manually disabled by defining 'EXCEPTIONS_DISABLED'
#endif // __GNUC__
#endif // __has_cpp_attribute
namespace detail {
template <typename T> T &staticValue() {
static T t;
return t;
}
template <typename T> void throwImpl(T t) {
#ifdef EXCEPTIONS_DISABLED
(void)t; // avoid unused
exit(1);
#else
throw t;
#endif
}
} // namespace detail
class NullptrAccess : public std::exception {};
template <typename T = void> void onNullPtrAccess() {
detail::throwImpl(NullptrAccess{});
}
// override by spcializing
// template<> void onNullPtrAccess<>(){ exit(1); }
namespace detail {
class bad_optional_access_impl : public std::exception {};
using bad_optional_access =
bad_optional_access_impl; // use c++17 version if available
template <typename T> class OptionalValueImpl {
public:
explicit OptionalValueImpl() {}
OptionalValueImpl &operator=(T const &t) {
val = t;
m_hasValue = true;
return *this;
}
explicit operator T() const {
if (!m_hasValue)
throwImpl(bad_optional_access{});
return val;
}
bool has_value() const { return m_hasValue; }
void reset() { m_hasValue = false; }
private:
T val;
bool m_hasValue = false;
};
template <typename T>
using optional = OptionalValueImpl<T>; // use c++17 version if available
template <typename T> class InstancePointer {
public:
explicit InstancePointer() {}
operator bool() const { return instancePtr != nullptr; }
bool operator==(T const *t) const { return instancePtr == t; }
bool operator!=(T const *t) const { return instancePtr != t; }
explicit operator T *() const { return operator->(); }
T &operator*() const & { return *instancePtr; }
T *operator->() const {
if (instancePtr == nullptr)
global::onNullPtrAccess<>();
return instancePtr;
}
template <typename Func> void ifAvailable(Func func) {
if (instancePtr != nullptr) {
func(*instancePtr);
return;
}
ifAvailableOps.emplace_back(func);
}
template <typename Func> void becomesUnavailable(Func func) {
becomesUnavailableOps.emplace_back(func); // never directly
}
private:
InstancePointer &operator=(T *t) {
if (instancePtr == t)
return *this; // nothing changed
auto before = instancePtr;
instancePtr = t;
if (instancePtr != nullptr) {
for (auto const &op : ifAvailableOps)
op(*instancePtr);
ifAvailableOps.clear();
}
if (before != nullptr && instancePtr == nullptr) {
for (auto const &op : becomesUnavailableOps)
op(*before);
becomesUnavailableOps.clear();
}
return *this;
}
template <typename> friend class ReplacingInstanceRegistration;
using ClassType = InstancePointer<T>;
InstancePointer(ClassType const &) = delete;
ClassType const &operator=(ClassType const &) = delete;
using DeferredOperation = std::list<std::function<void(T &)>>;
DeferredOperation ifAvailableOps;
DeferredOperation becomesUnavailableOps;
T *instancePtr = nullptr;
};
} // namespace detail
template <typename T> detail::InstancePointer<T> &instance() {
return detail::staticValue<detail::InstancePointer<T>>();
}
template <typename T> T &instanceRef() {
return *detail::staticValue<detail::InstancePointer<T>>();
}
class InstanceReplacementNotAllowed : public std::exception {};
class RegisteringNullNotAllowed : public std::exception {};
namespace detail {
// replaces existing for the time it exised
template <typename T> class ReplacingInstanceRegistration {
public:
ReplacingInstanceRegistration() {}
ReplacingInstanceRegistration(T *t) { registerInstance(t); }
void operator()(T *t) { registerInstance(t); }
virtual ~ReplacingInstanceRegistration() { deregisterInstance(); }
virtual void registerInstance(T *t) {
deregisterInstance();
replacedInstance = instance<T>().instancePtr;
instance<T>() = t; // possibly deregisters again
}
virtual void deregisterInstance() {
if (replacedInstance.has_value() == false)
return; // noting to do
T *tmp = static_cast<T *>(replacedInstance);
replacedInstance.reset();
instance<T>() = tmp; // possibly registers again
}
private:
ReplacingInstanceRegistration(ReplacingInstanceRegistration const &) =
delete; // no copy
detail::optional<T *> replacedInstance;
};
// expects nullptr to be registered beforehand
// expects registration-target not to be null
template <typename T>
class InstanceRegistration : ReplacingInstanceRegistration<T> {
public:
using Superclass = ReplacingInstanceRegistration<T>;
using Superclass::operator();
InstanceRegistration() : Superclass() {}
InstanceRegistration(T *t) : Superclass() { registerInstance(t); }
void registerInstance(T *t) override {
if (instance<T>() != nullptr)
throwImpl(InstanceReplacementNotAllowed{});
if (t == nullptr)
throwImpl(RegisteringNullNotAllowed{});
Superclass::registerInstance(t);
}
};
template <template <typename> class RegistrationType, typename AccessType,
typename InstanceType>
class RegisterdInstanceT {
InstanceType t;
RegistrationType<AccessType> reg;
public:
template <typename... Args>
RegisterdInstanceT(Args &&... args)
: t(std::forward<Args>(args)...), reg(&t) {}
};
} // namespace detail
template <typename AccessType, typename InstanceType = AccessType>
using Instance = detail::RegisterdInstanceT<detail::InstanceRegistration,
AccessType, InstanceType>;
template <typename AccessType, typename InstanceType = AccessType>
using TestInstance =
detail::RegisterdInstanceT<detail::ReplacingInstanceRegistration,
AccessType, InstanceType>;
#define GLOBAL_INSTANCE_IS_FRIEND \
template <template <typename, typename> class, typename, typename> \
friend class ::global::detail::RegisterdInstanceT
} // namespace global