mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 16:56:44 +08:00
Enable and document the new when_all
This commit is contained in:
parent
dd1b605d95
commit
9891543b1f
@ -43,20 +43,72 @@
|
||||
#include <continuable/detail/util.hpp>
|
||||
|
||||
namespace cti {
|
||||
/// Connects the given continuables with an *all* logic.
|
||||
/// Connects the given arguments with an all logic.
|
||||
/// All continuables contained inside the given nested pack are
|
||||
/// invoked at once. On completion the final handler is called
|
||||
/// with the aggregated result of all continuables.
|
||||
///
|
||||
/// \param continuables The continuable_base objects to connect.
|
||||
/// Requires at least 2 objects to connect.
|
||||
/// \param args Arbitrary arguments which are connected.
|
||||
/// Every type is allowed as arguments, continuables may be
|
||||
/// contained inside tuple like types (`std::tuple`)
|
||||
/// or in homogeneous containers such as `std::vector`.
|
||||
/// Non continuable arguments are preserved and passed
|
||||
/// to the final result as shown below:
|
||||
/// ```cpp
|
||||
/// cti::when_all(
|
||||
/// cti::make_ready_continuable(0, 1),
|
||||
/// 2, //< See this plain value
|
||||
/// std::vector<cti::continuable<int>>{cti::make_ready_continuable(3),
|
||||
/// cti::make_ready_continuable(4)},
|
||||
/// std::make_tuple(std::make_tuple(cti::make_ready_continuable(5))))
|
||||
/// .then([](int r0, int r1, int r2, std::vector<int> r34,
|
||||
/// std::tuple<std::tuple<int>> r5) {
|
||||
/// // ...
|
||||
/// });
|
||||
/// ```
|
||||
///
|
||||
/// \see continuable_base::operator && for details.
|
||||
/// \see continuable_base::operator&& for details.
|
||||
///
|
||||
/// \since 1.1.0
|
||||
template <typename... Continuables>
|
||||
auto when_all(Continuables&&... continuables) {
|
||||
static_assert(sizeof...(continuables) >= 2,
|
||||
"Requires at least 2 continuables!");
|
||||
return CONTINUABLE_FOLD_EXPRESSION(
|
||||
&&, std::forward<Continuables>(continuables)...);
|
||||
template <typename... Args>
|
||||
auto when_all(Args&&... args) {
|
||||
return detail::composition::apply_composition(
|
||||
detail::composition::composition_strategy_all_tag{},
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/// Connects the given arguments with an all logic.
|
||||
/// The content of the iterator is moved out and converted
|
||||
/// to a temporary `std::vector` which is then passed to when_all.
|
||||
///
|
||||
/// ```cpp
|
||||
/// std::vector<cti::continuable<int>> v{cti::make_ready_continuable(0),
|
||||
/// cti::make_ready_continuable(1)};
|
||||
///
|
||||
/// cti::when_all(v.begin(), v.end())
|
||||
/// .then([](std::vector<int> r01) {
|
||||
/// // ...
|
||||
/// });
|
||||
/// ```
|
||||
///
|
||||
/// \param begin The begin iterator to the range which will be moved out
|
||||
/// and used as the arguments to the all connection
|
||||
///
|
||||
/// \param end The end iterator to the range which will be moved out
|
||||
/// and used as the arguments to the all connection
|
||||
///
|
||||
/// \see when_all for details.
|
||||
///
|
||||
/// \attention Prefer to invoke when_all with the whole container the
|
||||
/// iterators were taken from, since this saves us
|
||||
/// the creation of a temporary storage.
|
||||
///
|
||||
/// \since 3.0.0
|
||||
template <
|
||||
typename Iterator,
|
||||
std::enable_if_t<detail::range::is_iterator<Iterator>::value>* = nullptr>
|
||||
auto when_all(Iterator begin, Iterator end) {
|
||||
return when_all(detail::range::persist_range(begin, end));
|
||||
}
|
||||
|
||||
/// Connects the given arguments with a sequential logic.
|
||||
|
||||
@ -177,9 +177,14 @@ struct materializer<
|
||||
}
|
||||
};
|
||||
|
||||
struct prepare_continuables {
|
||||
class prepare_continuables {
|
||||
util::ownership& ownership_;
|
||||
|
||||
public:
|
||||
explicit constexpr prepare_continuables(util::ownership& ownership)
|
||||
: ownership_(ownership) {
|
||||
}
|
||||
|
||||
template <typename Continuable,
|
||||
std::enable_if_t<base::is_continuable<
|
||||
std::decay_t<Continuable>>::value>* = nullptr>
|
||||
|
||||
@ -158,8 +158,7 @@ int main(int, char**) {
|
||||
// ...
|
||||
});
|
||||
|
||||
composition::apply_composition(
|
||||
composition::composition_strategy_all_tag{},
|
||||
cti::when_all(
|
||||
cti::make_ready_continuable(0, 1), 2, //< See this plain value
|
||||
std::vector<cti::continuable<int>>{cti::make_ready_continuable(3),
|
||||
cti::make_ready_continuable(4)},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user