flow_single_sender.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 E, class... VN>
  22. class any_flow_single_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, std::ptrdiff_t, 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, std::ptrdiff_t, E, VN...>) =
  39. vtable::s_submit;
  40. };
  41. static constexpr vtable const noop_ {};
  42. vtable const* vptr_ = &noop_;
  43. template <class Wrapped>
  44. any_flow_single_sender(Wrapped obj, std::false_type)
  45. : any_flow_single_sender() {
  46. struct s {
  47. static void op(data& src, data* dst) {
  48. if (dst)
  49. dst->pobj_ = std::exchange(src.pobj_, nullptr);
  50. delete static_cast<Wrapped const*>(src.pobj_);
  51. }
  52. static any_executor<E> executor(data& src) {
  53. return any_executor<E>{
  54. ::pushmi::executor(*static_cast<Wrapped*>(src.pobj_))};
  55. }
  56. static void submit(
  57. data& src,
  58. any_flow_receiver<PE, std::ptrdiff_t, E, VN...> out) {
  59. ::pushmi::submit(*static_cast<Wrapped*>(src.pobj_), std::move(out));
  60. }
  61. };
  62. static const vtable vtbl{s::op, s::executor, s::submit};
  63. data_.pobj_ = new Wrapped(std::move(obj));
  64. vptr_ = &vtbl;
  65. }
  66. template <class Wrapped>
  67. any_flow_single_sender(Wrapped obj, std::true_type) noexcept
  68. : any_flow_single_sender() {
  69. struct s {
  70. static void op(data& src, data* dst) {
  71. if (dst)
  72. new (dst->buffer_)
  73. Wrapped(std::move(*static_cast<Wrapped*>((void*)src.buffer_)));
  74. static_cast<Wrapped const*>((void*)src.buffer_)->~Wrapped();
  75. }
  76. static any_executor<E> executor(data& src) {
  77. return any_executor<E>{
  78. ::pushmi::executor(*static_cast<Wrapped*>((void*)src.buffer_))};
  79. }
  80. static void submit(
  81. data& src,
  82. any_flow_receiver<PE, std::ptrdiff_t, E, VN...> out) {
  83. ::pushmi::submit(
  84. *static_cast<Wrapped*>((void*)src.buffer_), std::move(out));
  85. }
  86. };
  87. static const vtable vtbl{s::op, s::executor, s::submit};
  88. new (data_.buffer_) Wrapped(std::move(obj));
  89. vptr_ = &vtbl;
  90. }
  91. template <class T, class U = std::decay_t<T>>
  92. using wrapped_t =
  93. std::enable_if_t<!std::is_same<U, any_flow_single_sender>::value, U>;
  94. public:
  95. using properties = property_set<is_sender<>, is_flow<>, is_single<>>;
  96. any_flow_single_sender() = default;
  97. any_flow_single_sender(any_flow_single_sender&& that) noexcept
  98. : any_flow_single_sender() {
  99. that.vptr_->op_(that.data_, &data_);
  100. std::swap(that.vptr_, vptr_);
  101. }
  102. PUSHMI_TEMPLATE (class Wrapped)
  103. (requires FlowSender<wrapped_t<Wrapped>, is_single<>>)
  104. explicit any_flow_single_sender(Wrapped obj) noexcept(insitu<Wrapped>())
  105. : any_flow_single_sender{std::move(obj), bool_<insitu<Wrapped>()>{}} {}
  106. ~any_flow_single_sender() {
  107. vptr_->op_(data_, nullptr);
  108. }
  109. any_flow_single_sender& operator=(any_flow_single_sender&& that) noexcept {
  110. this->~any_flow_single_sender();
  111. new ((void*)this) any_flow_single_sender(std::move(that));
  112. return *this;
  113. }
  114. any_executor<E> executor() {
  115. return vptr_->executor_(data_);
  116. }
  117. void submit(any_flow_receiver<PE, std::ptrdiff_t, E, VN...> out) {
  118. vptr_->submit_(data_, std::move(out));
  119. }
  120. };
  121. // Class static definitions:
  122. template <class PE, class E, class... VN>
  123. constexpr typename any_flow_single_sender<PE, E, VN...>::vtable const
  124. any_flow_single_sender<PE, E, VN...>::noop_;
  125. template <class SF, class EXF>
  126. class flow_single_sender<SF, EXF> {
  127. SF sf_;
  128. EXF exf_;
  129. public:
  130. using properties = property_set<is_sender<>, is_flow<>, is_single<>>;
  131. constexpr flow_single_sender() = default;
  132. constexpr explicit flow_single_sender(SF sf)
  133. : sf_(std::move(sf)) {}
  134. constexpr flow_single_sender(SF sf, EXF exf)
  135. : sf_(std::move(sf)), exf_(std::move(exf)) {}
  136. auto executor() { return exf_(); }
  137. PUSHMI_TEMPLATE(class Out)
  138. (requires Receiver<Out> && Invocable<SF&, Out>)
  139. void submit(Out out) {
  140. sf_(std::move(out));
  141. }
  142. };
  143. template <
  144. PUSHMI_TYPE_CONSTRAINT(Sender<is_single<>, is_flow<>>) Data,
  145. class DSF,
  146. class DEXF>
  147. class flow_single_sender<Data, DSF, DEXF> {
  148. Data data_;
  149. DSF sf_;
  150. DEXF exf_;
  151. public:
  152. using properties = property_set_insert_t<
  153. properties_t<Data>,
  154. property_set<is_sender<>, is_flow<>, is_single<>>>;
  155. constexpr flow_single_sender() = default;
  156. constexpr explicit flow_single_sender(Data data)
  157. : data_(std::move(data)) {}
  158. constexpr flow_single_sender(Data data, DSF sf)
  159. : data_(std::move(data)), sf_(std::move(sf)) {}
  160. constexpr flow_single_sender(Data data, DSF sf, DEXF exf)
  161. : data_(std::move(data)), sf_(std::move(sf)), exf_(std::move(exf)) {}
  162. auto executor() { return exf_(data_); }
  163. PUSHMI_TEMPLATE(class Out)
  164. (requires PUSHMI_EXP(lazy::Receiver<Out> PUSHMI_AND
  165. lazy::Invocable<DSF&, Data&, Out>))
  166. void submit(Out out) {
  167. sf_(data_, std::move(out));
  168. }
  169. };
  170. template <>
  171. class flow_single_sender<>
  172. : public flow_single_sender<ignoreSF, trampolineEXF> {
  173. public:
  174. flow_single_sender() = default;
  175. };
  176. ////////////////////////////////////////////////////////////////////////////////
  177. // make_flow_single_sender
  178. PUSHMI_INLINE_VAR constexpr struct make_flow_single_sender_fn {
  179. inline auto operator()() const {
  180. return flow_single_sender<ignoreSF, trampolineEXF>{};
  181. }
  182. PUSHMI_TEMPLATE(class SF)
  183. (requires True<> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  184. auto operator()(SF sf) const {
  185. return flow_single_sender<SF, trampolineEXF>{std::move(sf)};
  186. }
  187. PUSHMI_TEMPLATE(class SF, class EXF)
  188. (requires True<> && Invocable<EXF&>
  189. PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  190. auto operator()(SF sf, EXF exf) const {
  191. return flow_single_sender<SF, EXF>{std::move(sf), std::move(exf)};
  192. }
  193. PUSHMI_TEMPLATE(class Data)
  194. (requires True<> && Sender<Data, is_single<>, is_flow<>>)
  195. auto operator()(Data d) const {
  196. return flow_single_sender<Data, passDSF, passDEXF>{std::move(d)};
  197. }
  198. PUSHMI_TEMPLATE(class Data, class DSF)
  199. (requires Sender<Data, is_single<>, is_flow<>>)
  200. auto operator()(Data d, DSF sf) const {
  201. return flow_single_sender<Data, DSF, passDEXF>{std::move(d), std::move(sf)};
  202. }
  203. PUSHMI_TEMPLATE(class Data, class DSF, class DEXF)
  204. (requires Sender<Data, is_single<>, is_flow<>> && Invocable<DEXF&, Data&>)
  205. auto operator()(Data d, DSF sf, DEXF exf) const {
  206. return flow_single_sender<Data, DSF, DEXF>{std::move(d),
  207. std::move(sf), std::move(exf)};
  208. }
  209. } const make_flow_single_sender {};
  210. ////////////////////////////////////////////////////////////////////////////////
  211. // deduction guides
  212. #if __cpp_deduction_guides >= 201703
  213. flow_single_sender() -> flow_single_sender<ignoreSF, trampolineEXF>;
  214. PUSHMI_TEMPLATE(class SF)
  215. (requires True<> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  216. flow_single_sender(SF) -> flow_single_sender<SF, trampolineEXF>;
  217. PUSHMI_TEMPLATE(class SF, class EXF)
  218. (requires True<> && Invocable<EXF&>
  219. PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  220. flow_single_sender(SF, EXF) -> flow_single_sender<SF, EXF>;
  221. PUSHMI_TEMPLATE(class Data)
  222. (requires True<> && Sender<Data, is_single<>, is_flow<>>)
  223. flow_single_sender(Data) -> flow_single_sender<Data, passDSF, passDEXF>;
  224. PUSHMI_TEMPLATE(class Data, class DSF)
  225. (requires Sender<Data, is_single<>, is_flow<>>)
  226. flow_single_sender(Data, DSF) -> flow_single_sender<Data, DSF, passDEXF>;
  227. PUSHMI_TEMPLATE(class Data, class DSF, class DEXF)
  228. (requires Sender<Data, is_single<>, is_flow<>> && Invocable<DEXF&, Data&>)
  229. flow_single_sender(Data, DSF, DEXF) -> flow_single_sender<Data, DSF, DEXF>;
  230. #endif
  231. template<>
  232. struct construct_deduced<flow_single_sender>
  233. : make_flow_single_sender_fn {};
  234. } // namespace pushmi