diff --git a/include/continuable/detail/awaiting.hpp b/include/continuable/detail/awaiting.hpp index c73d7ad..d9b4980 100644 --- a/include/continuable/detail/awaiting.hpp +++ b/include/continuable/detail/awaiting.hpp @@ -37,34 +37,63 @@ #include #include +#include #include namespace cti { namespace detail { namespace awaiting { +/// We import the coroutine handle in our namespace using std::experimental::coroutine_handle; + +/// An object which provides the internal buffer and helper methods +/// for waiting on a continuable in a stackless coroutine. +template +struct awaitable { + Continuable continuable_; + + /// A cache which is used to + // expected::expected cache_; + + /// Since continuables are evaluated lazily we are not + /// capable to say whether the resumption will be instantly. + bool await_ready() const noexcept { + return false; + } + + /// Suspend the current context + // TODO Convert this to an r-value function once possible + void await_suspend(coroutine_handle<> h) { + // Forward every result to the current awaitable + std::move(continuable_).flow([h, this](auto&&... args) { + resolve(); + h.resume(); + }); + } + + void await_resume() { + // if ec throw + // return n; + // return + } + + /// Resolve the continuation through the result + template + void resolve(Args&&... args) { + // ... + } + + /// Resolve the continuation through an error + void resolve(types::dispatch_error_tag, types::error_type error) { + // ... + } +}; + /// 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(); + return awaitable>{std::forward(continuable)}; } } // namespace awaiting } // namespace detail diff --git a/include/continuable/detail/expected.hpp b/include/continuable/detail/expected.hpp new file mode 100644 index 0000000..b595b8c --- /dev/null +++ b/include/continuable/detail/expected.hpp @@ -0,0 +1,65 @@ + +/* + + /~` _ _ _|_. _ _ |_ | _ + \_,(_)| | | || ||_|(_||_)|(/_ + + 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_EXPECTED_HPP_INCLUDED__ +#define CONTINUABLE_DETAIL_EXPECTED_HPP_INCLUDED__ + +#include +#include + +#include +#include + +namespace cti { +namespace detail { +namespace expected { +/// A class similar to the one in the expected proposal, +/// however it is capable of carrying an exception_ptr if +/// exceptions are used. +template > +class expected { + enum class slot_t { empty, type, error }; + + Storage storage_; + slot_t slot_; + +public: + explicit expected() : slot_(slot_t::empty) { + } + + explicit expected(T value) : slot_(slot_t::error) { + } +}; +} // namespace expected +} // namespace detail +} // namespace cti + +#endif // CONTINUABLE_DETAIL_EXPECTED_HPP_INCLUDED__ diff --git a/test/playground/CMakeLists.txt b/test/playground/CMakeLists.txt index e1688ad..06c3cab 100644 --- a/test/playground/CMakeLists.txt +++ b/test/playground/CMakeLists.txt @@ -10,6 +10,7 @@ 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/expected.hpp ${CMAKE_SOURCE_DIR}/include/continuable/detail/hints.hpp ${CMAKE_SOURCE_DIR}/include/continuable/detail/features.hpp ${CMAKE_SOURCE_DIR}/include/continuable/detail/traits.hpp diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt index 0e54ab6..218aa2e 100644 --- a/test/unit-test/CMakeLists.txt +++ b/test/unit-test/CMakeLists.txt @@ -5,6 +5,7 @@ foreach(STEP RANGE 4) add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/test-continuable.hpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable.cpp + ${CMAKE_CURRENT_LIST_DIR}/test-continuable-await.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-base-chaining.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-base-destruct.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-base-errors.cpp diff --git a/test/unit-test/test-continuable-await.cpp b/test/unit-test/test-continuable-await.cpp new file mode 100644 index 0000000..8f02a30 --- /dev/null +++ b/test/unit-test/test-continuable-await.cpp @@ -0,0 +1,72 @@ + +/* + 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. +**/ + +#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE + +#include "test-continuable.hpp" + +namespace std { +namespace experimental { +template +struct coroutine_traits { + struct promise_type { + void get_return_object() { + } + + void set_exception(exception_ptr const&) noexcept { + } + + bool initial_suspend() noexcept { + return false; + } + + bool final_suspend() noexcept { + return false; + } + + void return_void() noexcept { + } + }; +}; +} // namespace experimental +} // namespace std + +TYPED_TEST(single_dimension_tests, is_awaitable) { + + {} +} + +auto mk() { + return cti::make_continuable([](auto&& promise) { + // ... + promise.set_value(); + }); +} + +void teststhh() { + auto c = mk(); + + co_await std::move(c); +} + +#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE