Add an example of registering a dynamic function
This commit is contained in:
parent
ab3b9e2fe7
commit
277fa83742
@ -20,17 +20,18 @@ class BoxedCPP_System
|
||||
typedef std::multimap<std::string, boost::shared_ptr<Proxy_Function> > Function_Map;
|
||||
typedef std::map<std::string, Type_Info> Type_Name_Map;
|
||||
|
||||
void register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name)
|
||||
{
|
||||
m_functions.insert(std::make_pair(name, f));
|
||||
}
|
||||
|
||||
|
||||
template<typename Function>
|
||||
void register_function(const Function &func, const std::string &name)
|
||||
{
|
||||
m_functions.insert(std::make_pair(name, boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<Function>(func))));
|
||||
}
|
||||
|
||||
void register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name)
|
||||
{
|
||||
m_functions.insert(std::make_pair(name, f));
|
||||
}
|
||||
|
||||
template<typename Class>
|
||||
void add_object(const std::string &name, const Class &obj)
|
||||
{
|
||||
@ -129,19 +130,19 @@ void dump_system(const BoxedCPP_System &s)
|
||||
}
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret add(const P1 &p1, const P2 &p2)
|
||||
Ret add(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 + p2;
|
||||
}
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret multiply(const P1 &p1, const P2 &p2)
|
||||
Ret multiply(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 * p2;
|
||||
}
|
||||
|
||||
template<typename Input>
|
||||
std::string to_string(const Input &i)
|
||||
std::string to_string(Input i)
|
||||
{
|
||||
return boost::lexical_cast<std::string>(i);
|
||||
}
|
||||
@ -154,8 +155,7 @@ void bootstrap(BoxedCPP_System &s)
|
||||
s.register_type<char>("char");
|
||||
s.register_type<std::string>("string");
|
||||
|
||||
s.register_function(boost::function<std::string (std::string, std::string)>(&add<std::string, std::string, std::string>), "+");
|
||||
|
||||
s.register_function(boost::function<std::string (const std::string &, const std::string&)>(&add<std::string, const std::string &, const std::string &>), "+");
|
||||
s.register_function(boost::function<int (int, int)>(&add<int, int, int>), "+");
|
||||
s.register_function(boost::function<double (int, double)>(&add<double, int, double>), "+");
|
||||
s.register_function(boost::function<double (double, int)>(&add<double, double, int>), "+");
|
||||
@ -167,8 +167,7 @@ void bootstrap(BoxedCPP_System &s)
|
||||
s.register_function(boost::function<double (double, double)>(&multiply<double, double, double>), "*");
|
||||
|
||||
s.register_function(boost::function<std::string (int)>(&to_string<int>), "to_string");
|
||||
//JDT: Was giving me compiler errors (not sure why)
|
||||
//s.register_function(boost::function<std::string (const std::string &)>(&to_string<const std::string &>), "to_string");
|
||||
s.register_function(boost::function<std::string (const std::string &)>(&to_string<const std::string &>), "to_string");
|
||||
s.register_function(boost::function<std::string (char)>(&to_string<char>), "to_string");
|
||||
s.register_function(boost::function<std::string (double)>(&to_string<double>), "to_string");
|
||||
|
||||
|
@ -45,6 +45,35 @@ void print(const std::string &s)
|
||||
std::cout << "Printed: " << s << std::endl;
|
||||
}
|
||||
|
||||
Boxed_Value dynamic_function(BoxedCPP_System &ss, const std::string &name,
|
||||
const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
|
||||
if (name == "concat_string")
|
||||
{
|
||||
Boxed_Value result;
|
||||
|
||||
if (params.size() == 0)
|
||||
{
|
||||
return result;
|
||||
} else {
|
||||
result =
|
||||
dispatch(ss.get_function("to_string"), Param_List_Builder() << params[0]);
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < params.size(); ++i)
|
||||
{
|
||||
result =
|
||||
dispatch(ss.get_function("+"), Param_List_Builder() << result <<
|
||||
dispatch(ss.get_function("to_string"), Param_List_Builder() << params[i]));
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
throw std::runtime_error("Unknown function call");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Test main
|
||||
int main()
|
||||
@ -77,6 +106,20 @@ int main()
|
||||
dispatch(ss.get_function("print"),
|
||||
Param_List_Builder() << dispatch(ss.get_function("to_string"), Param_List_Builder() << addresult));
|
||||
|
||||
// Now we are going to register a new dynamic function,
|
||||
// when this function is called the objects are not unboxed, but passed
|
||||
// in in their boxed state
|
||||
ss.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&dynamic_function, boost::ref(ss), "concat_string", _1))), "concat_string");
|
||||
|
||||
|
||||
// Call our newly defined dynamic function
|
||||
dispatch(ss.get_function("print"),
|
||||
Param_List_Builder() << dispatch(ss.get_function("concat_string"),
|
||||
Param_List_Builder() << std::string("\n\t") << std::string("The Value Was: ")
|
||||
<< double(42.5) << std::string(".")
|
||||
<< '\n'
|
||||
<< '\t' << std::string("The old value was: ")
|
||||
<< addresult << '.' << '\n' ));
|
||||
//
|
||||
//
|
||||
/* Older tests
|
||||
|
Loading…
x
Reference in New Issue
Block a user