forked from catid/libcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRefSingleton.hpp
307 lines (238 loc) · 7.87 KB
/
RefSingleton.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Copyright (c) 2011 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CAT_REF_SINGLETON_HPP
#define CAT_REF_SINGLETON_HPP
#include <cat/lang/Singleton.hpp>
#include <cat/lang/LinkedLists.hpp>
#include <cat/io/Log.hpp>
namespace cat {
/*
RefSingleton builds on the Singleton class, to add OnFinalize().
When the order of finalization matters, RefSingleton objects may call the DependsOn<>();
function inside of their OnInitialize() member. This function creates a reference counted
relationship between the two objects. Circular references will cause this system to break.
To declare a RefSingleton in the header file:
#include <cat/lang/RefSingleton.hpp>
class Settings : public RefSingleton<Settings>
{
bool OnInitialize();
void OnFinalize();
// No ctor or dtor
To define a Refsingleton in the source file:
CAT_REF_SINGLETON(Settings);
static Clock *m_clock = 0;
bool Settings::OnInitialize()
{
m_clock = Use<Clock>(); // Add a reference to Clock RefSingleton so order of finalization is correct.
// Optionally check if Clock initialized successfully
if (!IsInitialized())
{
// No need to return a failure here, this singleton can no longer be successfully initialized
// since one of its dependencies could not initialize.
}
return true;
}
void Settings::OnFinalize()
{
}
Just ~10 lines of code to convert an object into a singleton with correct finalization order!
*/
// Internal class
class CAT_EXPORT RefSingletonBase : public SListItem
{
friend class RefSingletons;
CAT_NO_COPY(RefSingletonBase);
protected:
int _final_priority; // shutdown order (lower = sooner)
bool _init_success; // OnInitialize() return value
RefSingletonBase *_skip_next; // for merge sort
template<class S>
CAT_INLINE void UpdatePriority(Singleton<S> *instance)
{
// If initialization failed,
if (!instance || !instance->IsInitialized())
{
// Initialization has failed for this one too
_init_success = false;
}
}
CAT_INLINE void UpdatePriority(RefSingletonBase *instance)
{
// If instance could not be acquired,
if (!instance)
{
_init_success = false;
return;
}
int prio = instance->_final_priority;
CAT_DEBUG_ENFORCE(prio >= 0) << "Circular dependency detected! This is not supported!";
// If their priority will bump mine,
if (_final_priority <= prio)
{
// Make my priority lower
_final_priority = prio + 1;
}
// If initialization failed,
if (!instance->IsInitialized())
{
// Initialization has failed for this one too
_init_success = false;
}
}
protected:
virtual bool OnInitialize() = 0;
virtual void OnFinalize() = 0;
public:
CAT_INLINE RefSingletonBase() {}
CAT_INLINE virtual ~RefSingletonBase() {}
CAT_INLINE bool IsInitialized() { return _init_success; }
static void MergeSort(SListForward &list);
};
// Internal class
class CAT_EXPORT RefSingletonImplBase
{
protected:
CAT_INLINE void Watch(RefSingletonBase *obj);
};
// Internal class
template<class T>
class RefSingletonImpl : public RefSingletonImplBase
{
T _instance;
bool _init;
public:
CAT_INLINE T *GetRef(Mutex &mutex)
{
if (_init) return &_instance;
AutoMutex lock(mutex);
if (_init) return &_instance;
// Initialize the object and record result, allowing Use<> to override
// NOTE: This way inside OnInitialize() the singleton can check if
// any dependencies failed with a single IsInitialized() check
RefSingleton<T> *ptr = &_instance;
ptr->_final_priority = -1;
ptr->_init_success = true;
if (!ptr->OnInitialize()) ptr->_init_success = false;
// If final priority not set, initialize it to 1
if (ptr->_final_priority < 0)
ptr->_final_priority = 1;
Watch(&_instance);
CAT_FENCE_COMPILER;
_init = true;
return &_instance;
}
};
// In the H file for the object, derive from this class:
template<class T>
class CAT_EXPORT RefSingleton : public RefSingletonBase
{
friend class RefSingletonImpl<T>;
protected:
// Called during initialization
CAT_INLINE virtual bool OnInitialize() { return true; }
// Called during finalization
CAT_INLINE virtual void OnFinalize() {}
// Call only from OnInitialize() to declare which other RefSingletons are used
template<class S>
CAT_INLINE S *Use()
{
S *instance = S::ref();
UpdatePriority(instance);
return instance;
}
// Alternative way to use another singleton
template<class S>
CAT_INLINE S *Use(S *&s)
{
return (s = Use<S>());
}
template<class S0, class S1>
CAT_INLINE void Use(S0 *&s0, S1 *&s1)
{
Use(s0);
Use(s1);
}
template<class S0, class S1, class S2>
CAT_INLINE void Use(S0 *&s0, S1 *&s1, S2 *&s2)
{
Use(s0, s1);
Use(s2);
}
template<class S0, class S1, class S2, class S3>
CAT_INLINE void Use(S0 *&s0, S1 *&s1, S2 *&s2, S3 *&s3)
{
Use(s0, s1);
Use(s2, s3);
}
template<class S0, class S1, class S2, class S3, class S4>
CAT_INLINE void Use(S0 *&s0, S1 *&s1, S2 *&s2, S3 *&s3, S4 *&s4)
{
Use(s0, s1, s2, s3);
Use(s4);
}
// Set the final priority to zero so that it is among the first to finalize
CAT_INLINE void FinalizeFirst()
{
_final_priority = 0;
}
CAT_INLINE void FinalizeLast()
{
_final_priority = 9001; // It's over 9000!
}
public:
CAT_INLINE virtual ~RefSingleton<T>() {}
static T *ref();
};
// Use this alternative form to specify which Mutex object to use
#define CAT_REF_SINGLETON_MUTEX(T, M) \
static cat::RefSingletonImpl<T> m_T_rss; \
template<> T *RefSingleton<T>::ref() { return m_T_rss.GetRef(M); }
// In the C file for the object, use this macro:
#define CAT_REF_SINGLETON(T) CAT_REF_SINGLETON_MUTEX(T, GetRefSingletonMutex())
// Internal free function
Mutex CAT_EXPORT &GetRefSingletonMutex();
// Internal class
class CAT_EXPORT RefSingletons : public Singleton<RefSingletons>
{
friend class RefSingletonImplBase;
SListForward _active_list;
typedef SListForward::Iterator<RefSingletonBase> iter;
bool OnInitialize();
void OnFinalize();
template<class T>
CAT_INLINE void Watch(T *obj)
{
_active_list.PushFront(obj);
}
public:
static void AtExit();
};
// Internal inline member function definition
CAT_INLINE void RefSingletonImplBase::Watch(RefSingletonBase *obj)
{
RefSingletons::ref()->Watch(obj);
}
} // namespace cat
#endif // CAT_REF_SINGLETON_HPP