Apply some if constexpr action

This commit is contained in:
Jason Turner 2017-11-17 06:12:50 -07:00
parent b03b90dee6
commit a6d30baa27
3 changed files with 28 additions and 82 deletions

View File

@ -29,66 +29,7 @@ namespace chaiscript
{ {
namespace detail namespace detail
{ {
/// Internal helper class for handling the return /// used internally for unwrapping a function call's types
/// value of a build_function_caller
template<typename Ret, bool is_arithmetic>
struct Function_Caller_Ret
{
static Ret call(const std::vector<Const_Proxy_Function> &t_funcs,
const Function_Params &params, const Type_Conversions_State *t_conversions)
{
if (t_conversions != nullptr) {
return boxed_cast<Ret>(dispatch::dispatch(t_funcs, params, *t_conversions), t_conversions);
} else {
Type_Conversions conv;
Type_Conversions_State state(conv, conv.conversion_saves());
return boxed_cast<Ret>(dispatch::dispatch(t_funcs, params, state), t_conversions);
}
}
};
/**
* Specialization for arithmetic return types
*/
template<typename Ret>
struct Function_Caller_Ret<Ret, true>
{
static Ret call(const std::vector<Const_Proxy_Function> &t_funcs,
const Function_Params &params, const Type_Conversions_State *t_conversions)
{
if (t_conversions != nullptr) {
return Boxed_Number(dispatch::dispatch(t_funcs, params, *t_conversions)).get_as<Ret>();
} else {
Type_Conversions conv;
Type_Conversions_State state(conv, conv.conversion_saves());
return Boxed_Number(dispatch::dispatch(t_funcs, params, state)).get_as<Ret>();
}
}
};
/**
* Specialization for void return types
*/
template<>
struct Function_Caller_Ret<void, false>
{
static void call(const std::vector<Const_Proxy_Function> &t_funcs,
const Function_Params &params, const Type_Conversions_State *t_conversions)
{
if (t_conversions != nullptr) {
dispatch::dispatch(t_funcs, params, *t_conversions);
} else {
Type_Conversions conv;
Type_Conversions_State state(conv, conv.conversion_saves());
dispatch::dispatch(t_funcs, params, state);
}
}
};
/**
* used internally for unwrapping a function call's types
*/
template<typename Ret, typename ... Param> template<typename Ret, typename ... Param>
struct Build_Function_Caller_Helper struct Build_Function_Caller_Helper
{ {
@ -98,6 +39,17 @@ namespace chaiscript
{ {
} }
Ret call(const Function_Params &params, const Type_Conversions_State &t_state)
{
if constexpr (std::is_arithmetic_v<Ret>) {
return Boxed_Number(dispatch::dispatch(m_funcs, params, t_state)).get_as<Ret>();
} else if constexpr (std::is_same_v<void, Ret>) {
dispatch::dispatch(m_funcs, params, t_state);
} else {
return boxed_cast<Ret>(dispatch::dispatch(m_funcs, params, t_state), &t_state);
}
}
template<typename ... P> template<typename ... P>
Ret operator()(P&& ... param) Ret operator()(P&& ... param)
{ {
@ -105,32 +57,26 @@ namespace chaiscript
if (m_conversions) { if (m_conversions) {
Type_Conversions_State state(*m_conversions, m_conversions->conversion_saves()); Type_Conversions_State state(*m_conversions, m_conversions->conversion_saves());
return Function_Caller_Ret<Ret, std::is_arithmetic<Ret>::value && !std::is_same<Ret, bool>::value>::call(m_funcs, Function_Params{params}, &state return call(Function_Params{params}, state);
);
} else { } else {
return Function_Caller_Ret<Ret, std::is_arithmetic<Ret>::value && !std::is_same<Ret, bool>::value>::call(m_funcs, Function_Params{params}, nullptr Type_Conversions conv;
); Type_Conversions_State state(conv, conv.conversion_saves());
return call(Function_Params{params}, state);
} }
} }
template<typename P, typename Q>
static auto box(Q&& q) -> typename std::enable_if<std::is_reference<P>::value&&!std::is_same<chaiscript::Boxed_Value, typename std::remove_const<typename std::remove_reference<P>::type>::type>::value, Boxed_Value>::type
{
return Boxed_Value(std::ref(std::forward<Q>(q)));
}
template<typename P, typename Q> template<typename P, typename Q>
static auto box(Q&& q) -> typename std::enable_if<!std::is_reference<P>::value&&!std::is_same<chaiscript::Boxed_Value, typename std::remove_const<typename std::remove_reference<P>::type>::type>::value, Boxed_Value>::type static Boxed_Value box(Q &&q) {
{ if constexpr (std::is_same_v<chaiscript::Boxed_Value, std::decay_t<Q>>) {
return Boxed_Value(std::forward<Q>(q)); return std::forward<Q>(q);
} } else if constexpr (std::is_reference_v<P>) {
return Boxed_Value(std::ref(std::forward<Q>(q)));
template<typename P> } else {
static Boxed_Value box(Boxed_Value bv) noexcept return Boxed_Value(std::forward<Q>(q));
{ }
return bv; }
}
std::vector<Const_Proxy_Function> m_funcs; std::vector<Const_Proxy_Function> m_funcs;

View File

@ -36,14 +36,14 @@ namespace chaiscript
struct Handle_Return struct Handle_Return
{ {
template<typename T, template<typename T,
typename = typename std::enable_if<std::is_pod<typename std::decay<T>::type>::value>::type> typename = std::enable_if_t<std::is_pod_v<std::decay_t<T>>>>
static Boxed_Value handle(T r) static Boxed_Value handle(T r)
{ {
return Boxed_Value(std::move(r), true); return Boxed_Value(std::move(r), true);
} }
template<typename T, template<typename T,
typename = typename std::enable_if<!std::is_pod<typename std::decay<T>::type>::value>::type> typename = std::enable_if_t<!std::is_pod_v<std::decay_t<T>>>>
static Boxed_Value handle(T &&r) static Boxed_Value handle(T &&r)
{ {
return Boxed_Value(std::make_shared<T>(std::forward<T>(r)), true); return Boxed_Value(std::make_shared<T>(std::forward<T>(r)), true);

View File

@ -920,6 +920,7 @@ namespace chaiscript
try { try {
this->children[2]->eval(t_ss); this->children[2]->eval(t_ss);
} catch (detail::Continue_Loop &) { } catch (detail::Continue_Loop &) {
// continue statement hit
} }
call_function(pop_front_funcs, range_obj); call_function(pop_front_funcs, range_obj);
} }
@ -1456,7 +1457,6 @@ namespace chaiscript
function_name); function_name);
} }
} catch (const exception::name_conflict_error &e) { } catch (const exception::name_conflict_error &e) {
std::cout << "Method!!" << std::endl;
throw exception::eval_error("Method redefined '" + e.name() + "'"); throw exception::eval_error("Method redefined '" + e.name() + "'");
} }
return void_var(); return void_var();