-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTypeTraits.h
More file actions
352 lines (291 loc) · 7.62 KB
/
TypeTraits.h
File metadata and controls
352 lines (291 loc) · 7.62 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#pragma once
#include <vector>
#include <string> //std::vector
#include "EnumReflect.h" //EnumToString
#pragma warning(push)
#pragma warning(disable: 4100) // Unreferenced parameter
class CppTypeInfo;
class ReflectClass;
//
// Base class for performing field conversion to string / from string.
//
class TypeTraits
{
public:
//
// Returns true if field type is primitive (int, string, etc...) - so all types which are not complex.
// Complex class type is derived from ReflectClass - so ReflectClassPtr() & GetType() returns non-null
//
virtual bool IsPrimitiveType()
{
return true;
}
virtual const char* name()
{
return "";
}
//
// Gets field complex type()
//
virtual CppTypeInfo* GetFieldType()
{
return nullptr;
}
//
// Converts instance pointers to ReflectClass*.
//
virtual ReflectClass* ReflectClassPtr( void* p )
{
return nullptr;
}
virtual bool GetArrayElementType( CppTypeInfo*& type )
{
// Not array type
return false;
}
//
// If GetArrayElementType() returns true, returns size of array.
//
virtual size_t ArraySize( void* p )
{
return 0;
}
virtual void SetArraySize( void* p, size_t size )
{
}
//
// Gets field (at p) array element at position i.
//
virtual void* ArrayElement( void* p, size_t i )
{
return nullptr; // Invalid operation, since not array
}
//
// Converts specific data to String.
//
// Default implementation: Don't know how to print given field, ignore it
//
virtual CStringW ToString( void* pField ) { return CStringW(); }
//
// Converts from String to data.
//
// Default implementation: Value cannot be set from string.
//
virtual void FromString( void* pField, const wchar_t* value ) { }
};
//
// Generic class definition, which can be applied to any class type. This implementation however does nothing -
// does not serializes or deserializes your type. You must override with your type specific implementation
// for each supported data type. For more examples - see below.
//
template <class T>
class TypeTraitsT : public TypeTraits
{
public:
virtual bool IsPrimitiveType()
{
return ! std::is_base_of<ReflectClass, T>::value;
}
virtual const char* name()
{
return typeid(T).name();
}
CppTypeInfo* GetFieldType()
{
__if_exists(T::GetType)
{
return &T::GetType();
}
return nullptr;
}
virtual ReflectClass* ReflectClassPtr( void* p )
{
if constexpr (std::is_base_of<ReflectClass, T>::value )
// Works without dynamic_cast, compiler does not likes dynamic_cast in here.
return (ReflectClass*)(T*)p;
else
return nullptr;
}
virtual CStringW ToString( void* p )
{
if constexpr ( std::is_enum<T>::value )
return EnumToString( *((T*)p) ).c_str();
else
return CStringW();
}
};
template <>
class TypeTraitsT<CStringW> : public TypeTraits
{
public:
virtual const char* name() { return "CStringW"; }
virtual CStringW ToString( void* pField )
{
CString* s = (CString*)pField;
return *s;
}
virtual void FromString( void* pField, const wchar_t* value )
{
CString* s = (CString*)pField;
*s = value;
}
};
template <>
class TypeTraitsT<CStringA> : public TypeTraits
{
public:
virtual const char* name() { return "CStringA"; }
virtual CStringW ToString(void* pField)
{
CStringA* s = (CStringA*)pField;
return CStringW(*s);
}
virtual void FromString(void* pField, const wchar_t* value)
{
CStringA* s = (CStringA*)pField;
*s = value;
}
};
template <>
class TypeTraitsT<std::wstring> : public TypeTraits
{
public:
virtual const char* name() { return "std::wstring"; }
virtual CStringW ToString(void* pField)
{
return ((std::wstring*)pField)->c_str();
}
virtual void FromString(void* pField, const wchar_t* value)
{
*((std::wstring*)pField) = value;
}
};
template <>
class TypeTraitsT<std::string> : public TypeTraits
{
public:
virtual const char* name() { return "std::string"; }
virtual CStringW ToString(void* pField)
{
return ((std::string*)pField)->c_str();
}
virtual void FromString(void* pField, const wchar_t* value)
{
auto& s = *((std::string*)pField);
s = CW2A(value);
}
};
template <>
class TypeTraitsT<int> : public TypeTraits
{
public:
virtual const char* name() { return "int"; }
virtual CStringW ToString( void* pField )
{
int* p = (int*) pField;
char buf[10];
_itoa_s(*p, buf, 10);
return buf;
}
virtual void FromString( void* pField, const wchar_t* value )
{
int* p = (int*)pField;
*p = _wtoi(value);
}
};
template <>
class TypeTraitsT<bool> : public TypeTraits
{
public:
virtual const char* name() { return "bool"; }
virtual CStringW ToString( void* p )
{
if( *(bool*)p )
return L"true";
return L"false";
}
virtual void FromString( void* pField, const wchar_t* value )
{
bool* pb = (bool*)pField;
if( _wcsicmp(value, L"true") == 0 )
{
*pb = true;
return;
}
*pb = false;
}
};
template <class E>
class TypeTraitsT< std::vector<E> > : public TypeTraits
{
public:
virtual const char* name() { return typeid(std::vector<E>).name(); }
virtual bool GetArrayElementType( CppTypeInfo*& type )
{
__if_exists(E::GetType)
{
type = &E::GetType();
return true;
}
return true;
}
virtual size_t ArraySize( void* p )
{
std::vector<E>* v = (std::vector<E>*) p;
return v->size();
}
virtual void SetArraySize( void* p, size_t size )
{
std::vector<E>* v = (std::vector<E>*) p;
v->resize(size);
}
virtual void* ArrayElement( void* p, size_t i )
{
std::vector<E>* v = (std::vector<E>*) p;
return &v->at( i );
}
virtual CStringW ToString( void* pField )
{
return CStringW();
}
};
/*
COLORREF is typedef'ed from DWORD. But it's possible that we will want to serialize DWORD as well as a number, but we want to threat
color separately and independently from DWORD. We define here extra clue class just to be able to not to mix COLORREF and DWORD.
*/
class ColorRef
{
public:
COLORREF color;
ColorRef() : color( 0 )
{
}
ColorRef( COLORREF _color ) : color( _color )
{
}
unsigned char GetR() { return GetRValue( color ); }
unsigned char GetG() { return GetGValue( color ); }
unsigned char GetB() { return GetBValue( color ); }
operator COLORREF&() { return color; }
};
template <>
class TypeTraitsT<ColorRef> : public TypeTraits
{
public:
virtual const char* name() { return "ColorRef"; }
virtual CStringW ToString( void* pField )
{
char buf[256];
ColorRef clr = *(ColorRef*)pField;
sprintf_s( buf, sizeof( buf ), "%u,%u,%u", clr.GetR(), clr.GetG(), clr.GetB() );
return buf;
}
virtual void FromString( void* pField, const wchar_t* value )
{
ColorRef* pclr = (ColorRef*)pField;
int clr[3];
if( swscanf_s( value, L"%u,%u,%u", &clr[0], &clr[1], &clr[2] ) == 3 )
*pclr = RGB( clr[0], clr[1], clr[2] );
}
};
#pragma warning(pop)