diff --git a/typelesscpp/scripting_constructors.hpp b/typelesscpp/scripting_constructors.hpp index b850bf7..8979f0a 100644 --- a/typelesscpp/scripting_constructors.hpp +++ b/typelesscpp/scripting_constructors.hpp @@ -1,29 +1,19 @@ +#include + +#ifndef BOOST_PP_IS_ITERATING #ifndef __scripting_constructors_hpp__ -#define __scripting_constrcutors_hpp__ +#define __scripting_constructors_hpp__ #include #include #include -//Constructors, to be made variadic template boost::shared_ptr constructor() { return boost::shared_ptr(new Class()); } -template -boost::shared_ptr constructor(Param1 p1) -{ - return boost::shared_ptr(new Class(p1)); -} - -template -boost::shared_ptr constructor(Param1 p1, Param2 p2) -{ - return boost::shared_ptr(new Class(p1, p2)); -} - template boost::function ()> build_constructor() { @@ -31,20 +21,26 @@ boost::function ()> build_constructor() return boost::function ()>(func(&(constructor))); } -template -boost::function (Param1)> build_constructor() -{ - typedef boost::shared_ptr (*func)(Param1); - return boost::function (Param1)>(func(&(constructor))); -} +#define BOOST_PP_ITERATION_LIMITS ( 1, 10 ) +#define BOOST_PP_FILENAME_1 "scripting_constructors.hpp" +#include BOOST_PP_ITERATE() +# endif +#else +# define n BOOST_PP_ITERATION() + -template -boost::function (Param1, Param2)> build_constructor() +template +boost::shared_ptr constructor( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) ) { - typedef boost::shared_ptr (*func)(Param1, Param2); - return boost::function (Param1, Param2)>(func(&(constructor))); + return boost::shared_ptr(new Class( BOOST_PP_ENUM_PARAMS(n, p) )); +} + +template +boost::function (BOOST_PP_ENUM_PARAMS(n, Param))> build_constructor() +{ + typedef boost::shared_ptr (*func)(BOOST_PP_ENUM_PARAMS(n, Param)); + return boost::function (BOOST_PP_ENUM_PARAMS(n, Param))>(func(&(constructor))); } - #endif diff --git a/typelesscpp/scripting_functions.hpp b/typelesscpp/scripting_functions.hpp index f046326..16ad365 100644 --- a/typelesscpp/scripting_functions.hpp +++ b/typelesscpp/scripting_functions.hpp @@ -1,6 +1,12 @@ -#ifndef __scripting_function_hpp__ -#define __scripting_function_hpp__ +#include +#define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info()()); +#define casthelper(z,n,text) ,Cast_Helper()(params[n]) + + +#ifndef BOOST_PP_IS_ITERATING +#ifndef __scripting_functions_hpp__ +#define __scripting_functions_hpp__ #include "scripting_object.hpp" #include "scripting_type_info.hpp" @@ -10,29 +16,6 @@ #include #include -template -std::vector build_param_type_list(const boost::function &f) -{ - return std::vector(); -} - -template -std::vector build_param_type_list(const boost::function &f) -{ - std::vector ti; - ti.push_back(Get_Type_Info()()); - return ti; -} - -template -std::vector build_param_type_list(const boost::function &f) -{ - std::vector ti; - ti.push_back(Get_Type_Info()()); - ti.push_back(Get_Type_Info()()); - return ti; -} - // handle_return implementations template struct Handle_Return @@ -63,31 +46,16 @@ struct Handle_Return }; - -// call_func implementations todo: handle reference return types -// to be made variadic -template -Scripting_Object call_func(const boost::function &f, const std::vector ¶ms) +// Build param type list (variadic) +template +std::vector build_param_type_list(const boost::function &f) { - if (params.size() != 2) - { - throw std::range_error("Incorrect number of parameters"); - } else { - return Handle_Return()(boost::bind(f, Cast_Helper()(params[0]), Cast_Helper()(params[1]))); - } -} - -template -Scripting_Object call_func(const boost::function &f, const std::vector ¶ms) -{ - if (params.size() != 1) - { - throw std::range_error("Incorrect number of parameters"); - } else { - return Handle_Return()(boost::bind(f, Cast_Helper()(params[0]))); - } + std::vector ti; + ti.push_back(Get_Type_Info()()); + return ti; } +// call_func implementations (variadic) template Scripting_Object call_func(const boost::function &f, const std::vector ¶ms) { @@ -100,6 +68,34 @@ Scripting_Object call_func(const boost::function &f, const std::vector + Param_List_Builder &operator<<(T t) + { + objects.push_back(Scripting_Object(t)); + return *this; + } + + operator const std::vector &() const + { + return objects; + } + + std::vector objects; + +}; + + +#define BOOST_PP_ITERATION_LIMITS ( 1, 10 ) +#define BOOST_PP_FILENAME_1 "scripting_functions.hpp" +#include BOOST_PP_ITERATE() class Function_Handler { @@ -132,26 +128,33 @@ class Function_Handler_Impl : public Function_Handler Func m_f; }; -std::vector build_param_list(const Scripting_Object &so) + + +# endif +#else +# define n BOOST_PP_ITERATION() + +template +std::vector build_param_type_list(const boost::function &f) { - std::vector sos; - sos.push_back(so); - return sos; + std::vector ti; + ti.push_back(Get_Type_Info()()); + + BOOST_PP_REPEAT(n, gettypeinfo, ~) + + return ti; } -std::vector build_param_list(const Scripting_Object &so1, const Scripting_Object &so2) + +template +Scripting_Object call_func(const boost::function &f, + const std::vector ¶ms) { - std::vector sos; - sos.push_back(so1); - sos.push_back(so2); - return sos; -} -std::vector build_param_list(const Scripting_Object &so1, const Scripting_Object &so2, const Scripting_Object &so3) -{ - std::vector sos; - sos.push_back(so1); - sos.push_back(so2); - sos.push_back(so3); - return sos; + if (params.size() != n) + { + throw std::range_error("Incorrect number of parameters"); + } else { + return Handle_Return()(boost::bind(f BOOST_PP_REPEAT(n, casthelper, ~))); + } } #endif diff --git a/typelesscpp/test.cpp b/typelesscpp/test.cpp index fbcd041..d0b2505 100644 --- a/typelesscpp/test.cpp +++ b/typelesscpp/test.cpp @@ -78,10 +78,10 @@ int main() (*method1)(sos); Scripting_Object o = (*method1)(sos); - (*ss.get_function("print"))(build_param_list(ss.get_object("str"))); + (*ss.get_function("print"))(Param_List_Builder() << ss.get_object("str")); //Add new dynamically created object from registered "Test" constructor - ss.add_object("testobj2", (*ss.get_function("Test"))(build_param_list(Scripting_Object(std::string("Yo"))))); + ss.add_object("testobj2", (*ss.get_function("Test"))(Param_List_Builder() << std::string("Yo"))); std::cout << Cast_Helper()(o) << std::endl;