#include #define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info::get()); #define casthelper(z,n,text) ,dispatchkit::boxed_cast< Param ## n >(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 #include namespace dispatchkit { // 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()); } }; 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; }; struct arity_error : std::range_error { arity_error(int t_got, int t_expected) : std::range_error("Function dispatch arity mismatch"), got(t_got), expected(t_expected) { } virtual ~arity_error() throw() {} int got; int expected; }; } #define BOOST_PP_ITERATION_LIMITS ( 0, 10 ) #define BOOST_PP_FILENAME_1 "proxy_functions.hpp" #include BOOST_PP_ITERATE() namespace dispatchkit { class Proxy_Function { public: virtual ~Proxy_Function() {} virtual Boxed_Value operator()(const std::vector ¶ms) = 0; virtual std::vector get_param_types() = 0; virtual bool operator==(const Proxy_Function &) const = 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) { } bool operator==(const Proxy_Function &f) const { return false; } 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 arity_error(params.size(), m_arity); } } 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 bool operator==(const Proxy_Function &t_func) const { try { dynamic_cast &>(t_func); return true; } catch (const std::bad_cast &) { return false; } } 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; }; struct dispatch_error : std::runtime_error { dispatch_error() throw() : std::runtime_error("No matching function to dispatch to") { } virtual ~dispatch_error() throw() {} }; 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 bad_boxed_cast &) { //try again } catch (const arity_error &) { //invalid num params, try again } } throw dispatch_error(); } } # endif #else # define n BOOST_PP_ITERATION() namespace dispatchkit { 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 arity_error(params.size(), n); } else { return Handle_Return()(boost::bind(f BOOST_PP_REPEAT(n, casthelper, ~))); } } } #endif