diff --git a/dispatchkit/function_call.hpp b/dispatchkit/function_call.hpp new file mode 100644 index 0000000..713d534 --- /dev/null +++ b/dispatchkit/function_call.hpp @@ -0,0 +1,88 @@ +#include + +#define addparam(z,n,text) params.push_back(Boxed_Value(BOOST_PP_CAT(p, n) )); +#define curry(z,n,text) BOOST_PP_CAT(_, BOOST_PP_INC(n)) + + +#ifndef BOOST_PP_IS_ITERATING +#ifndef __function_call_hpp__ +#define __function_call_hpp__ + +#include +#include +#include + +namespace dispatchkit +{ + template + class Function_Caller_Ret + { + public: + Function_Caller_Ret() + { + } + + Ret call(const std::vector > > &t_funcs, + const std::vector ¶ms) + { + return Cast_Helper()(dispatch(t_funcs, params)); + } + }; + + template<> + class Function_Caller_Ret + { + public: + Function_Caller_Ret() + { + } + + void call(const std::vector > > &t_funcs, + const std::vector ¶ms) + { + dispatch(t_funcs, params); + } + }; +} + +#define BOOST_PP_ITERATION_LIMITS ( 0, 9 ) +#define BOOST_PP_FILENAME_1 "function_call.hpp" +#include BOOST_PP_ITERATE() +# endif +#else +# define n BOOST_PP_ITERATION() + +namespace dispatchkit +{ + template + Ret function_caller(const std::vector > > &funcs + BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) ) + { + std::vector params; + + BOOST_PP_REPEAT(n, addparam, ~) + + return Function_Caller_Ret().call(funcs, params); + } + + template + boost::function + build_function_caller(boost::shared_ptr func) + { + std::vector > > funcs; + funcs.push_back(std::make_pair(std::string(), func)); + return boost::bind(&function_caller, funcs + BOOST_PP_ENUM_TRAILING(n, curry, ~)); + } + + template + boost::function + build_function_caller(const std::vector > > &funcs) + { + return boost::bind(&function_caller, funcs + BOOST_PP_ENUM_TRAILING(n, curry, ~)); + } +} + +#endif + diff --git a/dispatchkit/test.cpp b/dispatchkit/test.cpp index e2aab97..85408be 100644 --- a/dispatchkit/test.cpp +++ b/dispatchkit/test.cpp @@ -7,6 +7,7 @@ #include "dispatchkit.hpp" #include "bootstrap.hpp" #include "bootstrap_stl.hpp" +#include "function_call.hpp" using namespace dispatchkit; @@ -154,8 +155,14 @@ int main() std::vector sos; sos.push_back(ss.get_object("testobj2")); + //Build a bound function proxy for calling the script handled function + boost::function show_message = + build_function_caller(ss.get_function("show_message")); + + Test &t = Cast_Helper()(ss.get_object("testobj2")); + //Print the message the object was created with - dispatch(ss.get_function("show_message"), sos); + show_message(t); //Now, get a reference to the object's stored message Boxed_Value stringref = dispatch(ss.get_function("get_message"), sos); @@ -176,7 +183,7 @@ int main() //Now, prove that the reference was successfully acquired //and we are able to peek into the boxed types - dispatch(ss.get_function("show_message"), sos); + show_message(t); // Finally, we are going to register some named function aliases, for