-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.h
335 lines (287 loc) · 5.28 KB
/
misc.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#pragma once
#include <assert.h>
#include <stdlib.h>
#include <map>
#include <string>
struct PosColorVertex
{
float m_x;
float m_y;
float m_z;
float m_u;
float m_v;
unsigned int m_abgr;
};
typedef unsigned int Guid;
void GenerateGUID( Guid &g, const char *str );
long int fsize(FILE* _file);
void* fload(const char* _filePath, unsigned int *len=NULL);
const char *skipws( const char *txt );
char *skipws( char *txt );
bool ParseString( std::string &i, const char *&cursor );
bool ParseInt( int &i, const char *&cursor );
bool ParseFloat( float &f, const char *&cursor );
bool ParseExpect( const std::string &expect, const char *&cursor );
bool ParseTest( const std::string &expect, const char *&cursor );
bool ParseEOF( const char *cursor );
bool ParseToken( std::string &token, const char *&s );
bool isoneof( char c, const char *possible );
const char *firstof( const char *s, const char *possible );
const char *firstnotof( const char *s, const char *possible );
inline float Min( float a, float b )
{
return a < b ? a : b;
}
inline float Max( float a, float b )
{
return a > b ? a : b;
}
#pragma warning(push)
#pragma warning(disable: 4127)
struct CoreType
{
bool m_registered;
int m_from, m_to;
CoreType *m_parent, *m_sibling, *m_child;
CoreType *m_next;
static CoreType *s_root;
static CoreType *s_base;
explicit CoreType( CoreType *parent ) : m_registered(false), m_from(-1), m_to(-1), m_parent(parent), m_sibling(NULL), m_child(NULL)
{
m_next = s_root;
s_root = this;
}
void Register()
{
if ( m_registered )
return;
m_registered = true;
if ( m_parent )
{
m_parent->Register();
m_sibling = m_parent->m_child;
m_parent->m_child = this;
}
}
static void InitClassTypes();
};
inline bool IsTypeOf( CoreType const *a, CoreType const *b )
{
return a->m_from >= b->m_from && a->m_to <= b->m_to;
}
template <class A, class B>
inline A *CoreCast( B *ptr )
{
if ( IsTypeOf( &A::s_Type, &B::s_Type ) )
return (A*)ptr;
return NULL;
}
class CoreClass
{
public:
virtual ~CoreClass() {}
void *operator new( size_t s )
{
void *mem = malloc(s);
if ( mem )
memset( mem, 0, s );
return mem;
}
void operator delete( void *ptr )
{
free( ptr );
}
static CoreType s_Type;
virtual CoreType const *Type() const { return &s_Type; }
virtual CoreType const *SuperType() const { return NULL; }
};
class CoreClassCreator
{
CoreClassCreator *m_next;
static CoreClassCreator *s_root;
public:
CoreClassCreator()
{
m_next = s_root;
s_root = this;
}
virtual bool CanCreate( std::string const &name ) = 0;
virtual CoreClass *Create() = 0;
static CoreClass *Create( std::string const &name )
{
CoreClassCreator *c = s_root;
while ( c )
{
if ( c->CanCreate( name ) )
{
return c->Create();
}
c = c->m_next;
}
return NULL;
}
};
template <class T>
class ClassCreator : public CoreClassCreator
{
std::string m_name;
public:
ClassCreator( const char *name ) : m_name(name)
{
}
bool CanCreate( std::string const &name )
{
return name == m_name;
}
CoreClass *Create()
{
return new T;
}
};
class DefMgr
{
DefMgr *m_next;
static DefMgr *s_root;
public:
DefMgr();
virtual ~DefMgr();
virtual void Reload();
virtual void Clear();
};
template <class T, bool LOADGROUP=false >
class TypedDefMgr : public DefMgr
{
protected:
std::map< Guid, T* > m_defs;
public:
const T* Get( const char *name, bool createDef = true )
{
Guid g;
GenerateGUID( g, name );
std::map<Guid,T*>::const_iterator f = m_defs.find( g );
if ( f == m_defs.end() )
{
if ( !createDef )
{
return NULL;
}
else
{
T* def = new T;
def->Load( name );
m_defs[g] = def;
return def;
}
}
else
{
return f->second;
}
}
virtual void Reload()
{
std::map<Guid,T*>::iterator c = m_defs.begin();
while ( c != m_defs.end() )
{
T* def = c->second;
def->Reload();
c++;
}
}
virtual void Clear()
{
std::map<Guid,T*>::iterator c = m_defs.begin();
while ( c != m_defs.end() )
{
T* def = c->second;
def->Clear();
delete def;
c++;
}
m_defs.clear();
}
bool LoadGroup( const char *filename )
{
if ( LOADGROUP )
{
char* ents = (char*)fload(filename);
if ( ents )
{
const char *cursor = ents;
while ( cursor && *cursor )
{
std::string name, token;
if ( !ParseToken( name, cursor ) )
break;
if ( !ParseToken( token, cursor ) )
break;
if ( token == "{" )
{
T *def = new T;
def->Parse( name.c_str(), cursor );
Guid g;
GenerateGUID( g, name.c_str() );
m_defs[g] = def;
}
}
free( ents );
}
return true;
}
else
{
return false;
}
}
};
template <class T, int S>
class ChunkArray
{
struct chunk
{
T items[S];
int used;
chunk *next;
};
chunk *root;
public:
void Init()
{
root = NULL;
}
void Add( T item )
{
chunk *c = root;
while ( c )
{
if ( c->used < S )
{
c->items[c->used++] = item;
return;
}
c = c->next;
}
chunk *nc = new chunk;
nc->used = 1;
nc->next = root;
nc->items[0] = item;
}
void Remove( T item )
{
chunk *c = root;
while ( c )
{
for (int i=0; i<c->used; i++)
{
if ( c->items[i] == item )
{
c->used--;
c->items[i] = c->items[c->used];
return;
}
}
c = c->next;
}
}
};
#pragma warning(pop)