From cb225835d64342c2ffaebcbcf42fa4a9ba438a67 Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Sun, 14 Jan 2018 04:26:32 +0100 Subject: [PATCH] Preparation for making expected available inside the base namespace --- include/continuable/detail/awaiting.hpp | 51 +--------------------- include/continuable/detail/base.hpp | 22 +++------- include/continuable/detail/composition.hpp | 6 +-- include/continuable/detail/expected.hpp | 50 +++++++++++++++++++++ include/continuable/detail/hints.hpp | 15 +++++++ include/continuable/detail/transforms.hpp | 2 +- 6 files changed, 75 insertions(+), 71 deletions(-) diff --git a/include/continuable/detail/awaiting.hpp b/include/continuable/detail/awaiting.hpp index a6ab043..0bdb3b8 100644 --- a/include/continuable/detail/awaiting.hpp +++ b/include/continuable/detail/awaiting.hpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include @@ -53,54 +52,6 @@ namespace cti { namespace detail { namespace awaiting { -namespace detail { -struct void_guard_tag {}; - -template -struct result_trait; -template <> -struct result_trait> { - using expected = util::expected; - - static constexpr void_guard_tag wrap() noexcept { - return {}; - } - static void unwrap(expected&& e) { - assert(e.is_value()); - (void)e; - } -}; -template -struct result_trait> { - using expected = util::expected; - - static auto wrap(T arg) { - return std::move(arg); - } - static auto unwrap(expected&& e) { - assert(e.is_value()); - return std::move(e.get_value()); - } -}; -template -struct result_trait> { - using expected = util::expected>; - - static auto wrap(First first, Second second, Rest... rest) { - return std::make_tuple(std::move(first), std::move(second), - std::move(rest)...); - } - static auto unwrap(expected&& e) { - assert(e.is_value()); - return std::move(e.get_value()); - } -}; - -template -using result_trait_t = - result_trait()))>; -} // namespace detail - /// We import the coroutine handle in our namespace using std::experimental::coroutine_handle; @@ -108,7 +59,7 @@ using std::experimental::coroutine_handle; /// for waiting on a continuable in a stackless coroutine. template class awaitable { - using trait_t = detail::result_trait_t; + using trait_t = util::expected_result_trait_t; /// The continuable which is invoked upon suspension Continuable continuable_; diff --git a/include/continuable/detail/base.hpp b/include/continuable/detail/base.hpp index 2dd5997..2a4f663 100644 --- a/include/continuable/detail/base.hpp +++ b/include/continuable/detail/base.hpp @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -63,21 +64,6 @@ namespace detail { /// base::finalize_continuation(base::continuation continuation) /// -> void namespace base { -/// Returns the signature hint of the given continuable -template -constexpr auto hint_of(traits::identity) { - static_assert(traits::fail::value, - "Expected a continuation with an existing signature hint!"); - return traits::identity_of(); -} -/// Returns the signature hint of the given continuable -template -constexpr auto -hint_of(traits::identity< - continuable_base>>) { - return hints::signature_hint_tag{}; -} - template struct is_continuation : std::false_type {}; template @@ -179,6 +165,8 @@ invoker_of(traits::identity>) { using Type = decltype(attorney::materialize( std::declval>())); + auto constexpr const hint = hints::hint_of(traits::identity_of()); + return make_invoker( [](auto&& callback, auto&& next_callback, auto&&... args) { CONTINUABLE_BLOCK_TRY_BEGIN @@ -191,7 +179,7 @@ invoker_of(traits::identity>) { std::forward(next_callback)); CONTINUABLE_BLOCK_TRY_END }, - hint_of(traits::identity_of())); + hint); } /// - ? -> next_callback(?) @@ -510,7 +498,7 @@ auto chain_continuation(Continuation&& continuation, Callback&& callback, static_assert(is_continuation>{}, "Expected a continuation!"); - using Hint = decltype(hint_of(traits::identity_of(continuation))); + using Hint = decltype(hints::hint_of(traits::identity_of(continuation))); auto next_hint = next_hint_of(std::integral_constant{}, traits::identity_of(callback), Hint{}); diff --git a/include/continuable/detail/composition.hpp b/include/continuable/detail/composition.hpp index 2c7d73d..189065e 100644 --- a/include/continuable/detail/composition.hpp +++ b/include/continuable/detail/composition.hpp @@ -341,7 +341,7 @@ auto finalize_composition( // Merge all signature hints together auto signature = traits::unpack(composition, [](auto&... entries) { - return traits::merge(base::hint_of(traits::identity_of(entries))...); + return traits::merge(hints::hint_of(traits::identity_of(entries))...); }); return base::attorney::create( @@ -369,7 +369,7 @@ auto finalize_composition( // This is the length of the arguments of the current continuable auto arg_size = - traits::pack_size_of(base::hint_of(traits::identity_of(entry))); + traits::pack_size_of(hints::hint_of(traits::identity_of(entry))); // The next position in the result tuple auto next = current.second + arg_size; @@ -438,7 +438,7 @@ auto finalize_composition( // Determine the shared result between all continuations auto signature = traits::unpack(composition, [](auto const&... args) { return common_result_of(hints::signature_hint_tag<>{}, - base::hint_of(traits::identity_of(args))...); + hints::hint_of(traits::identity_of(args))...); }); return base::attorney::create( diff --git a/include/continuable/detail/expected.hpp b/include/continuable/detail/expected.hpp index 4ade3ba..b94a57f 100644 --- a/include/continuable/detail/expected.hpp +++ b/include/continuable/detail/expected.hpp @@ -37,6 +37,8 @@ #include #include +#include +#include #include #include @@ -331,6 +333,54 @@ private: this->slot_ = slot; } }; + +namespace detail { +struct void_guard_tag {}; + +template +struct expected_result_trait; +template <> +struct expected_result_trait> { + using expected = util::expected; + + static constexpr void_guard_tag wrap() noexcept { + return {}; + } + static void unwrap(expected&& e) { + assert(e.is_value()); + (void)e; + } +}; +template +struct expected_result_trait> { + using expected = util::expected; + + static auto wrap(T arg) { + return std::move(arg); + } + static auto unwrap(expected&& e) { + assert(e.is_value()); + return std::move(e.get_value()); + } +}; +template +struct expected_result_trait> { + using expected = util::expected>; + + static auto wrap(First first, Second second, Rest... rest) { + return std::make_tuple(std::move(first), std::move(second), + std::move(rest)...); + } + static auto unwrap(expected&& e) { + assert(e.is_value()); + return std::move(e.get_value()); + } +}; +} // namespace detail + +template +using expected_result_trait_t = detail::expected_result_trait()))>; } // namespace util } // namespace detail } // namespace cti diff --git a/include/continuable/detail/hints.hpp b/include/continuable/detail/hints.hpp index 0acf509..5a9b4fe 100644 --- a/include/continuable/detail/hints.hpp +++ b/include/continuable/detail/hints.hpp @@ -50,6 +50,21 @@ template struct is_absent_hint : std::false_type {}; template <> struct is_absent_hint : std::true_type {}; + + /// Returns the signature hint of the given continuable +template +constexpr auto hint_of(traits::identity) { + static_assert(traits::fail::value, + "Expected a continuation with an existing signature hint!"); + return traits::identity_of(); +} +/// Returns the signature hint of the given continuable +template +constexpr auto +hint_of(traits::identity< + continuable_base>>) { + return hints::signature_hint_tag{}; +} } // namespace hints } // namespace detail } // namespace cti diff --git a/include/continuable/detail/transforms.hpp b/include/continuable/detail/transforms.hpp index 52db2e4..889d3dc 100644 --- a/include/continuable/detail/transforms.hpp +++ b/include/continuable/detail/transforms.hpp @@ -120,7 +120,7 @@ public: template auto as_future(continuable_base&& continuable) { // Create the promise which is able to supply the current arguments - auto hint = base::hint_of(traits::identity_of(continuable)); + auto hint = hints::hint_of(traits::identity_of(continuable)); promise_callback> callback; (void)hint;