constrained_single_sender.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/receiver.h>
  18. #include <folly/experimental/pushmi/executor.h>
  19. #include <folly/experimental/pushmi/inline.h>
  20. namespace pushmi {
  21. template <class E, class CV, class... VN>
  22. class any_constrained_single_sender {
  23. union data {
  24. void* pobj_ = nullptr;
  25. char buffer_[sizeof(std::promise<int>)]; // 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 CV s_top(data&) { return CV{}; }
  35. static any_constrained_executor<E, CV> s_executor(data&) { return {}; }
  36. static void s_submit(data&, CV, any_receiver<E, VN...>) {}
  37. void (*op_)(data&, data*) = vtable::s_op;
  38. CV (*top_)(data&) = vtable::s_top;
  39. any_constrained_executor<E, CV> (*executor_)(data&) = vtable::s_executor;
  40. void (*submit_)(data&, CV, any_receiver<E, VN...>) = vtable::s_submit;
  41. };
  42. static constexpr vtable const noop_ {};
  43. vtable const* vptr_ = &noop_;
  44. template <class Wrapped>
  45. any_constrained_single_sender(Wrapped obj, std::false_type)
  46. : any_constrained_single_sender() {
  47. struct s {
  48. static void op(data& src, data* dst) {
  49. if (dst)
  50. dst->pobj_ = std::exchange(src.pobj_, nullptr);
  51. delete static_cast<Wrapped const*>(src.pobj_);
  52. }
  53. static CV top(data& src) {
  54. return ::pushmi::top(*static_cast<Wrapped*>(src.pobj_));
  55. }
  56. static any_constrained_executor<E, CV> executor(data& src) {
  57. return any_constrained_executor<E, CV>{::pushmi::executor(*static_cast<Wrapped*>(src.pobj_))};
  58. }
  59. static void submit(data& src, CV at, any_receiver<E, VN...> out) {
  60. ::pushmi::submit(
  61. *static_cast<Wrapped*>(src.pobj_),
  62. std::move(at),
  63. std::move(out));
  64. }
  65. };
  66. static const vtable vtbl{s::op, s::top, s::executor, s::submit};
  67. data_.pobj_ = new Wrapped(std::move(obj));
  68. vptr_ = &vtbl;
  69. }
  70. template <class Wrapped>
  71. any_constrained_single_sender(Wrapped obj, std::true_type) noexcept
  72. : any_constrained_single_sender() {
  73. struct s {
  74. static void op(data& src, data* dst) {
  75. if (dst)
  76. new (dst->buffer_) Wrapped(
  77. std::move(*static_cast<Wrapped*>((void*)src.buffer_)));
  78. static_cast<Wrapped const*>((void*)src.buffer_)->~Wrapped();
  79. }
  80. static CV top(data& src) {
  81. return ::pushmi::top(*static_cast<Wrapped*>((void*)src.buffer_));
  82. }
  83. static any_constrained_executor<E, CV> executor(data& src) {
  84. return any_constrained_executor<E, CV>{::pushmi::executor(*static_cast<Wrapped*>((void*)src.buffer_))};
  85. }
  86. static void submit(data& src, CV cv, any_receiver<E, VN...> out) {
  87. ::pushmi::submit(
  88. *static_cast<Wrapped*>((void*)src.buffer_),
  89. std::move(cv),
  90. std::move(out));
  91. }
  92. };
  93. static const vtable vtbl{s::op, s::top, s::executor, s::submit};
  94. new (data_.buffer_) Wrapped(std::move(obj));
  95. vptr_ = &vtbl;
  96. }
  97. template <class T, class U = std::decay_t<T>>
  98. using wrapped_t =
  99. std::enable_if_t<!std::is_same<U, any_constrained_single_sender>::value, U>;
  100. public:
  101. using properties = property_set<is_constrained<>, is_single<>>;
  102. any_constrained_single_sender() = default;
  103. any_constrained_single_sender(any_constrained_single_sender&& that) noexcept
  104. : any_constrained_single_sender() {
  105. that.vptr_->op_(that.data_, &data_);
  106. std::swap(that.vptr_, vptr_);
  107. }
  108. PUSHMI_TEMPLATE (class Wrapped)
  109. (requires ConstrainedSenderTo<wrapped_t<Wrapped>, any_receiver<E, VN...>>)
  110. explicit any_constrained_single_sender(Wrapped obj) noexcept(insitu<Wrapped>())
  111. : any_constrained_single_sender{std::move(obj), bool_<insitu<Wrapped>()>{}} {
  112. }
  113. ~any_constrained_single_sender() {
  114. vptr_->op_(data_, nullptr);
  115. }
  116. any_constrained_single_sender& operator=(any_constrained_single_sender&& that) noexcept {
  117. this->~any_constrained_single_sender();
  118. new ((void*)this) any_constrained_single_sender(std::move(that));
  119. return *this;
  120. }
  121. CV top() {
  122. return vptr_->top_(data_);
  123. }
  124. any_constrained_executor<E, CV> executor() {
  125. return vptr_->executor_(data_);
  126. }
  127. void submit(CV at, any_receiver<E, VN...> out) {
  128. vptr_->submit_(data_, std::move(at), std::move(out));
  129. }
  130. };
  131. // Class static definitions:
  132. template <class E, class CV, class... VN>
  133. constexpr typename any_constrained_single_sender<E, CV, VN...>::vtable const
  134. any_constrained_single_sender<E, CV, VN...>::noop_;
  135. template<class SF, class ZF, class EXF>
  136. // (requires Invocable<ZF&> && Invocable<EXF&> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  137. class constrained_single_sender<SF, ZF, EXF> {
  138. SF sf_;
  139. ZF zf_;
  140. EXF exf_;
  141. public:
  142. using properties = property_set<is_constrained<>, is_single<>>;
  143. constexpr constrained_single_sender() = default;
  144. constexpr explicit constrained_single_sender(SF sf)
  145. : sf_(std::move(sf)) {}
  146. constexpr constrained_single_sender(SF sf, EXF exf)
  147. : sf_(std::move(sf)), exf_(std::move(exf)) {}
  148. constexpr constrained_single_sender(SF sf, EXF exf, ZF zf)
  149. : sf_(std::move(sf)), zf_(std::move(zf)), exf_(std::move(exf)) {}
  150. auto top() {
  151. return zf_();
  152. }
  153. auto executor() { return exf_(); }
  154. PUSHMI_TEMPLATE(class CV, class Out)
  155. (requires Regular<CV> && Receiver<Out> &&
  156. Invocable<SF&, CV, Out>)
  157. void submit(CV cv, Out out) {
  158. sf_(std::move(cv), std::move(out));
  159. }
  160. };
  161. template <PUSHMI_TYPE_CONSTRAINT(ConstrainedSender<is_single<>>) Data, class DSF, class DZF, class DEXF>
  162. #if __cpp_concepts
  163. requires Invocable<DZF&, Data&> && Invocable<DEXF&, Data&>
  164. #endif
  165. class constrained_single_sender<Data, DSF, DZF, DEXF> {
  166. Data data_;
  167. DSF sf_;
  168. DZF zf_;
  169. DEXF exf_;
  170. public:
  171. using properties = property_set_insert_t<properties_t<Data>, property_set<is_single<>>>;
  172. constexpr constrained_single_sender() = default;
  173. constexpr explicit constrained_single_sender(Data data)
  174. : data_(std::move(data)) {}
  175. constexpr constrained_single_sender(Data data, DSF sf, DEXF exf = DEXF{})
  176. : data_(std::move(data)), sf_(std::move(sf)), exf_(std::move(exf)) {}
  177. constexpr constrained_single_sender(Data data, DSF sf, DEXF exf, DZF zf)
  178. : data_(std::move(data)), sf_(std::move(sf)), zf_(std::move(zf)), exf_(std::move(exf)) {}
  179. auto top() {
  180. return zf_(data_);
  181. }
  182. auto executor() { return exf_(data_); }
  183. PUSHMI_TEMPLATE(class CV, class Out)
  184. (requires Regular<CV> && Receiver<Out> &&
  185. Invocable<DSF&, Data&, CV, Out>)
  186. void submit(CV cv, Out out) {
  187. sf_(data_, std::move(cv), std::move(out));
  188. }
  189. };
  190. template <>
  191. class constrained_single_sender<>
  192. : public constrained_single_sender<ignoreSF, priorityZeroF, inlineConstrainedEXF> {
  193. public:
  194. constrained_single_sender() = default;
  195. };
  196. ////////////////////////////////////////////////////////////////////////////////
  197. // make_constrained_single_sender
  198. PUSHMI_INLINE_VAR constexpr struct make_constrained_single_sender_fn {
  199. inline auto operator()() const {
  200. return constrained_single_sender<ignoreSF, priorityZeroF, inlineConstrainedEXF>{};
  201. }
  202. PUSHMI_TEMPLATE(class SF)
  203. (requires True<> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  204. auto operator()(SF sf) const {
  205. return constrained_single_sender<SF, priorityZeroF, inlineConstrainedEXF>{std::move(sf)};
  206. }
  207. PUSHMI_TEMPLATE (class SF, class EXF)
  208. (requires Invocable<EXF&> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  209. auto operator()(SF sf, EXF exf) const {
  210. return constrained_single_sender<SF, priorityZeroF, EXF>{std::move(sf), std::move(exf)};
  211. }
  212. PUSHMI_TEMPLATE (class SF, class ZF, class EXF)
  213. (requires Invocable<ZF&> && Invocable<EXF&> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  214. auto operator()(SF sf, EXF exf, ZF zf) const {
  215. return constrained_single_sender<SF, ZF, EXF>{std::move(sf), std::move(exf), std::move(zf)};
  216. }
  217. PUSHMI_TEMPLATE (class Data)
  218. (requires ConstrainedSender<Data, is_single<>>)
  219. auto operator()(Data d) const {
  220. return constrained_single_sender<Data, passDSF, passDZF, passDEXF>{std::move(d)};
  221. }
  222. PUSHMI_TEMPLATE (class Data, class DSF)
  223. (requires ConstrainedSender<Data, is_single<>>)
  224. auto operator()(Data d, DSF sf) const {
  225. return constrained_single_sender<Data, DSF, passDZF, passDEXF>{std::move(d), std::move(sf)};
  226. }
  227. PUSHMI_TEMPLATE (class Data, class DSF, class DEXF)
  228. (requires ConstrainedSender<Data, is_single<>> && Invocable<DEXF&, Data&>)
  229. auto operator()(Data d, DSF sf, DEXF exf) const {
  230. return constrained_single_sender<Data, DSF, passDZF, DEXF>{std::move(d), std::move(sf),
  231. std::move(exf)};
  232. }
  233. PUSHMI_TEMPLATE (class Data, class DSF, class DZF, class DEXF)
  234. (requires ConstrainedSender<Data, is_single<>> && Invocable<DZF&, Data&> && Invocable<DEXF&, Data&>)
  235. auto operator()(Data d, DSF sf, DEXF exf, DZF zf) const {
  236. return constrained_single_sender<Data, DSF, DZF, DEXF>{std::move(d), std::move(sf),
  237. std::move(exf), std::move(zf)};
  238. }
  239. } const make_constrained_single_sender {};
  240. ////////////////////////////////////////////////////////////////////////////////
  241. // deduction guides
  242. #if __cpp_deduction_guides >= 201703
  243. constrained_single_sender() -> constrained_single_sender<ignoreSF, priorityZeroF, inlineConstrainedEXF>;
  244. PUSHMI_TEMPLATE(class SF)
  245. (requires True<> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  246. constrained_single_sender(SF) -> constrained_single_sender<SF, priorityZeroF, inlineConstrainedEXF>;
  247. PUSHMI_TEMPLATE (class SF, class EXF)
  248. (requires Invocable<EXF&> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  249. constrained_single_sender(SF, EXF) -> constrained_single_sender<SF, priorityZeroF, EXF>;
  250. PUSHMI_TEMPLATE (class SF, class ZF, class EXF)
  251. (requires Invocable<ZF&> && Invocable<EXF&> PUSHMI_BROKEN_SUBSUMPTION(&& not Sender<SF>))
  252. constrained_single_sender(SF, EXF, ZF) -> constrained_single_sender<SF, ZF, EXF>;
  253. PUSHMI_TEMPLATE (class Data, class DSF)
  254. (requires ConstrainedSender<Data, is_single<>>)
  255. constrained_single_sender(Data, DSF) -> constrained_single_sender<Data, DSF, passDZF, passDEXF>;
  256. PUSHMI_TEMPLATE (class Data, class DSF, class DEXF)
  257. (requires ConstrainedSender<Data, is_single<>> && Invocable<DEXF&, Data&>)
  258. constrained_single_sender(Data, DSF, DEXF) -> constrained_single_sender<Data, DSF, passDZF, DEXF>;
  259. PUSHMI_TEMPLATE (class Data, class DSF, class DZF, class DEXF)
  260. (requires ConstrainedSender<Data, is_single<>> && Invocable<DZF&, Data&> && Invocable<DEXF&, Data&>)
  261. constrained_single_sender(Data, DSF, DEXF, DZF) -> constrained_single_sender<Data, DSF, DZF, DEXF>;
  262. #endif
  263. template<>
  264. struct construct_deduced<constrained_single_sender>
  265. : make_constrained_single_sender_fn {};
  266. } // namespace pushmi