From a6d30baa27c6fbacb85d8f675baef9f809a6bf2d Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 17 Nov 2017 06:12:50 -0700 Subject: [PATCH] Apply some if constexpr action --- .../dispatchkit/function_call_detail.hpp | 104 +++++------------- .../chaiscript/dispatchkit/handle_return.hpp | 4 +- .../chaiscript/language/chaiscript_eval.hpp | 2 +- 3 files changed, 28 insertions(+), 82 deletions(-) diff --git a/include/chaiscript/dispatchkit/function_call_detail.hpp b/include/chaiscript/dispatchkit/function_call_detail.hpp index 5cd1932e..023947cc 100644 --- a/include/chaiscript/dispatchkit/function_call_detail.hpp +++ b/include/chaiscript/dispatchkit/function_call_detail.hpp @@ -29,66 +29,7 @@ namespace chaiscript { namespace detail { - /// Internal helper class for handling the return - /// value of a build_function_caller - template - struct Function_Caller_Ret - { - static Ret call(const std::vector &t_funcs, - const Function_Params ¶ms, const Type_Conversions_State *t_conversions) - { - if (t_conversions != nullptr) { - return boxed_cast(dispatch::dispatch(t_funcs, params, *t_conversions), t_conversions); - } else { - Type_Conversions conv; - Type_Conversions_State state(conv, conv.conversion_saves()); - return boxed_cast(dispatch::dispatch(t_funcs, params, state), t_conversions); - } - } - }; - - /** - * Specialization for arithmetic return types - */ - template - struct Function_Caller_Ret - { - static Ret call(const std::vector &t_funcs, - const Function_Params ¶ms, const Type_Conversions_State *t_conversions) - { - if (t_conversions != nullptr) { - return Boxed_Number(dispatch::dispatch(t_funcs, params, *t_conversions)).get_as(); - } else { - Type_Conversions conv; - Type_Conversions_State state(conv, conv.conversion_saves()); - return Boxed_Number(dispatch::dispatch(t_funcs, params, state)).get_as(); - } - } - }; - - - /** - * Specialization for void return types - */ - template<> - struct Function_Caller_Ret - { - static void call(const std::vector &t_funcs, - const Function_Params ¶ms, 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 - */ + /// used internally for unwrapping a function call's types template struct Build_Function_Caller_Helper { @@ -98,6 +39,17 @@ namespace chaiscript { } + Ret call(const Function_Params ¶ms, const Type_Conversions_State &t_state) + { + if constexpr (std::is_arithmetic_v) { + return Boxed_Number(dispatch::dispatch(m_funcs, params, t_state)).get_as(); + } else if constexpr (std::is_same_v) { + dispatch::dispatch(m_funcs, params, t_state); + } else { + return boxed_cast(dispatch::dispatch(m_funcs, params, t_state), &t_state); + } + } + template Ret operator()(P&& ... param) { @@ -105,32 +57,26 @@ namespace chaiscript if (m_conversions) { Type_Conversions_State state(*m_conversions, m_conversions->conversion_saves()); - return Function_Caller_Ret::value && !std::is_same::value>::call(m_funcs, Function_Params{params}, &state - ); + return call(Function_Params{params}, state); } else { - return Function_Caller_Ret::value && !std::is_same::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 - static auto box(Q&& q) -> typename std::enable_if::value&&!std::is_same::type>::type>::value, Boxed_Value>::type - { - return Boxed_Value(std::ref(std::forward(q))); - } template - static auto box(Q&& q) -> typename std::enable_if::value&&!std::is_same::type>::type>::value, Boxed_Value>::type - { - return Boxed_Value(std::forward(q)); - } - - template - static Boxed_Value box(Boxed_Value bv) noexcept - { - return bv; - } + static Boxed_Value box(Q &&q) { + if constexpr (std::is_same_v>) { + return std::forward(q); + } else if constexpr (std::is_reference_v

) { + return Boxed_Value(std::ref(std::forward(q))); + } else { + return Boxed_Value(std::forward(q)); + } + } std::vector m_funcs; diff --git a/include/chaiscript/dispatchkit/handle_return.hpp b/include/chaiscript/dispatchkit/handle_return.hpp index 4dc381d4..e06b44df 100644 --- a/include/chaiscript/dispatchkit/handle_return.hpp +++ b/include/chaiscript/dispatchkit/handle_return.hpp @@ -36,14 +36,14 @@ namespace chaiscript struct Handle_Return { template::type>::value>::type> + typename = std::enable_if_t>>> static Boxed_Value handle(T r) { return Boxed_Value(std::move(r), true); } template::type>::value>::type> + typename = std::enable_if_t>>> static Boxed_Value handle(T &&r) { return Boxed_Value(std::make_shared(std::forward(r)), true); diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index cac5d293..f368d62f 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -920,6 +920,7 @@ namespace chaiscript try { this->children[2]->eval(t_ss); } catch (detail::Continue_Loop &) { + // continue statement hit } call_function(pop_front_funcs, range_obj); } @@ -1456,7 +1457,6 @@ namespace chaiscript function_name); } } catch (const exception::name_conflict_error &e) { - std::cout << "Method!!" << std::endl; throw exception::eval_error("Method redefined '" + e.name() + "'"); } return void_var();