Add toString function to Boxed_Number.

For uint8_t, int8_t and char the value is first
converted to an appropriate int type.
This way the value is converted to a number
rather than a character.
This commit is contained in:
Markus Groß 2012-06-29 16:48:53 +02:00
parent dfcc415c31
commit f8feaf6ea8

View File

@ -305,6 +305,14 @@ namespace chaiscript
return static_cast<Target>(*static_cast<const Source *>(bv.get_const_ptr()));
}
template<typename Source>
std::string toStringAux(const Boxed_Value &v)
{
std::ostringstream oss;
oss << *static_cast<const Source *>(v.get_const_ptr());
return oss.str();
}
public:
Boxed_Number()
@ -365,6 +373,47 @@ namespace chaiscript
}
}
std::string toString()
{
const Type_Info &inp_ = bv.get_type_info();
if (inp_ == typeid(int)) {
return toStringAux<int>(bv);
} else if (inp_ == typeid(double)) {
return toStringAux<double>(bv);
} else if (inp_ == typeid(float)) {
return toStringAux<float>(bv);
} else if (inp_ == typeid(long double)) {
return toStringAux<long double>(bv);
} else if (inp_ == typeid(char)) {
return toStringAux<int>(Boxed_Value(getAsAux<int, char>()));
} else if (inp_ == typeid(unsigned int)) {
return toStringAux<unsigned int>(bv);
} else if (inp_ == typeid(long)) {
return toStringAux<long>(bv);
} else if (inp_ == typeid(unsigned long)) {
return toStringAux<unsigned long>(bv);
} else if (inp_ == typeid(boost::int8_t)) {
return toStringAux<int>(Boxed_Value(getAsAux<int, boost::int8_t>()));
} else if (inp_ == typeid(boost::int16_t)) {
return toStringAux<boost::int16_t>(bv);
} else if (inp_ == typeid(boost::int32_t)) {
return toStringAux<boost::int32_t>(bv);
} else if (inp_ == typeid(boost::int64_t)) {
return toStringAux<boost::int64_t>(bv);
} else if (inp_ == typeid(boost::uint8_t)) {
return toStringAux<unsigned int>(Boxed_Value(getAsAux<unsigned int, boost::uint8_t>()));
} else if (inp_ == typeid(boost::uint16_t)) {
return toStringAux<boost::uint16_t>(bv);
} else if (inp_ == typeid(boost::uint32_t)) {
return toStringAux<boost::uint32_t>(bv);
} else if (inp_ == typeid(boost::uint64_t)) {
return toStringAux<boost::uint64_t>(bv);
} else {
throw boost::bad_any_cast();
}
}
bool operator==(const Boxed_Number &t_rhs) const
{
return boxed_cast<bool>(oper(Operators::equals, this->bv, t_rhs.bv));