From b42dffb24ceb4a8eab2ba50a294332d21ccb5715 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 25 Nov 2017 14:07:10 -0700 Subject: [PATCH] ircd::json: Improve tuple assignment typography. --- include/ircd/json/tuple.h | 258 ++++++++++++++++++++++---------------- 1 file changed, 148 insertions(+), 110 deletions(-) diff --git a/include/ircd/json/tuple.h b/include/ircd/json/tuple.h index eaa100570..cc7e87e71 100644 --- a/include/ircd/json/tuple.h +++ b/include/ircd/json/tuple.h @@ -82,9 +82,15 @@ is_tuple() return std::is_base_of::value; } -template +template using enable_if_tuple = typename std::enable_if(), R>::type; +template +using enable_if_tuple_and = typename std::enable_if() && test(), R>::type; + template using tuple_type = typename tuple::tuple_type; @@ -231,6 +237,147 @@ val(const tuple &t) return static_cast &>(std::get(t)); } +template +constexpr bool +is_number() +{ + using type = typename std::remove_reference::type; + return std::is_arithmetic::value; +} + +template +constexpr bool +is_floating() +{ + using type = typename std::remove_reference::type; + return std::is_arithmetic() && std::is_floating_point(); +} + +template +constexpr bool +is_integer() +{ + return is_number() && !is_floating(); +} + +template +typename std::enable_if(), size_t>::type +serialized(T&& t) +{ + return lex_cast(t).size(); +} + +template +typename std::enable_if(), bool>::type +defined(T&& t) +{ + return !is_zero{}(t); +} + +template +typename std::enable_if +< + std::is_base_of() && + std::is_convertible(), +void>::type +_assign(dst &d, + src&& s) +{ + d = unquote(string_view{std::forward(s)}); +} + +template +typename std::enable_if +< + !std::is_base_of() && + std::is_convertible() && + !ircd::json::is_tuple() && + !std::is_same(), +void>::type +_assign(dst &d, + src&& s) +{ + d = std::forward(s); +} + +template +typename std::enable_if +< + !std::is_base_of() && + std::is_convertible() && + !ircd::json::is_tuple() && + std::is_same(), +void>::type +_assign(dst &d, + src&& s) +{ + static const is_zero test{}; + d = test(std::forward(s)); +} + +template +typename std::enable_if +< + std::is_arithmetic() && + std::is_base_of::type>() && + !std::is_base_of, typename std::remove_reference::type>(), +void>::type +_assign(dst &d, + src&& s) +try +{ + d = lex_cast(std::forward(s)); +} +catch(const bad_lex_cast &e) +{ + throw parse_error("cannot convert '%s' to '%s'", + demangle(), + demangle()); +} + +template +typename std::enable_if +< + std::is_arithmetic() && + std::is_base_of, typename std::remove_reference::type>(), +void>::type +_assign(dst &d, + src&& s) +{ + assert(!s.empty()); + d = byte_view(std::forward(s)); +} + +template +typename std::enable_if +< + std::is_base_of() && + std::is_pod::type>(), +void>::type +_assign(dst &d, + src&& s) +{ + d = byte_view(std::forward(s)); +} + +template +typename std::enable_if +< + ircd::json::is_tuple(), +void>::type +_assign(dst &d, + src&& s) +{ + d = dst{std::forward(s)}; +} + template enable_if_tuple()> &> @@ -659,115 +806,6 @@ runtil(tuple &t, false; } -template -constexpr bool -serialized_lex_cast() -{ - using type = typename std::remove_reference::type; - return std::is_arithmetic::value; -} - -template -typename std::enable_if(), size_t>::type -serialized(T&& t) -{ - return lex_cast(t).size(); -} - -template -typename std::enable_if(), bool>::type -defined(T&& t) -{ - return t != typename std::remove_reference::type {0}; -} - -template -typename std::enable_if -< - std::is_base_of() && - std::is_convertible(), -void>::type -_assign(dst &d, - src&& s) -{ - d = unquote(string_view{std::forward(s)}); -} - -template -typename std::enable_if -< - !std::is_base_of() && - std::is_convertible() && - !ircd::json::is_tuple(), -void>::type -_assign(dst &d, - src&& s) -{ - d = std::forward(s); -} - -template -typename std::enable_if -< - std::is_arithmetic() && - std::is_base_of::type>() && - !std::is_base_of, typename std::remove_reference::type>(), -void>::type -_assign(dst &d, - src&& s) -try -{ - d = lex_cast(std::forward(s)); -} -catch(const bad_lex_cast &e) -{ - throw parse_error("cannot convert '%s' to '%s'", - demangle(), - demangle()); -} - -template -typename std::enable_if -< - std::is_arithmetic() && - std::is_base_of, typename std::remove_reference::type>(), -void>::type -_assign(dst &d, - src&& s) -{ - assert(!s.empty()); - d = byte_view(std::forward(s)); -} - -template -typename std::enable_if -< - std::is_base_of() && - std::is_pod::type>(), -void>::type -_assign(dst &d, - src&& s) -{ - d = byte_view(std::forward(s)); -} - -template -typename std::enable_if -< - ircd::json::is_tuple(), -void>::type -_assign(dst &d, - src&& s) -{ - d = dst{std::forward(s)}; -} - template tuple &