From d8591d1f1384cc77241040734819c254045a87cf Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Mon, 13 Nov 2017 13:32:34 +0100 Subject: [PATCH] More work on using operator await --- include/continuable/continuable-base.hpp | 15 +---- include/continuable/detail/awaiting.hpp | 74 ++++++++++++++++++++++++ include/continuable/detail/types.hpp | 7 --- test/playground/CMakeLists.txt | 1 + 4 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 include/continuable/detail/awaiting.hpp diff --git a/include/continuable/continuable-base.hpp b/include/continuable/continuable-base.hpp index 2bfd1e5..9df34f6 100644 --- a/include/continuable/continuable-base.hpp +++ b/include/continuable/continuable-base.hpp @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -504,18 +505,8 @@ public: } #ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE - bool is_ready() const noexcept { - return false; - } - - void await_suspend(detail::types::coroutine_handle<> h) && { - - h.resume(); - } - - auto await_resume() { - // if ec throw - // return n; + auto operator co_await() && { + return detail::awaiting::create_awaiter(std::move(*this).materialize()); } #endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE diff --git a/include/continuable/detail/awaiting.hpp b/include/continuable/detail/awaiting.hpp new file mode 100644 index 0000000..c73d7ad --- /dev/null +++ b/include/continuable/detail/awaiting.hpp @@ -0,0 +1,74 @@ + +/* + + /~` _ _ _|_. _ _ |_ | _ + \_,(_)| | | || ||_|(_||_)|(/_ + + https://github.com/Naios/continuable + v2.0.0 + + Copyright(c) 2015 - 2017 Denis Blank + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files(the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and / or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions : + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +**/ + +#ifndef CONTINUABLE_DETAIL_AWAITING_HPP_INCLUDED__ +#define CONTINUABLE_DETAIL_AWAITING_HPP_INCLUDED__ + +// Exlude this header when coroutines are not available +#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE + +#include + +#include +#include + +namespace cti { +namespace detail { +namespace awaiting { +using std::experimental::coroutine_handle; +/// Converts a continuable into an awaitable object as described by +/// the C++ coroutine TS. +template +auto create_awaiter(T&& continuable) { + struct Awaiter { + bool is_ready() const noexcept { + return false; + } + + void await_suspend(coroutine_handle<> h) && { + + h.resume(); + } + + auto await_resume() { + // if ec throw + // return n; + // return + } + }; + + return Awaiter(); +} +} // namespace awaiting +} // namespace detail +} // namespace cti + +#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE +#endif // CONTINUABLE_DETAIL_UTIL_HPP_INCLUDED__ diff --git a/include/continuable/detail/types.hpp b/include/continuable/detail/types.hpp index dfc09de..12cf511 100644 --- a/include/continuable/detail/types.hpp +++ b/include/continuable/detail/types.hpp @@ -39,10 +39,6 @@ #endif // CONTINUABLE_WITH_NO_EXCEPTIONS #endif // CONTINUABLE_WITH_CUSTOM_ERROR_TYPE -#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE -#include -#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE - #include #include @@ -67,9 +63,6 @@ struct this_thread_executor_tag {}; /// A tag which is used to continue with an error struct dispatch_error_tag {}; -#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE -using std::experimental::coroutine_handle; -#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE } // namespace types } // namespace detail } // namespace cti diff --git a/test/playground/CMakeLists.txt b/test/playground/CMakeLists.txt index 0239375..e1688ad 100644 --- a/test/playground/CMakeLists.txt +++ b/test/playground/CMakeLists.txt @@ -7,6 +7,7 @@ set(LIB_SOURCES ${CMAKE_SOURCE_DIR}/include/continuable/continuable-transforms.hpp ${CMAKE_SOURCE_DIR}/include/continuable/continuable-testing.hpp) set(LIB_SOURCES_DETAIL + ${CMAKE_SOURCE_DIR}/include/continuable/detail/awaiting.hpp ${CMAKE_SOURCE_DIR}/include/continuable/detail/base.hpp ${CMAKE_SOURCE_DIR}/include/continuable/detail/composition.hpp ${CMAKE_SOURCE_DIR}/include/continuable/detail/hints.hpp