From 7d9dbc3d86582c36cb9e5a40212c33f2fe70b013 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 14 Apr 2016 19:06:37 -0600 Subject: [PATCH] Fix some boxed_cast issues introduced with refactor --- .../dispatchkit/boxed_cast_helper.hpp | 25 ++++++++++++++++--- include/chaiscript/utility/utility.hpp | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp index 9476e0a..104abc7 100644 --- a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp @@ -29,6 +29,23 @@ namespace chaiscript throw std::runtime_error("Attempted to dereference null Boxed_Value"); } + static const void *verify_type_no_throw(const Boxed_Value &ob, const std::type_info &ti, const void *ptr) { + if (ob.get_type_info() == ti) { + return ptr; + } else { + throw chaiscript::detail::exception::bad_any_cast(); + } + } + + static void *verify_type_no_throw(const Boxed_Value &ob, const std::type_info &ti, void *ptr) { + if (!ob.is_const() && ob.get_type_info() == ti) { + return ptr; + } else { + throw chaiscript::detail::exception::bad_any_cast(); + } + } + + static 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)) { return throw_if_null(ptr); @@ -65,9 +82,9 @@ namespace chaiscript template struct Cast_Helper_Inner { - static auto cast(const Boxed_Value &ob, const Type_Conversions_State *) + static const Result * cast(const Boxed_Value &ob, const Type_Conversions_State *) { - return static_cast(verify_type(ob, typeid(Result), ob.get_const_ptr())); + return static_cast(verify_type_no_throw(ob, typeid(Result), ob.get_const_ptr())); } }; @@ -75,9 +92,9 @@ namespace chaiscript template struct Cast_Helper_Inner { - static auto cast(const Boxed_Value &ob, const Type_Conversions_State *) + static Result * cast(const Boxed_Value &ob, const Type_Conversions_State *) { - return static_cast(verify_type(ob, typeid(Result), ob.get_ptr())); + return static_cast(verify_type_no_throw(ob, typeid(Result), ob.get_ptr())); } }; diff --git a/include/chaiscript/utility/utility.hpp b/include/chaiscript/utility/utility.hpp index 87dd876..1ca0dfb 100644 --- a/include/chaiscript/utility/utility.hpp +++ b/include/chaiscript/utility/utility.hpp @@ -15,7 +15,7 @@ #include "../chaiscript.hpp" #include "../dispatchkit/proxy_functions.hpp" #include "../dispatchkit/type_info.hpp" -//#include "../dispatchkit/operators.hpp" +#include "../dispatchkit/operators.hpp" namespace chaiscript