-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFactory.cpp
118 lines (103 loc) · 2.68 KB
/
Factory.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
#include "Factory.hpp"
#include "Operand.hpp"
Factory *Factory::instance = new Factory();
Factory::Factory(void)
{
initFunctArray();
return;
}
Factory::Factory(Factory const &src)
{
*this = src;
return;
}
Factory::~Factory(void)
{
return;
}
Factory &Factory::operator=(Factory const &rhs)
{
for (int i = 0; i < eOperandType::SizeMax; i++)
_functArray[i] = rhs._functArray[i];
return *this;
}
std::string const Factory::toString(void) const
{
// Return whatever needs to be returned
return std::string();
}
void Factory::initFunctArray()
{
if (Factory::instance == nullptr)
Factory::instance = this;
int i = 0;
this->_functArray[i++] = &Factory::createInt8;
this->_functArray[i++] = &Factory::createInt16;
this->_functArray[i++] = &Factory::createInt32;
this->_functArray[i++] = &Factory::createFloat;
this->_functArray[i++] = &Factory::createDouble;
}
void Factory::flowCompare(std::string const &value, std::string const &ref) const
{
if (value.length() > ref.length())
{
if (ref[0] == '-')
throw AvmException("underflow A!");
throw AvmException("overflow A!");
}
if (value.length() == ref.length() && value.compare(ref) > 0)
{
if (ref[0] == '-')
throw AvmException("underflow B!");
throw AvmException("overflow B!");
}
}
IOperand const *Factory::createOperand(eOperandType type, std::string const &value) const
{
return (this->*(_functArray[type]))(value);
}
IOperand const *Factory::createInt8(std::string const &value) const
{
if (value[0] == '-')
flowCompare(value, std::to_string(INT8_MIN));
else
flowCompare(value, std::to_string(INT8_MAX));
return new Operand<__int8_t>(value);
}
IOperand const *Factory::createInt16(std::string const &value) const
{
if (value[0] == '-')
flowCompare(value, std::to_string(INT16_MIN));
else
flowCompare(value, std::to_string(INT16_MAX));
return new Operand<__int16_t>(value);
}
IOperand const *Factory::createInt32(std::string const &value) const
{
if (value[0] == '-')
flowCompare(value, std::to_string(INT32_MIN));
else
flowCompare(value, std::to_string(INT32_MAX));
return new Operand<__int32_t>(value);
}
IOperand const *Factory::createFloat(std::string const &value) const
{
if (value[0] == '-')
flowCompare(value, std::to_string(-3.402823466e+38F));
else
flowCompare(value, std::to_string(3.402823466e+38F));
return new Operand<float>(value);
}
IOperand const *Factory::createDouble(std::string const &value) const
{
if (value[0] == '-')
flowCompare(value, std::to_string(-1.7976931348623158e+308));
else
flowCompare(value, std::to_string(1.7976931348623158e+308));
return new Operand<double>(value);
}
std::ostream &operator<<(std::ostream &o, Factory const &rhs)
{
o << rhs.toString();
return o;
}