diff --git a/include/chaiscript/dispatchkit/boxed_cast.hpp b/include/chaiscript/dispatchkit/boxed_cast.hpp index 5a11ee9..395cf83 100644 --- a/include/chaiscript/dispatchkit/boxed_cast.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast.hpp @@ -69,7 +69,7 @@ namespace chaiscript /// assert(i == 5); /// \endcode template - typename detail::Cast_Helper::Result_Type boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions = nullptr) + auto boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions = nullptr) -> decltype(detail::Cast_Helper::cast(bv, nullptr)) { if (!t_conversions || bv.get_type_info().bare_equal(user_type()) || (t_conversions && !(*t_conversions)->convertable_type())) { try { diff --git a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp index da1e222..36c15f7 100644 --- a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp @@ -29,21 +29,36 @@ namespace chaiscript throw std::runtime_error("Attempted to dereference null Boxed_Value"); } + const void *verify_type(const Boxed_Value &ob, const std::type_info &ti, const void *ptr) { + if (!ob.get_type_info().bare_equal_type_info(ti)) { + throw chaiscript::detail::exception::bad_any_cast(); + } else { + return throw_if_null(ptr); + } + } + + void *verify_type(const Boxed_Value &ob, const std::type_info &ti, void *ptr) { + if (ptr == nullptr || !ob.get_type_info().bare_equal_type_info(ti)) { + throw chaiscript::detail::exception::bad_any_cast(); + } else { + return ptr; + } + } + + /* + template> + Result cast_helper_inner(const Boxed_Value &ob, const Type_Conversions_State *) + { + } +*/ + /// Generic Cast_Helper_Inner, for casting to any type template struct Cast_Helper_Inner { - typedef typename std::add_const::type Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static Result cast(const Boxed_Value &ob, const Type_Conversions_State *) { - if (ob.get_type_info().bare_equal_type_info(typeid(Result))) - { - auto p = throw_if_null(ob.get_const_ptr()); - return *static_cast(p); - } else { - throw chaiscript::detail::exception::bad_any_cast(); - } + return *static_cast(verify_type(ob, typeid(Result), ob.get_const_ptr())); } }; @@ -57,15 +72,9 @@ namespace chaiscript template struct Cast_Helper_Inner { - typedef const Result * Result_Type; - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static auto cast(const Boxed_Value &ob, const Type_Conversions_State *) { - if (ob.get_type_info().bare_equal_type_info(typeid(Result))) - { - return static_cast(ob.get_const_ptr()); - } else { - throw chaiscript::detail::exception::bad_any_cast(); - } + return static_cast(verify_type(ob, typeid(Result), ob.get_const_ptr())); } }; @@ -73,15 +82,9 @@ namespace chaiscript template struct Cast_Helper_Inner { - typedef Result * Result_Type; - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static auto cast(const Boxed_Value &ob, const Type_Conversions_State *) { - if (!ob.get_type_info().is_const() && ob.get_type_info() == typeid(Result)) - { - return static_cast(ob.get_ptr()); - } else { - throw chaiscript::detail::exception::bad_any_cast(); - } + return static_cast(verify_type(ob, typeid(Result), ob.get_ptr())); } }; @@ -100,17 +103,9 @@ namespace chaiscript template struct Cast_Helper_Inner { - typedef const Result& Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static const Result & cast(const Boxed_Value &ob, const Type_Conversions_State *) { - if (ob.get_type_info().bare_equal_type_info(typeid(Result))) - { - auto p = throw_if_null(ob.get_const_ptr()); - return *static_cast(p); - } else { - throw chaiscript::detail::exception::bad_any_cast(); - } + return *static_cast(verify_type(ob, typeid(Result), ob.get_const_ptr())); } }; @@ -120,16 +115,9 @@ namespace chaiscript template struct Cast_Helper_Inner { - typedef Result& Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static Result& cast(const Boxed_Value &ob, const Type_Conversions_State *) { - if (!ob.get_type_info().is_const() && ob.get_type_info().bare_equal_type_info(typeid(Result))) - { - return *(static_cast(throw_if_null(ob.get_ptr()))); - } else { - throw chaiscript::detail::exception::bad_any_cast(); - } + return *static_cast(verify_type(ob, typeid(Result), ob.get_ptr())); } }; @@ -137,9 +125,7 @@ namespace chaiscript template struct Cast_Helper_Inner > { - typedef std::shared_ptr Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static auto cast(const Boxed_Value &ob, const Type_Conversions_State *) { return ob.get().cast >(); } @@ -149,9 +135,7 @@ namespace chaiscript template struct Cast_Helper_Inner > { - typedef std::shared_ptr Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static auto cast(const Boxed_Value &ob, const Type_Conversions_State *) { if (!ob.get_type_info().is_const()) { @@ -177,10 +161,7 @@ namespace chaiscript struct Cast_Helper_Inner &> { static_assert(!std::is_const::value, "Non-const reference to std::shared_ptr is not supported"); - - typedef Boxed_Value::Sentinel Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static auto cast(const Boxed_Value &ob, const Type_Conversions_State *) { std::shared_ptr &res = ob.get().cast >(); return ob.pointer_sentinel(res); @@ -204,9 +185,7 @@ namespace chaiscript template<> struct Cast_Helper_Inner { - typedef Boxed_Value Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static Boxed_Value cast(const Boxed_Value &ob, const Type_Conversions_State *) { return ob; } @@ -216,9 +195,7 @@ namespace chaiscript template<> struct Cast_Helper_Inner { - typedef std::reference_wrapper Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static std::reference_wrapper cast(const Boxed_Value &ob, const Type_Conversions_State *) { return std::ref(const_cast(ob)); } @@ -272,9 +249,7 @@ namespace chaiscript template struct Cast_Helper { - typedef typename Cast_Helper_Inner::Result_Type Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions) + static auto cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions) -> decltype(Cast_Helper_Inner::cast(ob, t_conversions)) { return Cast_Helper_Inner::cast(ob, t_conversions); } diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 6bbf088..ad2775b 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -1009,9 +1009,7 @@ namespace chaiscript template<> struct Cast_Helper { - typedef Boxed_Number Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *) + static Boxed_Number cast(const Boxed_Value &ob, const Type_Conversions_State *) { return Boxed_Number(ob); } diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index f10d340..1c5809c 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -424,7 +424,7 @@ namespace chaiscript /// \brief casts an object while applying any Dynamic_Conversion available template - typename detail::Cast_Helper::Result_Type boxed_cast(const Boxed_Value &bv) const + auto boxed_cast(const Boxed_Value &bv) const -> decltype(chaiscript::boxed_cast(bv, nullptr)) { Type_Conversions_State state(m_conversions, m_conversions.conversion_saves()); return chaiscript::boxed_cast(bv, &state); diff --git a/include/chaiscript/dispatchkit/function_call.hpp b/include/chaiscript/dispatchkit/function_call.hpp index 990be95..3efb703 100644 --- a/include/chaiscript/dispatchkit/function_call.hpp +++ b/include/chaiscript/dispatchkit/function_call.hpp @@ -81,9 +81,7 @@ namespace chaiscript template struct Cast_Helper &> { - typedef std::function Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions) + static std::function cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions) { if (ob.get_type_info().bare_equal(user_type())) { @@ -98,9 +96,7 @@ namespace chaiscript template struct Cast_Helper > { - typedef std::function Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions) + static std::function cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions) { if (ob.get_type_info().bare_equal(user_type())) { @@ -115,9 +111,7 @@ namespace chaiscript template struct Cast_Helper > { - typedef std::function Result_Type; - - static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions) + static std::function cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions) { if (ob.get_type_info().bare_equal(user_type())) { diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 84f25bc..761bd61 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -690,7 +690,7 @@ namespace chaiscript /// \brief casts an object while applying any Dynamic_Conversion available template - typename detail::Cast_Helper::Result_Type boxed_cast(const Boxed_Value &bv) const + auto boxed_cast(const Boxed_Value &bv) const -> decltype(m_engine.boxed_cast(bv)) { return m_engine.boxed_cast(bv); }