Removed all the construction pattern stuff, it was causing the base copy function...
[fridhskrift.git] / variable / variable.cpp
blobaa76e757ca9484822973121345f3e8ec57c7112c
1 #include <ail/string.hpp>
2 #include <fridh/symbol.hpp>
4 namespace fridh
6 variable::variable():
7 type(variable_type_identifier::undefined)
11 variable::variable(variable const & other)
13 copy(other);
16 variable::~variable()
18 destroy();
21 variable & variable::operator=(variable const & other)
23 destroy();
24 copy(other);
25 return *this;
28 void variable::copy(variable const & other_pattern)
30 variable const & other = dynamic_cast<variable const &>(other_pattern);
32 type = other.type;
34 #define COPY_MEMBER(type) \
35 case variable_type_identifier::type: \
36 type = other.type; \
37 break;
39 #define COPY_MEMBER_POINTER(type, member_type, member) \
40 case variable_type_identifier::type: \
41 member = new member_type(*other.member); \
42 break;
44 switch(type)
46 COPY_MEMBER(boolean)
47 COPY_MEMBER(signed_integer)
48 COPY_MEMBER(unsigned_integer)
49 COPY_MEMBER(floating_point_value)
51 COPY_MEMBER_POINTER(string, std::string, string)
52 COPY_MEMBER_POINTER(array, types::vector, array)
53 COPY_MEMBER_POINTER(map, types::map, map)
56 #undef COPY_MEMBER_POINTER
57 #undef COPY_MEMBER
61 void variable::destroy()
64 #define DELETE_MEMBER(type) \
65 case variable_type_identifier::type: \
66 delete type; \
67 break;
69 switch(type)
71 DELETE_MEMBER(string)
72 DELETE_MEMBER(array)
73 DELETE_MEMBER(map)
76 type = variable_type_identifier::undefined;
78 #undef DELETE_MEMBER
82 types::floating_point_value variable::get_floating_point_value() const
84 switch(type)
86 case variable_type_identifier::signed_integer:
87 return signed_integer;
89 case variable_type_identifier::unsigned_integer:
90 return unsigned_integer;
92 case variable_type_identifier::floating_point_value:
93 return floating_point_value;
96 throw ail::exception("Failed to retrieve floating point value");
99 std::string variable::get_string_representation() const
101 switch(type)
103 case variable_type_identifier::boolean:
104 return ail::bool_to_string(boolean);
106 case variable_type_identifier::signed_integer:
107 return ail::number_to_string<types::signed_integer>(signed_integer);
109 case variable_type_identifier::unsigned_integer:
110 return ail::number_to_string<types::unsigned_integer>(unsigned_integer);
112 case variable_type_identifier::floating_point_value:
113 return ail::number_to_string<types::floating_point_value>(floating_point_value);
115 case variable_type_identifier::string:
116 return *string;
119 unary_argument_type_error("String representation", type);
121 //should never get here, suppress warnings
122 return "Error";
125 bool variable::get_boolean_value() const
127 switch(type)
129 case variable_type_identifier::boolean:
130 return boolean;
132 case variable_type_identifier::signed_integer:
133 return signed_integer != 0;
135 case variable_type_identifier::unsigned_integer:
136 return unsigned_integer != 0;
139 unary_argument_type_error("Boolean representation", type);
141 //should never get here, suppress warnings
142 return false;
145 bool variable::is_zero() const
147 switch(type)
149 case variable_type_identifier::signed_integer:
150 return signed_integer == 0;
152 case variable_type_identifier::unsigned_integer:
153 return unsigned_integer == 0;
155 case variable_type_identifier::floating_point_value:
156 return floating_point_value == 0.0;
159 throw ail::exception("Unable to check if variable is zero");
162 bool variable::operator<(variable const & other) const
164 if(is_numeric_type() && other.is_numeric_type())
168 type == variable_type_identifier::floating_point_value ||
169 other.type == variable_type_identifier::floating_point_value
171 return get_floating_point_value() < other.get_floating_point_value();
172 else if
174 type == variable_type_identifier::unsigned_integer &&
175 other.type == variable_type_identifier::unsigned_integer
177 return unsigned_integer < other.unsigned_integer;
178 else
179 return signed_integer < other.signed_integer;
181 else if(type != other.type)
182 return static_cast<word>(type) < static_cast<word>(other.type);
183 else
185 if(type == variable_type_identifier::string)
186 return *string < *other.string;
187 else
188 return hash() < other.hash();
192 std::string get_type_string(variable_type type)
194 switch(type)
196 case variable_type_identifier::undefined:
197 return "undefined";
199 case variable_type_identifier::nil:
200 return "nil";
202 case variable_type_identifier::none:
203 return "none";
205 case variable_type_identifier::boolean:
206 return "boolean";
208 case variable_type_identifier::signed_integer:
209 return "integer";
211 case variable_type_identifier::unsigned_integer:
212 return "unsigned-integer";
214 case variable_type_identifier::floating_point_value:
215 return "float";
217 case variable_type_identifier::string:
218 return "string";
220 case variable_type_identifier::array:
221 return "array";
223 case variable_type_identifier::map:
224 return "map";
226 case variable_type_identifier::function:
227 return "function";
229 case variable_type_identifier::object:
230 return "object";
233 return "unknown";