forked from toptensoftware/simplelib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdyntype.h
More file actions
181 lines (154 loc) · 3.55 KB
/
dyntype.h
File metadata and controls
181 lines (154 loc) · 3.55 KB
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
#ifndef __simplelib_dyntype_h__
#define __simplelib_dyntype_h__
namespace SimpleLib
{
// WHAT THE HELL IS THIS???
// It's a cross platform version of __declspec(selectany) to store
// the global pointer to the first type entry
class CDynType;
template <int iDummy = 0>
struct CDynTypeFirstHolder
{
static CDynType* m_pFirst;
};
template <int iDummy> CDynType* CDynTypeFirstHolder<iDummy>::m_pFirst = nullptr;
// Store information about a CDynamicBase type
class CDynType
{
public:
// Construction
CDynType(int iID, void* (*pfnCreate)(), const char* pszName) :
m_iID(iID),
m_pfnCreate(pfnCreate),
m_pszName(pszName)
{
// Check for duplicate type id's
assert(iID == 0 || GetTypeFromID(iID) == nullptr);
m_pNext = CDynTypeFirstHolder<>::m_pFirst;
CDynTypeFirstHolder<>::m_pFirst = this;
}
// Given a type ID, return the CDynType for it
static CDynType* GetTypeFromID(int iID)
{
CDynType* p = CDynTypeFirstHolder<>::m_pFirst;
while (p)
{
if (p->m_iID == iID)
return p;
p = p->m_pNext;
}
return nullptr;
}
static CDynType* GetTypeFromName(const char* pszName)
{
CDynType* p = CDynTypeFirstHolder<>::m_pFirst;
while (p)
{
if (p->m_pszName != nullptr)
{
if (p->m_pszName == pszName || strcmp(p->m_pszName, pszName) == 0)
return p;
}
p = p->m_pNext;
}
return nullptr;
}
void* CreateInstance() const
{
assert(m_pfnCreate != nullptr);
return m_pfnCreate();
}
int GetID() const
{
return m_iID;
}
const char* GetName() const
{
return m_pszName;
}
protected:
int m_iID;
void* (*m_pfnCreate)();
const char* m_pszName;
CDynType* m_pNext;
};
// Base class for CDynamicBase classes that dont derive from another dynamic type
class CDynamicBaseNone
{
public:
virtual void* QueryCast(CDynType* ptype) { return nullptr; }
virtual CDynType* QueryType() { return nullptr; };
static CDynType* GetType() { return nullptr; };
};
// CDynamicBase
template <typename TSelf, typename TBase = CDynamicBaseNone>
class CDynamicBase : public TBase
{
public:
template <typename T>
T* As()
{
if (!this) return nullptr;
T* p = QueryAs<T>();
assert(p != nullptr);
return p;
}
template <typename T>
T* QueryAs()
{
return (T*)QueryCast(&T::dyntype);
}
static CDynType* GetType()
{
return &TSelf::dyntype;
}
static CDynType* GetBaseType()
{
return TBase::GetType();
}
static const char* GetTypeName()
{
return nullptr;
}
virtual void* QueryCast(CDynType* ptype)
{
if (ptype == GetType())
{
return static_cast<TSelf*>(this);
}
void* p = TBase::QueryCast(ptype);
return p;
}
virtual CDynType* QueryType()
{
return GetType();
}
};
// CDynamic
template <typename TSelf, typename TBase = CDynamicBaseNone>
class CDynamic : public CDynamicBase<TSelf, TBase>
{
public:
static CDynType dyntype;
};
// CDynamicCreatable
template <typename TSelf, typename TBase = CDynamicBaseNone, int iID = 0>
class CDynamicCreatable : public CDynamicBase<TSelf, TBase>
{
public:
static void* CreateInstance()
{
return new TSelf();
}
static int GenerateTypeID()
{
return 0;
}
static CDynType dyntype;
};
template <typename TSelf, typename TBase>
CDynType CDynamic<TSelf, TBase>::dyntype(0, nullptr, TSelf::GetTypeName());
template <typename TSelf, typename TBase, int iID>
CDynType CDynamicCreatable<TSelf, TBase, iID>::dyntype(iID ? iID : TSelf::GenerateTypeID(), TSelf::CreateInstance, TSelf::GetTypeName());
} // namespace
#endif // __simplelib_dyntype_h__