diff --git a/include/continuable/continuable-base.hpp b/include/continuable/continuable-base.hpp index 18c71c3..c15e5d3 100644 --- a/include/continuable/continuable-base.hpp +++ b/include/continuable/continuable-base.hpp @@ -40,7 +40,6 @@ #include #include #include -#include #include #include @@ -312,6 +311,19 @@ public: std::forward(executor)); } + /// A method which allows to apply this continuable to the given callable. + /// + /// \param transform A transform which shall accept this continuable + /// + /// \returns Returns the result of the given transform when this + /// continuable is passed into it. + /// + /// \since version 2.0.0 + template + auto apply(T&& transform) && { + return std::forward(transform)(std::move(*this)); + } + /// Ignores all error which ocured until the point the function was called /// /// \note This can be used to create a continuable which doesn't resolve @@ -446,23 +458,6 @@ public: std::move(right)); } - /// Starts the continuation chain and returns the asynchronous - /// result as `std::future<...>`. - /// - /// \returns Returns a `std::future<...>` which becomes ready as soon - /// as the the continuation chain has finished. - /// The signature of the future depends on the result type: - /// | Continuation type | Return type | - /// | : ------------------------------- | : -------------------------------- | - /// | `continuable_base with <>` | `std::future` | - /// | `continuable_base with ` | `std::future` | - /// | `continuable_base with ` | `std::future>` | - /// - /// \since version 1.0.0 - auto futurize() && { - return detail::transforms::as_future(std::move(*this).materialize()); - } - /// Invokes the continuation chain manually even before the /// cti::continuable_base is destructed. This will release the object. /// diff --git a/include/continuable/continuable-transforms.hpp b/include/continuable/continuable-transforms.hpp new file mode 100644 index 0000000..7ab2672 --- /dev/null +++ b/include/continuable/continuable-transforms.hpp @@ -0,0 +1,66 @@ + +/** + + /~` _ _ _|_. _ _ |_ | _ + \_,(_)| | | || ||_|(_||_)|(/_ + + 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_TRANSFORMS_HPP_INCLUDED__ +#define CONTINUABLE_TRANSFORMS_HPP_INCLUDED__ + +#include +#include + +namespace cti { +/// Returns a transform that if applied to a continuable, +/// it will start the continuation chain and returns the asynchronous +/// result as `std::future<...>`. +/// +/// \returns Returns a `std::future<...>` which becomes ready as soon +/// as the the continuation chain has finished. +/// The signature of the future depends on the result type: +/// | Continuation type | Return type | +/// | : ------------------------------- | : -------------------------------- | +/// | `continuable_base with <>` | `std::future` | +/// | `continuable_base with ` | `std::future` | +/// | `continuable_base with ` | `std::future>` | +/// +/// \alert If exceptions are used, exceptions that are thrown are forwarded +/// to the returned future. If there are no exceptions supported, +/// you shall not pass any errors to the end of the asynchronous +/// call chain! +/// Otherwise this will yield a trap that causes application exit. +/// +/// \since version 2.0.0 +inline auto to_future() { + return [](auto&& continuable) { + using detail::transforms::as_future; + return as_future(std::forward(continuable)); + }; +} +} // namespace cti + +#endif // CONTINUABLE_TRANSFORMS_HPP_INCLUDED__ diff --git a/test/playground/CMakeLists.txt b/test/playground/CMakeLists.txt index 8be7db5..0ef1494 100644 --- a/test/playground/CMakeLists.txt +++ b/test/playground/CMakeLists.txt @@ -3,6 +3,7 @@ set(LIB_SOURCES ${CMAKE_SOURCE_DIR}/include/continuable/continuable-base.hpp ${CMAKE_SOURCE_DIR}/include/continuable/continuable-trait.hpp ${CMAKE_SOURCE_DIR}/include/continuable/continuable-promise-base.hpp + ${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/base.hpp diff --git a/test/playground/test-playground.cpp b/test/playground/test-playground.cpp index 9716591..cc1a565 100644 --- a/test/playground/test-playground.cpp +++ b/test/playground/test-playground.cpp @@ -94,6 +94,10 @@ int main(int, char**) { }) .fail([](std::error_condition) { // ... + }) + .apply([](auto&& me) { + // ... + return std::forward(me); }); (http_request("github.com") || http_request("github.com")) diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt index e518179..affd20f 100644 --- a/test/unit-test/CMakeLists.txt +++ b/test/unit-test/CMakeLists.txt @@ -6,14 +6,14 @@ foreach(STEP RANGE 4) ${CMAKE_CURRENT_LIST_DIR}/test-continuable.hpp ${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-futurize.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-base-errors.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-base-partial.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-connection-all.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-connection-any.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-connection-seq.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-erasure.cpp - ${CMAKE_CURRENT_LIST_DIR}/test-continuable-regression.cpp) + ${CMAKE_CURRENT_LIST_DIR}/test-continuable-regression.cpp + ${CMAKE_CURRENT_LIST_DIR}/test-continuable-transforms.cpp) target_link_libraries(${PROJECT_NAME} PRIVATE diff --git a/test/unit-test/test-continuable-base-futurize.cpp b/test/unit-test/test-continuable-transforms.cpp similarity index 91% rename from test/unit-test/test-continuable-base-futurize.cpp rename to test/unit-test/test-continuable-transforms.cpp index c5f237e..0ec30a2 100644 --- a/test/unit-test/test-continuable-base-futurize.cpp +++ b/test/unit-test/test-continuable-transforms.cpp @@ -21,6 +21,8 @@ SOFTWARE. **/ +#include + #include "test-continuable.hpp" using namespace cti; @@ -36,7 +38,7 @@ TYPED_TEST(single_dimension_tests, are_convertible_to_futures) { }; { - auto future = this->supply().futurize(); + auto future = this->supply().apply(cti::to_future()); ASSERT_TRUE(is_ready(future)); future.get(); } @@ -47,7 +49,7 @@ TYPED_TEST(single_dimension_tests, are_convertible_to_futures) { // ... return 0xFD; }) - .futurize(); + .apply(cti::to_future()); ASSERT_TRUE(is_ready(future)); EXPECT_EQ(future.get(), 0xFD); @@ -61,7 +63,7 @@ TYPED_TEST(single_dimension_tests, are_convertible_to_futures) { // ... return canary; }) - .futurize(); + .apply(cti::to_future()); ASSERT_TRUE(is_ready(future)); EXPECT_EQ(future.get(), canary);