Provide some cleaner examples and some additional bootstrapped functions

This commit is contained in:
Jason Turner 2009-05-27 13:30:17 +00:00
parent 6538008172
commit fbb9534601
3 changed files with 72 additions and 10 deletions

View File

@ -5,6 +5,7 @@
#include <string>
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <stdexcept>
#include <vector>
@ -25,6 +26,11 @@ class BoxedCPP_System
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)
{
@ -134,12 +140,18 @@ Ret multiply(const P1 &p1, const P2 &p2)
return p1 * p2;
}
template<typename Input>
std::string to_string(const Input &i)
{
return boost::lexical_cast<std::string>(i);
}
void bootstrap(BoxedCPP_System &s)
{
s.register_type<void>("void");
s.register_type<double>("double");
s.register_type<int>("int");
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>), "+");
@ -153,6 +165,13 @@ void bootstrap(BoxedCPP_System &s)
s.register_function(boost::function<double (int, double)>(&multiply<double, int, double>), "*");
s.register_function(boost::function<double (double, int)>(&multiply<double, double, int>), "*");
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");
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");
}
#endif

View File

@ -103,6 +103,28 @@ class Proxy_Function
};
class Dynamic_Proxy_Function : public Proxy_Function
{
public:
Dynamic_Proxy_Function(const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f)
: m_f(t_f)
{
}
virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params)
{
return m_f(params);
}
virtual std::vector<Type_Info> get_param_types()
{
return build_param_type_list(m_f);
}
private:
boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
};
template<typename Func>
class Proxy_Function_Impl : public Proxy_Function
{

View File

@ -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<double>()(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<double>()(
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<void (const std::string &)>(&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>("Test");
ss.register_function(boost::function<int (Test*, double)>(&Test::method), "method");
ss.register_function(boost::function<std::string (const std::string &)>(&testprint), "print");
@ -72,8 +94,6 @@ int main()
ss.add_object("str", std::string("Hello World"));
dump_system(ss);
std::vector<Boxed_Value> 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);
*/
}