Many cleanups to bootstrapping process and function registration

This commit is contained in:
Jason Turner 2009-06-06 15:51:43 +00:00
parent 92f834da73
commit df41d7abe6
4 changed files with 160 additions and 72 deletions

View File

@ -2,6 +2,7 @@
#define __bootstrap_hpp__
#include "boxedcpp.hpp"
#include "register_function.hpp"
template<typename Ret, typename P1, typename P2>
Ret add(P1 p1, P2 p2)
@ -82,6 +83,102 @@ P1 &timesequal(P1 &p1, P2 p2)
return (p1 *= p2);
}
//Add canonical forms of operators
template<typename T>
void add_oper_equals(BoxedCPP_System &s)
{
register_function(s, &equals<const T&, const T&>, "=");
}
template<typename T>
void add_oper_add(BoxedCPP_System &s)
{
register_function(s, &add<T, const T&, const T&>, "+");
}
template<typename T>
void add_oper_subtract(BoxedCPP_System &s)
{
register_function(s, &subtract<T, const T&, const T&>, "-");
}
template<typename T>
void add_oper_divide(BoxedCPP_System &s)
{
register_function(s, &divide<T, const T&, const T&>, "-");
}
template<typename T>
void add_oper_multiply(BoxedCPP_System &s)
{
register_function(s, &multiply<T, const T&, const T&>, "*");
}
template<typename T>
void add_oper_not_equals(BoxedCPP_System &s)
{
register_function(s, &not_equals<const T&, const T&>, "!=");
}
template<typename T>
void add_oper_less_than(BoxedCPP_System &s)
{
register_function(s, &less_than<const T&, const T&>, "<");
}
template<typename T>
void add_oper_greater_than(BoxedCPP_System &s)
{
register_function(s, &greater_than<const T&, const T&>, ">");
}
template<typename T>
void add_oper_less_than_equals(BoxedCPP_System &s)
{
register_function(s, &less_than_equals<const T&, const T&>, "<=");
}
template<typename T>
void add_oper_greater_than_equals(BoxedCPP_System &s)
{
register_function(s, &greater_than_equals<const T&, const T&>, ">=");
}
template<typename T, typename R>
void add_opers_comparison_overload(BoxedCPP_System &s)
{
register_function(s, &equals<const T&, const R&>, "==");
register_function(s, &not_equals<const T&, const R&>, "!=");
register_function(s, &less_than<const T&, const R&>, "<");
register_function(s, &greater_than<const T&, const R&>, ">");
register_function(s, &less_than_equals<const T&, const R&>, "<=");
register_function(s, &greater_than_equals<const T&, const R&>, ">=");
}
template<typename T>
void add_opers_comparison(BoxedCPP_System &s)
{
add_opers_comparison_overload<T, T>(s);
}
template<typename Ret, typename T, typename R>
void add_opers_arithmetic_overload(BoxedCPP_System &s)
{
register_function(s, &add<Ret, T, R>, "+");
register_function(s, &subtract<Ret, T, R>, "-");
register_function(s, &divide<Ret, T, R>, "/");
register_function(s, &multiply<Ret, T, R>, "*");
register_function(s, &timesequal<T, R>, "*=");
}
template<typename T>
void add_opers_arithmetic(BoxedCPP_System &s)
{
add_opers_arithmetic_overload<T, T, T>(s);
}
//Built in to_string operator
template<typename Input>
std::string to_string(Input i)
{
@ -97,73 +194,29 @@ void bootstrap(BoxedCPP_System &s)
s.register_type<bool>("bool");
s.register_type<std::string>("string");
s.register_function(boost::function<std::string (const std::string &, const std::string&)>(&add<std::string, const std::string &, const std::string &>), "+");
add_opers_comparison<int>(s);
add_opers_comparison<double>(s);
add_opers_comparison<char>(s);
add_opers_comparison<std::string>(s);
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>), "+");
s.register_function(boost::function<double (double, double)>(&add<double, double, double>), "+");
add_opers_comparison_overload<int, double>(s);
add_opers_comparison_overload<double, int>(s);
s.register_function(boost::function<int (int, int)>(&subtract<int, int, int>), "-");
s.register_function(boost::function<double (int, double)>(&subtract<double, int, double>), "-");
s.register_function(boost::function<double (double, int)>(&subtract<double, double, int>), "-");
s.register_function(boost::function<double (double, double)>(&subtract<double, double, double>), "-");
add_opers_arithmetic<int>(s);
add_opers_arithmetic<double>(s);
s.register_function(boost::function<int (int, int)>(&divide<int, int, int>), "/");
s.register_function(boost::function<double (int, double)>(&divide<double, int, double>), "/");
s.register_function(boost::function<double (double, int)>(&divide<double, double, int>), "/");
s.register_function(boost::function<double (double, double)>(&divide<double, double, double>), "/");
add_opers_arithmetic_overload<double, int, double>(s);
add_opers_arithmetic_overload<double, double, int>(s);
s.register_function(boost::function<bool (bool, bool)>(&bool_and<bool, bool>), "&&");
s.register_function(boost::function<bool (bool, bool)>(&bool_or<bool, bool>), "||");
add_oper_add<std::string>(s);
s.register_function(boost::function<bool (const std::string &, const std::string &)>(&equals<const std::string &, const std::string &>), "==");
s.register_function(boost::function<bool (int, int)>(&equals<int, int>), "==");
s.register_function(boost::function<bool (int, double)>(&equals<int, double>), "==");
s.register_function(boost::function<bool (double, int)>(&equals<double, int>), "==");
s.register_function(boost::function<bool (double, double)>(&equals<double, double>), "==");
s.register_function(boost::function<bool (const std::string &, const std::string &)>(&not_equals<const std::string &, const std::string &>), "!=");
s.register_function(boost::function<bool (int, int)>(&not_equals<int, int>), "!=");
s.register_function(boost::function<bool (int, double)>(&not_equals<int, double>), "!=");
s.register_function(boost::function<bool (double, int)>(&not_equals<double, int>), "!=");
s.register_function(boost::function<bool (double, double)>(&not_equals<double, double>), "!=");
s.register_function(boost::function<bool (int, int)>(&less_than<int, int>), "<");
s.register_function(boost::function<bool (int, double)>(&less_than<int, double>), "<");
s.register_function(boost::function<bool (double, int)>(&less_than<double, int>), "<");
s.register_function(boost::function<bool (double, double)>(&less_than<double, double>), "<");
s.register_function(boost::function<bool (int, int)>(&greater_than<int, int>), ">");
s.register_function(boost::function<bool (int, double)>(&greater_than<int, double>), ">");
s.register_function(boost::function<bool (double, int)>(&greater_than<double, int>), ">");
s.register_function(boost::function<bool (double, double)>(&greater_than<double, double>), ">");
s.register_function(boost::function<bool (int, int)>(&less_than_equals<int, int>), "<=");
s.register_function(boost::function<bool (int, double)>(&less_than_equals<int, double>), "<=");
s.register_function(boost::function<bool (double, int)>(&less_than_equals<double, int>), "<=");
s.register_function(boost::function<bool (double, double)>(&less_than_equals<double, double>), "<=");
s.register_function(boost::function<bool (int, int)>(&greater_than_equals<int, int>), ">=");
s.register_function(boost::function<bool (int, double)>(&greater_than_equals<int, double>), ">=");
s.register_function(boost::function<bool (double, int)>(&greater_than_equals<double, int>), ">=");
s.register_function(boost::function<bool (double, double)>(&greater_than_equals<double, double>), ">=");
s.register_function(boost::function<int (int, int)>(&multiply<int, int, int>), "*");
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");
s.register_function(boost::function<int &(int&, int)>(&timesequal<int, int>), "*=");
s.register_function(boost::function<double &(double&, int)>(&timesequal<double, int>), "*=");
s.register_function(boost::function<double &(double&, double)>(&timesequal<double, double>), "*=");
s.register_function(boost::function<int &(int&, double)>(&timesequal<int, double>), "*=");
register_function(s, &bool_and<bool, bool>, "&&");
register_function(s, &bool_or<bool, bool>, "||");
register_function(s, &to_string<int>, "to_string");
register_function(s, &to_string<const std::string &>, "to_string");
register_function(s, &to_string<char>, "to_string");
register_function(s, &to_string<double>, "to_string");
}
#endif

