add capability for passing functions back to C++ from script land, plus an example usage.
This commit is contained in:
@@ -15,6 +15,9 @@ if(Boost_FOUND)
|
|||||||
add_executable(chaiscript_test main.cpp)
|
add_executable(chaiscript_test main.cpp)
|
||||||
target_link_libraries(chaiscript_test ${Boost_LIBRARIES})
|
target_link_libraries(chaiscript_test ${Boost_LIBRARIES})
|
||||||
|
|
||||||
|
add_executable(chaiscript_callbacktest callbacktest.cpp)
|
||||||
|
target_link_libraries(chaiscript_callbacktest ${Boost_LIBRARIES})
|
||||||
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
57
chaiscript/callbacktest.cpp
Normal file
57
chaiscript/callbacktest.cpp
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "chaiscript.hpp"
|
||||||
|
#include "function_call.hpp"
|
||||||
|
#include <boost/function.hpp>
|
||||||
|
|
||||||
|
struct Callback_Handler
|
||||||
|
{
|
||||||
|
typedef std::vector<std::pair<boost::function<std::string ()>,
|
||||||
|
boost::function<double (int)> > > Callbacks;
|
||||||
|
|
||||||
|
Callbacks m_callbacks;
|
||||||
|
|
||||||
|
void add_callbacks(boost::shared_ptr<dispatchkit::Proxy_Function> t_name,
|
||||||
|
boost::shared_ptr<dispatchkit::Proxy_Function> t_value)
|
||||||
|
{
|
||||||
|
m_callbacks.push_back(
|
||||||
|
std::make_pair(dispatchkit::build_function_caller<std::string>(t_name),
|
||||||
|
dispatchkit::build_function_caller<double, int>(t_value)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_callbacks()
|
||||||
|
{
|
||||||
|
int i=1;
|
||||||
|
for (Callbacks::iterator itr = m_callbacks.begin();
|
||||||
|
itr != m_callbacks.end();
|
||||||
|
++itr)
|
||||||
|
{
|
||||||
|
std::cout << "Name: " << itr->first() << " = " << itr->second(i) << std::endl;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
chaiscript::ChaiScript_Engine chai;
|
||||||
|
|
||||||
|
Callback_Handler cb_handler;
|
||||||
|
chai.get_eval_engine().add_object("cb_handler", boost::ref(cb_handler));
|
||||||
|
dispatchkit::register_function(chai.get_eval_engine(), &Callback_Handler::add_callbacks, "add_callbacks");
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; ++i) {
|
||||||
|
try {
|
||||||
|
dispatchkit::Boxed_Value val = chai.evaluate_file(argv[i]);
|
||||||
|
}
|
||||||
|
catch (std::exception &e) {
|
||||||
|
std::cerr << "Could not open: " << argv[i] << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cb_handler.do_callbacks();
|
||||||
|
}
|
||||||
|
|
7
samples/callbacks.chai
Normal file
7
samples/callbacks.chai
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
cb_handler.add_callbacks( function() { "First"; }, function (x) { x * 1.1; } );
|
||||||
|
cb_handler.add_callbacks( function() { "TimesThree"; }, function (x) { x * 3.0001; } );
|
||||||
|
cb_handler.add_callbacks( function() { "DivOnePtTwo"; }, function (x) { x / 1.2; } );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user