diff --git a/boxedcpp/bootstrap.hpp b/boxedcpp/bootstrap.hpp index 116b191..b4cc3d6 100644 --- a/boxedcpp/bootstrap.hpp +++ b/boxedcpp/bootstrap.hpp @@ -2,6 +2,7 @@ #define __bootstrap_hpp__ #include "boxedcpp.hpp" +#include "register_function.hpp" template Ret add(P1 p1, P2 p2) @@ -82,6 +83,102 @@ P1 ×equal(P1 &p1, P2 p2) return (p1 *= p2); } +//Add canonical forms of operators +template +void add_oper_equals(BoxedCPP_System &s) +{ + register_function(s, &equals, "="); +} + +template +void add_oper_add(BoxedCPP_System &s) +{ + register_function(s, &add, "+"); +} + +template +void add_oper_subtract(BoxedCPP_System &s) +{ + register_function(s, &subtract, "-"); +} + +template +void add_oper_divide(BoxedCPP_System &s) +{ + register_function(s, ÷, "-"); +} + +template +void add_oper_multiply(BoxedCPP_System &s) +{ + register_function(s, &multiply, "*"); +} + +template +void add_oper_not_equals(BoxedCPP_System &s) +{ + register_function(s, ¬_equals, "!="); +} + +template +void add_oper_less_than(BoxedCPP_System &s) +{ + register_function(s, &less_than, "<"); +} + +template +void add_oper_greater_than(BoxedCPP_System &s) +{ + register_function(s, &greater_than, ">"); +} + +template +void add_oper_less_than_equals(BoxedCPP_System &s) +{ + register_function(s, &less_than_equals, "<="); +} + +template +void add_oper_greater_than_equals(BoxedCPP_System &s) +{ + register_function(s, &greater_than_equals, ">="); +} + + +template +void add_opers_comparison_overload(BoxedCPP_System &s) +{ + register_function(s, &equals, "=="); + register_function(s, ¬_equals, "!="); + register_function(s, &less_than, "<"); + register_function(s, &greater_than, ">"); + register_function(s, &less_than_equals, "<="); + register_function(s, &greater_than_equals, ">="); +} + +template +void add_opers_comparison(BoxedCPP_System &s) +{ + add_opers_comparison_overload(s); +} + +template +void add_opers_arithmetic_overload(BoxedCPP_System &s) +{ + register_function(s, &add, "+"); + register_function(s, &subtract, "-"); + register_function(s, ÷, "/"); + register_function(s, &multiply, "*"); + register_function(s, ×equal, "*="); +} + +template +void add_opers_arithmetic(BoxedCPP_System &s) +{ + add_opers_arithmetic_overload(s); +} + +//Built in to_string operator template std::string to_string(Input i) { @@ -97,73 +194,29 @@ void bootstrap(BoxedCPP_System &s) s.register_type("bool"); s.register_type("string"); - s.register_function(boost::function(&add), "+"); + add_opers_comparison(s); + add_opers_comparison(s); + add_opers_comparison(s); + add_opers_comparison(s); - s.register_function(boost::function(&add), "+"); - s.register_function(boost::function(&add), "+"); - s.register_function(boost::function(&add), "+"); - s.register_function(boost::function(&add), "+"); + add_opers_comparison_overload(s); + add_opers_comparison_overload(s); - s.register_function(boost::function(&subtract), "-"); - s.register_function(boost::function(&subtract), "-"); - s.register_function(boost::function(&subtract), "-"); - s.register_function(boost::function(&subtract), "-"); + add_opers_arithmetic(s); + add_opers_arithmetic(s); - s.register_function(boost::function), "/"); - s.register_function(boost::function), "/"); - s.register_function(boost::function), "/"); - s.register_function(boost::function), "/"); + add_opers_arithmetic_overload(s); + add_opers_arithmetic_overload(s); - s.register_function(boost::function(&bool_and), "&&"); - s.register_function(boost::function(&bool_or), "||"); + add_oper_add(s); - s.register_function(boost::function(&equals), "=="); - s.register_function(boost::function(&equals), "=="); - s.register_function(boost::function(&equals), "=="); - s.register_function(boost::function(&equals), "=="); - s.register_function(boost::function(&equals), "=="); - - s.register_function(boost::function(¬_equals), "!="); - s.register_function(boost::function(¬_equals), "!="); - s.register_function(boost::function(¬_equals), "!="); - s.register_function(boost::function(¬_equals), "!="); - s.register_function(boost::function(¬_equals), "!="); - - s.register_function(boost::function(&less_than), "<"); - s.register_function(boost::function(&less_than), "<"); - s.register_function(boost::function(&less_than), "<"); - s.register_function(boost::function(&less_than), "<"); - - s.register_function(boost::function(&greater_than), ">"); - s.register_function(boost::function(&greater_than), ">"); - s.register_function(boost::function(&greater_than), ">"); - s.register_function(boost::function(&greater_than), ">"); - - s.register_function(boost::function(&less_than_equals), "<="); - s.register_function(boost::function(&less_than_equals), "<="); - s.register_function(boost::function(&less_than_equals), "<="); - s.register_function(boost::function(&less_than_equals), "<="); - - s.register_function(boost::function(&greater_than_equals), ">="); - s.register_function(boost::function(&greater_than_equals), ">="); - s.register_function(boost::function(&greater_than_equals), ">="); - s.register_function(boost::function(&greater_than_equals), ">="); - - s.register_function(boost::function(&multiply), "*"); - 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"); - - s.register_function(boost::function(×equal), "*="); - s.register_function(boost::function(×equal), "*="); - s.register_function(boost::function(×equal), "*="); - s.register_function(boost::function(×equal), "*="); + register_function(s, &bool_and, "&&"); + register_function(s, &bool_or, "||"); + register_function(s, &to_string, "to_string"); + register_function(s, &to_string, "to_string"); + register_function(s, &to_string, "to_string"); + register_function(s, &to_string, "to_string"); } #endif diff --git a/boxedcpp/register_function.hpp b/boxedcpp/register_function.hpp new file mode 100644 index 0000000..bba2216 --- /dev/null +++ b/boxedcpp/register_function.hpp @@ -0,0 +1,35 @@ +#include + +#ifndef BOOST_PP_IS_ITERATING +#ifndef __register_function_hpp__ +#define __register_function_hpp__ + +#include +#include +#include + + +#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 +void register_function(BoxedCPP_System &s, Ret (*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name) +{ + s.register_function(boost::function(f), name); +} + +template +void register_function(BoxedCPP_System &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name) +{ + s.register_function(boost::function(f), name); +} + + +#endif + diff --git a/boxedcpp/test.cpp b/boxedcpp/test.cpp index 340c5eb..02b313c 100644 --- a/boxedcpp/test.cpp +++ b/boxedcpp/test.cpp @@ -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(&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"); - ss.register_function(boost::function(&Test::get_message), "get_message"); - ss.register_function(boost::function(&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( diff --git a/wesley/main.cpp b/wesley/main.cpp index 9dc9bfd..37cdba1 100644 --- a/wesley/main.cpp +++ b/wesley/main.cpp @@ -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(&print), "print"); - ss.register_function(boost::function(&print), "print"); - ss.register_function(boost::function(&print), "print"); - ss.register_function(boost::function(&print), "print"); - ss.register_function(boost::function(concat_string), "concat_string"); + register_function(ss, &print, "print"); + register_function(ss, &print, "print"); + register_function(ss, &print, "print"); + register_function(ss, &concat_string, "concat_string"); + register_function(ss, &print, "print"); ss.register_function(boost::shared_ptr( - 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;