From 6491262491f6cfb647fd54544ea6fa0944725bcc Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 3 Jun 2011 12:41:38 -0600 Subject: [PATCH] Add direct access to Boxed_Value data * for high performance operations --- .../chaiscript/dispatchkit/boxed_numeric.hpp | 22 ++++---- .../chaiscript/dispatchkit/boxed_value.hpp | 54 ++++++++++--------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/include/chaiscript/dispatchkit/boxed_numeric.hpp b/include/chaiscript/dispatchkit/boxed_numeric.hpp index 1bcd46b..78c70aa 100644 --- a/include/chaiscript/dispatchkit/boxed_numeric.hpp +++ b/include/chaiscript/dispatchkit/boxed_numeric.hpp @@ -164,15 +164,15 @@ namespace chaiscript { if (t_oper > Operators::boolean_flag && t_oper < Operators::non_const_flag) { - return boolean::go(t_oper, boxed_cast(t_lhs), boxed_cast(t_rhs)); - } else if (t_oper > Operators::non_const_flag && t_oper < Operators::non_const_int_flag) { - return binary::go(t_oper, boxed_cast(t_lhs), boxed_cast(t_rhs)); - } else if (t_oper > Operators::non_const_int_flag && t_oper < Operators::const_int_flag) { - return binary_int::go(t_oper, boxed_cast(t_lhs), boxed_cast(t_rhs)); + return boolean::go(t_oper, *static_cast(t_lhs.get_const_ptr()), *static_cast(t_rhs.get_const_ptr())); + } else if (t_oper > Operators::non_const_flag && t_oper < Operators::non_const_int_flag && !t_lhs.is_const()) { + return binary::go(t_oper, *static_cast(t_lhs.get_ptr()), *static_cast(t_rhs.get_const_ptr())); + } else if (t_oper > Operators::non_const_int_flag && t_oper < Operators::const_int_flag && !t_lhs.is_const()) { + return binary_int::go(t_oper, *static_cast(t_lhs.get_ptr()), *static_cast(t_rhs.get_const_ptr())); } else if (t_oper > Operators::const_int_flag && t_oper < Operators::const_flag) { - return const_binary_int::go(t_oper, boxed_cast(t_lhs), boxed_cast(t_rhs)); + return const_binary_int::go(t_oper, *static_cast(t_lhs.get_const_ptr()), *static_cast(t_rhs.get_const_ptr())); } else if (t_oper > Operators::const_flag) { - return const_binary::go(t_oper, boxed_cast(t_lhs), boxed_cast(t_rhs)); + return const_binary::go(t_oper, *static_cast(t_lhs.get_const_ptr()), *static_cast(t_rhs.get_const_ptr())); } else { throw boost::bad_any_cast(); } @@ -186,15 +186,15 @@ namespace chaiscript { if (t_oper > Operators::boolean_flag && t_oper < Operators::non_const_flag) { - return boolean::go(t_oper, boxed_cast(t_lhs), boxed_cast(t_rhs)); - } else if (t_oper > Operators::non_const_flag && t_oper < Operators::non_const_int_flag) { - return binary::go(t_oper, boxed_cast(t_lhs), boxed_cast(t_rhs)); + return boolean::go(t_oper, *static_cast(t_lhs.get_const_ptr()), *static_cast(t_rhs.get_const_ptr())); + } else if (t_oper > Operators::non_const_flag && t_oper < Operators::non_const_int_flag && !t_lhs.is_const()) { + return binary::go(t_oper, *static_cast(t_lhs.get_ptr()), *static_cast(t_rhs.get_const_ptr())); } else if (t_oper > Operators::non_const_int_flag && t_oper < Operators::const_int_flag) { throw boost::bad_any_cast(); } else if (t_oper > Operators::const_int_flag && t_oper < Operators::const_flag) { throw boost::bad_any_cast(); } else if (t_oper > Operators::const_flag) { - return const_binary::go(t_oper, boxed_cast(t_lhs), boxed_cast(t_rhs)); + return const_binary::go(t_oper, *static_cast(t_lhs.get_const_ptr()), *static_cast(t_rhs.get_const_ptr())); } else { throw boost::bad_any_cast(); } diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index 15a2a9d..d49931f 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -23,6 +23,7 @@ namespace chaiscript { + /// \brief A wrapper for holding any valid C++ type. All types in ChaiScript are Boxed_Value objects /// \sa chaiscript::boxed_cast class Boxed_Value @@ -41,19 +42,12 @@ namespace chaiscript */ struct Data { - template - static bool is_null(boost::any *a) - { - boost::shared_ptr *ptr = boost::any_cast >(a); - return ptr->get() == 0; - } - Data(const Type_Info &ti, const boost::any &to, bool tr, - const boost::function &t_is_null = boost::function()) - : m_type_info(ti), m_obj(to), - m_is_ref(tr), m_is_null(t_is_null) + const void *t_void_ptr) + : m_type_info(ti), m_obj(to), m_data_ptr(ti.is_const()?0:const_cast(t_void_ptr)), m_const_data_ptr(t_void_ptr), + m_is_ref(tr) { } @@ -62,7 +56,8 @@ namespace chaiscript m_type_info = rhs.m_type_info; m_obj = rhs.m_obj; m_is_ref = rhs.m_is_ref; - m_is_null = rhs.m_is_null; + m_data_ptr = rhs.m_data_ptr; + m_const_data_ptr = rhs.m_const_data_ptr; return *this; } @@ -73,8 +68,9 @@ namespace chaiscript Type_Info m_type_info; boost::any m_obj; + void *m_data_ptr; + const void *m_const_data_ptr; bool m_is_ref; - boost::function m_is_null; std::vector > m_dependencies; }; @@ -85,7 +81,8 @@ namespace chaiscript return boost::shared_ptr (new Data( detail::Get_Type_Info::get(), boost::any(), - false) + false, + 0) ); } @@ -102,7 +99,7 @@ namespace chaiscript detail::Get_Type_Info::get(), boost::any(obj), false, - boost::function(&Data::is_null)) + obj.get()) ); } @@ -117,19 +114,21 @@ namespace chaiscript { return boost::shared_ptr(new Data( detail::Get_Type_Info::get(), - boost::any(obj), - true) + boost::any(obj), + true, + obj.get_pointer()) ); } template static boost::shared_ptr get(const T& t) { + boost::shared_ptr p(new T(t)); return boost::shared_ptr(new Data( detail::Get_Type_Info::get(), - boost::any(boost::shared_ptr(new T(t))), + boost::any(p), false, - boost::function(&Data::is_null)) + p.get()) ); } @@ -138,7 +137,8 @@ namespace chaiscript return boost::shared_ptr (new Data( Type_Info(), boost::any(), - false) + false, + 0) ); } @@ -224,12 +224,7 @@ namespace chaiscript bool is_null() const { - if (m_data->m_is_null) - { - return m_data->m_is_null(&m_data->m_obj); - } else { - return false; - } + return (m_data->m_data_ptr == 0 && m_data->m_const_data_ptr == 0); } const boost::any & get() const @@ -265,6 +260,15 @@ namespace chaiscript } } + void *get_ptr() const + { + return m_data->m_data_ptr; + } + + const void *get_const_ptr() const + { + return m_data->m_const_data_ptr; + } private: boost::shared_ptr m_data;