#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" #include #include #include #include #include // handle_return implementations template struct Handle_Return { Scripting_Object operator()(const boost::function &f) { return Scripting_Object(f()); } }; template struct Handle_Return { Scripting_Object operator()(const boost::function &f) { return Scripting_Object(boost::ref(f())); } }; template<> struct Handle_Return { Scripting_Object operator()(const boost::function &f) { f(); return Scripting_Object(); } }; // Build param type list (variadic) template std::vector build_param_type_list(const boost::function &f) { 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) { if (params.size() != 0) { throw std::range_error("Incorrect number of parameters"); } else { return Handle_Return()(f); } } struct Param_List_Builder { Param_List_Builder &operator<<(const Scripting_Object &so) { objects.push_back(so); return *this; } template 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 { public: virtual Scripting_Object operator()(const std::vector ¶ms) = 0; virtual std::vector get_param_types() = 0; }; template class Function_Handler_Impl : public Function_Handler { public: Function_Handler_Impl(const Func &f) : m_f(f) { } virtual Scripting_Object operator()(const std::vector ¶ms) { return call_func(m_f, params); } virtual std::vector get_param_types() { return build_param_type_list(m_f); } private: Func m_f; }; # endif #else # define n BOOST_PP_ITERATION() template std::vector build_param_type_list(const boost::function &f) { std::vector ti; ti.push_back(Get_Type_Info()()); BOOST_PP_REPEAT(n, gettypeinfo, ~) return ti; } template Scripting_Object call_func(const boost::function &f, const std::vector ¶ms) { 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