From 5f6ec9521b3ac186eaa9ea02f17986afa81aa7e9 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 28 May 2009 01:51:17 +0000 Subject: [PATCH] Add more boxedcpp testing and documentation --- boxedcpp/test.cpp | 93 ++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 54 deletions(-) diff --git a/boxedcpp/test.cpp b/boxedcpp/test.cpp index c13652b..ea17333 100644 --- a/boxedcpp/test.cpp +++ b/boxedcpp/test.cpp @@ -8,13 +8,8 @@ struct Test { - Test() - : md(0) - { - } - Test(const std::string &s) - : message(s), md(0) + : message(s) { std::cout << "Test class constructed with value: " << s << std::endl; } @@ -29,38 +24,41 @@ struct Test return message; } - int method(double d) - { - md += d; - std::cout << "Method called " << md << std::endl; - return int(md); - } - std::string message; - double md; }; + +//A function that prints any string passed to it void print(const std::string &s) { std::cout << "Printed: " << s << std::endl; } + +// A function that takes a dynamic list of params +// and calls a bunch of conversion functions on them and +// returns the result as a boxed_value Boxed_Value dynamic_function(BoxedCPP_System &ss, const std::string &name, const std::vector ¶ms) { - if (name == "concat_string") { Boxed_Value result; + //Return a void if there is nothing in the array if (params.size() == 0) { return result; } else { + //else, prepopulate the result with a string conversion of the first + //param result = dispatch(ss.get_function("to_string"), Param_List_Builder() << params[0]); } + //Then, loop over all remaining params, converting them to strings and adding + //them to the result. This example maybe bette served with a string += operator + //implementation, but it works. for (size_t i = 1; i < params.size(); ++i) { result = @@ -72,9 +70,9 @@ Boxed_Value dynamic_function(BoxedCPP_System &ss, const std::string &name, } else { throw std::runtime_error("Unknown function call"); } - } + //Test main int main() { @@ -112,7 +110,8 @@ int main() 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 + // Call our newly defined dynamic function with 10 parameters, then send + // its output to the "print" 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: ") @@ -120,52 +119,38 @@ int main() << '\n' << '\t' << std::string("The old value was: ") << addresult << '.' << '\n' )); - // - // - /* Older tests - ss.register_type("Test"); - ss.register_function(boost::function(&Test::method), "method"); - ss.register_function(boost::function(&testprint), "print"); - ss.register_function(boost::function(&print), "voidfunc"); + + + + + //Register some local methods of the "Test" class ss.register_function(build_constructor(), "Test"); - ss.register_function(boost::function(&Test::show_message), "show_message"); - ss.register_function(boost::function(&Test::get_message), "get_message"); - - - ss.add_object("testobj", boost::shared_ptr(new Test())); - ss.add_object("d", boost::shared_ptr(new double(10.2))); - ss.add_object("str", std::string("Hello World")); - + ss.register_function(boost::function(&Test::get_message), "get_message"); + ss.register_function(boost::function(&Test::show_message), "show_message"); + //Create a new object using the "Test" constructor, passing the param "Yo". + //Then, add the new object to the system with the name "testobj2" + ss.add_object("testobj2", + dispatch(ss.get_function("Test"), Param_List_Builder() << std::string("Yo"))); + // Look up and store a reference to our new object std::vector sos; - sos.push_back(ss.get_object("testobj")); - sos.push_back(ss.get_object("d")); + sos.push_back(ss.get_object("testobj2")); - boost::shared_ptr method1(ss.get_function("method").front().second); - (*method1)(sos); - (*method1)(sos); - Boxed_Value o = (*method1)(sos); + //Print the message the object was created with + dispatch(ss.get_function("show_message"), sos); - dispatch(ss.get_function("print"), Param_List_Builder() << ss.get_object("str")); - - //Add new dynamically created object from registered "Test" constructor - ss.add_object("testobj2", dispatch(ss.get_function("Test"), Param_List_Builder() << std::string("Yo"))); - - std::cout << Cast_Helper()(o) << std::endl; - - dispatch(ss.get_function("voidfunc"), Param_List_Builder()); - - std::vector sos3; - sos3.push_back(ss.get_object("testobj2")); - dispatch(ss.get_function("show_message"), sos3); - - Boxed_Value stringref = dispatch(ss.get_function("get_message"), sos3); + //Now, get a reference to the object's stored message + Boxed_Value stringref = dispatch(ss.get_function("get_message"), sos); + //Unbox it using Cast_Helper std::string &sr = Cast_Helper()(stringref); + + //Update the value of the reference sr = "Bob Updated The message"; - dispatch(ss.get_function("show_message"), sos3); - */ + //Now, prove that the reference was successfully acquired + //and we are able to peek into the boxed types + dispatch(ss.get_function("show_message"), sos); }