mirror of
https://github.com/Naios/continuable.git
synced 2026-02-14 22:29:48 +08:00
Implement the nested when_any connection
This commit is contained in:
parent
cdbc332287
commit
9ecbb00f5a
@ -39,6 +39,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include <continuable/continuable-promise-base.hpp>
|
#include <continuable/continuable-promise-base.hpp>
|
||||||
|
#include <continuable/continuable-traverse.hpp>
|
||||||
#include <continuable/detail/base.hpp>
|
#include <continuable/detail/base.hpp>
|
||||||
#include <continuable/detail/container-category.hpp>
|
#include <continuable/detail/container-category.hpp>
|
||||||
#include <continuable/detail/hints.hpp>
|
#include <continuable/detail/hints.hpp>
|
||||||
@ -94,14 +95,6 @@ private:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Creates a submitter that continues `any` chains
|
|
||||||
template <typename Callback>
|
|
||||||
auto make_any_result_submitter(Callback&& callback) {
|
|
||||||
return std::make_shared<
|
|
||||||
any_result_submitter<std::decay_t<decltype(callback)>>>(
|
|
||||||
std::forward<decltype(callback)>(callback));
|
|
||||||
}
|
|
||||||
|
|
||||||
struct result_deducer {
|
struct result_deducer {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static auto deduce_one(std::false_type, traits::identity<T>) {
|
static auto deduce_one(std::false_type, traits::identity<T>) {
|
||||||
@ -159,6 +152,21 @@ struct result_deducer {
|
|||||||
return deduce_tuple_like(i, id);
|
return deduce_tuple_like(i, id);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename Submitter>
|
||||||
|
struct continuable_dispatcher {
|
||||||
|
std::shared_ptr<Submitter>& submitter;
|
||||||
|
|
||||||
|
template <typename Continuable,
|
||||||
|
std::enable_if_t<base::is_continuable<
|
||||||
|
std::decay_t<Continuable>>::value>* = nullptr>
|
||||||
|
void operator()(Continuable&& continuable) const {
|
||||||
|
// Retrieve a callback from the submitter and attach it to the continuable
|
||||||
|
std::forward<Continuable>(continuable)
|
||||||
|
.next(submitter->create_callback())
|
||||||
|
.done();
|
||||||
|
}
|
||||||
|
};
|
||||||
} // namespace any
|
} // namespace any
|
||||||
|
|
||||||
/// Finalizes the any logic of a given composition
|
/// Finalizes the any logic of a given composition
|
||||||
@ -175,19 +183,17 @@ struct composition_finalizer<composition_strategy_any_tag> {
|
|||||||
static auto finalize(Composition&& composition) {
|
static auto finalize(Composition&& composition) {
|
||||||
return [composition = std::forward<Composition>(composition)](
|
return [composition = std::forward<Composition>(composition)](
|
||||||
auto&& callback) mutable {
|
auto&& callback) mutable {
|
||||||
|
|
||||||
|
using submitter_t =
|
||||||
|
any::any_result_submitter<std::decay_t<decltype(callback)>>;
|
||||||
|
|
||||||
// Create the submitter which calls the given callback once at the
|
// Create the submitter which calls the given callback once at the
|
||||||
// first callback invocation.
|
// first callback invocation.
|
||||||
auto submitter = any::make_any_result_submitter(
|
auto submitter = std::make_shared<submitter_t>(
|
||||||
std::forward<decltype(callback)>(callback));
|
std::forward<decltype(callback)>(callback));
|
||||||
|
|
||||||
traits::static_for_each_in(std::move(composition),
|
traverse_pack(any::continuable_dispatcher<submitter_t>{submitter},
|
||||||
[&](auto&& entry) mutable {
|
std::move(composition));
|
||||||
// Invoke the continuation with a
|
|
||||||
// submission callback
|
|
||||||
base::attorney::invoke_continuation(
|
|
||||||
std::forward<decltype(entry)>(entry),
|
|
||||||
submitter->create_callback());
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -198,18 +198,6 @@ constexpr auto static_if_impl(std::false_type, Type&& type,
|
|||||||
return std::forward<FalseCallback>(falseCallback)(std::forward<Type>(type));
|
return std::forward<FalseCallback>(falseCallback)(std::forward<Type>(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Applies the given callable to all objects in a sequence
|
|
||||||
template <typename C>
|
|
||||||
struct apply_to_all {
|
|
||||||
C callable;
|
|
||||||
|
|
||||||
template <typename... T>
|
|
||||||
constexpr void operator()(T&&... args) {
|
|
||||||
(void)std::initializer_list<int>{
|
|
||||||
0, ((void)callable(std::forward<decltype(args)>(args)), 0)...};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Evaluates to the size of the given tuple like type,
|
/// Evaluates to the size of the given tuple like type,
|
||||||
// / if the type has no static size it will be one.
|
// / if the type has no static size it will be one.
|
||||||
template <typename T, typename Enable = void>
|
template <typename T, typename Enable = void>
|
||||||
@ -364,16 +352,6 @@ constexpr auto unpack(F&& first_sequenceable, S&& second_sequenceable,
|
|||||||
sequence_of(identity_of(second_sequenceable)));
|
sequence_of(identity_of(second_sequenceable)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Applies the handler function to each element contained in the sequenceable
|
|
||||||
// TODO Maybe crashes MSVC in constexpr mode
|
|
||||||
template <typename Sequenceable, typename Handler>
|
|
||||||
constexpr void static_for_each_in(Sequenceable&& sequenceable,
|
|
||||||
Handler&& handler) {
|
|
||||||
unpack(std::forward<Sequenceable>(sequenceable),
|
|
||||||
detail::apply_to_all<std::decay_t<Handler>>{
|
|
||||||
std::forward<Handler>(handler)});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds the given type at the back of the left sequenceable
|
/// Adds the given type at the back of the left sequenceable
|
||||||
template <typename Left, typename Element>
|
template <typename Left, typename Element>
|
||||||
constexpr auto push(Left&& left, Element&& element) {
|
constexpr auto push(Left&& left, Element&& element) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user