From fbb9534601fcdb6cee85654f8105f34244c004ef Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 27 May 2009 13:30:17 +0000 Subject: [PATCH] Provide some cleaner examples and some additional bootstrapped functions --- boxedcpp/boxedcpp.hpp | 19 +++++++++++++++++ boxedcpp/proxy_functions.hpp | 22 +++++++++++++++++++ boxedcpp/test.cpp | 41 +++++++++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/boxedcpp/boxedcpp.hpp b/boxedcpp/boxedcpp.hpp index 139a231..8ae8543 100644 --- a/boxedcpp/boxedcpp.hpp +++ b/boxedcpp/boxedcpp.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -25,6 +26,11 @@ class BoxedCPP_System 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) { @@ -134,12 +140,18 @@ Ret multiply(const P1 &p1, const P2 &p2) return p1 * p2; } +template +std::string to_string(const Input &i) +{ + return boost::lexical_cast(i); +} void bootstrap(BoxedCPP_System &s) { s.register_type("void"); s.register_type("double"); s.register_type("int"); + s.register_type("char"); s.register_type("string"); s.register_function(boost::function(&add), "+"); @@ -153,6 +165,13 @@ void bootstrap(BoxedCPP_System &s) s.register_function(boost::function(&multiply), "*"); s.register_function(boost::function(&multiply), "*"); s.register_function(boost::function(&multiply), "*"); + + 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"); + + } #endif diff --git a/boxedcpp/proxy_functions.hpp b/boxedcpp/proxy_functions.hpp index 20eb695..4453a35 100644 --- a/boxedcpp/proxy_functions.hpp +++ b/boxedcpp/proxy_functions.hpp @@ -103,6 +103,28 @@ class Proxy_Function }; +class Dynamic_Proxy_Function : public Proxy_Function +{ + public: + Dynamic_Proxy_Function(const boost::function &)> &t_f) + : m_f(t_f) + { + } + + virtual Boxed_Value operator()(const std::vector ¶ms) + { + return m_f(params); + } + + virtual std::vector get_param_types() + { + return build_param_type_list(m_f); + } + + private: + boost::function &)> m_f; +}; + template class Proxy_Function_Impl : public Proxy_Function { diff --git a/boxedcpp/test.cpp b/boxedcpp/test.cpp index d96bd6b..e5023fb 100644 --- a/boxedcpp/test.cpp +++ b/boxedcpp/test.cpp @@ -40,15 +40,9 @@ struct Test double md; }; -std::string testprint(const std::string &p) +void print(const std::string &s) { - std::cout << p << std::endl; - return p; -} - -void print() -{ - std::cout << "Test void function succeeded" << std::endl; + std::cout << "Printed: " << s << std::endl; } @@ -57,7 +51,35 @@ int main() { BoxedCPP_System ss; bootstrap(ss); + dump_system(ss); + + //Calling a function by name and allowing the built in dispatch mechanism to + //choose the most appropriate version of the function + Boxed_Value addresult = dispatch(ss.get_function("+"), Param_List_Builder() << double(5.1) << double(10.3)); + //Using the Cast_Helper to unbox the resultant value and output it + std::cout << Cast_Helper()(addresult) << std::endl; + + //Using the Boxed_Value as input to another function, again with automatic dispatch. + //This time we will not bother saving the result and will instead send it straight out + std::cout << Cast_Helper()( + dispatch(ss.get_function("*"), Param_List_Builder() << 2 << addresult) + ) << std::endl; + + //Register a new function, this one with typing for us, so we don't have to ubox anything + //right here + ss.register_function(boost::function(&print), "print"); + + //Now we have a print method, let's try to print out the earlier example: + //so, we dispatch the to_string and pass its result as a param to "print" + //In this example we don't bother with temporaries and we don't have to know + //anything about types + dispatch(ss.get_function("print"), + Param_List_Builder() << dispatch(ss.get_function("to_string"), Param_List_Builder() << addresult)); + + // + // + /* Older tests ss.register_type("Test"); ss.register_function(boost::function(&Test::method), "method"); ss.register_function(boost::function(&testprint), "print"); @@ -72,8 +94,6 @@ int main() ss.add_object("str", std::string("Hello World")); - dump_system(ss); - std::vector sos; sos.push_back(ss.get_object("testobj")); @@ -103,5 +123,6 @@ int main() sr = "Bob Updated The message"; dispatch(ss.get_function("show_message"), sos3); + */ }