Add arity registration for dynamic functions
This commit is contained in:
parent
5799d7f0ef
commit
37a36f783c
@ -107,8 +107,8 @@ class Proxy_Function
|
|||||||
class Dynamic_Proxy_Function : public Proxy_Function
|
class Dynamic_Proxy_Function : public Proxy_Function
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Dynamic_Proxy_Function(const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f)
|
Dynamic_Proxy_Function(const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f, int arity=-1)
|
||||||
: m_f(t_f)
|
: 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<Boxed_Value> ¶ms)
|
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶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<Type_Info> get_param_types()
|
virtual std::vector<Type_Info> get_param_types()
|
||||||
@ -126,6 +131,7 @@ class Dynamic_Proxy_Function : public Proxy_Function
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
|
boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
|
||||||
|
int m_arity;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Func>
|
template<typename Func>
|
||||||
@ -165,6 +171,8 @@ Boxed_Value dispatch(const std::vector<std::pair<const std::string, boost::share
|
|||||||
return (*itr->second)(plist);
|
return (*itr->second)(plist);
|
||||||
} catch (const std::bad_cast &) {
|
} catch (const std::bad_cast &) {
|
||||||
//try again
|
//try again
|
||||||
|
} catch (const std::range_error &) {
|
||||||
|
//invalid num params, try again
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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<Boxed_Value> &vals) {
|
const Boxed_Value add_two(BoxedCPP_System &ss, const std::vector<Boxed_Value> &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);
|
return dispatch(ss.get_function("+"), vals);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -414,7 +404,7 @@ BoxedCPP_System build_eval_system() {
|
|||||||
ss.register_function(boost::function<std::string (const std::string &, const std::string &)>(concat_string), "concat_string");
|
ss.register_function(boost::function<std::string (const std::string &, const std::string &)>(concat_string), "concat_string");
|
||||||
|
|
||||||
ss.register_function(boost::shared_ptr<Proxy_Function>(
|
ss.register_function(boost::shared_ptr<Proxy_Function>(
|
||||||
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;
|
return ss;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user