-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathSerializer.mqh
449 lines (379 loc) · 14.2 KB
/
Serializer.mqh
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Prevents processing this includes file for the second time.
#ifndef SERIALIZER_MQH
#define SERIALIZER_MQH
// Includes.
#include "Convert.mqh"
#include "Serializer.define.h"
#include "Serializer.enum.h"
#include "SerializerConverter.mqh"
#include "SerializerNode.mqh"
#include "SerializerNodeIterator.mqh"
#include "SerializerNodeParam.mqh"
#define SERIALIZER_DEFAULT_FP_PRECISION 8
class Serializer {
protected:
SerializerNode* _node;
SerializerNode* _root;
SerializerMode _mode;
bool _root_node_ownership;
bool _skip_hidden;
string _single_value_name;
unsigned int _flags;
// Floating-point precision.
int fp_precision;
public:
/**
* Constructor.
*/
Serializer(SerializerNode* node, SerializerMode mode, int flags) : _node(node), _mode(mode), _flags(flags) {
_root = node;
_root_node_ownership = true;
fp_precision = SERIALIZER_DEFAULT_FP_PRECISION;
if (_flags == 0) {
// Preventing flags misuse.
_flags = SERIALIZER_FLAG_INCLUDE_ALL;
}
}
/**
* Destructor.
*/
~Serializer() {
if (_root_node_ownership && _root != NULL) delete _root;
}
template <typename X>
SerializerIterator<X> Begin() {
SerializerIterator<X> iter(&this, _node);
return iter;
}
void FreeRootNodeOwnership() { _root_node_ownership = false; }
/**
* Enters object or array for a given key or just iterates over objects/array during unserializing.
*/
bool Enter(SerializerEnterMode mode = SerializerEnterObject, string key = "") {
if (IsWriting()) {
#ifdef __MQL__
SerializerNodeParam* nameParam = (key != "" && key != "") ? SerializerNodeParam::FromString(key) : NULL;
#else
SerializerNodeParam* nameParam = (key != "") ? SerializerNodeParam::FromString(key) : NULL;
#endif
// When writing, we need to make parent->child structure. It is not
// required when reading, because structure is full done by parsing the
// string.
_node = new SerializerNode(mode == SerializerEnterObject ? SerializerNodeObject : SerializerNodeArray, _node,
nameParam);
if (PTR_ATTRIB(_node, GetParent()) != NULL) PTR_ATTRIB(PTR_ATTRIB(_node, GetParent()), AddChild(_node));
if (_root == NULL) _root = _node;
} else {
if (_node == NULL) {
_node = _root;
return true;
}
SerializerNode* child;
if (key != "") {
// We need to enter object that matches given key.
for (unsigned int i = 0; i < PTR_ATTRIB(_node, NumChildren()); ++i) {
child = PTR_ATTRIB(_node, GetChild(i));
if (PTR_ATTRIB(PTR_ATTRIB(child, GetKeyParam()), AsString(false, false)) == key) {
_node = child;
return true;
}
}
// We didn't enter into child node.
return false;
} else if (key == "") {
_node = PTR_ATTRIB(_node, GetNextChild());
}
}
return true;
}
/**
* Leaves current object/array. Used in custom Serialize() method.
*/
void Leave() { _node = PTR_ATTRIB(_node, GetParent()); }
/**
* Checks whether we are in serialization process. Used in custom Serialize() method.
*/
bool IsWriting() { return _mode == Serialize || bool(_flags & SERIALIZER_FLAG_SIMULATE_SERIALIZE); }
/**
* Checks whether we are in unserialization process. Used in custom Serialize() method.
*/
bool IsReading() { return !IsWriting(); }
/**
* Checks whether current node is inside array. Used in custom Serialize() method.
*/
bool IsArray() {
return _mode == Unserialize && _node != NULL && PTR_ATTRIB(_node, GetType()) == SerializerNodeArray;
}
/**
* Returns number of array items inside current array.
*/
unsigned int NumArrayItems() { return _node != NULL ? PTR_ATTRIB(_node, NumChildren()) : 0; }
/**
* Checks whether current node is an object. Used in custom Serialize() method.
*/
bool IsObject() {
return _mode == Unserialize && _node != NULL && PTR_ATTRIB(_node, GetType()) == SerializerNodeObject;
}
/**
* Returns number of child nodes.
*/
unsigned int NumChildren() { return _node ? PTR_ATTRIB(_node, NumChildren()) : 0; }
/**
* Returns root node or NULL. Could be used after unserialization.
*/
SerializerNode* GetRoot() { return _root; }
/**
* Returns child node for a given index or NULL.
*/
SerializerNode* GetChild(unsigned int index) { return _node ? PTR_ATTRIB(_node, GetChild(index)) : NULL; }
/**
* Returns floating-point precision.
*/
int GetFloatingPointPrecision() { return fp_precision; }
/**
* Sets floating-point precision.
*/
void SetPrecision(int _fp_precision) { fp_precision = _fp_precision; }
/**
* Serializes or unserializes object.
*/
template <typename T, typename V>
void PassObject(T& self, string name, V& value, unsigned int flags = SERIALIZER_FIELD_FLAG_DEFAULT) {
PassStruct(self, name, value, flags);
}
/**
* Serializes or unserializes object that acts as a value.
*/
template <typename T, typename V>
void PassValueObject(T& self, string name, V& value, unsigned int flags = SERIALIZER_FIELD_FLAG_DEFAULT) {
if (_mode == Serialize) {
value.Serialize(this);
fp_precision = SERIALIZER_DEFAULT_FP_PRECISION;
SerializerNode* obj = _node.GetChild(_node.NumChildren() - 1);
obj.SetKey(name);
} else {
_single_value_name = name;
value.Serialize(this);
}
}
bool IsFieldVisible(int serializer_flags, int field_flags) {
// Is field visible? Such field cannot be exluded in anyway.
if ((field_flags & SERIALIZER_FIELD_FLAG_VISIBLE) == SERIALIZER_FIELD_FLAG_VISIBLE) {
return true;
}
if ((field_flags & SERIALIZER_FIELD_FLAG_HIDDEN) == SERIALIZER_FIELD_FLAG_HIDDEN) {
if ((serializer_flags & SERIALIZER_FLAG_SKIP_HIDDEN) != SERIALIZER_FLAG_SKIP_HIDDEN) {
// Field is hidden, but serializer has no SERIALIZER_FLAG_SKIP_HIDDEN flag set, so field will be serialized.
return true;
}
}
// Is field hidden?
if ((serializer_flags & SERIALIZER_FLAG_SKIP_HIDDEN) == SERIALIZER_FLAG_SKIP_HIDDEN) {
if ((field_flags & SERIALIZER_FIELD_FLAG_HIDDEN) == SERIALIZER_FIELD_FLAG_HIDDEN) {
return false;
}
}
// Is field default?
if ((serializer_flags & SERIALIZER_FLAG_EXCLUDE_DEFAULT) == SERIALIZER_FLAG_EXCLUDE_DEFAULT) {
if ((field_flags & SERIALIZER_FIELD_FLAG_DEFAULT) == SERIALIZER_FIELD_FLAG_DEFAULT) {
if ((serializer_flags & SERIALIZER_FLAG_INCLUDE_DEFAULT) == SERIALIZER_FLAG_INCLUDE_DEFAULT) {
// Field was excluded by e.g., dynamic or feature type, but included explicitly by flag.
return true;
} else {
// Field was excluded by e.g., dynamic or feature type, but not included again explicitly by flag.
return false;
}
}
}
// Is field dynamic?
if ((serializer_flags & SERIALIZER_FLAG_INCLUDE_DYNAMIC) == SERIALIZER_FLAG_INCLUDE_DYNAMIC) {
if ((field_flags & SERIALIZER_FIELD_FLAG_DYNAMIC) == SERIALIZER_FIELD_FLAG_DYNAMIC) {
return true;
}
}
// Is field a feature?
if ((serializer_flags & SERIALIZER_FLAG_INCLUDE_FEATURE) == SERIALIZER_FLAG_INCLUDE_FEATURE) {
if ((field_flags & SERIALIZER_FIELD_FLAG_FEATURE) == SERIALIZER_FIELD_FLAG_FEATURE) {
return true;
}
}
return false;
}
/**
* Serializes or unserializes structure.
*/
template <typename T, typename V>
void PassStruct(T& self, string name, V& value, unsigned int flags = SERIALIZER_FIELD_FLAG_DEFAULT) {
if (_mode == Serialize) {
if (!IsFieldVisible(_flags, flags)) return;
}
// Entering object or array. value's Serialize() method should check if it's array by s.IsArray().
// Note that binary serializer shouldn't rely on the property names and just skip entering/leaving at all.
// Entering a root node does nothing, because we would end up going to first child node, which we don't want to do.
if (_mode == Serialize || (_mode == Unserialize && name != "")) {
Enter(SerializerEnterObject, name);
}
SerializerNodeType newType = value.Serialize(THIS_REF);
fp_precision = SERIALIZER_DEFAULT_FP_PRECISION;
// value's Serialize() method returns which type of node it should be treated as.
if (newType != SerializerNodeUnknown) PTR_ATTRIB(_node, SetType(newType));
// Goes to the sibling node. In other words, it goes to the parent's next node.
if (_mode == Serialize || (_mode == Unserialize && name != "")) {
Leave();
}
}
void Next() {
if (PTR_ATTRIB(_node, GetParent()) == NULL) {
return;
}
_node = PTR_ATTRIB(PTR_ATTRIB(_node, GetParent()), GetNextChild());
}
/**
* Serializes or unserializes enum value (stores it as integer).
*/
template <typename T, typename V>
void PassEnum(T& self, string name, V& value, unsigned int flags = SERIALIZER_FIELD_FLAG_DEFAULT) {
int enumValue;
if (_mode == Serialize) {
if (!IsFieldVisible(_flags, flags)) {
return;
}
enumValue = (int)value;
Pass(self, name, enumValue, flags);
} else {
Pass(self, name, enumValue, flags);
value = (V)enumValue;
}
}
template <typename T, typename VT>
void PassArray(T& self, string name, ARRAY_REF(VT, array), unsigned int flags = SERIALIZER_FIELD_FLAG_DEFAULT) {
int num_items;
if (_mode == Serialize) {
if (!IsFieldVisible(_flags, flags)) {
// Skipping prematurely instead of creating object by new.
return;
}
}
if (_mode == Serialize) {
if (Enter(SerializerEnterArray, name)) {
num_items = ArraySize(array);
for (int i = 0; i < num_items; ++i) {
PassStruct(this, "", array[i]);
}
Leave();
}
} else {
if (Enter(SerializerEnterArray, name)) {
SerializerNode* parent = _node;
num_items = (int)NumArrayItems();
ArrayResize(array, num_items);
for (SerializerIterator<VT> si = Begin<VT>(); si.IsValid(); ++si) {
if (si.HasKey()) {
// Should not happen.
} else {
_node = parent.GetChild(si.Index());
array[si.Index()] = si.Struct();
}
}
Leave();
}
}
}
/**
* Serializes or unserializes pointer to object.
*/
template <typename T, typename V>
void Pass(T& self, string name, V*& value, unsigned int flags = SERIALIZER_FIELD_FLAG_DEFAULT) {
if (_mode == Serialize) {
if (!IsFieldVisible(_flags, flags)) {
return;
}
PassObject(self, name, value, flags);
} else {
V* newborn = new V();
PassObject(self, name, newborn, flags);
value = newborn;
}
}
/**
* Serializes or unserializes simple value.
*/
template <typename T, typename V>
SerializerNode* Pass(T& self, string name, V& value, unsigned int flags = SERIALIZER_FIELD_FLAG_DEFAULT) {
SerializerNode* child = NULL;
bool _skip_push = (_flags & SERIALIZER_FLAG_SKIP_PUSH) == SERIALIZER_FLAG_SKIP_PUSH;
if (_mode == Serialize) {
if (!IsFieldVisible(_flags, flags)) {
return NULL;
}
SerializerNodeParam* key = name != "" ? SerializerNodeParam::FromString(name) : NULL;
SerializerNodeParam* val = SerializerNodeParam::FromValue(value);
PTR_ATTRIB(val, SetFloatingPointPrecision(GetFloatingPointPrecision()));
child = new SerializerNode(SerializerNodeObjectProperty, _node, key, val, flags);
if (!_skip_push) {
PTR_ATTRIB(_node, AddChild(child));
}
return child;
} else {
if (name == "") {
// Determining name from Serializer's SingleValueName().
name = _single_value_name;
}
for (unsigned int i = 0; i < PTR_ATTRIB(_node, NumChildren()); ++i) {
child = PTR_ATTRIB(_node, GetChild(i));
if (PTR_ATTRIB(PTR_ATTRIB(child, GetKeyParam()), AsString(false, false)) == name) {
SerializerNodeParamType paramType = PTR_ATTRIB(PTR_ATTRIB(child, GetValueParam()), GetType());
switch (paramType) {
case SerializerNodeParamBool:
Convert::BoolToType(PTR_ATTRIB(PTR_ATTRIB(child, GetValueParam()), _integral)._bool, value);
break;
case SerializerNodeParamLong:
Convert::LongToType(PTR_ATTRIB(PTR_ATTRIB(child, GetValueParam()), _integral)._long, value);
break;
case SerializerNodeParamDouble:
Convert::DoubleToType(PTR_ATTRIB(PTR_ATTRIB(child, GetValueParam()), _integral)._double, value);
break;
case SerializerNodeParamString:
// There shouldn't be a conversion to int!
Convert::StringToType(PTR_ATTRIB(PTR_ATTRIB(child, GetValueParam()), _string), value);
break;
}
return NULL;
}
}
}
return NULL;
}
template <typename X>
static SerializerConverter MakeStubObject(int _serializer_flags = SERIALIZER_FLAG_INCLUDE_ALL, int _n1 = 1,
int _n2 = 1, int _n3 = 1, int _n4 = 1, int _n5 = 1) {
if (_serializer_flags == 0) {
// Avoiding flags misuse.
_serializer_flags = SERIALIZER_FLAG_INCLUDE_ALL;
}
X stub;
stub.SerializeStub(_n1, _n2, _n3, _n4, _n5);
return SerializerConverter::FromObject(stub, _serializer_flags);
}
};
#endif // End: SERIALIZER_MQH