diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index e10a201..c38f751 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -300,7 +300,20 @@ namespace chaiscript throw chaiscript::detail::exception::bad_any_cast(); } } - + + template + Target get_as_aux() + { + return static_cast(*static_cast(bv.get_const_ptr())); + } + + template + std::string to_string_aux(const Boxed_Value &v) + { + std::ostringstream oss; + oss << *static_cast(v.get_const_ptr()); + return oss.str(); + } public: @@ -315,6 +328,93 @@ namespace chaiscript validate_boxed_number(v); } + template explicit Boxed_Number(T t) + : bv(Boxed_Value(t)) + { + validate_boxed_number(bv); + } + + template Target get_as() + { + const Type_Info &inp_ = bv.get_type_info(); + + if (inp_ == typeid(int)) { + return get_as_aux(); + } else if (inp_ == typeid(double)) { + return get_as_aux(); + } else if (inp_ == typeid(float)) { + return get_as_aux(); + } else if (inp_ == typeid(long double)) { + return get_as_aux(); + } else if (inp_ == typeid(char)) { + return get_as_aux(); + } else if (inp_ == typeid(unsigned int)) { + return get_as_aux(); + } else if (inp_ == typeid(long)) { + return get_as_aux(); + } else if (inp_ == typeid(unsigned long)) { + return get_as_aux(); + } else if (inp_ == typeid(boost::int8_t)) { + return get_as_aux(); + } else if (inp_ == typeid(boost::int16_t)) { + return get_as_aux(); + } else if (inp_ == typeid(boost::int32_t)) { + return get_as_aux(); + } else if (inp_ == typeid(boost::int64_t)) { + return get_as_aux(); + } else if (inp_ == typeid(boost::uint8_t)) { + return get_as_aux(); + } else if (inp_ == typeid(boost::uint16_t)) { + return get_as_aux(); + } else if (inp_ == typeid(boost::uint32_t)) { + return get_as_aux(); + } else if (inp_ == typeid(boost::uint64_t)) { + return get_as_aux(); + } else { + throw boost::bad_any_cast(); + } + } + + std::string to_string() + { + const Type_Info &inp_ = bv.get_type_info(); + + if (inp_ == typeid(int)) { + return to_string_aux(bv); + } else if (inp_ == typeid(double)) { + return to_string_aux(bv); + } else if (inp_ == typeid(float)) { + return to_string_aux(bv); + } else if (inp_ == typeid(long double)) { + return to_string_aux(bv); + } else if (inp_ == typeid(char)) { + return to_string_aux(Boxed_Value(get_as_aux())); + } else if (inp_ == typeid(unsigned int)) { + return to_string_aux(bv); + } else if (inp_ == typeid(long)) { + return to_string_aux(bv); + } else if (inp_ == typeid(unsigned long)) { + return to_string_aux(bv); + } else if (inp_ == typeid(boost::int8_t)) { + return to_string_aux(Boxed_Value(get_as_aux())); + } else if (inp_ == typeid(boost::int16_t)) { + return to_string_aux(bv); + } else if (inp_ == typeid(boost::int32_t)) { + return to_string_aux(bv); + } else if (inp_ == typeid(boost::int64_t)) { + return to_string_aux(bv); + } else if (inp_ == typeid(boost::uint8_t)) { + return to_string_aux(Boxed_Value(get_as_aux())); + } else if (inp_ == typeid(boost::uint16_t)) { + return to_string_aux(bv); + } else if (inp_ == typeid(boost::uint32_t)) { + return to_string_aux(bv); + } else if (inp_ == typeid(boost::uint64_t)) { + return to_string_aux(bv); + } else { + throw boost::bad_any_cast(); + } + } bool operator==(const Boxed_Number &t_rhs) const {