diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index 9b78095..55cdb61 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -135,7 +135,7 @@ namespace dispatchkit boost::shared_ptr(new Data::Shared_Ptr_Proxy_Impl())) ); - std::map::iterator itr + std::map::iterator itr = m_ptrs.find(obj.get()); if (itr != m_ptrs.end()) @@ -157,10 +157,18 @@ namespace dispatchkit true) ); - std::map::iterator itr + std::map::iterator itr = m_ptrs.find(obj.get_pointer()); - if (itr != m_ptrs.end()) + // If the ptr is found in the cache and it is the correct type, + // return it. It may be the incorrect type when two variables share the + // same memory location. Example: + // struct test { int x; }; + // test t; + // Both t and t.x share the same memory location, but do not represent + // objects of the same type. + if (itr != m_ptrs.end() + && itr->second.m_type_info.m_bare_type_info == data->m_type_info.m_bare_type_info) { (*data) = (itr->second); } @@ -199,13 +207,13 @@ namespace dispatchkit */ void cull() { - std::map::iterator itr = m_ptrs.begin(); + std::map::iterator itr = m_ptrs.begin(); while (itr != m_ptrs.end()) { if (itr->second.m_ptr_proxy->unique(&itr->second.m_obj) == 1) { - std::map::iterator todel = itr; + std::map::iterator todel = itr; ++itr; m_ptrs.erase(todel); } else { @@ -214,7 +222,7 @@ namespace dispatchkit } } - std::map m_ptrs; + std::map m_ptrs; }; public: diff --git a/include/chaiscript/dispatchkit/proxy_functions.hpp b/include/chaiscript/dispatchkit/proxy_functions.hpp index d7d91c8..4566768 100644 --- a/include/chaiscript/dispatchkit/proxy_functions.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions.hpp @@ -52,6 +52,18 @@ namespace dispatchkit } }; + /** + * Used internally for handling a return value from a Proxy_Function call + */ + template + struct Handle_Return + { + Boxed_Value operator()(const boost::function &f) + { + return Boxed_Value(boost::ref(*f())); + } + }; + /** * Used internally for handling a return value from a Proxy_Function call */ diff --git a/include/chaiscript/dispatchkit/register_function.hpp b/include/chaiscript/dispatchkit/register_function.hpp index a6252d6..b026863 100644 --- a/include/chaiscript/dispatchkit/register_function.hpp +++ b/include/chaiscript/dispatchkit/register_function.hpp @@ -22,7 +22,7 @@ namespace dispatchkit template T &get_member(T Class::* m, Class *obj) { - return obj->*m; + return (obj->*m); } /** @@ -33,7 +33,7 @@ namespace dispatchkit template void register_member(Dispatch_Engine &s, T Class::* m, const std::string &name) { - s.register_function(boost::function(boost::bind(&get_member, m, _1)), name); + s.register_function(boost::function(boost::bind(&get_member, m, _1)), name); } }