Fix broken type conversion call implementation

- We need to properly order the function so that the one with the least
   number if type differences is the one that is tried first.
This commit is contained in:
Jason Turner
2014-11-01 15:52:02 -06:00
parent e2cf8a48be
commit 87cee688a8
2 changed files with 34 additions and 24 deletions

View File

@@ -314,7 +314,7 @@ namespace chaiscript
protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
{
return dispatch::dispatch(m_funcs.cbegin(), m_funcs.cend(), params, t_conversions);
return dispatch::dispatch(m_funcs, params, t_conversions);
}
private:
@@ -775,9 +775,7 @@ namespace chaiscript
Boxed_Value call_function(const std::string &t_name, const std::vector<Boxed_Value> &params) const
{
std::vector<Proxy_Function> functions = get_function(t_name);
return dispatch::dispatch(functions.begin(), functions.end(), params, m_conversions);
return dispatch::dispatch(get_function(t_name), params, m_conversions);
}
Boxed_Value call_function(const std::string &t_name) const

View File

@@ -712,17 +712,42 @@ namespace chaiscript
* each function against the set of parameters, in order, until a matching
* function is found or throw dispatch_error if no matching function is found
*/
template<typename InItr>
Boxed_Value dispatch(InItr begin, const InItr &end,
template<typename Funcs>
Boxed_Value dispatch(const Funcs &funcs,
const std::vector<Boxed_Value> &plist, const Type_Conversions &t_conversions)
{
InItr orig(begin);
while (begin != end)
std::multimap<int, const Proxy_Function_Base *> ordered_funcs;
for (const auto &func : funcs)
{
int numdiffs = 0;
const auto arity = func->get_arity();
if (arity == -1)
{
numdiffs = plist.size();
} else if (arity == plist.size()) {
for (size_t i = 0; i < plist.size(); ++i)
{
if (!func->get_param_types()[i+1].bare_equal(plist[i].get_type_info()))
{
++numdiffs;
}
}
} else {
continue;
}
ordered_funcs.insert(std::make_pair(numdiffs, func.get()));
}
for (const auto &func : ordered_funcs )
{
try {
if ((*begin)->filter(plist, t_conversions))
if (func.second->filter(plist, t_conversions))
{
return (*(*begin))(plist, t_conversions);
return (*(func.second))(plist, t_conversions);
}
} catch (const exception::bad_boxed_cast &) {
//parameter failed to cast, try again
@@ -732,22 +757,9 @@ namespace chaiscript
//guard failed to allow the function to execute,
//try again
}
++begin;
}
return detail::dispatch_with_conversions(orig, end, plist, t_conversions);
}
/**
* Take a vector of functions and a vector of parameters. Attempt to execute
* each function against the set of parameters, in order, until a matching
* function is found or throw dispatch_error if no matching function is found
*/
template<typename Funcs>
Boxed_Value dispatch(const Funcs &funcs,
const std::vector<Boxed_Value> &plist, const Type_Conversions &t_conversions)
{
return dispatch::dispatch(funcs.begin(), funcs.end(), plist, t_conversions);
return detail::dispatch_with_conversions(funcs.cbegin(), funcs.cend(), plist, t_conversions);
}
}
}