concepts.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/extension_points.h>
  18. #include <folly/experimental/pushmi/forwards.h>
  19. #include <folly/experimental/pushmi/properties.h>
  20. namespace pushmi {
  21. // traits & tags
  22. // cardinality affects both sender and receiver
  23. struct cardinality_category {};
  24. // Trait
  25. template <class PS>
  26. struct has_cardinality : category_query<PS, cardinality_category> {};
  27. template <class PS>
  28. PUSHMI_INLINE_VAR constexpr bool has_cardinality_v = has_cardinality<PS>::value;
  29. PUSHMI_CONCEPT_DEF(
  30. template(class PS) concept Cardinality,
  31. has_cardinality_v<PS>);
  32. // flow affects both sender and receiver
  33. struct flow_category {};
  34. // sender and receiver are mutually exclusive
  35. struct receiver_category {};
  36. struct sender_category {};
  37. // for senders that are executors
  38. struct executor_category {};
  39. // time and constrained are mutually exclusive refinements of sender (time is a
  40. // special case of constrained and may be folded in later)
  41. // blocking affects senders
  42. struct blocking_category {};
  43. // sequence affects senders
  44. struct sequence_category {};
  45. // Single trait and tag
  46. template <class... TN>
  47. struct is_single;
  48. // Tag
  49. template <>
  50. struct is_single<> {
  51. using property_category = cardinality_category;
  52. };
  53. // Trait
  54. template <class PS>
  55. struct is_single<PS> : property_query<PS, is_single<>> {};
  56. template <class PS>
  57. PUSHMI_INLINE_VAR constexpr bool is_single_v = is_single<PS>::value;
  58. PUSHMI_CONCEPT_DEF(template(class PS) concept Single, is_single_v<PS>);
  59. // Many trait and tag
  60. template <class... TN>
  61. struct is_many;
  62. // Tag
  63. template <>
  64. struct is_many<> {
  65. using property_category = cardinality_category;
  66. }; // many::value() does not terminate, so it is not a refinement of single
  67. // Trait
  68. template <class PS>
  69. struct is_many<PS> : property_query<PS, is_many<>> {};
  70. template <class PS>
  71. PUSHMI_INLINE_VAR constexpr bool is_many_v = is_many<PS>::value;
  72. PUSHMI_CONCEPT_DEF(template(class PS) concept Many, is_many_v<PS>);
  73. // Flow trait and tag
  74. template <class... TN>
  75. struct is_flow;
  76. // Tag
  77. template <>
  78. struct is_flow<> {
  79. using property_category = flow_category;
  80. };
  81. // Trait
  82. template <class PS>
  83. struct is_flow<PS> : property_query<PS, is_flow<>> {};
  84. template <class PS>
  85. PUSHMI_INLINE_VAR constexpr bool is_flow_v = is_flow<PS>::value;
  86. PUSHMI_CONCEPT_DEF(template(class PS) concept Flow, is_flow_v<PS>);
  87. // Receiver trait and tag
  88. template <class... TN>
  89. struct is_receiver;
  90. // Tag
  91. template <>
  92. struct is_receiver<> {
  93. using property_category = receiver_category;
  94. };
  95. // Trait
  96. template <class PS>
  97. struct is_receiver<PS> : property_query<PS, is_receiver<>> {};
  98. template <class PS>
  99. PUSHMI_INLINE_VAR constexpr bool is_receiver_v = is_receiver<PS>::value;
  100. // PUSHMI_CONCEPT_DEF(
  101. // template (class PS)
  102. // concept Receiver,
  103. // is_receiver_v<PS>
  104. // );
  105. // Sender trait and tag
  106. template <class... TN>
  107. struct is_sender;
  108. // Tag
  109. template <>
  110. struct is_sender<> {
  111. using property_category = sender_category;
  112. };
  113. // Trait
  114. template <class PS>
  115. struct is_sender<PS> : property_query<PS, is_sender<>> {};
  116. template <class PS>
  117. PUSHMI_INLINE_VAR constexpr bool is_sender_v = is_sender<PS>::value;
  118. // PUSHMI_CONCEPT_DEF(
  119. // template (class PS)
  120. // concept Sender,
  121. // is_sender_v<PS>
  122. // );
  123. // Executor trait and tag
  124. template <class... TN>
  125. struct is_executor;
  126. // Tag
  127. template <>
  128. struct is_executor<> {
  129. using property_category = executor_category;
  130. };
  131. // Trait
  132. template <class PS>
  133. struct is_executor<PS> : property_query<PS, is_executor<>> {};
  134. template <class PS>
  135. PUSHMI_INLINE_VAR constexpr bool is_executor_v = is_executor<PS>::value;
  136. PUSHMI_CONCEPT_DEF(
  137. template(class PS) concept Executor,
  138. is_executor_v<PS>&& is_sender_v<PS>&& is_single_v<PS>);
  139. // Constrained trait and tag
  140. template <class... TN>
  141. struct is_constrained;
  142. // Tag
  143. template <>
  144. struct is_constrained<> : is_sender<> {};
  145. // Trait
  146. template <class PS>
  147. struct is_constrained<PS> : property_query<PS, is_constrained<>> {};
  148. template <class PS>
  149. PUSHMI_INLINE_VAR constexpr bool is_constrained_v = is_constrained<PS>::value;
  150. PUSHMI_CONCEPT_DEF(
  151. template(class PS) concept Constrained,
  152. is_constrained_v<PS>&& is_sender_v<PS>);
  153. // Time trait and tag
  154. template <class... TN>
  155. struct is_time;
  156. // Tag
  157. template <>
  158. struct is_time<> : is_constrained<> {};
  159. // Trait
  160. template <class PS>
  161. struct is_time<PS> : property_query<PS, is_time<>> {};
  162. template <class PS>
  163. PUSHMI_INLINE_VAR constexpr bool is_time_v = is_time<PS>::value;
  164. PUSHMI_CONCEPT_DEF(
  165. template(class PS) concept Time,
  166. is_time_v<PS>&& is_constrained_v<PS>&& is_sender_v<PS>);
  167. // AlwaysBlocking trait and tag
  168. template <class... TN>
  169. struct is_always_blocking;
  170. // Tag
  171. template <>
  172. struct is_always_blocking<> {
  173. using property_category = blocking_category;
  174. };
  175. // Trait
  176. template <class PS>
  177. struct is_always_blocking<PS> : property_query<PS, is_always_blocking<>> {};
  178. template <class PS>
  179. PUSHMI_INLINE_VAR constexpr bool is_always_blocking_v =
  180. is_always_blocking<PS>::value;
  181. PUSHMI_CONCEPT_DEF(
  182. template(class PS) concept AlwaysBlocking,
  183. is_always_blocking_v<PS>&& is_sender_v<PS>);
  184. // NeverBlocking trait and tag
  185. template <class... TN>
  186. struct is_never_blocking;
  187. // Tag
  188. template <>
  189. struct is_never_blocking<> {
  190. using property_category = blocking_category;
  191. };
  192. // Trait
  193. template <class PS>
  194. struct is_never_blocking<PS> : property_query<PS, is_never_blocking<>> {};
  195. template <class PS>
  196. PUSHMI_INLINE_VAR constexpr bool is_never_blocking_v =
  197. is_never_blocking<PS>::value;
  198. PUSHMI_CONCEPT_DEF(
  199. template(class PS) concept NeverBlocking,
  200. is_never_blocking_v<PS>&& is_sender_v<PS>);
  201. // MaybeBlocking trait and tag
  202. template <class... TN>
  203. struct is_maybe_blocking;
  204. // Tag
  205. template <>
  206. struct is_maybe_blocking<> {
  207. using property_category = blocking_category;
  208. };
  209. // Trait
  210. template <class PS>
  211. struct is_maybe_blocking<PS> : property_query<PS, is_maybe_blocking<>> {};
  212. template <class PS>
  213. PUSHMI_INLINE_VAR constexpr bool is_maybe_blocking_v =
  214. is_maybe_blocking<PS>::value;
  215. PUSHMI_CONCEPT_DEF(
  216. template(class PS) concept MaybeBlocking,
  217. is_maybe_blocking_v<PS>&& is_sender_v<PS>);
  218. // FifoSequence trait and tag
  219. template <class... TN>
  220. struct is_fifo_sequence;
  221. // Tag
  222. template <>
  223. struct is_fifo_sequence<> {
  224. using property_category = sequence_category;
  225. };
  226. // Trait
  227. template <class PS>
  228. struct is_fifo_sequence<PS> : property_query<PS, is_fifo_sequence<>> {};
  229. template <class PS>
  230. PUSHMI_INLINE_VAR constexpr bool is_fifo_sequence_v =
  231. is_fifo_sequence<PS>::value;
  232. PUSHMI_CONCEPT_DEF(
  233. template(class PS) concept FifoSequence,
  234. is_fifo_sequence_v<PS>&& is_sender_v<PS>);
  235. // ConcurrentSequence trait and tag
  236. template <class... TN>
  237. struct is_concurrent_sequence;
  238. // Tag
  239. template <>
  240. struct is_concurrent_sequence<> {
  241. using property_category = sequence_category;
  242. };
  243. // Trait
  244. template <class PS>
  245. struct is_concurrent_sequence<PS>
  246. : property_query<PS, is_concurrent_sequence<>> {};
  247. template <class PS>
  248. PUSHMI_INLINE_VAR constexpr bool is_concurrent_sequence_v =
  249. is_concurrent_sequence<PS>::value;
  250. PUSHMI_CONCEPT_DEF(
  251. template(class PS) concept ConcurrentSequence,
  252. is_concurrent_sequence_v<PS>&& is_sender_v<PS>);
  253. PUSHMI_CONCEPT_DEF(
  254. template(class R, class... PropertyN)(concept Receiver)(R, PropertyN...),
  255. requires(R& r)(
  256. ::pushmi::set_done(r),
  257. ::pushmi::set_error(r, std::exception_ptr{})) &&
  258. SemiMovable<R> && property_query_v<R, PropertyN...> &&
  259. is_receiver_v<R> && !is_sender_v<R>);
  260. PUSHMI_CONCEPT_DEF(
  261. template(class R, class... VN)(concept ReceiveValue)(R, VN...),
  262. requires(R& r)(::pushmi::set_value(r, std::declval<VN&&>()...)) &&
  263. Receiver<R> &&
  264. // GCC w/-fconcepts ICE on SemiMovable<VN>...
  265. True<> // And<SemiMovable<VN>...>
  266. );
  267. PUSHMI_CONCEPT_DEF(
  268. template(class R, class E = std::exception_ptr)(concept ReceiveError)(R, E),
  269. requires(R& r, E&& e)(::pushmi::set_error(r, (E &&) e)) && Receiver<R> &&
  270. SemiMovable<E>);
  271. PUSHMI_CONCEPT_DEF(
  272. template(class D, class... PropertyN)(concept Sender)(D, PropertyN...),
  273. requires(D& d)(
  274. ::pushmi::executor(d),
  275. requires_<Executor<decltype(::pushmi::executor(d))>>) &&
  276. SemiMovable<D> && Cardinality<D> && property_query_v<D, PropertyN...> &&
  277. is_sender_v<D> && !is_receiver_v<D>);
  278. PUSHMI_CONCEPT_DEF(
  279. template(class D, class S, class... PropertyN)(
  280. concept SenderTo)(D, S, PropertyN...),
  281. requires(D& d, S&& s)(::pushmi::submit(d, (S &&) s)) && Sender<D> &&
  282. Receiver<S> && property_query_v<D, PropertyN...>);
  283. template <class D>
  284. PUSHMI_PP_CONSTRAINED_USING(
  285. Sender<D>,
  286. executor_t =,
  287. decltype(::pushmi::executor(std::declval<D&>())));
  288. // add concepts to support cancellation
  289. //
  290. PUSHMI_CONCEPT_DEF(
  291. template(class S, class... PropertyN)(
  292. concept FlowReceiver)(S, PropertyN...),
  293. Receiver<S>&& property_query_v<S, PropertyN...>&& Flow<S>);
  294. PUSHMI_CONCEPT_DEF(
  295. template(class R, class... VN)(concept FlowReceiveValue)(R, VN...),
  296. Flow<R>&& ReceiveValue<R, VN...>);
  297. PUSHMI_CONCEPT_DEF(
  298. template(class R, class E = std::exception_ptr)(
  299. concept FlowReceiveError)(R, E),
  300. Flow<R>&& ReceiveError<R, E>);
  301. PUSHMI_CONCEPT_DEF(
  302. template(class R, class Up)(concept FlowUpTo)(R, Up),
  303. requires(R& r, Up&& up)(::pushmi::set_starting(r, (Up &&) up)) && Flow<R>);
  304. PUSHMI_CONCEPT_DEF(
  305. template(class S, class... PropertyN)(concept FlowSender)(S, PropertyN...),
  306. Sender<S>&& property_query_v<S, PropertyN...>&& Flow<S>);
  307. PUSHMI_CONCEPT_DEF(
  308. template(class D, class S, class... PropertyN)(
  309. concept FlowSenderTo)(D, S, PropertyN...),
  310. FlowSender<D>&& property_query_v<D, PropertyN...>&& FlowReceiver<S>);
  311. // add concepts for constraints
  312. //
  313. // the constraint could be time or priority enum or any other
  314. // ordering constraint value-type.
  315. //
  316. // top() returns the constraint value that will cause the item to run asap.
  317. // So now() for time and NORMAL for priority.
  318. //
  319. PUSHMI_CONCEPT_DEF(
  320. template(class D, class... PropertyN)(
  321. concept ConstrainedSender)(D, PropertyN...),
  322. requires(D& d)(
  323. ::pushmi::top(d),
  324. requires_<Regular<decltype(::pushmi::top(d))>>) &&
  325. Sender<D> && property_query_v<D, PropertyN...> && Constrained<D>);
  326. PUSHMI_CONCEPT_DEF(
  327. template(class D, class S, class... PropertyN)(
  328. concept ConstrainedSenderTo)(D, S, PropertyN...),
  329. requires(D& d, S&& s)(::pushmi::submit(d, ::pushmi::top(d), (S &&) s)) &&
  330. ConstrainedSender<D> && property_query_v<D, PropertyN...> &&
  331. Receiver<S>);
  332. template <class D>
  333. PUSHMI_PP_CONSTRAINED_USING(
  334. ConstrainedSender<D>,
  335. constraint_t =,
  336. decltype(::pushmi::top(std::declval<D&>())));
  337. PUSHMI_CONCEPT_DEF(
  338. template(class D, class... PropertyN)(concept TimeSender)(D, PropertyN...),
  339. requires(D& d)(
  340. ::pushmi::now(d),
  341. requires_<
  342. Regular<decltype(::pushmi::now(d) + std::chrono::seconds(1))>>) &&
  343. ConstrainedSender<D, PropertyN...> && Time<D>);
  344. PUSHMI_CONCEPT_DEF(
  345. template(class D, class S, class... PropertyN)(
  346. concept TimeSenderTo)(D, S, PropertyN...),
  347. ConstrainedSenderTo<D, S, PropertyN...>&& TimeSender<D>);
  348. template <class D>
  349. PUSHMI_PP_CONSTRAINED_USING(
  350. TimeSender<D>,
  351. time_point_t =,
  352. decltype(::pushmi::now(std::declval<D&>())));
  353. } // namespace pushmi