/
Value.cpp
executable file
·141 lines (118 loc) · 3.06 KB
/
Value.cpp
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
//
// Created by pedro on 01/12/17.
//
#include <iostream>
#include <TigreFramework/Core/Kernel/Exception.h>
#include <TigreFramework/Types/Math.h>
#include "Value.h"
Value::Value(int rhs) : iValue(rhs), type(2) { }
Value::Value(float rhs) : fValue(rhs), type(3) { }
Value::Value(double rhs) : dValue(rhs), type(4) { }
Value::Value(string rhs) : sValue(rhs), type(1) { }
Value::Value(const char rhs[]) : sValue(rhs), type(1) { }
Value& Value::operator=(const int &rhs) {
this->type = 2;
this->iValue = rhs;
return *this;
}
Value& Value::operator=(const float &rhs) {
this->type = 3;
this->fValue = rhs;
return *this;
}
Value& Value::operator=(const double &rhs) {
this->type = 4;
this->dValue = rhs;
return *this;
}
Value& Value::operator=(const string &rhs) {
this->type = 1;
this->sValue = rhs;
return *this;
}
/*Value::operator std::string() const {
if(isString()){
return this->sValue;
} else if(isInteger()) {
return to_string(this->iValue);
} else if(isFloat()) {
return to_string(this->fValue);
} else if(isDouble()) {
return to_string(this->dValue);
}
throw Exception("Value not found");
}*/
Value::operator int() const {
if(isString()){
return Math::toInt(this->sValue);
} else if(isInteger()) {
return this->iValue;
} else if(isFloat()) {
return (int)(this->fValue);
} else if(isDouble()) {
return (int)(this->dValue);
}
throw Exception("Value not found, the type is (" + std::to_string(this->type) + ")");
}
Value::operator float() const {
if(isString()){
return Math::toFloat(this->sValue);
} else if(isInteger()) {
return (float)(this->iValue);
} else if(isFloat()) {
return this->fValue;
} else if(isDouble()) {
return (float)(this->dValue);
}
throw Exception("Value not found, the type is (" + std::to_string(this->type) + ")");
}
Value::operator double() const {
if(isString()){
return Math::toDouble(this->sValue);
} else if(isInteger()) {
return (double)(this->iValue);
} else if(isFloat()) {
return (double)(this->fValue);
} else if(isDouble()) {
return this->dValue;
}
throw Exception("Value not found, the type is (" + std::to_string(this->type) + ")");
}
bool Value::isDouble() const {
return this->type == 4;
}
bool Value::isFloat() const {
return this->type == 3;
}
bool Value::isInteger() const {
return this->type == 2;
}
bool Value::isString() const {
return this->type == 1;
}
double Value::getDouble() const {
return this->dValue;
}
float Value::getFloat() const {
return this->fValue;
}
int Value::getInteger() const {
return this->iValue;
}
string Value::getString() const {
return this->sValue;
}
void to_json(nlohmann::json& j, const Value& p) {
if(p.isDouble()){
j = p.getDouble();
}
if(p.isFloat()){
j = p.getFloat();
}
if(p.isInteger()){
j = p.getInteger();
}
if(p.isString()){
j = p.getString();
}
}