Fission
FSN
Loading...
Searching...
No Matches
libs
fsCore
src
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
30
#include <
fsCore/src/debug/fsAssert.h
>
31
32
template
<
class
T>
33
class
fsTSingleton
34
{
35
public
:
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
56
protected
:
57
58
fsTSingleton
() =
default
;
59
~fsTSingleton
() =
default
;
60
61
static
T*
mInstance
;
62
63
private
:
64
65
fsTSingleton
(
fsTSingleton
const
&) =
delete
;
66
fsTSingleton
& operator=(
fsTSingleton
const
&) =
delete
;
67
};
68
69
template
<
class
T>
70
T*
fsTSingleton<T>::mInstance
=
nullptr
;
71
72
#endif
fsTSingleton::~fsTSingleton
~fsTSingleton()=default
fsTSingleton::instanceGet
static T * instanceGet()
Definition
fsTSingleton.h:43
fsTSingleton::mInstance
static T * mInstance
Definition
fsTSingleton.h:61
fsTSingleton::destroy
static void destroy()
Definition
fsTSingleton.h:49
fsTSingleton::fsTSingleton
fsTSingleton()=default
fsTSingleton::create
static void create()
Definition
fsTSingleton.h:37
fsAssert.h
fsAssert
#define fsAssert(pStatement, pFmtString,...)
Definition
fsAssert.h:23
Generated on
for Fission by
1.16.1