flow_many_sender.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #pragma once
  2. /*
  3. * Copyright 2018-present Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <folly/experimental/pushmi/flow_receiver.h>
  18. #include <folly/experimental/pushmi/executor.h>
  19. #include <folly/experimental/pushmi/trampoline.h>
  20. namespace pushmi {
  21. template <class PE, class PV, class E, class... VN>
  22. class any_flow_many_sender {
  23. union data {
  24. void* pobj_ = nullptr;
  25. char buffer_[sizeof(std::tuple<VN...>)]; // can hold a V in-situ
  26. } data_{};
  27. template <class Wrapped>
  28. static constexpr bool insitu() {
  29. return sizeof(Wrapped) <= sizeof(data::buffer_) &&
  30. std::is_nothrow_move_constructible<Wrapped>::value;
  31. }
  32. struct vtable {
  33. static void s_op(data&, data*) {}
  34. static any_executor<E> s_executor(data&) { return {}; }
  35. static void s_submit(data&, any_flow_receiver<PE, PV, E, VN...>) {}
  36. void (*op_)(data&, data*) = vtable::s_op;
  37. any_executor<E> (*executor_)(data&) = vtable::s_executor;
  38. void (*submit_)(data&, any_flow_receiver<PE, PV, E, VN...>) = vtable::s_submit;
  39. };
  40. static constexpr vtable const noop_ {};
  41. vtable const* vptr_ = &noop_;
  42. template <class Wrapped>
  43. any_flow_many_sender(Wrapped obj, std::false_type) : any_flow_many_sender() {
  44. struct s {
  45. static void op(data& src, data* dst) {
  46. if (dst)
  47. dst->pobj_ = std::exchange(src.pobj_, nullptr);
  48. delete static_cast<Wrapped const*>(src.pobj_);
  49. }
  50. static any_executor<E> executor(data& src) {
  51. return any_executor<E>{::pushmi::executor(*static_cast<Wrapped*>(src.pobj_))};
  52. }
  53. static void submit(data& src, any_flow_receiver<PE, PV, E, VN...> out) {
  54. ::pushmi::submit(*static_cast<Wrapped*>(src.pobj_), std::move(out));
  55. }
  56. };
  57. static const vtable vtbl{s::op, s::executor, s::submit};
  58. data_.pobj_ = new Wrapped(std::move(obj));
  59. vptr_ = &vtbl;
  60. }
  61. template <class Wrapped>
  62. any_flow_many_sender(Wrapped obj, std::true_type) noexcept
  63. : any_flow_many_sender() {
  64. struct s {
  65. static void op(data& src, data* dst) {
  66. if (dst)
  67. new (dst->buffer_) Wrapped(
  68. std::move(*static_cast<Wrapped*>((void*)src.buffer_)));
  69. static_cast<Wrapped const*>((void*)src.buffer_)->~Wrapped();
  70. }
  71. static any_executor<E> executor(data& src) {
  72. return any_executor<E>{::pushmi::executor(*static_cast<Wrapped*>((void*)src.buffer_))};
  73. }
  74. static void submit(data& src, any_flow_receiver<PE, PV, E, VN...> out) {
  75. ::pushmi::submit(
  76. *static_cast<Wrapped*>((void*)src.buffer_),
  77. std::move(out));
  78. }
  79. };
  80. static const vtable vtbl{s::op, s::executor, s::submit};
  81. new (data_.buffer_) Wrapped(std::move(obj));
  82. vptr_ = &vtbl;
  83. }
  84. template <class T, class U = std::decay_t<T>>
  85. using wrapped_t =
  86. std::enable_if_t<!std::is_same<U, any_flow_many_sender>::value, U>;
  87. public:
  88. using properties = property_set<is_sender<>, is_flow<>, is_many<>>;
  89. any_flow_many_sender() = default;
  90. any_flow_many_sender(any_flow_many_sender&& that) noexcept
  91. : any_flow_many_sender() {
  92. that.vptr_->op_(that.data_, &data_);
  93. std::swap(that.vptr_, vptr_);
  94. }
  95. PUSHMI_TEMPLATE (class Wrapped)
  96. (requires FlowSender<wrapped_t<Wrapped>, is_many<>>)
  97. explicit any_flow_many_sender(Wrapped obj) noexcept(insitu<Wrapped>())
  98. : any_flow_many_sender{std::move(obj), bool_<insitu<Wrapped>()>{}} {}
  99. ~any_flow_many_sender() {
  100. vptr_->op_(data_, nullptr);
  101. }
  102. any_flow_many_sender& operator=(any_flow_many_sender&& that) noexcept {
  103. this->~any_flow_many_sender();
  104. new ((void*)this) any_flow_many_sender(std::move(that));
  105. return *this;
  106. }
  107. any_executor<E> executor() {
  108. return vptr_->executor_(data_);
  109. }
  110. void submit(any_flow_receiver<PE, PV, E, VN...> out) {
  111. vptr_->submit_(data_, std::move(out));
  112. }
  113. };
  114. // Class static definitions:
  115. template <class PE, class PV, class E, class... VN>
  116. constexpr typename any_flow_many_sender<PE, PV, E, VN...>::vtable const
  117. any_flow_many_sender<PE, PV, E, VN...>::noop_;
  118. template <class SF, class EXF>
  119. class flow_many_sender<SF, EXF> {
  120. SF sf_;
  121. EXF exf_;
  122. public:
  123. using properties = property_set<is_sender<>, is_flow<>, is_many<>>;
  124. constexpr flow_many_sender() = default;
  125. constexpr explicit flow_many_sender(SF sf)
  126. : sf_(std::move(sf)) {}
  127. constexpr flow_many_sender(SF sf, EXF exf)
  128. : sf_(std::move(sf)), exf_(std::move(exf)) {}
  129. auto executor() { return exf_(); }
  130. PUSHMI_TEMPLATE(class Out)
  131. (requires FlowReceiver<Out> && Invocable<SF&, Out>)
  132. void submit(Out out) {
  133. sf_(std::move(out));
  134. }
  135. };
  136. template <PUSHMI_TYPE_CONSTRAINT(Sender<is_many<>, is_flow<>>) Data, class DSF, class DEXF>
  137. class flow_many_sender<Data, DSF, DEXF> {
  138. Data data_;
  139. DSF sf_;
  140. DEXF exf_;
  141. public:
  142. using properties = property_set_insert_t<properties_t<Data>, property_set<is_sender<>, is_flow<>, is_many<>>>;
  143. constexpr flow_many_sender() = default;
  144. constexpr explicit flow_many_sender(Data data)
  145. : data_(std::move(data)) {}
  146. constexpr flow_many_sender(Data data, DSF sf)
  147. : data_(std::move(data)), sf_(std::move(sf)) {}
  148. constexpr flow_many_sender(Data data, DSF sf, DEXF exf)
  149. : data_(std::move(data)), sf_(std::move(sf)), exf_(std::move(exf)) {}
  150. auto executor() { return exf_(data_); }
  151. PUSHMI_TEMPLATE(class Out)
  152. (requires PUSHMI_EXP(lazy::FlowReceiver<Out> PUSHMI_AND
  153. lazy::Invocable<DSF&, Data&, Out>))
  154. void submit(Out out) {
  155. sf_(data_, std::move(out));
  156. }
  157. };
  158. template <>
  159. class flow_many_sender<>
  160. : public flow_many_sender<ignoreSF, trampolineEXF> {
  161. public:
  162. flow_many_sender() = default;
  163. };
  164. ////////////////////////////////////////////////////////////////////////////////
  165. // make_flow_many_sender
  166. PUSHMI_INLINE_VAR constexpr struct make_flow_many_sender_fn {
  167. inline auto operator()() const {
  168. return flow_many_sender<ignoreSF, trampolineEXF>{};
  169. }
  170. PUSHMI_TEMPLATE(class SF)
  171. (requires True<> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  172. auto operator()(SF sf) const {
  173. return flow_many_sender<SF, trampolineEXF>{std::move(sf)};
  174. }
  175. PUSHMI_TEMPLATE(class SF, class EXF)
  176. (requires True<> && Invocable<EXF&> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  177. auto operator()(SF sf, EXF exf) const {
  178. return flow_many_sender<SF, EXF>{std::move(sf), std::move(exf)};
  179. }
  180. PUSHMI_TEMPLATE(class Data)
  181. (requires True<> && Sender<Data, is_many<>, is_flow<>>)
  182. auto operator()(Data d) const {
  183. return flow_many_sender<Data, passDSF, passDEXF>{std::move(d)};
  184. }
  185. PUSHMI_TEMPLATE(class Data, class DSF)
  186. (requires Sender<Data, is_many<>, is_flow<>>)
  187. auto operator()(Data d, DSF sf) const {
  188. return flow_many_sender<Data, DSF, passDEXF>{std::move(d), std::move(sf)};
  189. }
  190. PUSHMI_TEMPLATE(class Data, class DSF, class DEXF)
  191. (requires Sender<Data, is_many<>, is_flow<>> && Invocable<DEXF&, Data&>)
  192. auto operator()(Data d, DSF sf, DEXF exf) const {
  193. return flow_many_sender<Data, DSF, DEXF>{std::move(d), std::move(sf), std::move(exf)};
  194. }
  195. } const make_flow_many_sender {};
  196. ////////////////////////////////////////////////////////////////////////////////
  197. // deduction guides
  198. #if __cpp_deduction_guides >= 201703
  199. flow_many_sender() -> flow_many_sender<ignoreSF, trampolineEXF>;
  200. PUSHMI_TEMPLATE(class SF)
  201. (requires True<> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  202. flow_many_sender(SF) -> flow_many_sender<SF, trampolineEXF>;
  203. PUSHMI_TEMPLATE(class SF, class EXF)
  204. (requires True<> && Invocable<EXF&> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  205. flow_many_sender(SF, EXF) -> flow_many_sender<SF, EXF>;
  206. PUSHMI_TEMPLATE(class Data)
  207. (requires True<> && Sender<Data, is_many<>, is_flow<>>)
  208. flow_many_sender(Data) -> flow_many_sender<Data, passDSF, passDEXF>;
  209. PUSHMI_TEMPLATE(class Data, class DSF)
  210. (requires Sender<Data, is_many<>, is_flow<>>)
  211. flow_many_sender(Data, DSF) -> flow_many_sender<Data, DSF, passDEXF>;
  212. PUSHMI_TEMPLATE(class Data, class DSF, class DEXF)
  213. (requires Sender<Data, is_many<>, is_flow<>> && Invocable<DEXF&, Data&>)
  214. flow_many_sender(Data, DSF, DEXF) -> flow_many_sender<Data, DSF, DEXF>;
  215. #endif
  216. template<>
  217. struct construct_deduced<flow_many_sender>
  218. : make_flow_many_sender_fn {};
  219. // // TODO constrain me
  220. // template <class V, class E = std::exception_ptr, Sender Wrapped>
  221. // auto erase_cast(Wrapped w) {
  222. // return flow_many_sender<V, E>{std::move(w)};
  223. // }
  224. } // namespace pushmi