-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTypeTraits.h
218 lines (183 loc) · 4.77 KB
/
TypeTraits.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
#pragma once
#include <vector>
#include <string> //std::vector
#pragma warning(push)
#pragma warning(disable: 4100) // Unreferenced parameter
class CppTypeInfo;
//
// Base class for performing field conversion to string / from string.
//
class TypeTraits
{
public:
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 CStringW ToString( void* pField ) { return CStringW(); }
};
template <>
class TypeTraitsT<CString> : public TypeTraits
{
public:
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<int> : public TypeTraits
{
public:
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 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 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 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)