-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvariables.cc
More file actions
347 lines (306 loc) · 9.5 KB
/
variables.cc
File metadata and controls
347 lines (306 loc) · 9.5 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
#include <map>
#include <string>
#include <cstdio>
#include <cstring>
#include <cmath>
#include "basicutils/msg.hh"
#include "basicutils/stringutils.hh"
#include "values.hh"
#include "variables.hh"
static std::map<std::string, Variable> variables;
void registerVariable(const std::string ¶mname, const std::string &typestr, const std::string &initval)
{
if (paramname.empty())
{
error_msg("Attempting to create a variable without a name");
return;
}
if (typestr.empty())
{
error_msg("Attempting to create the variable \"" << paramname << "\" without a specified type");
return;
}
std::string mylabel;
if (paramname[0] == '@') mylabel = paramname.substr(1);
else mylabel = paramname;
Variable *vinfo = new Variable;
vinfo->setType(typestr);
vinfo->setToString(initval);
variables[mylabel] = *vinfo;
}
Variable *getVariable(const std::string &label)
{
if (label.empty()) return 0x0;
if (label[0] == '@')
{
std::string mylabel = label.substr(1);
if (variables.find(mylabel) != variables.end()) return &(variables[mylabel]);
}
else
{
if (variables.find(label) != variables.end()) return &(variables[label]);
}
warning_msg("Invalid variable label: " << label);
return 0x0;
}
// get_pointer should only be used in the auto-generated dcapp header files used by the logic plug-ins. Since they're
// auto-generated, this routine should never return 0x0. But if it does return 0x0, a crash could occur.
void *get_pointer(const char *label)
{
Variable *myvar = getVariable(label);
if (myvar) return myvar->getPointer();
else return 0x0;
}
std::string create_virtual_variable(const char *typestr, const char *initval)
{
static unsigned id_count = 0;
std::string myvar = "@dcappVirtualVariable" + std::to_string(id_count);
registerVariable(myvar, typestr, initval);
id_count++;
return myvar;
}
Variable::Variable() : type(UNDEFINED_TYPE) { }
Variable::~Variable() { }
bool Variable::operator == (const Variable &that)
{
switch (this->type)
{
case DECIMAL_TYPE: if (this->decval == that.decval) return true; break;
case INTEGER_TYPE: if (this->intval == that.intval) return true; break;
case STRING_TYPE: if (this->strval == that.strval) return true; break;
}
return false;
}
bool Variable::operator != (const Variable &that)
{
return !(*this == that);
}
void Variable::setToString(const std::string &input)
{
switch (this->type)
{
case DECIMAL_TYPE: this->decval = StringToDecimal(input); break;
case INTEGER_TYPE: this->intval = StringToInteger(input); break;
case STRING_TYPE: this->strval = input; break;
}
}
void Variable::setToDecimal(double val) { this->decval = val; }
void Variable::setToInteger(int val) { this->intval = val; }
void Variable::setToBoolean(bool input)
{
switch (this->type)
{
case DECIMAL_TYPE:
if (input) this->decval = 1.0;
else this->decval = 0.0;
break;
case INTEGER_TYPE:
if (input) this->intval = 1;
else this->intval = 0;
break;
case STRING_TYPE:
if (input) this->strval = "true";
else this->strval = "false";
break;
}
}
void Variable::setToValue(Value &that)
{
switch (this->type)
{
case DECIMAL_TYPE: this->decval = that.getDecimal(); break;
case INTEGER_TYPE: this->intval = that.getInteger(); break;
case STRING_TYPE: this->strval = that.getString(); break;
}
}
void Variable::incrementByValue(Value &that)
{
switch (this->type)
{
case DECIMAL_TYPE: this->decval += that.getDecimal(); break;
case INTEGER_TYPE: this->intval += that.getInteger(); break;
case STRING_TYPE: this->strval += that.getString(); break;
}
}
void Variable::decrementByValue(Value &that)
{
switch (this->type)
{
case DECIMAL_TYPE: this->decval -= that.getDecimal(); break;
case INTEGER_TYPE: this->intval -= that.getInteger(); break;
// There is no good way to decrement a String variable
}
}
void Variable::multiplyByValue(Value &that)
{
switch (this->type)
{
case DECIMAL_TYPE: this->decval *= that.getDecimal(); break;
case INTEGER_TYPE: this->intval *= that.getInteger(); break;
}
}
void Variable::divideByValue(Value &that)
{
switch (this->type)
{
case DECIMAL_TYPE: this->decval /= that.getDecimal(); break;
case INTEGER_TYPE: this->intval /= that.getInteger(); break;
// There is no good way to decrement a String variable
}
}
void Variable::applyMinimumByValue(Value &that)
{
switch (this->type)
{
case DECIMAL_TYPE:
if (this->decval < that.getDecimal()) this->decval = that.getDecimal();
break;
case INTEGER_TYPE:
if (this->intval < that.getInteger()) this->intval = that.getInteger();
break;
// There is no good way to bound a String variable
}
}
void Variable::applyMaximumByValue(Value &that)
{
switch (this->type)
{
case DECIMAL_TYPE:
if (this->decval > that.getDecimal()) this->decval = that.getDecimal();
break;
case INTEGER_TYPE:
if (this->intval > that.getInteger()) this->intval = that.getInteger();
break;
// There is no good way to bound a String variable
}
}
unsigned Variable::compareToValue(Value &that)
{
switch (this->type)
{
case DECIMAL_TYPE:
if (this->decval > that.getDecimal()) return isGreaterThan;
else if (this->decval < that.getDecimal()) return isLessThan;
else return isEqual;
case INTEGER_TYPE:
if (this->intval > that.getInteger()) return isGreaterThan;
else if (this->intval < that.getInteger()) return isLessThan;
else return isEqual;
case STRING_TYPE:
if (this->strval > that.getString()) return isGreaterThan;
else if (this->strval < that.getString()) return isLessThan;
else return isEqual;
}
return isEqual; // undefined behavior if type isn't defined
}
double Variable::getDecimal(void)
{
switch (this->type)
{
case DECIMAL_TYPE: return this->decval;
case INTEGER_TYPE: return (double)(this->intval);
case STRING_TYPE: return StringToDecimal(this->strval);
default: return 0.0;
}
}
int Variable::getInteger(void)
{
switch (this->type)
{
case DECIMAL_TYPE: return (int)(this->decval);
case INTEGER_TYPE: return this->intval;
case STRING_TYPE: return StringToInteger(this->strval);
default: return 0;
}
}
std::string Variable::getString(std::string format, double zeroTrim)
{
double dval;
if (zeroTrim && (fabs(this->decval) < fabs(zeroTrim))) dval = 0;
else dval = this->decval;
if (format.empty())
{
switch (this->type)
{
case DECIMAL_TYPE:
return ConvertToString(dval);
case INTEGER_TYPE:
return ConvertToString(this->intval);
case STRING_TYPE:
return this->strval;
default:
return "";
}
}
else
{
char *tmp_str = 0x0;
switch (this->type)
{
case DECIMAL_TYPE:
asprintf(&tmp_str, format.c_str(), dval);
break;
case INTEGER_TYPE:
asprintf(&tmp_str, format.c_str(), this->intval);
break;
case STRING_TYPE:
asprintf(&tmp_str, format.c_str(), this->strval.c_str());
break;
default:
return "";
}
if (tmp_str)
{
std::string ret_str = tmp_str;
free(tmp_str);
return ret_str;
}
else return "";
}
}
bool Variable::getBoolean(void)
{
switch (this->type)
{
case DECIMAL_TYPE: if (this->decval) return true; break;
case INTEGER_TYPE: if (this->intval) return true; break;
case STRING_TYPE: if (StringToBoolean(this->strval)) return true; break;
}
return false;
}
void Variable::setType(int type_spec) { this->type = type_spec; }
void Variable::setType(const std::string &type_spec)
{
if (type_spec == "Decimal" || type_spec == "Float")
{
this->type = DECIMAL_TYPE;
}
else if (type_spec == "Integer")
{
this->type = INTEGER_TYPE;
}
else if (type_spec == "String")
{
this->type = STRING_TYPE;
}
else
{
this->type = STRING_TYPE;
warning_msg("Attempting to create variable with unknown type (" << type_spec << ") - assuming \"String\"");
}
}
void Variable::setAttributes(Variable &that) { this->type = that.type; }
void * Variable::getPointer(void)
{
switch (this->type)
{
case DECIMAL_TYPE: return (void *)(&(this->decval));
case INTEGER_TYPE: return (void *)(&(this->intval));
case STRING_TYPE: return (void *)(&(this->strval));
default: return 0x0;
}
}
bool Variable::isDecimal(void) { if (this->type == DECIMAL_TYPE) return true; else return false; }
bool Variable::isInteger(void) { if (this->type == INTEGER_TYPE) return true; else return false; }
bool Variable::isString(void) { if (this->type == STRING_TYPE) return true; else return false; }
bool Variable::isVariable(void) { return true; };