#include #define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info::get()); #define casthelper(z,n,text) ,Cast_Helper()(params[n]) #ifndef BOOST_PP_IS_ITERATING #ifndef __proxy_functions_hpp__ #define __proxy_functions_hpp__ #include "boxed_value.hpp" #include "type_info.hpp" #include #include #include #include #include // handle_return implementations template struct Handle_Return { Boxed_Value operator()(const boost::function &f) { return Boxed_Value(f()); } }; template struct Handle_Return { Boxed_Value operator()(const boost::function &f) { return Boxed_Value(boost::ref(f())); } }; template<> struct Handle_Return { Boxed_Value operator()(const boost::function &f) { return f(); } }; template<> struct Handle_Return { Boxed_Value operator()(const boost::function &f) { return f(); } }; template<> struct Handle_Return { Boxed_Value operator()(const boost::function &f) { f(); return Boxed_Value(Boxed_Value::Void_Type()); } }; // 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::get()); return ti; } // call_func implementations (variadic) template Boxed_Value 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 Boxed_Value &so) { objects.push_back(so); return *this; } template Param_List_Builder &operator<<(T t) { objects.push_back(Boxed_Value(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 "proxy_functions.hpp" #include BOOST_PP_ITERATE() class Proxy_Function { public: virtual ~Proxy_Function() {} virtual Boxed_Value operator()(const std::vector ¶ms) = 0; virtual std::vector get_param_types() = 0; }; class Dynamic_Proxy_Function : public Proxy_Function { public: Dynamic_Proxy_Function(const boost::function &)> &t_f, int arity=-1) : m_f(t_f), m_arity(arity) { } virtual ~Dynamic_Proxy_Function() {} virtual Boxed_Value operator()(const std::vector ¶ms) { if (m_arity < 0 || params.size() == size_t(m_arity)) { return m_f(params); } else { throw std::range_error("Incorrect number of parameters"); } } virtual std::vector get_param_types() { return build_param_type_list(m_f); } private: boost::function &)> m_f; int m_arity; }; template class Proxy_Function_Impl : public Proxy_Function { public: Proxy_Function_Impl(const Func &f) : m_f(f) { } virtual ~Proxy_Function_Impl() {} virtual Boxed_Value 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; }; Boxed_Value dispatch(const std::vector > > &funcs, const std::vector &plist) { for (std::vector > >::const_iterator itr = funcs.begin(); itr != funcs.end(); ++itr) { try { return (*itr->second)(plist); } catch (const std::bad_cast &) { //try again } catch (const std::range_error &) { //invalid num params, try again } } throw std::runtime_error("No matching function to dispatch to"); } # 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::get()); BOOST_PP_REPEAT(n, gettypeinfo, ~) return ti; } template Boxed_Value 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