-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconstants.cc
More file actions
120 lines (102 loc) · 2.78 KB
/
constants.cc
File metadata and controls
120 lines (102 loc) · 2.78 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
#include <list>
#include <string>
#include "basicutils/stringutils.hh"
#include "values.hh"
#include "constants.hh"
static std::list<Constant> constants;
static Constant *findOrRegisterConstant(Constant &vinfo)
{
std::list<Constant>::iterator it;
for (it = constants.begin(); it != constants.end(); it++)
{
if (*it == vinfo) return &(*it);
}
constants.push_back(vinfo);
return &(constants.back());
}
Constant *getConstantFromDecimal(double inval)
{
Constant vinfo;
vinfo.setToDecimal(inval);
return findOrRegisterConstant(vinfo);
}
Constant *getConstantFromInteger(int inval)
{
Constant vinfo;
vinfo.setToInteger(inval);
return findOrRegisterConstant(vinfo);
}
Constant *getConstantFromString(const std::string &inval)
{
Constant vinfo;
vinfo.setToString(inval);
return findOrRegisterConstant(vinfo);
}
Constant *getConstantFromBoolean(bool inval)
{
Constant vinfo;
vinfo.setToBoolean(inval);
return findOrRegisterConstant(vinfo);
}
Constant::Constant() : boolval(false) { }
Constant::~Constant() { }
bool Constant::operator == (const Constant &that)
{
if (this->decval == that.decval && this->intval == that.intval && this->strval == that.strval && this->boolval == that.boolval) return true;
else return false;
}
bool Constant::operator != (const Constant &that)
{
return !(*this == that);
}
void Constant::setToDecimal(double val)
{
this->decval = val;
this->intval = (int)val;
this->strval = ConvertToString(val);
if (val) this->boolval = true;
else this->boolval = false;
}
void Constant::setToInteger(int val)
{
this->decval = (double)val;
this->intval = val;
this->strval = ConvertToString(val);
if (val) this->boolval = true;
else this->boolval = false;
}
void Constant::setToString(const std::string &val)
{
this->decval = StringToDecimal(val);
this->intval = StringToInteger(val);
this->strval = val;
this->boolval = StringToBoolean(val);
}
void Constant::setToBoolean(bool val)
{
if (val)
{
this->decval = 1.0;
this->intval = 1;
this->strval = "true";
}
else
{
this->decval = 0.0;
this->intval = 0;
this->strval = "false";
}
this->boolval = val;
}
unsigned Constant::compareToValue(Value &that)
{
// evaluate as String by default
if (this->strval > that.getString()) return isGreaterThan;
else if (this->strval < that.getString()) return isLessThan;
else return isEqual;
}
double Constant::getDecimal(void) { return this->decval; }
int Constant::getInteger(void) { return this->intval; }
std::string Constant::getString(std::string, double) { return this->strval; }
bool Constant::getBoolean(void) { return this->boolval; }
bool Constant::isConstant(void) { return true; };