-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathSerializerNodeParam.mqh
338 lines (285 loc) · 9.96 KB
/
SerializerNodeParam.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
//+------------------------------------------------------------------+
//| 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.
#include "SerializerConversions.h"
#ifndef JSON_PARAM_MQH
#define JSON_PARAM_MQH
/**
* Enumeration.
*/
enum SerializerNodeParamType {
SerializerNodeParamBool,
SerializerNodeParamLong,
SerializerNodeParamDouble,
SerializerNodeParamString
};
class SerializerNode;
/**
* Key or value.
*/
class SerializerNodeParam {
public:
/**
* Storing all integral values in a single union. We can't hold string here.
*/
union USerializerNodeValue {
bool _bool;
long _long;
double _double;
} _integral;
string _string;
SerializerNodeParamType _type;
// Floating-point precision.
int fp_precision;
/**
* Returns floating-point precision.
*/
int GetFloatingPointPrecision() { return fp_precision; }
/**
* Sets floating-point precision.
*/
void SetFloatingPointPrecision(int _fp_precision) { fp_precision = _fp_precision; }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromBool(long value);
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromLong(long value);
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromDouble(double value);
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromString(string& value);
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(bool value) { return FromBool(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(char value) { return FromLong(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(color value) { return FromLong(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(datetime value) { return FromLong(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(double value) { return FromDouble(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(int value) { return FromLong(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(long value) { return FromLong(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(short value) { return FromLong(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(string& value) { return FromString(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(unsigned char value) { return FromLong(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(unsigned int value) { return FromLong(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(unsigned long value) { return FromLong(value); }
/**
* Returns new SerializerNodeParam object from given source value.
*/
static SerializerNodeParam* FromValue(unsigned short value) { return FromLong(value); }
/**
* Returns stringified version of the value. Note "forceQuotesOnString" flag.
*/
string AsString(bool includeQuotes = false, bool forceQuotesOnString = true, bool escapeString = true,
int _fp_precision = 8) {
switch (_type) {
case SerializerNodeParamBool:
return SerializerConversions::ValueToString(_integral._bool, includeQuotes, escapeString, _fp_precision);
case SerializerNodeParamLong:
return SerializerConversions::ValueToString(_integral._long, includeQuotes, escapeString, _fp_precision);
case SerializerNodeParamDouble:
return SerializerConversions::ValueToString(_integral._double, includeQuotes, escapeString, _fp_precision);
case SerializerNodeParamString:
return SerializerConversions::ValueToString(_string, includeQuotes || forceQuotesOnString, escapeString,
_fp_precision);
}
#ifdef __debug__
PrintFormat("%s: Error: SerializerNodeParam.AsString() called for an unknown value type: %d!", __FUNCTION__, _type);
#endif
return "<invalid param type " + IntegerToString(_type) + ">";
}
/**
* Returns type of the param.
*/
SerializerNodeParamType GetType() { return _type; }
long ToBool() {
switch (_type) {
case SerializerNodeParamBool:
return _integral._bool;
case SerializerNodeParamLong:
return _integral._long != 0;
case SerializerNodeParamDouble:
return _integral._double != 0;
case SerializerNodeParamString:
return _string != "" && _string != "0";
default:
Alert("Internal Error. Cannot convert source type to bool");
}
return false;
}
int ToInt() {
switch (_type) {
case SerializerNodeParamBool:
return _integral._bool ? 1 : 0;
case SerializerNodeParamLong:
return (int)_integral._long;
case SerializerNodeParamDouble:
return (int)_integral._double;
case SerializerNodeParamString:
return (int)StringToInteger(_string);
default:
Alert("Internal Error. Cannot convert source type to int");
}
return 0;
}
long ToLong() {
switch (_type) {
case SerializerNodeParamBool:
return _integral._bool ? 1 : 0;
case SerializerNodeParamLong:
return _integral._long;
case SerializerNodeParamDouble:
return (long)_integral._double;
case SerializerNodeParamString:
return StringToInteger(_string);
default:
Alert("Internal Error. Cannot convert source type to long");
}
return 0;
}
float ToFloat() {
switch (_type) {
case SerializerNodeParamBool:
return _integral._bool ? 1.0f : 0.0f;
case SerializerNodeParamLong:
return (float)_integral._long;
case SerializerNodeParamDouble:
return (float)_integral._double;
case SerializerNodeParamString:
return (float)StringToDouble(_string);
default:
Alert("Internal Error. Cannot convert source type to float");
}
return 0;
}
double ToDouble() {
switch (_type) {
case SerializerNodeParamBool:
return _integral._bool ? 1.0 : 0.0;
case SerializerNodeParamLong:
return (double)_integral._long;
case SerializerNodeParamDouble:
return _integral._double;
case SerializerNodeParamString:
return StringToDouble(_string);
default:
Alert("Internal Error. Cannot convert source type to double");
}
return 0;
}
string ToString() {
switch (_type) {
case SerializerNodeParamBool:
return _integral._bool ? "1" : "0";
case SerializerNodeParamLong:
return IntegerToString(_integral._long);
case SerializerNodeParamDouble:
return DoubleToString(_integral._double);
case SerializerNodeParamString:
return _string;
default:
Alert("Internal Error. Cannot convert source type to string");
}
return "";
}
int ConvertTo(int) { return ToInt(); }
long ConvertTo(long) { return ToInt(); }
float ConvertTo(float) { return ToFloat(); }
double ConvertTo(double) { return ToDouble(); }
string ConvertTo(string) { return ToString(); }
};
/**
* Returns new SerializerNodeParam object from given source value.
*/
SerializerNodeParam* SerializerNodeParam::FromBool(long value) {
SerializerNodeParam* param = new SerializerNodeParam();
PTR_ATTRIB(param, _type) = SerializerNodeParamBool;
PTR_ATTRIB(param, _integral)._bool = value;
return param;
}
/**
* Returns new SerializerNodeParam object from given source value.
*/
SerializerNodeParam* SerializerNodeParam::FromLong(long value) {
SerializerNodeParam* param = new SerializerNodeParam();
PTR_ATTRIB(param, _type) = SerializerNodeParamLong;
PTR_ATTRIB(param, _integral)._long = value;
return param;
}
/**
* Returns new SerializerNodeParam object from given source value.
*/
SerializerNodeParam* SerializerNodeParam::FromDouble(double value) {
SerializerNodeParam* param = new SerializerNodeParam();
PTR_ATTRIB(param, _type) = SerializerNodeParamDouble;
PTR_ATTRIB(param, _integral)._double = value;
return param;
}
/**
* Returns new SerializerNodeParam object from given source value.
*/
SerializerNodeParam* SerializerNodeParam::FromString(string& value) {
SerializerNodeParam* param = new SerializerNodeParam();
PTR_ATTRIB(param, _type) = SerializerNodeParamString;
PTR_ATTRIB(param, _string) = value;
return param;
}
#endif