From 34534c1386143bcac31e5c8ee790b5266cef426f Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 8 Aug 2017 17:06:36 -0600 Subject: [PATCH] Changes that noexcept want to happen --- include/chaiscript/chaiscript_threading.hpp | 8 +- include/chaiscript/dispatchkit/any.hpp | 15 +- .../chaiscript/dispatchkit/bad_boxed_cast.hpp | 13 +- include/chaiscript/dispatchkit/bootstrap.hpp | 10 +- .../chaiscript/dispatchkit/boxed_number.hpp | 240 ++++++++++++------ .../dispatchkit/proxy_functions.hpp | 4 + .../dispatchkit/type_conversions.hpp | 9 +- 7 files changed, 192 insertions(+), 107 deletions(-) diff --git a/include/chaiscript/chaiscript_threading.hpp b/include/chaiscript/chaiscript_threading.hpp index 47b97288..cd2c1bd3 100644 --- a/include/chaiscript/chaiscript_threading.hpp +++ b/include/chaiscript/chaiscript_threading.hpp @@ -78,22 +78,22 @@ namespace chaiscript t().erase(this); } - inline const T *operator->() const + inline const T *operator->() const noexcept { return &(t()[this]); } - inline const T &operator*() const + inline const T &operator*() const noexcept { return t()[this]; } - inline T *operator->() + inline T *operator->() noexcept { return &(t()[this]); } - inline T &operator*() + inline T &operator*() noexcept { return t()[this]; } diff --git a/include/chaiscript/dispatchkit/any.hpp b/include/chaiscript/dispatchkit/any.hpp index 47b75a59..1f010ea4 100644 --- a/include/chaiscript/dispatchkit/any.hpp +++ b/include/chaiscript/dispatchkit/any.hpp @@ -21,20 +21,11 @@ namespace chaiscript { class bad_any_cast : public std::bad_cast { public: - bad_any_cast() = default; - - bad_any_cast(const bad_any_cast &) = default; - - ~bad_any_cast() noexcept override = default; - /// \brief Description of what error occurred const char * what() const noexcept override { - return m_what.c_str(); + return "bad any cast"; } - - private: - std::string m_what = "bad any cast"; }; } @@ -54,7 +45,7 @@ namespace chaiscript { virtual void *data() noexcept = 0; - const std::type_info &type() const + const std::type_info &type() const noexcept { return m_type; } @@ -91,7 +82,7 @@ namespace chaiscript { public: // construct/copy/destruct - Any() = default; + Any() noexcept = default; Any(Any &&) = default; Any &operator=(Any &&t_any) = default; diff --git a/include/chaiscript/dispatchkit/bad_boxed_cast.hpp b/include/chaiscript/dispatchkit/bad_boxed_cast.hpp index 1c1d729e..637b284c 100644 --- a/include/chaiscript/dispatchkit/bad_boxed_cast.hpp +++ b/include/chaiscript/dispatchkit/bad_boxed_cast.hpp @@ -15,6 +15,7 @@ #include #include "../chaiscript_defines.hpp" +#include "../utility/static_string.hpp" #include "type_info.hpp" namespace chaiscript { @@ -34,22 +35,22 @@ namespace chaiscript { public: bad_boxed_cast(Type_Info t_from, const std::type_info &t_to, - std::string t_what) noexcept + utility::Static_String t_what) noexcept : from(t_from), to(&t_to), m_what(std::move(t_what)) { } - bad_boxed_cast(Type_Info t_from, const std::type_info &t_to) - : from(t_from), to(&t_to), m_what("Cannot perform boxed_cast: " + t_from.name() + " to: " + t_to.name()) + bad_boxed_cast(Type_Info t_from, const std::type_info &t_to) noexcept + : from(t_from), to(&t_to), m_what("Cannot perform boxed_cast") { } - explicit bad_boxed_cast(std::string t_what) noexcept + explicit bad_boxed_cast(utility::Static_String t_what) noexcept : m_what(std::move(t_what)) { } - bad_boxed_cast(const bad_boxed_cast &) = default; + bad_boxed_cast(const bad_boxed_cast &) noexcept = default; ~bad_boxed_cast() noexcept override = default; /// \brief Description of what error occurred @@ -62,7 +63,7 @@ namespace chaiscript const std::type_info *to = nullptr; ///< std::type_info of the desired (but failed) result type private: - std::string m_what; + utility::Static_String m_what; }; } } diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 8aedb353..201c3af7 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -199,12 +199,12 @@ namespace chaiscript } } - static void print(const std::string &s) + static void print(const std::string &s) noexcept { fwrite(s.c_str(), 1, s.size(), stdout); } - static void println(const std::string &s) + static void println(const std::string &s) noexcept { puts(s.c_str()); } @@ -268,10 +268,10 @@ namespace chaiscript } - static bool has_guard(const Const_Proxy_Function &t_pf) + static bool has_guard(const Const_Proxy_Function &t_pf) noexcept { auto pf = std::dynamic_pointer_cast(t_pf); - return pf && pf->get_guard(); + return pf && pf->has_guard(); } static Const_Proxy_Function get_guard(const Const_Proxy_Function &t_pf) @@ -302,7 +302,7 @@ namespace chaiscript } - static bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) + static bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) noexcept { const auto pf = std::dynamic_pointer_cast(t_pf); return bool(pf); diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index e81a563f..0606cd69 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -161,29 +161,29 @@ namespace chaiscript } template - static Boxed_Value boolean_go(Operators::Opers t_oper, const T &t, const T &u) + static bool boolean_go(Operators::Opers t_oper, const T &t, const T &u) noexcept { switch (t_oper) { case Operators::Opers::equals: - return const_var(t == u); + return t == u; case Operators::Opers::less_than: - return const_var(t < u); + return t < u; case Operators::Opers::greater_than: - return const_var(t > u); + return t > u; case Operators::Opers::less_than_equal: - return const_var(t <= u); + return t <= u; case Operators::Opers::greater_than_equal: - return const_var(t >= u); + return t >= u; case Operators::Opers::not_equal: - return const_var(t != u); + return t != u; default: - throw chaiscript::detail::exception::bad_any_cast(); + assert(false); } } template - static Boxed_Value unary_go(Operators::Opers t_oper, T &t, const Boxed_Value &t_lhs) + static void unary_go(Operators::Opers t_oper, T &t) noexcept { switch (t_oper) { @@ -194,14 +194,13 @@ namespace chaiscript --t; break; default: - throw chaiscript::detail::exception::bad_any_cast(); + assert(false); } - - return t_lhs; } template - static Boxed_Value binary_go(Operators::Opers t_oper, T &t, const U &u, const Boxed_Value &t_lhs) + static void binary_go(Operators::Opers t_oper, T &t, const U &u) + noexcept(noexcept(check_divide_by_zero(u))) { switch (t_oper) { @@ -222,14 +221,12 @@ namespace chaiscript t -= u; break; default: - throw chaiscript::detail::exception::bad_any_cast(); + assert(false); } - - return t_lhs; } template - static Boxed_Value binary_int_go(Operators::Opers t_oper, T &t, const U &u, const Boxed_Value &t_lhs) + static void binary_int_go(Operators::Opers t_oper, T &t, const U &u) noexcept { switch (t_oper) { @@ -253,76 +250,77 @@ namespace chaiscript t ^= u; break; default: - throw chaiscript::detail::exception::bad_any_cast(); + assert(false); } - return t_lhs; } template - static Boxed_Value const_unary_int_go(Operators::Opers t_oper, const T &t) + static auto const_unary_int_go(Operators::Opers t_oper, const T &t) noexcept { switch (t_oper) { case Operators::Opers::bitwise_complement: - return const_var(~t); + return ~t; default: - throw chaiscript::detail::exception::bad_any_cast(); + assert(false); } } template - static Boxed_Value const_binary_int_go(Operators::Opers t_oper, const T &t, const T &u) + static auto const_binary_int_go(Operators::Opers t_oper, const T &t, const T &u) + noexcept(noexcept(check_divide_by_zero(u))) { switch (t_oper) { case Operators::Opers::shift_left: - return const_var(t << u); + return t << u; case Operators::Opers::shift_right: - return const_var(t >> u); + return t >> u; case Operators::Opers::remainder: check_divide_by_zero(u); - return const_var(t % u); + return t % u; case Operators::Opers::bitwise_and: - return const_var(t & u); + return t & u; case Operators::Opers::bitwise_or: - return const_var(t | u); + return t | u; case Operators::Opers::bitwise_xor: - return const_var(t ^ u); + return t ^ u; default: - throw chaiscript::detail::exception::bad_any_cast(); + assert(false); } } template - static Boxed_Value const_unary_go(Operators::Opers t_oper, const T &t) + static auto const_unary_go(Operators::Opers t_oper, const T &t) noexcept { switch (t_oper) { case Operators::Opers::unary_minus: - return const_var(-t); + return -t; case Operators::Opers::unary_plus: - return const_var(+t); + return +t; default: - throw chaiscript::detail::exception::bad_any_cast(); + assert(false); } } template - static Boxed_Value const_binary_go(Operators::Opers t_oper, const T &t, const T &u) + static auto const_binary_go(Operators::Opers t_oper, const T &t, const T &u) + noexcept(noexcept(check_divide_by_zero(u))) { switch (t_oper) { case Operators::Opers::sum: - return const_var(t + u); + return t + u; case Operators::Opers::quotient: check_divide_by_zero(u); - return const_var(t / u); + return t / u; case Operators::Opers::product: - return const_var(t * u); + return t * u; case Operators::Opers::difference: - return const_var(t - u); + return t - u; default: - throw chaiscript::detail::exception::bad_any_cast(); + assert(false); } } @@ -331,19 +329,60 @@ namespace chaiscript -> typename std::enable_if::value && !std::is_floating_point::value, Boxed_Value>::type { typedef typename std::common_type::type common_type; - if (t_oper > Operators::Opers::boolean_flag && t_oper < Operators::Opers::non_const_flag) - { - return boolean_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs)); - } else if (t_oper > Operators::Opers::non_const_flag && t_oper < Operators::Opers::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) { - return binary_go(t_oper, *static_cast(t_lhs.get_ptr()), get_as_aux(t_rhs), t_lhs); - } else if (t_oper > Operators::Opers::non_const_int_flag && t_oper < Operators::Opers::const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) { - return binary_int_go(t_oper, *static_cast(t_lhs.get_ptr()), get_as_aux(t_rhs), t_lhs); - } else if (t_oper > Operators::Opers::const_int_flag && t_oper < Operators::Opers::const_flag) { - return const_binary_int_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs)); - } else if (t_oper > Operators::Opers::const_flag) { - return const_binary_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs)); - } else { - throw chaiscript::detail::exception::bad_any_cast(); + + switch (t_oper) { + case Operators::Opers::equals: + case Operators::Opers::less_than: + case Operators::Opers::greater_than: + case Operators::Opers::less_than_equal: + case Operators::Opers::greater_than_equal: + case Operators::Opers::not_equal: + return const_var(boolean_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs))); + case Operators::Opers::assign: + case Operators::Opers::assign_product: + case Operators::Opers::assign_sum: + case Operators::Opers::assign_quotient: + case Operators::Opers::assign_difference: + if (!t_lhs.is_const() && !t_lhs.is_return_value()) { + binary_go(t_oper, *static_cast(t_lhs.get_ptr()), get_as_aux(t_rhs)); + return t_lhs; + } else { + throw chaiscript::detail::exception::bad_any_cast(); + } + case Operators::Opers::assign_bitwise_and: + case Operators::Opers::assign_bitwise_or: + case Operators::Opers::assign_shift_left: + case Operators::Opers::assign_shift_right: + case Operators::Opers::assign_remainder: + case Operators::Opers::assign_bitwise_xor: + if (!t_lhs.is_const() && !t_lhs.is_return_value()) { + binary_int_go(t_oper, *static_cast(t_lhs.get_ptr()), get_as_aux(t_rhs)); + return t_lhs; + } else { + throw chaiscript::detail::exception::bad_any_cast(); + } + case Operators::Opers::shift_left: + case Operators::Opers::shift_right: + case Operators::Opers::remainder: + case Operators::Opers::bitwise_and: + case Operators::Opers::bitwise_or: + case Operators::Opers::bitwise_xor: + { + const auto result + = const_binary_int_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs)); + return const_var(result); + } + case Operators::Opers::sum: + case Operators::Opers::quotient: + case Operators::Opers::product: + case Operators::Opers::difference: + { + const auto result + = const_binary_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs)); + return const_var(result); + } + default: + throw chaiscript::detail::exception::bad_any_cast(); } } @@ -352,16 +391,39 @@ namespace chaiscript -> typename std::enable_if::value || std::is_floating_point::value, Boxed_Value>::type { typedef typename std::common_type::type common_type; - if (t_oper > Operators::Opers::boolean_flag && t_oper < Operators::Opers::non_const_flag) - { - return boolean_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs)); - } else if (t_oper > Operators::Opers::non_const_flag && t_oper < Operators::Opers::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) { - return binary_go(t_oper, *static_cast(t_lhs.get_ptr()), get_as_aux(t_rhs), t_lhs); - } else if (t_oper > Operators::Opers::const_flag) { - return const_binary_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs)); - } else { - throw chaiscript::detail::exception::bad_any_cast(); + + switch (t_oper) { + case Operators::Opers::equals: + case Operators::Opers::less_than: + case Operators::Opers::greater_than: + case Operators::Opers::less_than_equal: + case Operators::Opers::greater_than_equal: + case Operators::Opers::not_equal: + return const_var(boolean_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs))); + case Operators::Opers::assign: + case Operators::Opers::assign_product: + case Operators::Opers::assign_sum: + case Operators::Opers::assign_quotient: + case Operators::Opers::assign_difference: + if (!t_lhs.is_const() && !t_lhs.is_return_value()) { + binary_go(t_oper, *static_cast(t_lhs.get_ptr()), get_as_aux(t_rhs)); + return t_lhs; + } else { + throw chaiscript::detail::exception::bad_any_cast(); + } + case Operators::Opers::sum: + case Operators::Opers::quotient: + case Operators::Opers::product: + case Operators::Opers::difference: + { + const auto result + = const_binary_go(t_oper, get_as_aux(t_lhs), get_as_aux(t_rhs)); + return const_var(result); + } + default: + throw chaiscript::detail::exception::bad_any_cast(); } + } // Unary @@ -369,27 +431,53 @@ namespace chaiscript static auto go(Operators::Opers t_oper, const Boxed_Value &t_lhs) -> typename std::enable_if::value, Boxed_Value>::type { - if (t_oper > Operators::Opers::non_const_flag && t_oper < Operators::Opers::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) { - return unary_go(t_oper, *static_cast(t_lhs.get_ptr()), t_lhs); - } else if (t_oper > Operators::Opers::const_int_flag && t_oper < Operators::Opers::const_flag) { - return const_unary_int_go(t_oper, *static_cast(t_lhs.get_const_ptr())); - } else if (t_oper > Operators::Opers::const_flag) { - return const_unary_go(t_oper, *static_cast(t_lhs.get_const_ptr())); - } else { - throw chaiscript::detail::exception::bad_any_cast(); + switch (t_oper) { + case Operators::Opers::pre_increment: + case Operators::Opers::pre_decrement: + if (!t_lhs.is_const() && !t_lhs.is_return_value()) { + unary_go(t_oper, *static_cast(t_lhs.get_ptr())); + return t_lhs; + } else { + throw chaiscript::detail::exception::bad_any_cast(); + } + case Operators::Opers::bitwise_complement: + { + const auto val = const_unary_int_go(t_oper, *static_cast(t_lhs.get_const_ptr())); + return const_var(val); + } + case Operators::Opers::unary_minus: + case Operators::Opers::unary_plus: + { + const auto val = const_unary_go(t_oper, *static_cast(t_lhs.get_const_ptr())); + return const_var(val); + } + default: + throw chaiscript::detail::exception::bad_any_cast(); } + } template static auto go(Operators::Opers t_oper, const Boxed_Value &t_lhs) -> typename std::enable_if::value, Boxed_Value>::type { - if (t_oper > Operators::Opers::non_const_flag && t_oper < Operators::Opers::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) { - return unary_go(t_oper, *static_cast(t_lhs.get_ptr()), t_lhs); - } else if (t_oper > Operators::Opers::const_flag) { - return const_unary_go(t_oper, *static_cast(t_lhs.get_const_ptr())); - } else { - throw chaiscript::detail::exception::bad_any_cast(); + switch (t_oper) { + case Operators::Opers::pre_increment: + case Operators::Opers::pre_decrement: + if (!t_lhs.is_const() && !t_lhs.is_return_value()) { + unary_go(t_oper, *static_cast(t_lhs.get_ptr())); + return t_lhs; + } else { + throw chaiscript::detail::exception::bad_any_cast(); + } + case Operators::Opers::unary_minus: + case Operators::Opers::unary_plus: + { + const auto val = const_unary_go(t_oper, *static_cast(t_lhs.get_const_ptr())); + return const_var(val); + } + default: + throw chaiscript::detail::exception::bad_any_cast(); } } diff --git a/include/chaiscript/dispatchkit/proxy_functions.hpp b/include/chaiscript/dispatchkit/proxy_functions.hpp index b80e66a8..566792b5 100644 --- a/include/chaiscript/dispatchkit/proxy_functions.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions.hpp @@ -374,6 +374,10 @@ namespace chaiscript return call_match_internal(vals, t_conversions).first; } + bool has_guard() const noexcept + { + return bool(m_guard); + } Proxy_Function get_guard() const { diff --git a/include/chaiscript/dispatchkit/type_conversions.hpp b/include/chaiscript/dispatchkit/type_conversions.hpp index 728494ce..fbab2f81 100644 --- a/include/chaiscript/dispatchkit/type_conversions.hpp +++ b/include/chaiscript/dispatchkit/type_conversions.hpp @@ -20,6 +20,7 @@ #include #include "../chaiscript_threading.hpp" +#include "../utility/static_string.hpp" #include "bad_boxed_cast.hpp" #include "boxed_cast_helper.hpp" #include "boxed_value.hpp" @@ -33,7 +34,7 @@ namespace chaiscript { public: bad_boxed_dynamic_cast(const Type_Info &t_from, const std::type_info &t_to, - const std::string &t_what) noexcept + const utility::Static_String &t_what) noexcept : bad_boxed_cast(t_from, t_to, t_what) { } @@ -43,7 +44,7 @@ namespace chaiscript { } - explicit bad_boxed_dynamic_cast(const std::string &w) noexcept + explicit bad_boxed_dynamic_cast(const utility::Static_String &w) noexcept : bad_boxed_cast(w) { } @@ -57,7 +58,7 @@ namespace chaiscript { public: bad_boxed_type_cast(const Type_Info &t_from, const std::type_info &t_to, - const std::string &t_what) noexcept + const utility::Static_String &t_what) noexcept : bad_boxed_cast(t_from, t_to, t_what) { } @@ -67,7 +68,7 @@ namespace chaiscript { } - explicit bad_boxed_type_cast(const std::string &w) noexcept + explicit bad_boxed_type_cast(const utility::Static_String &w) noexcept : bad_boxed_cast(w) { }