-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariable.cpp
74 lines (66 loc) · 1.6 KB
/
variable.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
#include <iostream>
#include <string>
#include <memory>
#include "variable.hpp"
namespace sre { namespace console {
//== Variable ==//
//// ctor ////
Variable::Variable (std::string name, Variable::TYPE type, std::string val)
{
this->name = name;
this->type = type;
this->val = val;
}
void Variable::set (std::string value) {
if (type==STRING)
this->val = value;
else
std::cerr<<"ERROR: variable is not string type"<<std::endl;
}
void Variable::set (int value) {
if (type==INT)
this->val = value;
else
std::cerr<<"ERROR: variable is not integer type"<<std::endl;
}
void Variable::set (float value) {
if (type==FLOAT)
this->val = value;
else
std::cerr<<"ERROR: variable is not float type"<<std::endl;
}
void Variable::set (char value) {
if (type==CHAR)
this->val = value;
else
std::cerr<<"ERROR: variable is not char type"<<std::endl;
}
std::string Variable::toStr() {
if(type == STRING)
return val;
std::cerr<<"Error: variable is not string type"<<std::endl;
return "";
}
int Variable::toInt() {
if(type == INT)
return std::stoi(val);
std::cerr<<"Error: variable is not int type"<<std::endl;
return 0;
}
float Variable::toFloat() {
if(type == FLOAT)
return std::stof(val);
std::cerr<<"Error: variable is not float type"<<std::endl;
return 0;
}
char Variable::toChar() {
if(type == CHAR)
return (val.front());
std::cerr<<"Error: variable is not char type"<<std::endl;
return 0;
}
//// dtor ////
Variable::~Variable ()
{
}
}} // namespaces