Fission FSN
Loading...
Searching...
No Matches
fsTSingleton.h
Go to the documentation of this file.
1/********************************************************************
2*
3* File: fsTSingleton.h
4*
5* Purpose: Shared create/instanceGet/destroy singleton base.
6*
7* T publicly inherits fsTSingleton<T> and declares
8* friend class fsTSingleton<T>; so the base can reach
9* T's protected constructor. T gets create()/
10* instanceGet()/destroy() for free and needs no
11* mInstance, .cpp bodies, or singleton boilerplate of
12* its own.
13*
14* Not for classes whose create() takes constructor
15* arguments, returns something other than void, or
16* constructs a different (e.g. backend-specific)
17* concrete type than T itself.
18*
19* mInstance is protected, not private: a further
20* subclass of T that re-implements its own covariant
21* create()/instanceGet()/destroy() (constructing
22* itself instead of T) needs direct access to it, the
23* same as when mInstance lived straight in T.
24*
25*********************************************************************/
26
27#ifndef fsTSingletonhdr
28#define fsTSingletonhdr
29
31
32template <class T>
34{
35public:
36
37 static void create()
38 {
39 fsAssert(mInstance == nullptr, "Already created.\n");
40 mInstance = new T();
41 }
42
43 static T* instanceGet()
44 {
45 fsAssert(mInstance != nullptr, "Not created.\n");
46 return mInstance;
47 }
48
49 static void destroy()
50 {
51 fsAssert(mInstance != nullptr, "Not created.\n");
52 delete mInstance;
53 mInstance = nullptr;
54 }
55
56protected:
57
58 fsTSingleton() = default;
59 ~fsTSingleton() = default;
60
61 static T* mInstance;
62
63private:
64
65 fsTSingleton(fsTSingleton const&) = delete;
66 fsTSingleton& operator=(fsTSingleton const&) = delete;
67};
68
69template <class T>
71
72#endif
~fsTSingleton()=default
static T * instanceGet()
Definition fsTSingleton.h:43
static T * mInstance
Definition fsTSingleton.h:61
static void destroy()
Definition fsTSingleton.h:49
fsTSingleton()=default
static void create()
Definition fsTSingleton.h:37
#define fsAssert(pStatement, pFmtString,...)
Definition fsAssert.h:23