From b1d12fdc91d16f3ecca2255d1d414a8e9870b89d Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 15 Oct 2009 15:27:16 +0000 Subject: [PATCH] Further updates to the new bound function support, plus general cleanup of how it is used --- include/chaiscript/dispatchkit/bind_first.hpp | 17 +++- .../dispatchkit/register_function.hpp | 97 ++++++++----------- .../chaiscript/language/chaiscript_engine.hpp | 27 +++--- 3 files changed, 63 insertions(+), 78 deletions(-) diff --git a/include/chaiscript/dispatchkit/bind_first.hpp b/include/chaiscript/dispatchkit/bind_first.hpp index 765316d..9e42653 100644 --- a/include/chaiscript/dispatchkit/bind_first.hpp +++ b/include/chaiscript/dispatchkit/bind_first.hpp @@ -7,7 +7,7 @@ #include #include -#define param(z,n,text) BOOST_PP_CAT(_, BOOST_PP_INC(n)) +#define param(z,n,text) BOOST_PP_CAT(text, BOOST_PP_INC(n)) #ifndef BOOST_PP_IS_ITERATING #ifndef __bind_first_hpp__ @@ -30,26 +30,33 @@ namespace chaiscript { - + template boost::function bind_first(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const O &o) { - return boost::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, ~)); + return boost::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, _)); } template boost::function bind_first(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param))const, const O &o) { - return boost::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, ~)); + return boost::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, _)); } template boost::function bind_first(Ret (*f)(BOOST_PP_ENUM_PARAMS(m, Param)), const O &o) { - return boost::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, ~)); + return boost::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, _)); + } + + template + boost::function + bind_first(const boost::function &f, const O &o) + { + return boost::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, _)); } diff --git a/include/chaiscript/dispatchkit/register_function.hpp b/include/chaiscript/dispatchkit/register_function.hpp index df1555a..bd9da43 100644 --- a/include/chaiscript/dispatchkit/register_function.hpp +++ b/include/chaiscript/dispatchkit/register_function.hpp @@ -4,9 +4,6 @@ // and Jason Turner (lefticus@gmail.com) // http://www.chaiscript.com -#include - -#ifndef BOOST_PP_IS_ITERATING #ifndef __register_function_hpp__ #define __register_function_hpp__ @@ -14,6 +11,8 @@ #include "bind_first.hpp" #include #include +#include +#include namespace chaiscript { @@ -29,81 +28,63 @@ namespace chaiscript } template - Proxy_Function fun_helper(const boost::function &f) - { - return Proxy_Function(new Proxy_Function_Impl(f)); - } + boost::function mk_boost_fun(const boost::function &f) + { + return f; + } + + template + boost::function< + typename boost::function_types::function_type >::type + > mk_boost_fun(T t) + { + return + boost::function< + typename boost::function_types::function_type >::type + >(t); + } - /** - * Automatically create a get_member helper function for an object - * to allow for runtime dispatched access to public data members - * for example, the case of std::pair<>::first and std::pair<>::second - */ template Proxy_Function fun_helper(T Class::* m) { return fun_helper(boost::function(boost::bind(&detail::get_member, m, _1))); } + + + template + Proxy_Function fun_helper(const boost::function &f) + { + return Proxy_Function(new Proxy_Function_Impl(f)); + } } -} -#define BOOST_PP_ITERATION_LIMITS ( 0, 10 ) -#define BOOST_PP_FILENAME_1 -#include BOOST_PP_ITERATE() + template + Proxy_Function fun(const boost::function &f) + { + return detail::fun_helper(f); + } -namespace chaiscript -{ template Proxy_Function fun(T t) { - return detail::fun_helper(t); + return detail::fun_helper(detail::mk_boost_fun(t)); } + template - Proxy_Function bound_fun(T t, Q q) + Proxy_Function fun(T t, const Q &q) { return detail::fun_helper(bind_first(t, q)); } + + template + Proxy_Function fun(T t, const Q &q, const R &r) + { + return detail::fun_helper(bind_first(bind_first(t, q), r)); + } + } -# endif -#else -# define n BOOST_PP_ITERATION() - -namespace chaiscript -{ - namespace detail - { - /** - * Register a global function of n parameters with name - */ - template - Proxy_Function fun_helper(Ret (*f)(BOOST_PP_ENUM_PARAMS(n, Param))) - { - return fun_helper(boost::function(f)); - } - - /** - * Register a class method of n parameters with name - */ - template - Proxy_Function fun_helper(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param))) - { - return fun_helper(boost::function(f)); - } - - /** - * Register a const class method of n parameters with name - */ - template - Proxy_Function fun_helper(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param))const) - { - return fun_helper(boost::function(f)); - } - - } -} - #endif diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 3afcd96..350a176 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -493,30 +493,27 @@ namespace chaiscript add(Bootstrap::bootstrap()); - engine.add(bound_fun(&Eval_Engine::dump_system, boost::ref(engine)), "dump_system"); - engine.add(bound_fun(&Eval_Engine::dump_object, boost::ref(engine)), "dump_object"); - engine.add(bound_fun(&Eval_Engine::is_type, boost::ref(engine)), "is_type"); - engine.add(bound_fun(&Eval_Engine::type_name, boost::ref(engine)), "type_name"); - engine.add(bound_fun(&Eval_Engine::function_exists, boost::ref(engine)), "function_exists"); + engine.add(fun(&Eval_Engine::dump_system, boost::ref(engine)), "dump_system"); + engine.add(fun(&Eval_Engine::dump_object, boost::ref(engine)), "dump_object"); + engine.add(fun(&Eval_Engine::is_type, boost::ref(engine)), "is_type"); + engine.add(fun(&Eval_Engine::type_name, boost::ref(engine)), "type_name"); + engine.add(fun(&Eval_Engine::function_exists, boost::ref(engine)), "function_exists"); - engine.add(fun(boost::function( - boost::bind(static_cast::*)(const std::string&)>( - &ChaiScript_System::load_module), boost::ref(*this), _1))), - "load_module"); + typedef void (ChaiScript_System::*load_mod_1)(const std::string&); + typedef void (ChaiScript_System::*load_mod_2)(const std::string&, const std::string&); + + engine.add(fun(static_cast(&ChaiScript_System::load_module)), "load_module"); + engine.add(fun(static_cast(&ChaiScript_System::load_module)), "load_module"); - engine.add(fun(boost::function( - boost::bind(static_cast::*)(const std::string&, const std::string&)>( - &ChaiScript_System::load_module), boost::ref(*this), _1, _2))), - "load_module"); add(vector_type >("Vector")); add(string_type("string")); add(map_type >("Map")); add(pair_type >("Pair")); - engine.add(bound_fun(&ChaiScript_System::use, this), "use"); - engine.add(bound_fun(&ChaiScript_System::internal_eval, this), "eval"); + engine.add(fun(&ChaiScript_System::use, this), "use"); + engine.add(fun(&ChaiScript_System::internal_eval, this), "eval"); do_eval(chaiscript_prelude, "standard prelude");