From 62337062bf365be4b702c7fa18f9244bab29aaf6 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 25 Mar 2015 13:36:02 -0600 Subject: [PATCH] Reduce the number of exceptions created at runtime --- include/chaiscript/dispatchkit/boxed_cast.hpp | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/include/chaiscript/dispatchkit/boxed_cast.hpp b/include/chaiscript/dispatchkit/boxed_cast.hpp index 40b7857..d7ba410 100644 --- a/include/chaiscript/dispatchkit/boxed_cast.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast.hpp @@ -74,46 +74,47 @@ namespace chaiscript template typename detail::Cast_Helper::Result_Type boxed_cast(const Boxed_Value &bv, const Type_Conversions *t_conversions = nullptr) { - try { - return detail::Cast_Helper::cast(bv, t_conversions); - } catch (const chaiscript::detail::exception::bad_any_cast &) { + if (!t_conversions || bv.get_type_info().bare_equal(user_type()) || (t_conversions && !t_conversions->convertable_type())) { + try { + return detail::Cast_Helper::cast(bv, t_conversions); + } catch (const chaiscript::detail::exception::bad_any_cast &) { + } + } #ifdef CHAISCRIPT_MSVC - //Thank you MSVC, yes we know that a constant value is being used in the if - // statment in THIS VERSION of the template instantiation + //Thank you MSVC, yes we know that a constant value is being used in the if + // statment in THIS VERSION of the template instantiation #pragma warning(push) #pragma warning(disable : 4127) #endif - if (t_conversions && t_conversions->convertable_type()) - { + if (t_conversions && t_conversions->convertable_type()) + { + try { + // std::cout << "trying an up conversion " << typeid(Type).name() << '\n'; + // We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it + // either way, we are not responsible if it doesn't work + return detail::Cast_Helper::cast(t_conversions->boxed_type_conversion(bv), t_conversions); + } catch (...) { try { - // std::cout << "trying an up conversion " << typeid(Type).name() << '\n'; - // We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it - // either way, we are not responsible if it doesn't work - return detail::Cast_Helper::cast(t_conversions->boxed_type_conversion(bv), t_conversions); - } catch (...) { - try { - // std::cout << "trying a down conversion " << typeid(Type).name() << '\n'; - // try going the other way - down the inheritance graph - return detail::Cast_Helper::cast(t_conversions->boxed_type_down_conversion(bv), t_conversions); - } catch (const chaiscript::detail::exception::bad_any_cast &) { - throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type)); - } + // std::cout << "trying a down conversion " << typeid(Type).name() << '\n'; + // try going the other way - down the inheritance graph + return detail::Cast_Helper::cast(t_conversions->boxed_type_down_conversion(bv), t_conversions); + } catch (const chaiscript::detail::exception::bad_any_cast &) { + throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type)); } - } else { - // If it's not polymorphic, just throw the error, don't waste the time on the - // attempted dynamic_cast - throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type)); } + } else { + // If it's not polymorphic, just throw the error, don't waste the time on the + // attempted dynamic_cast + throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type)); + } #ifdef CHAISCRIPT_MSVC #pragma warning(pop) #endif - - } } }