Handle user defined conversions of return types from functor

This commit is contained in:
Jason Turner
2015-07-04 08:43:52 -06:00
parent 484ff7a98b
commit 37120f486f
4 changed files with 12 additions and 23 deletions

View File

@@ -36,7 +36,7 @@ namespace chaiscript
} }
bad_boxed_cast(Type_Info t_from, const std::type_info &t_to) bad_boxed_cast(Type_Info t_from, const std::type_info &t_to)
: from(std::move(t_from)), to(&t_to), m_what("Cannot perform boxed_cast") : from(std::move(t_from)), to(&t_to), m_what("Cannot perform boxed_cast: " + t_from.name() + " to: " + t_to.name())
{ {
} }

View File

@@ -79,13 +79,6 @@ namespace chaiscript
} }
#ifdef CHAISCRIPT_MSVC
//Thank you MSVC, yes we know that a constant value is being used in the if
// statment in THIS VERSION of the template instantiation
#pragma warning(push)
#pragma warning(disable : 4127)
#endif
if (t_conversions && t_conversions->convertable_type<Type>()) if (t_conversions && t_conversions->convertable_type<Type>())
{ {
try { try {
@@ -108,10 +101,6 @@ namespace chaiscript
throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type)); throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type));
} }
#ifdef CHAISCRIPT_MSVC
#pragma warning(pop)
#endif
} }
} }

View File

@@ -31,9 +31,9 @@ namespace chaiscript
struct Function_Caller_Ret struct Function_Caller_Ret
{ {
static Ret call(const std::vector<Const_Proxy_Function> &t_funcs, static Ret call(const std::vector<Const_Proxy_Function> &t_funcs,
const std::vector<Boxed_Value> &params, const Type_Conversions &t_conversions) const std::vector<Boxed_Value> &params, const Type_Conversions *t_conversions)
{ {
return boxed_cast<Ret>(dispatch::dispatch(t_funcs, params, t_conversions)); return boxed_cast<Ret>(dispatch::dispatch(t_funcs, params, t_conversions?*t_conversions:Type_Conversions()), t_conversions);
} }
}; };
@@ -44,9 +44,9 @@ namespace chaiscript
struct Function_Caller_Ret<Ret, true> struct Function_Caller_Ret<Ret, true>
{ {
static Ret call(const std::vector<Const_Proxy_Function> &t_funcs, static Ret call(const std::vector<Const_Proxy_Function> &t_funcs,
const std::vector<Boxed_Value> &params, const Type_Conversions &t_conversions) const std::vector<Boxed_Value> &params, const Type_Conversions *t_conversions)
{ {
return Boxed_Number(dispatch::dispatch(t_funcs, params, t_conversions)).get_as<Ret>(); return Boxed_Number(dispatch::dispatch(t_funcs, params, t_conversions?*t_conversions:Type_Conversions())).get_as<Ret>();
} }
}; };
@@ -58,9 +58,9 @@ namespace chaiscript
struct Function_Caller_Ret<void, false> struct Function_Caller_Ret<void, false>
{ {
static void call(const std::vector<Const_Proxy_Function> &t_funcs, static void call(const std::vector<Const_Proxy_Function> &t_funcs,
const std::vector<Boxed_Value> &params, const Type_Conversions &t_conversions) const std::vector<Boxed_Value> &params, const Type_Conversions *t_conversions)
{ {
dispatch::dispatch(t_funcs, params, t_conversions); dispatch::dispatch(t_funcs, params, t_conversions?*t_conversions:Type_Conversions());
} }
}; };
@@ -70,7 +70,7 @@ namespace chaiscript
template<typename Ret, typename ... Param> template<typename Ret, typename ... Param>
struct Build_Function_Caller_Helper struct Build_Function_Caller_Helper
{ {
Build_Function_Caller_Helper(std::vector<Const_Proxy_Function> t_funcs, const Type_Conversions &t_conversions) Build_Function_Caller_Helper(std::vector<Const_Proxy_Function> t_funcs, const Type_Conversions *t_conversions)
: m_funcs(std::move(t_funcs)), : m_funcs(std::move(t_funcs)),
m_conversions(t_conversions) m_conversions(t_conversions)
{ {
@@ -107,11 +107,12 @@ namespace chaiscript
std::vector<Const_Proxy_Function> m_funcs; std::vector<Const_Proxy_Function> m_funcs;
Type_Conversions m_conversions; const Type_Conversions *m_conversions;
}; };
/// \todo what happens if t_conversions is deleted out from under us?!
template<typename Ret, typename ... Params> template<typename Ret, typename ... Params>
std::function<Ret (Params...)> build_function_caller_helper(Ret (Params...), const std::vector<Const_Proxy_Function> &funcs, const Type_Conversions *t_conversions) std::function<Ret (Params...)> build_function_caller_helper(Ret (Params...), const std::vector<Const_Proxy_Function> &funcs, const Type_Conversions *t_conversions)
{ {
@@ -131,7 +132,7 @@ namespace chaiscript
} }
*/ */
return std::function<Ret (Params...)>(Build_Function_Caller_Helper<Ret, Params...>(funcs, t_conversions?*t_conversions:Type_Conversions())); return std::function<Ret (Params...)>(Build_Function_Caller_Helper<Ret, Params...>(funcs, t_conversions));
} }
} }
} }

View File

@@ -337,7 +337,7 @@ namespace chaiscript
Type_Conversions(const Type_Conversions &t_other) Type_Conversions(const Type_Conversions &t_other)
: m_mutex(), : m_mutex(),
m_conversions(t_other.get_conversions()), m_conversions(t_other.get_conversions()),
m_convertableTypes(), m_convertableTypes(t_other.m_convertableTypes),
m_num_types(m_conversions.size()), m_num_types(m_conversions.size()),
m_thread_cache(this), m_thread_cache(this),
m_conversion_saves(this) m_conversion_saves(this)
@@ -581,7 +581,6 @@ namespace chaiscript
auto func = [](const Boxed_Value &t_bv) -> Boxed_Value { auto func = [](const Boxed_Value &t_bv) -> Boxed_Value {
const std::vector<Boxed_Value> &from_vec = detail::Cast_Helper<const std::vector<Boxed_Value> &>::cast(t_bv, nullptr); const std::vector<Boxed_Value> &from_vec = detail::Cast_Helper<const std::vector<Boxed_Value> &>::cast(t_bv, nullptr);
To vec; To vec;
for (const Boxed_Value &bv : from_vec) { for (const Boxed_Value &bv : from_vec) {