mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 08:46:44 +08:00
Move generic transforms out of the main class
* Introduce apply to use generic transforms which are defined out of class.
This commit is contained in:
parent
ec65ce0bf3
commit
0eb272a46f
@ -40,7 +40,6 @@
|
||||
#include <continuable/detail/base.hpp>
|
||||
#include <continuable/detail/composition.hpp>
|
||||
#include <continuable/detail/traits.hpp>
|
||||
#include <continuable/detail/transforms.hpp>
|
||||
#include <continuable/detail/types.hpp>
|
||||
#include <continuable/detail/util.hpp>
|
||||
|
||||
@ -312,6 +311,19 @@ public:
|
||||
std::forward<E>(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 <typename T>
|
||||
auto apply(T&& transform) && {
|
||||
return std::forward<T>(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<void>` |
|
||||
/// | `continuable_base with <Arg>` | `std::future<Arg>` |
|
||||
/// | `continuable_base with <Args...>` | `std::future<std::tuple<Args...>>` |
|
||||
///
|
||||
/// \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.
|
||||
///
|
||||
|
||||
66
include/continuable/continuable-transforms.hpp
Normal file
66
include/continuable/continuable-transforms.hpp
Normal file
@ -0,0 +1,66 @@
|
||||
|
||||
/**
|
||||
|
||||
/~` _ _ _|_. _ _ |_ | _
|
||||
\_,(_)| | | || ||_|(_||_)|(/_
|
||||
|
||||
https://github.com/Naios/continuable
|
||||
v2.0.0
|
||||
|
||||
Copyright(c) 2015 - 2017 Denis Blank <denis.blank at outlook dot com>
|
||||
|
||||
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 <continuable/detail/api.hpp>
|
||||
#include <continuable/detail/transforms.hpp>
|
||||
|
||||
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<void>` |
|
||||
/// | `continuable_base with <Arg>` | `std::future<Arg>` |
|
||||
/// | `continuable_base with <Args...>` | `std::future<std::tuple<Args...>>` |
|
||||
///
|
||||
/// \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<decltype(continuable)>(continuable));
|
||||
};
|
||||
}
|
||||
} // namespace cti
|
||||
|
||||
#endif // CONTINUABLE_TRANSFORMS_HPP_INCLUDED__
|
||||
@ -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
|
||||
|
||||
@ -94,6 +94,10 @@ int main(int, char**) {
|
||||
})
|
||||
.fail([](std::error_condition) {
|
||||
// ...
|
||||
})
|
||||
.apply([](auto&& me) {
|
||||
// ...
|
||||
return std::forward<decltype(me)>(me);
|
||||
});
|
||||
|
||||
(http_request("github.com") || http_request("github.com"))
|
||||
|
||||
@ -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
|
||||
|
||||
@ -21,6 +21,8 @@
|
||||
SOFTWARE.
|
||||
**/
|
||||
|
||||
#include <continuable/continuable-transforms.hpp>
|
||||
|
||||
#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);
|
||||
Loading…
x
Reference in New Issue
Block a user