diff --git a/boxedcpp/boxedcpp.hpp b/boxedcpp/boxedcpp.hpp index ffb0d41..982455d 100644 --- a/boxedcpp/boxedcpp.hpp +++ b/boxedcpp/boxedcpp.hpp @@ -20,17 +20,18 @@ class BoxedCPP_System typedef std::multimap > Function_Map; typedef std::map Type_Name_Map; + void register_function(const boost::shared_ptr &f, const std::string &name) + { + m_functions.insert(std::make_pair(name, f)); + } + + template void register_function(const Function &func, const std::string &name) { m_functions.insert(std::make_pair(name, boost::shared_ptr(new Proxy_Function_Impl(func)))); } - void register_function(const boost::shared_ptr &f, const std::string &name) - { - m_functions.insert(std::make_pair(name, f)); - } - template void add_object(const std::string &name, const Class &obj) { @@ -129,19 +130,19 @@ void dump_system(const BoxedCPP_System &s) } template -Ret add(const P1 &p1, const P2 &p2) +Ret add(P1 p1, P2 p2) { return p1 + p2; } template -Ret multiply(const P1 &p1, const P2 &p2) +Ret multiply(P1 p1, P2 p2) { return p1 * p2; } template -std::string to_string(const Input &i) +std::string to_string(Input i) { return boost::lexical_cast(i); } @@ -154,8 +155,7 @@ void bootstrap(BoxedCPP_System &s) s.register_type("char"); s.register_type("string"); - s.register_function(boost::function(&add), "+"); - + s.register_function(boost::function(&add), "+"); s.register_function(boost::function(&add), "+"); s.register_function(boost::function(&add), "+"); s.register_function(boost::function(&add), "+"); @@ -167,8 +167,7 @@ void bootstrap(BoxedCPP_System &s) s.register_function(boost::function(&multiply), "*"); s.register_function(boost::function(&to_string), "to_string"); - //JDT: Was giving me compiler errors (not sure why) - //s.register_function(boost::function(&to_string), "to_string"); + s.register_function(boost::function(&to_string), "to_string"); s.register_function(boost::function(&to_string), "to_string"); s.register_function(boost::function(&to_string), "to_string"); diff --git a/boxedcpp/test.cpp b/boxedcpp/test.cpp index 050adb8..c13652b 100644 --- a/boxedcpp/test.cpp +++ b/boxedcpp/test.cpp @@ -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 ¶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(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