Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify mmdeploy::Expand sender adaptor #617

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 22 additions & 49 deletions csrc/mmdeploy/execution/expand.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,39 @@ namespace mmdeploy {

namespace _expand {

template <typename Sender, typename Receiver>
struct _Operation {
struct type;
};
template <typename Sender, typename Receiver>
using operation_t = typename _Operation<Sender, remove_cvref_t<Receiver>>::type;

template <typename Sender, typename Receiver>
struct _Receiver {
struct type;
struct type {
Receiver receiver_;
template <class Tuple>
friend void tag_invoke(set_value_t, type&& self, Tuple&& tup) noexcept {
std::apply(
[&](auto&&... args) {
SetValue((Receiver &&) self.receiver_, (decltype(args)&&)args...);
},
(Tuple &&) tup);
}
};
};
template <typename Sender, typename Receiver>
using receiver_t = typename _Receiver<Sender, Receiver>::type;

template <typename Sender, typename Receiver>
struct _Receiver<Sender, Receiver>::type {
operation_t<Sender, Receiver>* op_state_;

template <class Tuple>
friend void tag_invoke(set_value_t, type&& self, Tuple&& tup) noexcept {
std::apply(
[&](auto&&... args) {
SetValue((Receiver &&) self.op_state_->receiver_, (decltype(args)&&)args...);
},
(Tuple &&) tup);
}
};

template <typename Sender, typename Receiver>
struct _Operation<Sender, Receiver>::type {
connect_result_t<Sender, receiver_t<Sender, Receiver>> op_state2_;
Receiver receiver_;

template <typename Sender2>
type(Sender2&& sender, Receiver&& receiver)
: op_state2_(Connect((Sender2 &&) sender, receiver_t<Sender, Receiver>{this})),
receiver_((Receiver &&) receiver) {}

friend void tag_invoke(start_t, type& op_state) { Start(op_state.op_state2_); }
};
using receiver_t = typename _Receiver<Sender, remove_cvref_t<Receiver>>::type;

template <typename Sender>
struct _Sender {
struct type;
struct type {
using value_types = std::tuple_element_t<0, completion_signatures_of_t<Sender>>;
Sender sender_;

template <typename Self, typename Receiver, _decays_to<Self, type, bool> = true>
friend auto tag_invoke(connect_t, Self&& self, Receiver&& receiver) {
return Connect(((Self &&) self).sender_,
receiver_t<Sender, Receiver>{(Receiver &&) receiver});
}
};
};
template <typename Sender>
using sender_t = typename _Sender<remove_cvref_t<Sender>>::type;

template <typename Sender>
struct _Sender<Sender>::type {
using value_types = std::tuple_element_t<0, completion_signatures_of_t<Sender>>;
Sender sender_;

template <typename Self, typename Receiver, _decays_to<Self, type, bool> = true>
friend auto tag_invoke(connect_t, Self&& self, Receiver&& receiver)
-> operation_t<Sender, Receiver> {
return operation_t<Sender, Receiver>(((Self &&) self).sender_, (Receiver &&) receiver);
}
};

struct expand_t {
template <typename Sender, std::enable_if_t<_is_sender<Sender>, int> = 0>
auto operator()(Sender&& sender) const {
Expand Down