diff --git a/include/chaiscript/dispatchkit/bind_first.hpp b/include/chaiscript/dispatchkit/bind_first.hpp new file mode 100644 index 0000000..41c9073 --- /dev/null +++ b/include/chaiscript/dispatchkit/bind_first.hpp @@ -0,0 +1,79 @@ +// This file is distributed under the BSD License. +// See "license.txt" for details. +// Copyright 2009, Jonathan Turner (jturner@minnow-lang.org) +// and Jason Turner (lefticus@gmail.com) +// http://www.chaiscript.com + +#include +#include + +#define param(z,n,text) BOOST_PP_CAT(_, BOOST_PP_INC(n)) + +#ifndef BOOST_PP_IS_ITERATING +#ifndef __bind_first_hpp__ +#define __bind_first_hpp__ + +#include +#include +#include + +#define BOOST_PP_ITERATION_LIMITS ( 0, 8 ) +#define BOOST_PP_FILENAME_1 + +#include BOOST_PP_ITERATE() + + +# endif +#else +# define n BOOST_PP_ITERATION() + +namespace chaiscript +{ + + template + boost::function + bind_first(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), boost::reference_wrapper &o) + { + 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, boost::reference_wrapper &o) + { + 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)), Class *o) + { + 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, Class *o) + { + 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)), boost::shared_ptr o) + { + 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, boost::shared_ptr o) + { + return boost::bind(f, o BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, param, ~)); + } + + +} + +#endif diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 36edadb..b785d7a 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -575,7 +575,7 @@ namespace chaiscript /** * return true if the Boxed_Value matches the registered type by name */ - bool is_type(const std::string &user_typename, Boxed_Value r) const + bool is_type(Boxed_Value r, const std::string &user_typename) const { try { if (get_type(user_typename).bare_equal(r.get_type_info())) diff --git a/include/chaiscript/dispatchkit/register_function.hpp b/include/chaiscript/dispatchkit/register_function.hpp index 1871315..df1555a 100644 --- a/include/chaiscript/dispatchkit/register_function.hpp +++ b/include/chaiscript/dispatchkit/register_function.hpp @@ -11,6 +11,7 @@ #define __register_function_hpp__ #include "dispatchkit.hpp" +#include "bind_first.hpp" #include #include @@ -57,6 +58,12 @@ namespace chaiscript { return detail::fun_helper(t); } + + template + Proxy_Function bound_fun(T t, Q q) + { + return detail::fun_helper(bind_first(t, q)); + } } diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index f3475f8..3afcd96 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -300,8 +300,8 @@ namespace chaiscript /** * Evaluates the given boxed string, used during eval() inside of a script */ - const Boxed_Value internal_eval(const std::vector &vals) { - return do_eval(boxed_cast(vals.at(0)), "__EVAL__", true); + const Boxed_Value internal_eval(const std::string &e) { + return do_eval(e, "__EVAL__", true); } void use(const std::string &filename) @@ -491,18 +491,13 @@ namespace chaiscript engine.add_reserved_word("false"); engine.add_reserved_word("_"); - add(Bootstrap::bootstrap()); - engine.add(fun(boost::function(boost::bind(&Eval_Engine::dump_system, boost::ref(engine)))), "dump_system"); - engine.add(fun(boost::function(boost::bind(&Eval_Engine::dump_object, boost::ref(engine), _1))), "dump_object"); - engine.add(fun(boost::function(boost::bind(&Eval_Engine::is_type, boost::ref(engine), _2, _1))), - "is_type"); - - engine.add(fun(boost::function(boost::bind(&Eval_Engine::type_name, boost::ref(engine), _1))), - "type_name"); - engine.add(fun(boost::function(boost::bind(&Eval_Engine::function_exists, boost::ref(engine), _1))), - "function_exists"); + 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(boost::function( @@ -520,13 +515,8 @@ namespace chaiscript add(map_type >("Map")); add(pair_type >("Pair")); - engine.add(fun(boost::function(boost::bind(&ChaiScript_System::use, this, _1))), "use"); - - engine.add(Proxy_Function( - new Dynamic_Proxy_Function(boost::bind(&ChaiScript_System::internal_eval, boost::ref(*this), _1), 1)), "eval"); - - - + engine.add(bound_fun(&ChaiScript_System::use, this), "use"); + engine.add(bound_fun(&ChaiScript_System::internal_eval, this), "eval"); do_eval(chaiscript_prelude, "standard prelude"); diff --git a/src/example.cpp b/src/example.cpp index 3a08508..5145724 100644 --- a/src/example.cpp +++ b/src/example.cpp @@ -71,6 +71,9 @@ int main(int argc, char *argv[]) { System system; chai.add(var(&system), "system"); + //Add a bound callback method + chai.add(bound_fun(&System::add_callback, system), "add_callback_bound"); + //Register the two methods of the System structure. chai.add(fun(&System::add_callback), "add_callback"); chai.add(fun(&System::do_callbacks), "do_callbacks");