Fission FSN
Loading...
Searching...
No Matches
fsTSingletonBackend.h
Go to the documentation of this file.
1/********************************************************************
2*
3* File: fsTSingletonBackend.h
4*
5* Purpose: Shared create/instanceGet/destroy singleton base for
6* classes whose actual instance is a different,
7* per-backend concrete type (fsIDebug -> fsCDebug,
8* fsIInputManager -> fsCInputManager, fsIAudioManager
9* -> fsCAudioManager, ...).
10*
11* T publicly inherits fsTSingletonBackend<T> and
12* declares:
13* friend class fsTSingletonBackend<T>;
14* static T* backendInstanceCreate();
15* with backendInstanceCreate() defined in T's own .cpp
16* exactly the way create() used to build the concrete
17* type directly (including any #if defined
18* fsFRAMEWORK_* backend dispatch). That keeps every
19* backend-specific header confined to T's own .cpp,
20* same as before - this template never sees them and
21* neither do T's callers.
22*
23* postCreateDo()/preDestroyDo() are optional protected
24* virtual hooks (default no-op) for a step that must
25* run right after construction / right before
26* destruction (e.g. fsIInputManager::initialise()/
27* uninitialise(), fsIAudioManager::shutdown()). T
28* overrides them only if it needs to; no friend
29* declaration required for that, since they're members
30* of this template already.
31*
32* Not for classes whose create() takes constructor
33* arguments, returns something other than void, or
34* (like fsCAppCoreMain's subclasses) share another
35* class's mInstance instead of owning their own.
36*
37* Uses plain <cassert> rather than this codebase's own
38* fsAssert: fsAssert.h pulls in fsLog.h, which pulls in
39* fsIDebug.h - and fsIDebug is itself one of this
40* template's consumers. Depending on fsAssert.h here
41* creates a real header cycle the moment fsIDebug.h
42* tries to inherit from this template (caught by
43* directly compiling it, not by inspection). assert()
44* aborts rather than logging-and-continuing on misuse,
45* which is arguably the better failure mode for a
46* programming-error-only check like "don't call
47* create() twice" anyway.
48*
49*********************************************************************/
50
51#ifndef fsTSingletonBackendhdr
52#define fsTSingletonBackendhdr
53
54#include <cassert>
55
56template <class T>
58{
59public:
60
61 static void create()
62 {
63 assert(mInstance == nullptr && "Already created.");
64 delete mInstance;
65 mInstance = T::backendInstanceCreate();
66 mInstance->postCreateDo();
67 }
68
69 static T* instanceGet()
70 {
71 assert(mInstance != nullptr && "Not created.");
72 return mInstance;
73 }
74
75 static void destroy()
76 {
77 assert(mInstance != nullptr && "Not created.");
78 mInstance->preDestroyDo();
79 delete mInstance;
80 mInstance = nullptr;
81 }
82
83protected:
84
86 virtual ~fsTSingletonBackend() = default;
87
88 virtual void postCreateDo() {}
89 virtual void preDestroyDo() {}
90
91 static T* mInstance;
92
93private:
94
96 fsTSingletonBackend& operator=(fsTSingletonBackend const&) = delete;
97};
98
99template <class T>
101
102#endif
static T * mInstance
Definition fsTSingletonBackend.h:91
virtual ~fsTSingletonBackend()=default
virtual void preDestroyDo()
Definition fsTSingletonBackend.h:89
virtual void postCreateDo()
Definition fsTSingletonBackend.h:88
static T * instanceGet()
Definition fsTSingletonBackend.h:69
static void create()
Definition fsTSingletonBackend.h:61
static void destroy()
Definition fsTSingletonBackend.h:75
fsTSingletonBackend()=default