Add build_functor algorithm for constructing a boost function_object from a script snippet

This commit is contained in:
Jason Turner 2009-06-21 18:09:00 +00:00
parent d4ec79607f
commit dc11237af9
4 changed files with 20 additions and 1 deletions

View File

@ -1,7 +1,7 @@
#include <iostream>
#include "chaiscript.hpp"
#include "function_call.hpp"
#include "chaiscript.hpp"
#include <boost/function.hpp>
struct Callback_Handler
@ -53,5 +53,11 @@ int main(int argc, char *argv[]) {
}
cb_handler.do_callbacks();
boost::function<std::string (const std::string&, const std::string &)> f =
dispatchkit::build_functor<std::string (const std::string &, const std::string &)>
(chai, "function(x, y) { x + y }");
std::cout << "Functor call: " << f("Hello", " World") << std::endl;
}

View File

@ -2,6 +2,7 @@
#define __boxed_value_hpp__
#include "type_info.hpp"
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/any.hpp>
#include <boost/function.hpp>

View File

@ -222,6 +222,8 @@ namespace dispatchkit
}
std::cout << std::endl;
}
}
#endif

View File

@ -11,6 +11,9 @@
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <string>
#include <vector>
#include "proxy_functions.hpp"
namespace dispatchkit
{
@ -58,6 +61,13 @@ namespace dispatchkit
FunctionType *p;
return build_function_caller_helper(p, func);
}
template<typename FunctionType, typename ScriptEngine>
boost::function<FunctionType> build_functor(ScriptEngine &e, const std::string &script)
{
return build_function_caller<FunctionType>(Cast_Helper<boost::shared_ptr<Proxy_Function> >()(e.evaluate_string(script)));
}
}
# endif