-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathSerializerNode.mqh
369 lines (300 loc) · 9.96 KB
/
SerializerNode.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
//+------------------------------------------------------------------+
//| 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 JSON_NODE_MQH
#define JSON_NODE_MQH
// Includes.
#include "Math.extern.h"
#include "SerializerNode.enum.h"
#include "SerializerNodeParam.mqh"
class SerializerNode {
protected:
SerializerNodeType _type;
SerializerNode* _parent;
SerializerNodeParam* _key;
SerializerNodeParam* _value;
ARRAY(SerializerNode*, _children);
unsigned int _numChildren;
unsigned int _currentChildIndex;
unsigned int _flags;
int _index;
public:
/**
* Constructor.
*/
SerializerNode(SerializerNodeType type, SerializerNode* parent = NULL, SerializerNodeParam* key = NULL,
SerializerNodeParam* value = NULL, unsigned int flags = 0)
: _type(type), _parent(parent), _key(key), _value(value), _numChildren(0), _currentChildIndex(0), _flags(flags) {}
/**
* Destructor.
*/
~SerializerNode() {
if (_key) delete _key;
if (_value) delete _value;
for (unsigned int i = 0; i < _numChildren; ++i) delete _children[i];
}
void SetKey(string name) {
if (_key != NULL) {
delete _key;
}
_key = name != "" ? SerializerNodeParam::FromString(name) : NULL;
}
/**
* Sets node flags.
*/
void SetFlags(unsigned int flags) { _flags = flags; }
/**
* Returns node flags.
*/
unsigned int GetFlags() { return _flags; }
/**
* Checks whether node has specified key.
*/
bool HasKey() { return _key != NULL && PTR_ATTRIB(_key, _string) != ""; }
/**
* Checks whether node is an array.
*/
bool IsArray() { return _type == SerializerNodeArray; }
/**
* Checks whether node is an objec with properties.
*/
bool IsObject() { return _type == SerializerNodeObject; }
/**
* Checks whether node is a container.
*/
bool IsContainer() { return _type == SerializerNodeArray || _type == SerializerNodeObject; }
/**
* Checks whether node is a container for values.
*/
bool IsValuesContainer() {
return (_type == SerializerNodeArray || _type == SerializerNodeObject) && _numChildren > 0 &&
!PTR_ATTRIB(_children[0], IsContainer());
}
/**
* Returns index of the current node in the parent.
*/
int Index() { return _index; }
/**
* Returns key specified for a node or empty string (not a NULL).
*/
string Key() { return _key != NULL ? PTR_ATTRIB(_key, AsString(false, false)) : ""; }
/**
* Returns tree size in bytes.
*/
int BinarySize() {
int _result = 0;
if (!IsContainer()) {
switch (PTR_ATTRIB(_value, GetType())) {
case SerializerNodeParamBool:
_result += 1;
break;
case SerializerNodeParamDouble:
_result += 8;
break;
case SerializerNodeParamLong:
_result += 4;
break;
case SerializerNodeParamString:
_result += StringLen(PTR_ATTRIB(_value, _string)) + 1;
break;
default:
Print("Error: Wrong value type ", EnumToString(GetType()), "!");
DebugBreak();
}
}
for (unsigned int i = 0; i < _numChildren; ++i) _result += PTR_ATTRIB(_children[i], BinarySize());
return _result;
}
/**
* Overrides floating-point precision for this node and all the children.
*/
void OverrideFloatingPointPrecision(int _fp_precision) {
SerializerNodeParam* _value_param = GetValueParam();
if (_value_param != NULL) {
PTR_ATTRIB(_value_param, SetFloatingPointPrecision(_fp_precision));
}
for (unsigned int i = 0; i < _numChildren; ++i) {
PTR_ATTRIB(_children[i], OverrideFloatingPointPrecision(_fp_precision));
}
}
/**
* Returns total number of children and their children inside this node.
*/
unsigned int TotalNumChildren() {
if (!IsContainer()) return 1;
unsigned int _result = 0;
for (unsigned int i = 0; i < _numChildren; ++i) _result += PTR_ATTRIB(_children[i], TotalNumChildren());
return _result;
}
/**
* Returns maximum number of children in the last "dimension".
*/
unsigned int MaximumNumChildrenInDeepEnd() {
unsigned int _result = 0, i;
if (GetParent() == NULL) {
for (i = 0; i < _numChildren; ++i) {
if (IsObject())
_result += PTR_ATTRIB(_children[i], MaximumNumChildrenInDeepEnd());
else
_result = MathMax(_result, PTR_ATTRIB(_children[i], MaximumNumChildrenInDeepEnd()));
}
return _result;
}
if (IsObject() || IsArray()) {
for (i = 0; i < _numChildren; ++i) {
_result += PTR_ATTRIB(_children[i], MaximumNumChildrenInDeepEnd());
}
return _result;
}
return 1;
}
/**
* Returns maximum number of containers before the last "dimension".
*/
unsigned int MaximumNumContainersInDeepEnd() {
unsigned int _result = 1, _sum = 0;
if (GetType() == SerializerNodeArrayItem || GetType() == SerializerNodeObjectProperty) {
return 1;
}
for (unsigned int i = 0; i < _numChildren; ++i) {
if (PTR_ATTRIB(_children[i], GetType()) == SerializerNodeArray ||
PTR_ATTRIB(_children[i], GetType()) == SerializerNodeObject) {
_sum += PTR_ATTRIB(_children[i], MaximumNumContainersInDeepEnd());
}
}
return _result * _sum;
}
/**
* Returns pointer to SerializerNodeParam holding the key or NULL.
*/
SerializerNodeParam* GetKeyParam() { return _key; }
/**
* Returns pointer to SerializerNodeParam holding the value or NULL.
*/
SerializerNodeParam* GetValueParam() { return _value; }
/**
* Returns parent node or NULL.
*/
SerializerNode* GetParent() { return _parent; }
/**
* Returns next child node (increments index each time the method is called).
*/
SerializerNode* GetNextChild() {
if (_currentChildIndex >= _numChildren) return _children[_numChildren - 1];
return _children[_currentChildIndex++];
}
/**
* Returns type of the node (object, array, object property, array item).
*/
SerializerNodeType GetType() { return _type; }
/**
* Sets type of the node. Should be used only internally.
*/
void SetType(SerializerNodeType type) { _type = type; }
/**
* Adds child to this node.
*/
void AddChild(SerializerNode* child) {
if (_numChildren == (unsigned int)ArraySize(_children)) ArrayResize(_children, _numChildren + 10);
PTR_ATTRIB(child, _index) = (int)_numChildren;
_children[_numChildren++] = child;
}
/**
* Checks whether node has children.
*/
bool HasChildren() { return _numChildren > 0; }
/**
* Returns number of child nodes.
*/
unsigned int NumChildren() { return _numChildren; }
/**
* Returns pointer to the child node at given index or NULL.
*/
SerializerNode* GetChild(unsigned int index) { return index >= _numChildren ? NULL : _children[index]; }
/**
* Removes child with given index.
*/
void RemoveChild(unsigned int index) {
delete _children[index];
for (unsigned int i = ArraySize(_children) - 2; i >= index; --i) {
_children[i] = _children[i + 1];
}
}
/**
* Checks whether this node is last in its parent.
*/
bool IsLast() {
if (!_parent) return true;
for (unsigned int i = 0; i < PTR_ATTRIB(_parent, NumChildren()); ++i) {
if (PTR_ATTRIB(_parent, GetChild(i)) == THIS_PTR && i != PTR_ATTRIB(_parent, NumChildren() - 1)) return false;
}
return true;
}
/**
* Serializes node and its children into string in generic format (JSON at now).
*/
string ToString(bool trimWhitespaces = false, unsigned int indentSize = 2, unsigned int indent = 0) {
string repr;
string ident;
if (!trimWhitespaces)
for (unsigned int i = 0; i < indent * indentSize; ++i) ident += " ";
repr += ident;
if (GetKeyParam() != NULL && PTR_ATTRIB(GetKeyParam(), AsString(false, false)) != "")
repr += PTR_ATTRIB(GetKeyParam(), AsString(false, true)) + ":" + (trimWhitespaces ? "" : " ");
if (GetValueParam() != NULL) repr += PTR_ATTRIB(GetValueParam(), AsString(false, true));
switch (GetType()) {
case SerializerNodeObject:
repr += string("{") + (trimWhitespaces ? "" : "\n");
break;
case SerializerNodeArray:
repr += string("[") + (trimWhitespaces ? "" : "\n");
break;
case SerializerNodeUnknown:
case SerializerNodeValue:
case SerializerNodeObjectProperty:
case SerializerNodeArrayItem:
default:;
}
if (HasChildren()) {
for (unsigned int j = 0; j < NumChildren(); ++j) {
repr += PTR_ATTRIB(GetChild(j), ToString(trimWhitespaces, indentSize, indent + 1));
}
}
switch (GetType()) {
case SerializerNodeObject:
repr += ident + "}";
break;
case SerializerNodeArray:
repr += ident + "]";
break;
case SerializerNodeUnknown:
case SerializerNodeValue:
case SerializerNodeObjectProperty:
case SerializerNodeArrayItem:
default:;
}
if (!IsLast()) repr += ",";
// Appending newline only when inside the root node.
if (indent != 0) repr += (trimWhitespaces ? "" : "\n");
return repr;
}
};
#endif