diff --git a/boxedcpp/proxy_functions.hpp b/boxedcpp/proxy_functions.hpp index cb2d6cf..4758975 100644 --- a/boxedcpp/proxy_functions.hpp +++ b/boxedcpp/proxy_functions.hpp @@ -107,8 +107,8 @@ class Proxy_Function class Dynamic_Proxy_Function : public Proxy_Function { public: - Dynamic_Proxy_Function(const boost::function &)> &t_f) - : m_f(t_f) + Dynamic_Proxy_Function(const boost::function &)> &t_f, int arity=-1) + : m_f(t_f), m_arity(arity) { } @@ -116,7 +116,12 @@ class Dynamic_Proxy_Function : public Proxy_Function virtual Boxed_Value operator()(const std::vector ¶ms) { - return m_f(params); + 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() @@ -126,6 +131,7 @@ class Dynamic_Proxy_Function : public Proxy_Function private: boost::function &)> m_f; + int m_arity; }; template @@ -165,6 +171,8 @@ Boxed_Value dispatch(const std::vectorsecond)(plist); } catch (const std::bad_cast &) { //try again + } catch (const std::range_error &) { + //invalid num params, try again } } diff --git a/wesley/main.cpp b/wesley/main.cpp index 26bb891..58bcd71 100644 --- a/wesley/main.cpp +++ b/wesley/main.cpp @@ -82,16 +82,6 @@ std::string concat_string(const std::string &s1, const std::string &s2) { } const Boxed_Value add_two(BoxedCPP_System &ss, const std::vector &vals) { - //We can check the arity if we want, or in this case just allow the dispatch - //to do it for us: - if (vals.size() != 2) - { - //I very much need to do a cleanup and provide a custom set of - //bad_cast and wrong_arity exceptions so they can be caught more - //cleanly - throw std::runtime_error("Wrong number of args"); - } - return dispatch(ss.get_function("+"), vals); } @@ -414,7 +404,7 @@ BoxedCPP_System build_eval_system() { ss.register_function(boost::function(concat_string), "concat_string"); ss.register_function(boost::shared_ptr( - new Dynamic_Proxy_Function(boost::bind(&add_two, boost::ref(ss), _1))), "add_two"); + new Dynamic_Proxy_Function(boost::bind(&add_two, boost::ref(ss), _1), 2)), "add_two"); return ss;