-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModule.h
139 lines (121 loc) · 2.13 KB
/
Module.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
#ifndef ____MODULE____
#define ____MODULE____
#include "NoCopy.h"
#include <typeinfo>
#include "Type.h"
namespace nicehero
{
template <typename T>
class Module:public NoCopy
{
class Zero
{
public:
operator int() const;
private:
char m_reserved[11];
};
class True
{
public:
operator bool() const;
private:
char m_reserved[11];
};
public:
static T * getInstance(void);
bool isStarted() const;
True checkStart();
const char* getModuleName() const;
protected:
Module();
virtual ~Module() = 0;
Zero initial();
Zero start();
Zero run();
Zero stop();
private:
bool m_started;
unsigned int m_refs;
const char * m_moduleName;
static T* m_instance;
bool checkStop();
};
template <typename T>
nicehero::Module<T>::~Module()
{
}
template <typename T>
nicehero::Module<T>::Module()
{
if (sizeof(static_cast<T *>(0)->initial()) == sizeof(int) || sizeof(static_cast<T *>(0)->initial()) == sizeof(bool))
{
initial();
}
m_moduleName = typeid(*this).name();
}
template <class T>
Module<T>::Zero::operator int() const
{
return 0;
}
template <class T>
Module<T>::True::operator bool() const
{
return true;
}
template <class T>
bool Module<T>::isStarted() const
{
return m_started;
}
template <class T>
typename Module<T>::True Module<T>::checkStart()
{
return True();
}
template <class T>
typename Module<T>::Zero Module<T>::initial()
{
return Zero();
}
template <class T>
typename Module<T>::Zero Module<T>::start()
{
return Zero();
}
template <class T>
typename Module<T>::Zero Module<T>::run()
{
return Zero();
}
template <class T>
typename Module<T>::Zero Module<T>::stop()
{
return Zero();
}
template <class T>
bool Module<T>::checkStop()
{
return m_refs == 0;
}
template <class T>
const char* Module<T>::getModuleName() const
{
return m_moduleName;
}
}
#define MODULE_IMPL(T) \
namespace nicehero \
{\
template <> \
T* Module<T>::m_instance = nullptr;\
template <> \
T * Module<T>::getInstance() \
{ \
static T g_instance;\
m_instance = &g_instance;\
return m_instance; \
}\
}\
#endif // !____MODULE____