From 05bec3b4a80becb827d140c77672ce4edccbebf9 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 21 Jun 2015 21:09:26 -0600 Subject: [PATCH] Avoid attempting convert_down when not possible @arBmind this should be significant for you --- .../dispatchkit/type_conversions.hpp | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/include/chaiscript/dispatchkit/type_conversions.hpp b/include/chaiscript/dispatchkit/type_conversions.hpp index a5abed4..91a496e 100644 --- a/include/chaiscript/dispatchkit/type_conversions.hpp +++ b/include/chaiscript/dispatchkit/type_conversions.hpp @@ -92,6 +92,11 @@ namespace chaiscript return m_from; } + virtual bool bidir() const + { + return true; + } + virtual ~Type_Conversion_Base() {} protected: @@ -262,6 +267,11 @@ namespace chaiscript throw chaiscript::exception::bad_boxed_dynamic_cast(t_base.get_type_info(), typeid(Derived), "Unable to cast down inheritance hierarchy with non-polymorphic types"); } + virtual bool bidir() const + { + return false; + } + virtual Boxed_Value convert(const Boxed_Value &t_derived) const CHAISCRIPT_OVERRIDE { return Static_Caster::cast(t_derived); @@ -291,6 +301,12 @@ namespace chaiscript return m_func(t_from); } + virtual bool bidir() const + { + return false; + } + + private: Callable m_func; }; @@ -367,7 +383,7 @@ namespace chaiscript const auto &types = thread_cache(); if (types.count(to.bare_type_info()) != 0 && types.count(from.bare_type_info()) != 0) { - return has_conversion(to, from) || has_conversion(from, to); + return has_conversion(to, from); } else { return false; } @@ -417,7 +433,7 @@ namespace chaiscript bool has_conversion(const Type_Info &to, const Type_Info &from) const { chaiscript::detail::threading::shared_lock l(m_mutex); - return find(to, from) != m_conversions.end(); + return find_bidir(to, from) != m_conversions.end(); } std::shared_ptr get_conversion(const Type_Info &to, const Type_Info &from) const @@ -435,6 +451,19 @@ namespace chaiscript } private: + std::set >::const_iterator find_bidir( + const Type_Info &to, const Type_Info &from) const + { + return std::find_if(m_conversions.begin(), m_conversions.end(), + [&to, &from](const std::shared_ptr &conversion) + { + return (conversion->to().bare_equal(to) && conversion->from().bare_equal(from)) + || (conversion->bidir() && conversion->from().bare_equal(to) && conversion->to().bare_equal(from)); +; + } + ); + } + std::set >::const_iterator find( const Type_Info &to, const Type_Info &from) const {