From 927235d8713e26c344d0bfd0dae74922bb390309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Gro=C3=9F?= Date: Fri, 29 Jun 2012 07:42:16 +0200 Subject: [PATCH 1/5] Add templated constructor to Boxed_Number to allow creating from primitive number types. --- include/chaiscript/dispatchkit/boxed_number.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 89775d3..1fe4e95 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -313,6 +313,11 @@ namespace chaiscript validate_boxed_number(v); } + template explicit Boxed_Number(T t) + : bv(Boxed_Value(t)) + { + validate_boxed_number(bv); + } bool operator==(const Boxed_Number &t_rhs) const { From dfcc415c317519452545c436c6c6a70dba58a1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Gro=C3=9F?= Date: Fri, 29 Jun 2012 08:00:00 +0200 Subject: [PATCH 2/5] Add getAs function to Boxed_Number to enable casting to a target type. --- .../chaiscript/dispatchkit/boxed_number.hpp | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 1fe4e95..ccb93d1 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -298,7 +298,12 @@ namespace chaiscript throw boost::bad_any_cast(); } } - + + template + Target getAsAux() + { + return static_cast(*static_cast(bv.get_const_ptr())); + } public: @@ -319,6 +324,47 @@ namespace chaiscript validate_boxed_number(bv); } + template Target getAs() + { + const Type_Info &inp_ = bv.get_type_info(); + + if (inp_ == typeid(int)) { + return getAsAux(); + } else if (inp_ == typeid(double)) { + return getAsAux(); + } else if (inp_ == typeid(float)) { + return getAsAux(); + } else if (inp_ == typeid(long double)) { + return getAsAux(); + } else if (inp_ == typeid(char)) { + return getAsAux(); + } else if (inp_ == typeid(unsigned int)) { + return getAsAux(); + } else if (inp_ == typeid(long)) { + return getAsAux(); + } else if (inp_ == typeid(unsigned long)) { + return getAsAux(); + } else if (inp_ == typeid(boost::int8_t)) { + return getAsAux(); + } else if (inp_ == typeid(boost::int16_t)) { + return getAsAux(); + } else if (inp_ == typeid(boost::int32_t)) { + return getAsAux(); + } else if (inp_ == typeid(boost::int64_t)) { + return getAsAux(); + } else if (inp_ == typeid(boost::uint8_t)) { + return getAsAux(); + } else if (inp_ == typeid(boost::uint16_t)) { + return getAsAux(); + } else if (inp_ == typeid(boost::uint32_t)) { + return getAsAux(); + } else if (inp_ == typeid(boost::uint64_t)) { + return getAsAux(); + } else { + throw boost::bad_any_cast(); + } + } + bool operator==(const Boxed_Number &t_rhs) const { return boxed_cast(oper(Operators::equals, this->bv, t_rhs.bv)); From f8feaf6ea867aec5c3118bd504ef043f0a47c248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Gro=C3=9F?= Date: Fri, 29 Jun 2012 16:48:53 +0200 Subject: [PATCH 3/5] 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. --- .../chaiscript/dispatchkit/boxed_number.hpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index ccb93d1..4a2f94e 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -305,6 +305,14 @@ namespace chaiscript return static_cast(*static_cast(bv.get_const_ptr())); } + template + std::string toStringAux(const Boxed_Value &v) + { + std::ostringstream oss; + oss << *static_cast(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(bv); + } else if (inp_ == typeid(double)) { + return toStringAux(bv); + } else if (inp_ == typeid(float)) { + return toStringAux(bv); + } else if (inp_ == typeid(long double)) { + return toStringAux(bv); + } else if (inp_ == typeid(char)) { + return toStringAux(Boxed_Value(getAsAux())); + } else if (inp_ == typeid(unsigned int)) { + return toStringAux(bv); + } else if (inp_ == typeid(long)) { + return toStringAux(bv); + } else if (inp_ == typeid(unsigned long)) { + return toStringAux(bv); + } else if (inp_ == typeid(boost::int8_t)) { + return toStringAux(Boxed_Value(getAsAux())); + } else if (inp_ == typeid(boost::int16_t)) { + return toStringAux(bv); + } else if (inp_ == typeid(boost::int32_t)) { + return toStringAux(bv); + } else if (inp_ == typeid(boost::int64_t)) { + return toStringAux(bv); + } else if (inp_ == typeid(boost::uint8_t)) { + return toStringAux(Boxed_Value(getAsAux())); + } else if (inp_ == typeid(boost::uint16_t)) { + return toStringAux(bv); + } else if (inp_ == typeid(boost::uint32_t)) { + return toStringAux(bv); + } else if (inp_ == typeid(boost::uint64_t)) { + return toStringAux(bv); + } else { + throw boost::bad_any_cast(); + } + } + bool operator==(const Boxed_Number &t_rhs) const { return boxed_cast(oper(Operators::equals, this->bv, t_rhs.bv)); From 935276fccd591d5c984da98c71939dadbd87677a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Gro=C3=9F?= Date: Tue, 3 Jul 2012 18:42:04 +0200 Subject: [PATCH 4/5] Adapt getAs function to proper naming scheme. --- .../chaiscript/dispatchkit/boxed_number.hpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 4a2f94e..6ad614f 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -300,7 +300,7 @@ namespace chaiscript } template - Target getAsAux() + Target get_as_aux() { return static_cast(*static_cast(bv.get_const_ptr())); } @@ -332,42 +332,42 @@ namespace chaiscript validate_boxed_number(bv); } - template Target getAs() + template Target get_as() { const Type_Info &inp_ = bv.get_type_info(); if (inp_ == typeid(int)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(double)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(float)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(long double)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(char)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(unsigned int)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(long)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(unsigned long)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(boost::int8_t)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(boost::int16_t)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(boost::int32_t)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(boost::int64_t)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(boost::uint8_t)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(boost::uint16_t)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(boost::uint32_t)) { - return getAsAux(); + return get_as_aux(); } else if (inp_ == typeid(boost::uint64_t)) { - return getAsAux(); + return get_as_aux(); } else { throw boost::bad_any_cast(); } From 08d9d9e28e0e1b10c08af34cc72f315dccd35f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Gro=C3=9F?= Date: Tue, 3 Jul 2012 18:42:27 +0200 Subject: [PATCH 5/5] Adapt toString to proper naming scheme. --- .../chaiscript/dispatchkit/boxed_number.hpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 6ad614f..69b4601 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -306,7 +306,7 @@ namespace chaiscript } template - std::string toStringAux(const Boxed_Value &v) + std::string to_string_aux(const Boxed_Value &v) { std::ostringstream oss; oss << *static_cast(v.get_const_ptr()); @@ -373,42 +373,42 @@ namespace chaiscript } } - std::string toString() + std::string to_string() { const Type_Info &inp_ = bv.get_type_info(); if (inp_ == typeid(int)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(double)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(float)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(long double)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(char)) { - return toStringAux(Boxed_Value(getAsAux())); + return to_string_aux(Boxed_Value(get_as_aux())); } else if (inp_ == typeid(unsigned int)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(long)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(unsigned long)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(boost::int8_t)) { - return toStringAux(Boxed_Value(getAsAux())); + return to_string_aux(Boxed_Value(get_as_aux())); } else if (inp_ == typeid(boost::int16_t)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(boost::int32_t)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(boost::int64_t)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(boost::uint8_t)) { - return toStringAux(Boxed_Value(getAsAux())); + return to_string_aux(Boxed_Value(get_as_aux())); } else if (inp_ == typeid(boost::uint16_t)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(boost::uint32_t)) { - return toStringAux(bv); + return to_string_aux(bv); } else if (inp_ == typeid(boost::uint64_t)) { - return toStringAux(bv); + return to_string_aux(bv); } else { throw boost::bad_any_cast(); }