-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_variable.hpp
More file actions
executable file
·94 lines (77 loc) · 2.35 KB
/
t_variable.hpp
File metadata and controls
executable file
·94 lines (77 loc) · 2.35 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
#ifndef MP_T_VARIABLE_HPP
#define MP_T_VARIABLE_HPP
#include "token.hpp"
namespace mp
{
class VariableInstance
{
public:
VariableInstance() = default;
VariableInstance(const std::string &var) : name{var}, val{0}
{}
static std::vector<std::shared_ptr<VariableInstance>> stored;
std::string name;
double val;
};
std::vector<std::shared_ptr<VariableInstance>> VariableInstance::stored;
/**
* @class Variable
* Class for Variable
*
* @fn evaluate Return Variable value
*
* @param val Stored value
*/
class Variable : public Token
{
friend class VariableInstance;
public:
Variable(const std::string &var);
double evaluate() const { return sp_link->val; }
//double evaluate() const { return val; }
double evaluate(double const) const
{ throw std::invalid_argument("Error while performing token evaluation.\nToken: Variable, no argument needed"); }
double evaluate(double const, double const) const
{ throw std::invalid_argument("Error while performing token evaluation.\nToken: Variable, no argument needed"); }
bool is_variable() const { return true; };
void set_value(double const &a) { sp_link->val = a; }
//void set_value(double const &a) { val = a; }
// Friend streaming function
virtual std::ostream& print(std::ostream& os) const
{
os << to_string();
return os;
}
// Transform the Token to string
std::string to_string() const { return sp_link->name; }
// std::string to_string() const { return name; }
// Count the number of variables
size_t count() const { return sp_link.use_count(); }
private:
std::shared_ptr<VariableInstance> sp_link;
//std::string name;
//double val;
};
Variable::Variable(const std::string &var)
{
// Check if the variable has already been created
// Equivalent that they share same VariableInstance
bool is_already_created {false};
for(auto vi : VariableInstance::stored)
{
if(vi->name == var)
{
is_already_created = true;
sp_link = vi;
break;
}
}
if(!is_already_created)
{
VariableInstance::stored.push_back( std::make_shared<VariableInstance>(var) );
sp_link = VariableInstance::stored[VariableInstance::stored.size()-1];
}
// PRINT("var : " << to_string() << "\t" << sp_link.use_count());
}
}
#endif // MP_T_VARIABLE_HPP