From fde90ad980aaf6a06fae1c80bafe6a73432a83b0 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 9 Sep 2014 13:43:05 -0600 Subject: [PATCH] Throw exception if user attempts to use null Boxed_Value --- .../dispatchkit/boxed_cast_helper.hpp | 74 +++++++++++++------ unittests/null_object_access.chai | 2 +- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp index a9c3c846..222fd904 100644 --- a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp @@ -23,6 +23,13 @@ namespace chaiscript { // Cast_Helper_Inner helper classes + template + T* throw_if_null(T *t) + { + if (t) return t; + throw std::runtime_error("Attempted to dereference null Boxed_Value"); + } + /** * Generic Cast_Helper_Inner, for casting to any type */ @@ -33,21 +40,11 @@ namespace chaiscript static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *) { - if (ob.is_ref()) + if (ob.get_type_info().bare_equal_type_info(typeid(Result))) { - if (!ob.get_type_info().is_const()) - { - return std::cref((ob.get().cast >()).get()); - } else { - return ob.get().cast >(); - } + return *(static_cast(throw_if_null(ob.get_const_ptr()))); } else { - if (!ob.get_type_info().is_const()) - { - return std::cref(*(ob.get().cast >())); - } else { - return std::cref(*(ob.get().cast >())); - } + throw chaiscript::detail::exception::bad_any_cast(); } } }; @@ -65,14 +62,46 @@ namespace chaiscript { }; - /** - * Cast_Helper_Inner for casting to a const * type - */ + /* + /// Cast_Helper_Inner for casting to a const * type template struct Cast_Helper_Inner { typedef const Result * Result_Type; + static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *) + { + 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(); + } + } + }; + + /// Cast_Helper_Inner for casting to a * type + template + struct Cast_Helper_Inner + { + typedef Result * Result_Type; + + static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *) + { + if (!ob.get_type_info().is_const() && ob.get_type_info().bare_equal_type_info(typeid(Result))) + { + return static_cast(ob.get_ptr()); + } else { + throw chaiscript::detail::exception::bad_any_cast(); + } + } + }; +*/ + + template + struct Cast_Helper_Inner + { + typedef const Result * Result_Type; static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *) { if (ob.is_ref()) @@ -95,13 +124,12 @@ namespace chaiscript }; /** - * Cast_Helper_Inner for casting to a * type - */ + * * Cast_Helper_Inner for casting to a * type + * */ template struct Cast_Helper_Inner { typedef Result * Result_Type; - static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *) { if (ob.is_ref()) @@ -113,6 +141,7 @@ namespace chaiscript } }; + /** * Cast_Helper_Inner for casting to a & type */ @@ -123,12 +152,11 @@ namespace chaiscript static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *) { - if (ob.is_ref()) + if (!ob.get_type_info().is_const() && ob.get_type_info().bare_equal_type_info(typeid(Result))) { - return ob.get().cast >(); + return *(static_cast(throw_if_null(ob.get_ptr()))); } else { - Result &r = *(ob.get().cast >()); - return r; + throw chaiscript::detail::exception::bad_any_cast(); } } }; diff --git a/unittests/null_object_access.chai b/unittests/null_object_access.chai index 3ee1c9ab..2a4a06c7 100644 --- a/unittests/null_object_access.chai +++ b/unittests/null_object_access.chai @@ -1,6 +1,6 @@ load_module("test_module") -auto o = null_factory(); +auto o := null_factory(); try { o.func();