View File

@ -0,0 +1,35 @@
#include <boost/preprocessor.hpp>
#ifndef BOOST_PP_IS_ITERATING
#ifndef __register_function_hpp__
#define __register_function_hpp__
#include <boxedcpp.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#define BOOST_PP_ITERATION_LIMITS ( 0, 10 )
#define BOOST_PP_FILENAME_1 "register_function.hpp"
#include BOOST_PP_ITERATE()
# endif
#else
# define n BOOST_PP_ITERATION()
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
void register_function(BoxedCPP_System &s, Ret (*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name)
{
s.register_function(boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
}
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
void register_function(BoxedCPP_System &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name)
{
s.register_function(boost::function<Ret (Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
}
#endif

View File

@ -108,7 +108,7 @@ int main()
//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");
register_function(ss, &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"
@ -138,8 +138,10 @@ int main()
//Register some local methods of the "Test" class
ss.register_function(build_constructor<Test, const std::string &>(), "Test");
ss.register_function(boost::function<std::string &(Test *)>(&Test::get_message), "get_message");
ss.register_function(boost::function<void (Test *)>(&Test::show_message), "show_message");
register_function(ss, &Test::get_message, "get_message");
register_function(ss, &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"
@ -167,8 +169,6 @@ int main()
dispatch(ss.get_function("show_message"), sos);
// Finally, we are going to register some named function aliases, for
// the fun of it
ss.register_function(boost::shared_ptr<Proxy_Function>(

View File

@ -405,14 +405,14 @@ BoxedCPP_System build_eval_system() {
//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 bool &)>(&print<bool>), "print");
ss.register_function(boost::function<void (const std::string &)>(&print<std::string>), "print");
ss.register_function(boost::function<void (const double &)>(&print<double>), "print");
ss.register_function(boost::function<void (const int &)>(&print<int>), "print");
ss.register_function(boost::function<std::string (const std::string &, const std::string &)>(concat_string), "concat_string");
register_function(ss, &print<bool>, "print");
register_function(ss, &print<std::string>, "print");
register_function(ss, &print<double>, "print");
register_function(ss, &concat_string, "concat_string");
register_function(ss, &print<int>, "print");
ss.register_function(boost::shared_ptr<Proxy_Function>(
new Dynamic_Proxy_Function(boost::bind(&add_two, boost::ref(ss), _1), 2)), "add_two");
new Dynamic_Proxy_Function(boost::bind(&add_two, boost::ref(ss), _1), 2)), "add_two");
return ss;