-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathExpressionParts.h
More file actions
365 lines (301 loc) · 7.96 KB
/
ExpressionParts.h
File metadata and controls
365 lines (301 loc) · 7.96 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#pragma once
#include "RasterMatrix.h"
enum tkElementType
{
etValue = 0,
etOperation = 1,
etPart = 2,
etNone = 3,
};
enum tkOperation
{
operEqual = 0, // =
operNotEqual = 1, // <>
operLessEqual = 2, // <=
operGrEqual = 3, // >=
operGreater = 4, // >
operLess = 5, // <
operOR = 6, // OR
operAND = 7, // AND
operNOT = 8, // NOT (unary)
operXOR = 9, // XOR
operCONSEQ = 10, // :> consequence (if left operand is true, the right operand must be true as well; is this conditions isn't fullfiled returns false)
operPlus = 11, // +
operMinus = 12, // -
operDiv = 13, // /
operMult = 14, // *
operMOD = 15, // MOD
operDivInt = 16, /* \ */
operExpon = 17, // ^
operChangeSign = 18, // - (unary)
operNone = 19,
operLike = 20, // default LIKE is case-sensitive
operILike = 21, // case-Insensitive LIKE
};
class CExpressionValue;
class COperation;
class CElement;
class CExpressionPart;
class CustomFunction;
typedef bool(*ExpressionFunction)(const vector<CExpressionValue*>& arguments, IShape* shape, CExpressionValue& result);
// ********************************************************
// COperation
// ********************************************************
class COperation
{
public:
int id; // id of operator element
int left; // id of the left operand (element)
int right; // id of the right operand (element)
bool binaryOperation; // whether it's binary operation, i.e. with 2 operands
void CopyFrom(COperation& source)
{
id = source.id;
left = source.left;
right = source.right;
binaryOperation = source.binaryOperation;
}
};
// ********************************************************
// CExpressionValue
// ********************************************************
// Represents single value within expression, either double, boolean, string or matrix
class CExpressionValue
{
RasterMatrix* _matrix;
GDALRasterBand* _band;
tkValueType _type;
CStringW _str;
double _dbl;
bool _bln;
public:
CExpressionValue(void)
{
_dbl = 0.0;
_bln = false;
_type = vtDouble;
_matrix = NULL;
_band = NULL;
}
~CExpressionValue()
{
clearMatrix();
}
CExpressionValue& operator=(CExpressionValue& val)
{
if (this == &val)
return *this;
_bln = val.bln();
_dbl = val.dbl();
_str = val.str();
_type = val.type();
return *this;
}
bool bln() { return _bln; }
CStringW str() { return _str; }
double dbl() { return _dbl; }
tkValueType type() { return _type; }
RasterMatrix* matrix() { return _matrix; }
GDALRasterBand* band() { return _band; }
bool IsDouble() { return _type == vtDouble; }
bool isBoolean() { return _type == vtBoolean; }
bool isString() { return _type == vtString; }
bool IsFloatArray() { return _type == vtFloatArray; }
void bln(bool value)
{
_bln = value;
_type = vtBoolean;
}
void str(CStringW s)
{
_str = s;
_type = vtString;
}
void dbl(double dbl)
{
_dbl = dbl;
_type = vtDouble;
}
void band(GDALRasterBand* band)
{
_band = band;
}
void matrix(RasterMatrix* m)
{
clearMatrix();
_matrix = m;
_type = vtFloatArray;
}
void clearMatrix()
{
if (_matrix) {
delete _matrix;
_matrix = NULL;
}
}
void copyFrom(CExpressionValue& value)
{
switch (value.type())
{
case vtBoolean:
bln(value.bln());
break;
case vtString:
str(value.str());
break;
case vtDouble:
dbl(value.dbl());
break;
case vtFloatArray:
// TODO: implement for geometry
break;
}
}
};
// ********************************************************
// CElement
// ********************************************************
// An element of the expression tree, either operator or operand
class CElement
{
public:
CElement()
{
wasCalculated = false;
type = etNone;
turnedOff = false;
priority = 255;
operation = operNone;
isField = false;
fieldIndex = -1;
partIndex = -1;
val = new CExpressionValue();
calcVal = new CExpressionValue();
}
~CElement()
{
delete val;
delete calcVal;
}
CStringW fieldName; // name of field (in [square brackets])
tkElementType type; // type of element
tkOperation operation; // type of operation
CExpressionValue* val; // initial value
CExpressionValue* calcVal; // value after calculation (in case of consecutive calculations it doesn't rewrite the initial value)
// therefor no additional parsing is needed
// parameters
int priority; // priority of operation, with less absolute values of priority are preformed first
int fieldIndex; // index of field in attribute table
bool isField; // the element is field from table
// performing calculation
bool wasCalculated; // the value has been calculated, so calculated value should be used henceforth
bool turnedOff; // turned off till the end of calculation
int partIndex; // the element is result of calculations on the bracket with given index
};
// ********************************************************
// FunctionParameter
// ********************************************************
class FunctionParameter
{
public:
CStringW description;
CStringW name;
FunctionParameter(CStringW name, CStringW description)
:name(name), description(description)
{
}
};
// ********************************************************
// Function
// ********************************************************
// Statically defined function (treated as expression part)
class CustomFunction
{
private:
int _numParams;
bool _useGeometry;
tkFunctionGroup _group;
ExpressionFunction _fn;
vector<CStringW> _aliases;
CStringW _description;
FunctionId _fnId;
vector<FunctionParameter*> _params;
public:
CustomFunction(FunctionId fnId, CStringW name, int numParams, ExpressionFunction function, tkFunctionGroup group, bool useGeometry = false)
: _fnId(fnId), _useGeometry(useGeometry), _group(group), _fn(function)
{
_numParams = numParams;
ParseName(name);
InitOverloads();
CheckNumParams();
}
~CustomFunction()
{
Clear();
}
bool CheckNumParams();
void Clear();
void InitOverloads();
void ParseName(CStringW name);
vector<CStringW>* getAliases() { return &_aliases; }
CStringW GetName() { return _aliases[0]; }
int numParams() { return _params.size(); }
bool useGeometry() { return _useGeometry; }
tkFunctionGroup group() { return _group; }
CStringW description() { return _description; }
void description(CStringW value) { _description = value; }
FunctionId FunctionId() { return _fnId; }
CStringW GetSignature();
// optionally type of parameters can be checked as well,
// provided we define all the types in CustomFunction.InitOverloads
bool CheckArguments(int argSize, CStringW& errorMessage);
void AddParameter(CStringW name, CStringW description)
{
FunctionParameter* p = new FunctionParameter(name, description);
_params.push_back(p);
}
FunctionParameter* getParameter(int parameterIndex)
{
if (parameterIndex < 0 || parameterIndex >= static_cast<int>(_params.size()))
{
return NULL;
}
return _params[parameterIndex];
}
bool call(vector<CExpressionValue*>& arguments, IShape* shape, CExpressionValue& result)
{
return _fn(arguments, shape, result);
}
};
// ********************************************************
// CExpressionPart
// ********************************************************
// Part of the expression inside brackets
class CExpressionPart
{
public:
CStringW expression; // for debugging only
bool isArgument; // for debugging only
vector<CElement*> elements; // fields, operators, literals
vector<CExpressionPart*> arguments; // references to other parts
CustomFunction* function;
CExpressionValue* val; // resulting value
int activeCount; // number of unprocessed elements
CExpressionPart()
{
isArgument = false;
activeCount = 0;
function = NULL;
val = NULL;
}
~CExpressionPart()
{
ClearElements();
}
private:
void ClearElements();
void ReleaseValue();
public:
bool isFunction() { return function != NULL; }
void Reset();
};