any_executor.hpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  1. //
  2. // execution/any_executor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_EXECUTION_ANY_EXECUTOR_HPP
  11. #define BOOST_ASIO_EXECUTION_ANY_EXECUTOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <new>
  17. #include <typeinfo>
  18. #include <boost/asio/detail/assert.hpp>
  19. #include <boost/asio/detail/atomic_count.hpp>
  20. #include <boost/asio/detail/cstddef.hpp>
  21. #include <boost/asio/detail/executor_function.hpp>
  22. #include <boost/asio/detail/memory.hpp>
  23. #include <boost/asio/detail/non_const_lvalue.hpp>
  24. #include <boost/asio/detail/scoped_ptr.hpp>
  25. #include <boost/asio/detail/type_traits.hpp>
  26. #include <boost/asio/detail/throw_exception.hpp>
  27. #include <boost/asio/execution/bad_executor.hpp>
  28. #include <boost/asio/execution/blocking.hpp>
  29. #include <boost/asio/execution/executor.hpp>
  30. #include <boost/asio/prefer.hpp>
  31. #include <boost/asio/query.hpp>
  32. #include <boost/asio/require.hpp>
  33. #include <boost/asio/detail/push_options.hpp>
  34. namespace boost {
  35. namespace asio {
  36. #if defined(GENERATING_DOCUMENTATION)
  37. namespace execution {
  38. /// Polymorphic executor wrapper.
  39. template <typename... SupportableProperties>
  40. class any_executor
  41. {
  42. public:
  43. /// Default constructor.
  44. any_executor() noexcept;
  45. /// Construct in an empty state. Equivalent effects to default constructor.
  46. any_executor(nullptr_t) noexcept;
  47. /// Copy constructor.
  48. any_executor(const any_executor& e) noexcept;
  49. /// Move constructor.
  50. any_executor(any_executor&& e) noexcept;
  51. /// Construct to point to the same target as another any_executor.
  52. template <class... OtherSupportableProperties>
  53. any_executor(any_executor<OtherSupportableProperties...> e);
  54. /// Construct to point to the same target as another any_executor.
  55. template <class... OtherSupportableProperties>
  56. any_executor(std::nothrow_t,
  57. any_executor<OtherSupportableProperties...> e) noexcept;
  58. /// Construct to point to the same target as another any_executor.
  59. any_executor(std::nothrow_t, const any_executor& e) noexcept;
  60. /// Construct to point to the same target as another any_executor.
  61. any_executor(std::nothrow_t, any_executor&& e) noexcept;
  62. /// Construct a polymorphic wrapper for the specified executor.
  63. template <typename Executor>
  64. any_executor(Executor e);
  65. /// Construct a polymorphic wrapper for the specified executor.
  66. template <typename Executor>
  67. any_executor(std::nothrow_t, Executor e) noexcept;
  68. /// Assignment operator.
  69. any_executor& operator=(const any_executor& e) noexcept;
  70. /// Move assignment operator.
  71. any_executor& operator=(any_executor&& e) noexcept;
  72. /// Assignment operator that sets the polymorphic wrapper to the empty state.
  73. any_executor& operator=(nullptr_t);
  74. /// Assignment operator to create a polymorphic wrapper for the specified
  75. /// executor.
  76. template <typename Executor>
  77. any_executor& operator=(Executor e);
  78. /// Destructor.
  79. ~any_executor();
  80. /// Swap targets with another polymorphic wrapper.
  81. void swap(any_executor& other) noexcept;
  82. /// Obtain a polymorphic wrapper with the specified property.
  83. /**
  84. * Do not call this function directly. It is intended for use with the
  85. * boost::asio::require and boost::asio::prefer customisation points.
  86. *
  87. * For example:
  88. * @code execution::any_executor<execution::blocking_t::possibly_t> ex = ...;
  89. * auto ex2 = boost::asio::requre(ex, execution::blocking.possibly); @endcode
  90. */
  91. template <typename Property>
  92. any_executor require(Property) const;
  93. /// Obtain a polymorphic wrapper with the specified property.
  94. /**
  95. * Do not call this function directly. It is intended for use with the
  96. * boost::asio::prefer customisation point.
  97. *
  98. * For example:
  99. * @code execution::any_executor<execution::blocking_t::possibly_t> ex = ...;
  100. * auto ex2 = boost::asio::prefer(ex, execution::blocking.possibly); @endcode
  101. */
  102. template <typename Property>
  103. any_executor prefer(Property) const;
  104. /// Obtain the value associated with the specified property.
  105. /**
  106. * Do not call this function directly. It is intended for use with the
  107. * boost::asio::query customisation point.
  108. *
  109. * For example:
  110. * @code execution::any_executor<execution::occupancy_t> ex = ...;
  111. * size_t n = boost::asio::query(ex, execution::occupancy); @endcode
  112. */
  113. template <typename Property>
  114. typename Property::polymorphic_query_result_type query(Property) const;
  115. /// Execute the function on the target executor.
  116. /**
  117. * Throws boost::asio::bad_executor if the polymorphic wrapper has no target.
  118. */
  119. template <typename Function>
  120. void execute(Function&& f) const;
  121. /// Obtain the underlying execution context.
  122. /**
  123. * This function is provided for backward compatibility. It is automatically
  124. * defined when the @c SupportableProperties... list includes a property of
  125. * type <tt>execution::context_as<U></tt>, for some type <tt>U</tt>.
  126. */
  127. automatically_determined context() const;
  128. /// Determine whether the wrapper has a target executor.
  129. /**
  130. * @returns @c true if the polymorphic wrapper has a target executor,
  131. * otherwise false.
  132. */
  133. explicit operator bool() const noexcept;
  134. /// Get the type of the target executor.
  135. const type_info& target_type() const noexcept;
  136. /// Get a pointer to the target executor.
  137. template <typename Executor> Executor* target() noexcept;
  138. /// Get a pointer to the target executor.
  139. template <typename Executor> const Executor* target() const noexcept;
  140. };
  141. /// Equality operator.
  142. /**
  143. * @relates any_executor
  144. */
  145. template <typename... SupportableProperties>
  146. bool operator==(const any_executor<SupportableProperties...>& a,
  147. const any_executor<SupportableProperties...>& b) noexcept;
  148. /// Equality operator.
  149. /**
  150. * @relates any_executor
  151. */
  152. template <typename... SupportableProperties>
  153. bool operator==(const any_executor<SupportableProperties...>& a,
  154. nullptr_t) noexcept;
  155. /// Equality operator.
  156. /**
  157. * @relates any_executor
  158. */
  159. template <typename... SupportableProperties>
  160. bool operator==(nullptr_t,
  161. const any_executor<SupportableProperties...>& b) noexcept;
  162. /// Inequality operator.
  163. /**
  164. * @relates any_executor
  165. */
  166. template <typename... SupportableProperties>
  167. bool operator!=(const any_executor<SupportableProperties...>& a,
  168. const any_executor<SupportableProperties...>& b) noexcept;
  169. /// Inequality operator.
  170. /**
  171. * @relates any_executor
  172. */
  173. template <typename... SupportableProperties>
  174. bool operator!=(const any_executor<SupportableProperties...>& a,
  175. nullptr_t) noexcept;
  176. /// Inequality operator.
  177. /**
  178. * @relates any_executor
  179. */
  180. template <typename... SupportableProperties>
  181. bool operator!=(nullptr_t,
  182. const any_executor<SupportableProperties...>& b) noexcept;
  183. } // namespace execution
  184. #else // defined(GENERATING_DOCUMENTATION)
  185. namespace execution {
  186. #if !defined(BOOST_ASIO_EXECUTION_ANY_EXECUTOR_FWD_DECL)
  187. #define BOOST_ASIO_EXECUTION_ANY_EXECUTOR_FWD_DECL
  188. template <typename... SupportableProperties>
  189. class any_executor;
  190. #endif // !defined(BOOST_ASIO_EXECUTION_ANY_EXECUTOR_FWD_DECL)
  191. template <typename U>
  192. struct context_as_t;
  193. namespace detail {
  194. // Traits used to detect whether a property is requirable or preferable, taking
  195. // into account that T::is_requirable or T::is_preferable may not not be well
  196. // formed.
  197. template <typename T, typename = void>
  198. struct is_requirable : false_type {};
  199. template <typename T>
  200. struct is_requirable<T, enable_if_t<T::is_requirable>> : true_type {};
  201. template <typename T, typename = void>
  202. struct is_preferable : false_type {};
  203. template <typename T>
  204. struct is_preferable<T, enable_if_t<T::is_preferable>> : true_type {};
  205. // Trait used to detect context_as property, for backward compatibility.
  206. template <typename T>
  207. struct is_context_as : false_type {};
  208. template <typename U>
  209. struct is_context_as<context_as_t<U>> : true_type {};
  210. // Helper template to:
  211. // - Check if a target can supply the supportable properties.
  212. // - Find the first convertible-from-T property in the list.
  213. template <std::size_t I, typename Props>
  214. struct supportable_properties;
  215. template <std::size_t I, typename Prop>
  216. struct supportable_properties<I, void(Prop)>
  217. {
  218. template <typename T>
  219. struct is_valid_target : integral_constant<bool,
  220. (
  221. is_requirable<Prop>::value
  222. ? can_require<T, Prop>::value
  223. : true
  224. )
  225. &&
  226. (
  227. is_preferable<Prop>::value
  228. ? can_prefer<T, Prop>::value
  229. : true
  230. )
  231. &&
  232. (
  233. !is_requirable<Prop>::value && !is_preferable<Prop>::value
  234. ? can_query<T, Prop>::value
  235. : true
  236. )
  237. >
  238. {
  239. };
  240. struct found
  241. {
  242. static constexpr bool value = true;
  243. typedef Prop type;
  244. typedef typename Prop::polymorphic_query_result_type query_result_type;
  245. static constexpr std::size_t index = I;
  246. };
  247. struct not_found
  248. {
  249. static constexpr bool value = false;
  250. };
  251. template <typename T>
  252. struct find_convertible_property :
  253. conditional_t<
  254. is_same<T, Prop>::value || is_convertible<T, Prop>::value,
  255. found,
  256. not_found
  257. > {};
  258. template <typename T>
  259. struct find_convertible_requirable_property :
  260. conditional_t<
  261. is_requirable<Prop>::value
  262. && (is_same<T, Prop>::value || is_convertible<T, Prop>::value),
  263. found,
  264. not_found
  265. > {};
  266. template <typename T>
  267. struct find_convertible_preferable_property :
  268. conditional_t<
  269. is_preferable<Prop>::value
  270. && (is_same<T, Prop>::value || is_convertible<T, Prop>::value),
  271. found,
  272. not_found
  273. > {};
  274. struct find_context_as_property :
  275. conditional_t<
  276. is_context_as<Prop>::value,
  277. found,
  278. not_found
  279. > {};
  280. };
  281. template <std::size_t I, typename Head, typename... Tail>
  282. struct supportable_properties<I, void(Head, Tail...)>
  283. {
  284. template <typename T>
  285. struct is_valid_target : integral_constant<bool,
  286. (
  287. supportable_properties<I,
  288. void(Head)>::template is_valid_target<T>::value
  289. &&
  290. supportable_properties<I + 1,
  291. void(Tail...)>::template is_valid_target<T>::value
  292. )
  293. >
  294. {
  295. };
  296. template <typename T>
  297. struct find_convertible_property :
  298. conditional_t<
  299. is_convertible<T, Head>::value,
  300. typename supportable_properties<I, void(Head)>::found,
  301. typename supportable_properties<I + 1,
  302. void(Tail...)>::template find_convertible_property<T>
  303. > {};
  304. template <typename T>
  305. struct find_convertible_requirable_property :
  306. conditional_t<
  307. is_requirable<Head>::value
  308. && is_convertible<T, Head>::value,
  309. typename supportable_properties<I, void(Head)>::found,
  310. typename supportable_properties<I + 1,
  311. void(Tail...)>::template find_convertible_requirable_property<T>
  312. > {};
  313. template <typename T>
  314. struct find_convertible_preferable_property :
  315. conditional_t<
  316. is_preferable<Head>::value
  317. && is_convertible<T, Head>::value,
  318. typename supportable_properties<I, void(Head)>::found,
  319. typename supportable_properties<I + 1,
  320. void(Tail...)>::template find_convertible_preferable_property<T>
  321. > {};
  322. struct find_context_as_property :
  323. conditional_t<
  324. is_context_as<Head>::value,
  325. typename supportable_properties<I, void(Head)>::found,
  326. typename supportable_properties<I + 1,
  327. void(Tail...)>::find_context_as_property
  328. > {};
  329. };
  330. template <typename T, typename Props>
  331. struct is_valid_target_executor :
  332. conditional_t<
  333. is_executor<T>::value,
  334. typename supportable_properties<0, Props>::template is_valid_target<T>,
  335. false_type
  336. >
  337. {
  338. };
  339. template <typename Props>
  340. struct is_valid_target_executor<int, Props> : false_type
  341. {
  342. };
  343. class shared_target_executor
  344. {
  345. public:
  346. template <typename E>
  347. shared_target_executor(E&& e, decay_t<E>*& target)
  348. {
  349. impl<decay_t<E>>* i =
  350. new impl<decay_t<E>>(static_cast<E&&>(e));
  351. target = &i->ex_;
  352. impl_ = i;
  353. }
  354. template <typename E>
  355. shared_target_executor(std::nothrow_t, E&& e, decay_t<E>*& target) noexcept
  356. {
  357. impl<decay_t<E>>* i =
  358. new (std::nothrow) impl<decay_t<E>>(static_cast<E&&>(e));
  359. target = i ? &i->ex_ : 0;
  360. impl_ = i;
  361. }
  362. shared_target_executor(const shared_target_executor& other) noexcept
  363. : impl_(other.impl_)
  364. {
  365. if (impl_)
  366. boost::asio::detail::ref_count_up(impl_->ref_count_);
  367. }
  368. shared_target_executor(shared_target_executor&& other) noexcept
  369. : impl_(other.impl_)
  370. {
  371. other.impl_ = 0;
  372. }
  373. ~shared_target_executor()
  374. {
  375. if (impl_)
  376. if (boost::asio::detail::ref_count_down(impl_->ref_count_))
  377. delete impl_;
  378. }
  379. void* get() const noexcept
  380. {
  381. return impl_ ? impl_->get() : 0;
  382. }
  383. private:
  384. shared_target_executor& operator=(
  385. const shared_target_executor& other) = delete;
  386. shared_target_executor& operator=(
  387. shared_target_executor&& other) = delete;
  388. struct impl_base
  389. {
  390. impl_base() : ref_count_(1) {}
  391. virtual ~impl_base() {}
  392. virtual void* get() = 0;
  393. boost::asio::detail::atomic_count ref_count_;
  394. };
  395. template <typename Executor>
  396. struct impl : impl_base
  397. {
  398. impl(Executor ex) : ex_(static_cast<Executor&&>(ex)) {}
  399. virtual void* get() { return &ex_; }
  400. Executor ex_;
  401. };
  402. impl_base* impl_;
  403. };
  404. class any_executor_base
  405. {
  406. public:
  407. any_executor_base() noexcept
  408. : object_fns_(0),
  409. target_(0),
  410. target_fns_(0)
  411. {
  412. }
  413. template <BOOST_ASIO_EXECUTION_EXECUTOR Executor>
  414. any_executor_base(Executor ex, false_type)
  415. : target_fns_(target_fns_table<Executor>(
  416. any_executor_base::query_blocking(ex,
  417. can_query<const Executor&, const execution::blocking_t&>())
  418. == execution::blocking.always))
  419. {
  420. any_executor_base::construct_object(ex,
  421. integral_constant<bool,
  422. sizeof(Executor) <= sizeof(object_type)
  423. && alignment_of<Executor>::value <= alignment_of<object_type>::value
  424. >());
  425. }
  426. template <BOOST_ASIO_EXECUTION_EXECUTOR Executor>
  427. any_executor_base(std::nothrow_t, Executor ex, false_type) noexcept
  428. : target_fns_(target_fns_table<Executor>(
  429. any_executor_base::query_blocking(ex,
  430. can_query<const Executor&, const execution::blocking_t&>())
  431. == execution::blocking.always))
  432. {
  433. any_executor_base::construct_object(std::nothrow, ex,
  434. integral_constant<bool,
  435. sizeof(Executor) <= sizeof(object_type)
  436. && alignment_of<Executor>::value <= alignment_of<object_type>::value
  437. >());
  438. if (target_ == 0)
  439. {
  440. object_fns_ = 0;
  441. target_fns_ = 0;
  442. }
  443. }
  444. template <BOOST_ASIO_EXECUTION_EXECUTOR Executor>
  445. any_executor_base(Executor other, true_type)
  446. : object_fns_(object_fns_table<shared_target_executor>()),
  447. target_fns_(other.target_fns_)
  448. {
  449. Executor* p = 0;
  450. new (&object_) shared_target_executor(
  451. static_cast<Executor&&>(other), p);
  452. target_ = p->template target<void>();
  453. }
  454. template <BOOST_ASIO_EXECUTION_EXECUTOR Executor>
  455. any_executor_base(std::nothrow_t,
  456. Executor other, true_type) noexcept
  457. : object_fns_(object_fns_table<shared_target_executor>()),
  458. target_fns_(other.target_fns_)
  459. {
  460. Executor* p = 0;
  461. new (&object_) shared_target_executor(
  462. std::nothrow, static_cast<Executor&&>(other), p);
  463. if (p)
  464. target_ = p->template target<void>();
  465. else
  466. {
  467. target_ = 0;
  468. object_fns_ = 0;
  469. target_fns_ = 0;
  470. }
  471. }
  472. any_executor_base(const any_executor_base& other) noexcept
  473. {
  474. if (!!other)
  475. {
  476. object_fns_ = other.object_fns_;
  477. target_fns_ = other.target_fns_;
  478. object_fns_->copy(*this, other);
  479. }
  480. else
  481. {
  482. object_fns_ = 0;
  483. target_ = 0;
  484. target_fns_ = 0;
  485. }
  486. }
  487. ~any_executor_base() noexcept
  488. {
  489. if (!!*this)
  490. object_fns_->destroy(*this);
  491. }
  492. any_executor_base& operator=(
  493. const any_executor_base& other) noexcept
  494. {
  495. if (this != &other)
  496. {
  497. if (!!*this)
  498. object_fns_->destroy(*this);
  499. if (!!other)
  500. {
  501. object_fns_ = other.object_fns_;
  502. target_fns_ = other.target_fns_;
  503. object_fns_->copy(*this, other);
  504. }
  505. else
  506. {
  507. object_fns_ = 0;
  508. target_ = 0;
  509. target_fns_ = 0;
  510. }
  511. }
  512. return *this;
  513. }
  514. any_executor_base& operator=(nullptr_t) noexcept
  515. {
  516. if (target_)
  517. object_fns_->destroy(*this);
  518. target_ = 0;
  519. object_fns_ = 0;
  520. target_fns_ = 0;
  521. return *this;
  522. }
  523. any_executor_base(any_executor_base&& other) noexcept
  524. {
  525. if (other.target_)
  526. {
  527. object_fns_ = other.object_fns_;
  528. target_fns_ = other.target_fns_;
  529. other.object_fns_ = 0;
  530. other.target_fns_ = 0;
  531. object_fns_->move(*this, other);
  532. other.target_ = 0;
  533. }
  534. else
  535. {
  536. object_fns_ = 0;
  537. target_ = 0;
  538. target_fns_ = 0;
  539. }
  540. }
  541. any_executor_base& operator=(
  542. any_executor_base&& other) noexcept
  543. {
  544. if (this != &other)
  545. {
  546. if (!!*this)
  547. object_fns_->destroy(*this);
  548. if (!!other)
  549. {
  550. object_fns_ = other.object_fns_;
  551. target_fns_ = other.target_fns_;
  552. other.object_fns_ = 0;
  553. other.target_fns_ = 0;
  554. object_fns_->move(*this, other);
  555. other.target_ = 0;
  556. }
  557. else
  558. {
  559. object_fns_ = 0;
  560. target_ = 0;
  561. target_fns_ = 0;
  562. }
  563. }
  564. return *this;
  565. }
  566. void swap(any_executor_base& other) noexcept
  567. {
  568. if (this != &other)
  569. {
  570. any_executor_base tmp(static_cast<any_executor_base&&>(other));
  571. other = static_cast<any_executor_base&&>(*this);
  572. *this = static_cast<any_executor_base&&>(tmp);
  573. }
  574. }
  575. template <typename F>
  576. void execute(F&& f) const
  577. {
  578. if (target_)
  579. {
  580. if (target_fns_->blocking_execute != 0)
  581. {
  582. boost::asio::detail::non_const_lvalue<F> f2(f);
  583. target_fns_->blocking_execute(*this, function_view(f2.value));
  584. }
  585. else
  586. {
  587. target_fns_->execute(*this,
  588. function(static_cast<F&&>(f), std::allocator<void>()));
  589. }
  590. }
  591. else
  592. {
  593. bad_executor ex;
  594. boost::asio::detail::throw_exception(ex);
  595. }
  596. }
  597. template <typename Executor>
  598. Executor* target()
  599. {
  600. return target_ && (is_same<Executor, void>::value
  601. || target_fns_->target_type() == target_type_ex<Executor>())
  602. ? static_cast<Executor*>(target_) : 0;
  603. }
  604. template <typename Executor>
  605. const Executor* target() const
  606. {
  607. return target_ && (is_same<Executor, void>::value
  608. || target_fns_->target_type() == target_type_ex<Executor>())
  609. ? static_cast<const Executor*>(target_) : 0;
  610. }
  611. #if !defined(BOOST_ASIO_NO_TYPEID)
  612. const std::type_info& target_type() const
  613. {
  614. return target_ ? target_fns_->target_type() : typeid(void);
  615. }
  616. #else // !defined(BOOST_ASIO_NO_TYPEID)
  617. const void* target_type() const
  618. {
  619. return target_ ? target_fns_->target_type() : 0;
  620. }
  621. #endif // !defined(BOOST_ASIO_NO_TYPEID)
  622. struct unspecified_bool_type_t {};
  623. typedef void (*unspecified_bool_type)(unspecified_bool_type_t);
  624. static void unspecified_bool_true(unspecified_bool_type_t) {}
  625. operator unspecified_bool_type() const noexcept
  626. {
  627. return target_ ? &any_executor_base::unspecified_bool_true : 0;
  628. }
  629. bool operator!() const noexcept
  630. {
  631. return target_ == 0;
  632. }
  633. protected:
  634. bool equality_helper(const any_executor_base& other) const noexcept
  635. {
  636. if (target_ == other.target_)
  637. return true;
  638. if (target_ && !other.target_)
  639. return false;
  640. if (!target_ && other.target_)
  641. return false;
  642. if (target_fns_ != other.target_fns_)
  643. return false;
  644. return target_fns_->equal(*this, other);
  645. }
  646. template <typename Ex>
  647. Ex& object()
  648. {
  649. return *static_cast<Ex*>(static_cast<void*>(&object_));
  650. }
  651. template <typename Ex>
  652. const Ex& object() const
  653. {
  654. return *static_cast<const Ex*>(static_cast<const void*>(&object_));
  655. }
  656. struct object_fns
  657. {
  658. void (*destroy)(any_executor_base&);
  659. void (*copy)(any_executor_base&, const any_executor_base&);
  660. void (*move)(any_executor_base&, any_executor_base&);
  661. const void* (*target)(const any_executor_base&);
  662. };
  663. static void destroy_shared(any_executor_base& ex)
  664. {
  665. typedef shared_target_executor type;
  666. ex.object<type>().~type();
  667. }
  668. static void copy_shared(any_executor_base& ex1, const any_executor_base& ex2)
  669. {
  670. typedef shared_target_executor type;
  671. new (&ex1.object_) type(ex2.object<type>());
  672. ex1.target_ = ex2.target_;
  673. }
  674. static void move_shared(any_executor_base& ex1, any_executor_base& ex2)
  675. {
  676. typedef shared_target_executor type;
  677. new (&ex1.object_) type(static_cast<type&&>(ex2.object<type>()));
  678. ex1.target_ = ex2.target_;
  679. ex2.object<type>().~type();
  680. }
  681. static const void* target_shared(const any_executor_base& ex)
  682. {
  683. typedef shared_target_executor type;
  684. return ex.object<type>().get();
  685. }
  686. template <typename Obj>
  687. static const object_fns* object_fns_table(
  688. enable_if_t<
  689. is_same<Obj, shared_target_executor>::value
  690. >* = 0)
  691. {
  692. static const object_fns fns =
  693. {
  694. &any_executor_base::destroy_shared,
  695. &any_executor_base::copy_shared,
  696. &any_executor_base::move_shared,
  697. &any_executor_base::target_shared
  698. };
  699. return &fns;
  700. }
  701. template <typename Obj>
  702. static void destroy_object(any_executor_base& ex)
  703. {
  704. ex.object<Obj>().~Obj();
  705. }
  706. template <typename Obj>
  707. static void copy_object(any_executor_base& ex1, const any_executor_base& ex2)
  708. {
  709. new (&ex1.object_) Obj(ex2.object<Obj>());
  710. ex1.target_ = &ex1.object<Obj>();
  711. }
  712. template <typename Obj>
  713. static void move_object(any_executor_base& ex1, any_executor_base& ex2)
  714. {
  715. new (&ex1.object_) Obj(static_cast<Obj&&>(ex2.object<Obj>()));
  716. ex1.target_ = &ex1.object<Obj>();
  717. ex2.object<Obj>().~Obj();
  718. }
  719. template <typename Obj>
  720. static const void* target_object(const any_executor_base& ex)
  721. {
  722. return &ex.object<Obj>();
  723. }
  724. template <typename Obj>
  725. static const object_fns* object_fns_table(
  726. enable_if_t<
  727. !is_same<Obj, void>::value
  728. && !is_same<Obj, shared_target_executor>::value
  729. >* = 0)
  730. {
  731. static const object_fns fns =
  732. {
  733. &any_executor_base::destroy_object<Obj>,
  734. &any_executor_base::copy_object<Obj>,
  735. &any_executor_base::move_object<Obj>,
  736. &any_executor_base::target_object<Obj>
  737. };
  738. return &fns;
  739. }
  740. typedef boost::asio::detail::executor_function function;
  741. typedef boost::asio::detail::executor_function_view function_view;
  742. struct target_fns
  743. {
  744. #if !defined(BOOST_ASIO_NO_TYPEID)
  745. const std::type_info& (*target_type)();
  746. #else // !defined(BOOST_ASIO_NO_TYPEID)
  747. const void* (*target_type)();
  748. #endif // !defined(BOOST_ASIO_NO_TYPEID)
  749. bool (*equal)(const any_executor_base&, const any_executor_base&);
  750. void (*execute)(const any_executor_base&, function&&);
  751. void (*blocking_execute)(const any_executor_base&, function_view);
  752. };
  753. #if !defined(BOOST_ASIO_NO_TYPEID)
  754. template <typename Ex>
  755. static const std::type_info& target_type_ex()
  756. {
  757. return typeid(Ex);
  758. }
  759. #else // !defined(BOOST_ASIO_NO_TYPEID)
  760. template <typename Ex>
  761. static const void* target_type_ex()
  762. {
  763. static int unique_id;
  764. return &unique_id;
  765. }
  766. #endif // !defined(BOOST_ASIO_NO_TYPEID)
  767. template <typename Ex>
  768. static bool equal_ex(const any_executor_base& ex1,
  769. const any_executor_base& ex2)
  770. {
  771. const Ex* p1 = ex1.target<Ex>();
  772. const Ex* p2 = ex2.target<Ex>();
  773. BOOST_ASIO_ASSUME(p1 != 0 && p2 != 0);
  774. return *p1 == *p2;
  775. }
  776. template <typename Ex>
  777. static void execute_ex(const any_executor_base& ex, function&& f)
  778. {
  779. const Ex* p = ex.target<Ex>();
  780. BOOST_ASIO_ASSUME(p != 0);
  781. p->execute(static_cast<function&&>(f));
  782. }
  783. template <typename Ex>
  784. static void blocking_execute_ex(const any_executor_base& ex, function_view f)
  785. {
  786. const Ex* p = ex.target<Ex>();
  787. BOOST_ASIO_ASSUME(p != 0);
  788. p->execute(f);
  789. }
  790. template <typename Ex>
  791. static const target_fns* target_fns_table(bool is_always_blocking,
  792. enable_if_t<
  793. !is_same<Ex, void>::value
  794. >* = 0)
  795. {
  796. static const target_fns fns_with_execute =
  797. {
  798. &any_executor_base::target_type_ex<Ex>,
  799. &any_executor_base::equal_ex<Ex>,
  800. &any_executor_base::execute_ex<Ex>,
  801. 0
  802. };
  803. static const target_fns fns_with_blocking_execute =
  804. {
  805. &any_executor_base::target_type_ex<Ex>,
  806. &any_executor_base::equal_ex<Ex>,
  807. 0,
  808. &any_executor_base::blocking_execute_ex<Ex>
  809. };
  810. return is_always_blocking ? &fns_with_blocking_execute : &fns_with_execute;
  811. }
  812. #if defined(BOOST_ASIO_MSVC)
  813. # pragma warning (push)
  814. # pragma warning (disable:4702)
  815. #endif // defined(BOOST_ASIO_MSVC)
  816. static void query_fn_void(void*, const void*, const void*)
  817. {
  818. bad_executor ex;
  819. boost::asio::detail::throw_exception(ex);
  820. }
  821. template <typename Ex, class Prop>
  822. static void query_fn_non_void(void*, const void* ex, const void* prop,
  823. enable_if_t<
  824. boost::asio::can_query<const Ex&, const Prop&>::value
  825. && is_same<typename Prop::polymorphic_query_result_type, void>::value
  826. >*)
  827. {
  828. boost::asio::query(*static_cast<const Ex*>(ex),
  829. *static_cast<const Prop*>(prop));
  830. }
  831. template <typename Ex, class Prop>
  832. static void query_fn_non_void(void*, const void*, const void*,
  833. enable_if_t<
  834. !boost::asio::can_query<const Ex&, const Prop&>::value
  835. && is_same<typename Prop::polymorphic_query_result_type, void>::value
  836. >*)
  837. {
  838. }
  839. template <typename Ex, class Prop>
  840. static void query_fn_non_void(void* result, const void* ex, const void* prop,
  841. enable_if_t<
  842. boost::asio::can_query<const Ex&, const Prop&>::value
  843. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  844. && is_reference<typename Prop::polymorphic_query_result_type>::value
  845. >*)
  846. {
  847. *static_cast<remove_reference_t<
  848. typename Prop::polymorphic_query_result_type>**>(result)
  849. = &static_cast<typename Prop::polymorphic_query_result_type>(
  850. boost::asio::query(*static_cast<const Ex*>(ex),
  851. *static_cast<const Prop*>(prop)));
  852. }
  853. template <typename Ex, class Prop>
  854. static void query_fn_non_void(void*, const void*, const void*,
  855. enable_if_t<
  856. !boost::asio::can_query<const Ex&, const Prop&>::value
  857. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  858. && is_reference<typename Prop::polymorphic_query_result_type>::value
  859. >*)
  860. {
  861. std::terminate(); // Combination should not be possible.
  862. }
  863. template <typename Ex, class Prop>
  864. static void query_fn_non_void(void* result, const void* ex, const void* prop,
  865. enable_if_t<
  866. boost::asio::can_query<const Ex&, const Prop&>::value
  867. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  868. && is_scalar<typename Prop::polymorphic_query_result_type>::value
  869. >*)
  870. {
  871. *static_cast<typename Prop::polymorphic_query_result_type*>(result)
  872. = static_cast<typename Prop::polymorphic_query_result_type>(
  873. boost::asio::query(*static_cast<const Ex*>(ex),
  874. *static_cast<const Prop*>(prop)));
  875. }
  876. template <typename Ex, class Prop>
  877. static void query_fn_non_void(void* result, const void*, const void*,
  878. enable_if_t<
  879. !boost::asio::can_query<const Ex&, const Prop&>::value
  880. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  881. && is_scalar<typename Prop::polymorphic_query_result_type>::value
  882. >*)
  883. {
  884. *static_cast<typename Prop::polymorphic_query_result_type*>(result)
  885. = typename Prop::polymorphic_query_result_type();
  886. }
  887. template <typename Ex, class Prop>
  888. static void query_fn_non_void(void* result, const void* ex, const void* prop,
  889. enable_if_t<
  890. boost::asio::can_query<const Ex&, const Prop&>::value
  891. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  892. && !is_reference<typename Prop::polymorphic_query_result_type>::value
  893. && !is_scalar<typename Prop::polymorphic_query_result_type>::value
  894. >*)
  895. {
  896. *static_cast<typename Prop::polymorphic_query_result_type**>(result)
  897. = new typename Prop::polymorphic_query_result_type(
  898. boost::asio::query(*static_cast<const Ex*>(ex),
  899. *static_cast<const Prop*>(prop)));
  900. }
  901. template <typename Ex, class Prop>
  902. static void query_fn_non_void(void* result, const void*, const void*, ...)
  903. {
  904. *static_cast<typename Prop::polymorphic_query_result_type**>(result)
  905. = new typename Prop::polymorphic_query_result_type();
  906. }
  907. template <typename Ex, class Prop>
  908. static void query_fn_impl(void* result, const void* ex, const void* prop,
  909. enable_if_t<
  910. is_same<Ex, void>::value
  911. >*)
  912. {
  913. query_fn_void(result, ex, prop);
  914. }
  915. template <typename Ex, class Prop>
  916. static void query_fn_impl(void* result, const void* ex, const void* prop,
  917. enable_if_t<
  918. !is_same<Ex, void>::value
  919. >*)
  920. {
  921. query_fn_non_void<Ex, Prop>(result, ex, prop, 0);
  922. }
  923. template <typename Ex, class Prop>
  924. static void query_fn(void* result, const void* ex, const void* prop)
  925. {
  926. query_fn_impl<Ex, Prop>(result, ex, prop, 0);
  927. }
  928. template <typename Poly, typename Ex, class Prop>
  929. static Poly require_fn_impl(const void*, const void*,
  930. enable_if_t<
  931. is_same<Ex, void>::value
  932. >*)
  933. {
  934. bad_executor ex;
  935. boost::asio::detail::throw_exception(ex);
  936. return Poly();
  937. }
  938. template <typename Poly, typename Ex, class Prop>
  939. static Poly require_fn_impl(const void* ex, const void* prop,
  940. enable_if_t<
  941. !is_same<Ex, void>::value && Prop::is_requirable
  942. >*)
  943. {
  944. return boost::asio::require(*static_cast<const Ex*>(ex),
  945. *static_cast<const Prop*>(prop));
  946. }
  947. template <typename Poly, typename Ex, class Prop>
  948. static Poly require_fn_impl(const void*, const void*, ...)
  949. {
  950. return Poly();
  951. }
  952. template <typename Poly, typename Ex, class Prop>
  953. static Poly require_fn(const void* ex, const void* prop)
  954. {
  955. return require_fn_impl<Poly, Ex, Prop>(ex, prop, 0);
  956. }
  957. template <typename Poly, typename Ex, class Prop>
  958. static Poly prefer_fn_impl(const void*, const void*,
  959. enable_if_t<
  960. is_same<Ex, void>::value
  961. >*)
  962. {
  963. bad_executor ex;
  964. boost::asio::detail::throw_exception(ex);
  965. return Poly();
  966. }
  967. template <typename Poly, typename Ex, class Prop>
  968. static Poly prefer_fn_impl(const void* ex, const void* prop,
  969. enable_if_t<
  970. !is_same<Ex, void>::value && Prop::is_preferable
  971. >*)
  972. {
  973. return boost::asio::prefer(*static_cast<const Ex*>(ex),
  974. *static_cast<const Prop*>(prop));
  975. }
  976. template <typename Poly, typename Ex, class Prop>
  977. static Poly prefer_fn_impl(const void*, const void*, ...)
  978. {
  979. return Poly();
  980. }
  981. template <typename Poly, typename Ex, class Prop>
  982. static Poly prefer_fn(const void* ex, const void* prop)
  983. {
  984. return prefer_fn_impl<Poly, Ex, Prop>(ex, prop, 0);
  985. }
  986. template <typename Poly>
  987. struct prop_fns
  988. {
  989. void (*query)(void*, const void*, const void*);
  990. Poly (*require)(const void*, const void*);
  991. Poly (*prefer)(const void*, const void*);
  992. };
  993. #if defined(BOOST_ASIO_MSVC)
  994. # pragma warning (pop)
  995. #endif // defined(BOOST_ASIO_MSVC)
  996. private:
  997. template <typename Executor>
  998. static execution::blocking_t query_blocking(const Executor& ex, true_type)
  999. {
  1000. return boost::asio::query(ex, execution::blocking);
  1001. }
  1002. template <typename Executor>
  1003. static execution::blocking_t query_blocking(const Executor&, false_type)
  1004. {
  1005. return execution::blocking_t();
  1006. }
  1007. template <typename Executor>
  1008. void construct_object(Executor& ex, true_type)
  1009. {
  1010. object_fns_ = object_fns_table<Executor>();
  1011. target_ = new (&object_) Executor(static_cast<Executor&&>(ex));
  1012. }
  1013. template <typename Executor>
  1014. void construct_object(Executor& ex, false_type)
  1015. {
  1016. object_fns_ = object_fns_table<shared_target_executor>();
  1017. Executor* p = 0;
  1018. new (&object_) shared_target_executor(
  1019. static_cast<Executor&&>(ex), p);
  1020. target_ = p;
  1021. }
  1022. template <typename Executor>
  1023. void construct_object(std::nothrow_t,
  1024. Executor& ex, true_type) noexcept
  1025. {
  1026. object_fns_ = object_fns_table<Executor>();
  1027. target_ = new (&object_) Executor(static_cast<Executor&&>(ex));
  1028. }
  1029. template <typename Executor>
  1030. void construct_object(std::nothrow_t,
  1031. Executor& ex, false_type) noexcept
  1032. {
  1033. object_fns_ = object_fns_table<shared_target_executor>();
  1034. Executor* p = 0;
  1035. new (&object_) shared_target_executor(
  1036. std::nothrow, static_cast<Executor&&>(ex), p);
  1037. target_ = p;
  1038. }
  1039. /*private:*/public:
  1040. // template <typename...> friend class any_executor;
  1041. typedef aligned_storage<
  1042. sizeof(boost::asio::detail::shared_ptr<void>) + sizeof(void*),
  1043. alignment_of<boost::asio::detail::shared_ptr<void>>::value
  1044. >::type object_type;
  1045. object_type object_;
  1046. const object_fns* object_fns_;
  1047. void* target_;
  1048. const target_fns* target_fns_;
  1049. };
  1050. template <typename Derived, typename Property, typename = void>
  1051. struct any_executor_context
  1052. {
  1053. };
  1054. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  1055. template <typename Derived, typename Property>
  1056. struct any_executor_context<Derived, Property, enable_if_t<Property::value>>
  1057. {
  1058. typename Property::query_result_type context() const
  1059. {
  1060. return static_cast<const Derived*>(this)->query(typename Property::type());
  1061. }
  1062. };
  1063. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  1064. } // namespace detail
  1065. template <>
  1066. class any_executor<> : public detail::any_executor_base
  1067. {
  1068. public:
  1069. any_executor() noexcept
  1070. : detail::any_executor_base()
  1071. {
  1072. }
  1073. any_executor(nullptr_t) noexcept
  1074. : detail::any_executor_base()
  1075. {
  1076. }
  1077. template <typename Executor>
  1078. any_executor(Executor ex,
  1079. enable_if_t<
  1080. conditional_t<
  1081. !is_same<Executor, any_executor>::value
  1082. && !is_base_of<detail::any_executor_base, Executor>::value,
  1083. is_executor<Executor>,
  1084. false_type
  1085. >::value
  1086. >* = 0)
  1087. : detail::any_executor_base(
  1088. static_cast<Executor&&>(ex), false_type())
  1089. {
  1090. }
  1091. template <typename Executor>
  1092. any_executor(std::nothrow_t, Executor ex,
  1093. enable_if_t<
  1094. conditional_t<
  1095. !is_same<Executor, any_executor>::value
  1096. && !is_base_of<detail::any_executor_base, Executor>::value,
  1097. is_executor<Executor>,
  1098. false_type
  1099. >::value
  1100. >* = 0) noexcept
  1101. : detail::any_executor_base(std::nothrow,
  1102. static_cast<Executor&&>(ex), false_type())
  1103. {
  1104. }
  1105. template <typename... OtherSupportableProperties>
  1106. any_executor(any_executor<OtherSupportableProperties...> other)
  1107. : detail::any_executor_base(
  1108. static_cast<const detail::any_executor_base&>(other))
  1109. {
  1110. }
  1111. template <typename... OtherSupportableProperties>
  1112. any_executor(std::nothrow_t,
  1113. any_executor<OtherSupportableProperties...> other) noexcept
  1114. : detail::any_executor_base(
  1115. static_cast<const detail::any_executor_base&>(other))
  1116. {
  1117. }
  1118. any_executor(const any_executor& other) noexcept
  1119. : detail::any_executor_base(
  1120. static_cast<const detail::any_executor_base&>(other))
  1121. {
  1122. }
  1123. any_executor(std::nothrow_t, const any_executor& other) noexcept
  1124. : detail::any_executor_base(
  1125. static_cast<const detail::any_executor_base&>(other))
  1126. {
  1127. }
  1128. any_executor& operator=(const any_executor& other) noexcept
  1129. {
  1130. if (this != &other)
  1131. {
  1132. detail::any_executor_base::operator=(
  1133. static_cast<const detail::any_executor_base&>(other));
  1134. }
  1135. return *this;
  1136. }
  1137. any_executor& operator=(nullptr_t p) noexcept
  1138. {
  1139. detail::any_executor_base::operator=(p);
  1140. return *this;
  1141. }
  1142. any_executor(any_executor&& other) noexcept
  1143. : detail::any_executor_base(
  1144. static_cast<any_executor_base&&>(
  1145. static_cast<any_executor_base&>(other)))
  1146. {
  1147. }
  1148. any_executor(std::nothrow_t, any_executor&& other) noexcept
  1149. : detail::any_executor_base(
  1150. static_cast<any_executor_base&&>(
  1151. static_cast<any_executor_base&>(other)))
  1152. {
  1153. }
  1154. any_executor& operator=(any_executor&& other) noexcept
  1155. {
  1156. if (this != &other)
  1157. {
  1158. detail::any_executor_base::operator=(
  1159. static_cast<detail::any_executor_base&&>(
  1160. static_cast<detail::any_executor_base&>(other)));
  1161. }
  1162. return *this;
  1163. }
  1164. void swap(any_executor& other) noexcept
  1165. {
  1166. detail::any_executor_base::swap(
  1167. static_cast<detail::any_executor_base&>(other));
  1168. }
  1169. using detail::any_executor_base::execute;
  1170. using detail::any_executor_base::target;
  1171. using detail::any_executor_base::target_type;
  1172. using detail::any_executor_base::operator unspecified_bool_type;
  1173. using detail::any_executor_base::operator!;
  1174. bool equality_helper(const any_executor& other) const noexcept
  1175. {
  1176. return any_executor_base::equality_helper(other);
  1177. }
  1178. template <typename AnyExecutor1, typename AnyExecutor2>
  1179. friend enable_if_t<
  1180. is_base_of<any_executor, AnyExecutor1>::value
  1181. || is_base_of<any_executor, AnyExecutor2>::value,
  1182. bool
  1183. > operator==(const AnyExecutor1& a,
  1184. const AnyExecutor2& b) noexcept
  1185. {
  1186. return static_cast<const any_executor&>(a).equality_helper(b);
  1187. }
  1188. template <typename AnyExecutor>
  1189. friend enable_if_t<
  1190. is_same<AnyExecutor, any_executor>::value,
  1191. bool
  1192. > operator==(const AnyExecutor& a, nullptr_t) noexcept
  1193. {
  1194. return !a;
  1195. }
  1196. template <typename AnyExecutor>
  1197. friend enable_if_t<
  1198. is_same<AnyExecutor, any_executor>::value,
  1199. bool
  1200. > operator==(nullptr_t, const AnyExecutor& b) noexcept
  1201. {
  1202. return !b;
  1203. }
  1204. template <typename AnyExecutor1, typename AnyExecutor2>
  1205. friend enable_if_t<
  1206. is_base_of<any_executor, AnyExecutor1>::value
  1207. || is_base_of<any_executor, AnyExecutor2>::value,
  1208. bool
  1209. > operator!=(const AnyExecutor1& a,
  1210. const AnyExecutor2& b) noexcept
  1211. {
  1212. return !static_cast<const any_executor&>(a).equality_helper(b);
  1213. }
  1214. template <typename AnyExecutor>
  1215. friend enable_if_t<
  1216. is_same<AnyExecutor, any_executor>::value,
  1217. bool
  1218. > operator!=(const AnyExecutor& a, nullptr_t) noexcept
  1219. {
  1220. return !!a;
  1221. }
  1222. template <typename AnyExecutor>
  1223. friend enable_if_t<
  1224. is_same<AnyExecutor, any_executor>::value,
  1225. bool
  1226. > operator!=(nullptr_t, const AnyExecutor& b) noexcept
  1227. {
  1228. return !!b;
  1229. }
  1230. };
  1231. inline void swap(any_executor<>& a, any_executor<>& b) noexcept
  1232. {
  1233. return a.swap(b);
  1234. }
  1235. template <typename... SupportableProperties>
  1236. class any_executor :
  1237. public detail::any_executor_base,
  1238. public detail::any_executor_context<
  1239. any_executor<SupportableProperties...>,
  1240. typename detail::supportable_properties<
  1241. 0, void(SupportableProperties...)>::find_context_as_property>
  1242. {
  1243. public:
  1244. any_executor() noexcept
  1245. : detail::any_executor_base(),
  1246. prop_fns_(prop_fns_table<void>())
  1247. {
  1248. }
  1249. any_executor(nullptr_t) noexcept
  1250. : detail::any_executor_base(),
  1251. prop_fns_(prop_fns_table<void>())
  1252. {
  1253. }
  1254. template <typename Executor>
  1255. any_executor(Executor ex,
  1256. enable_if_t<
  1257. conditional_t<
  1258. !is_same<Executor, any_executor>::value
  1259. && !is_base_of<detail::any_executor_base, Executor>::value,
  1260. detail::is_valid_target_executor<
  1261. Executor, void(SupportableProperties...)>,
  1262. false_type
  1263. >::value
  1264. >* = 0)
  1265. : detail::any_executor_base(
  1266. static_cast<Executor&&>(ex), false_type()),
  1267. prop_fns_(prop_fns_table<Executor>())
  1268. {
  1269. }
  1270. template <typename Executor>
  1271. any_executor(std::nothrow_t, Executor ex,
  1272. enable_if_t<
  1273. conditional_t<
  1274. !is_same<Executor, any_executor>::value
  1275. && !is_base_of<detail::any_executor_base, Executor>::value,
  1276. detail::is_valid_target_executor<
  1277. Executor, void(SupportableProperties...)>,
  1278. false_type
  1279. >::value
  1280. >* = 0) noexcept
  1281. : detail::any_executor_base(std::nothrow,
  1282. static_cast<Executor&&>(ex), false_type()),
  1283. prop_fns_(prop_fns_table<Executor>())
  1284. {
  1285. if (this->template target<void>() == 0)
  1286. prop_fns_ = prop_fns_table<void>();
  1287. }
  1288. template <typename... OtherSupportableProperties>
  1289. any_executor(any_executor<OtherSupportableProperties...> other,
  1290. enable_if_t<
  1291. conditional_t<
  1292. !is_same<
  1293. any_executor<OtherSupportableProperties...>,
  1294. any_executor
  1295. >::value,
  1296. typename detail::supportable_properties<
  1297. 0, void(SupportableProperties...)>::template is_valid_target<
  1298. any_executor<OtherSupportableProperties...>>,
  1299. false_type
  1300. >::value
  1301. >* = 0)
  1302. : detail::any_executor_base(
  1303. static_cast<any_executor<OtherSupportableProperties...>&&>(other),
  1304. true_type()),
  1305. prop_fns_(prop_fns_table<any_executor<OtherSupportableProperties...>>())
  1306. {
  1307. }
  1308. template <typename... OtherSupportableProperties>
  1309. any_executor(std::nothrow_t,
  1310. any_executor<OtherSupportableProperties...> other,
  1311. enable_if_t<
  1312. conditional_t<
  1313. !is_same<
  1314. any_executor<OtherSupportableProperties...>,
  1315. any_executor
  1316. >::value,
  1317. typename detail::supportable_properties<
  1318. 0, void(SupportableProperties...)>::template is_valid_target<
  1319. any_executor<OtherSupportableProperties...>>,
  1320. false_type
  1321. >::value
  1322. >* = 0) noexcept
  1323. : detail::any_executor_base(std::nothrow,
  1324. static_cast<any_executor<OtherSupportableProperties...>&&>(other),
  1325. true_type()),
  1326. prop_fns_(prop_fns_table<any_executor<OtherSupportableProperties...>>())
  1327. {
  1328. if (this->template target<void>() == 0)
  1329. prop_fns_ = prop_fns_table<void>();
  1330. }
  1331. any_executor(const any_executor& other) noexcept
  1332. : detail::any_executor_base(
  1333. static_cast<const detail::any_executor_base&>(other)),
  1334. prop_fns_(other.prop_fns_)
  1335. {
  1336. }
  1337. any_executor(std::nothrow_t, const any_executor& other) noexcept
  1338. : detail::any_executor_base(
  1339. static_cast<const detail::any_executor_base&>(other)),
  1340. prop_fns_(other.prop_fns_)
  1341. {
  1342. }
  1343. any_executor& operator=(const any_executor& other) noexcept
  1344. {
  1345. if (this != &other)
  1346. {
  1347. prop_fns_ = other.prop_fns_;
  1348. detail::any_executor_base::operator=(
  1349. static_cast<const detail::any_executor_base&>(other));
  1350. }
  1351. return *this;
  1352. }
  1353. any_executor& operator=(nullptr_t p) noexcept
  1354. {
  1355. prop_fns_ = prop_fns_table<void>();
  1356. detail::any_executor_base::operator=(p);
  1357. return *this;
  1358. }
  1359. any_executor(any_executor&& other) noexcept
  1360. : detail::any_executor_base(
  1361. static_cast<any_executor_base&&>(
  1362. static_cast<any_executor_base&>(other))),
  1363. prop_fns_(other.prop_fns_)
  1364. {
  1365. other.prop_fns_ = prop_fns_table<void>();
  1366. }
  1367. any_executor(std::nothrow_t, any_executor&& other) noexcept
  1368. : detail::any_executor_base(
  1369. static_cast<any_executor_base&&>(
  1370. static_cast<any_executor_base&>(other))),
  1371. prop_fns_(other.prop_fns_)
  1372. {
  1373. other.prop_fns_ = prop_fns_table<void>();
  1374. }
  1375. any_executor& operator=(any_executor&& other) noexcept
  1376. {
  1377. if (this != &other)
  1378. {
  1379. prop_fns_ = other.prop_fns_;
  1380. detail::any_executor_base::operator=(
  1381. static_cast<detail::any_executor_base&&>(
  1382. static_cast<detail::any_executor_base&>(other)));
  1383. }
  1384. return *this;
  1385. }
  1386. void swap(any_executor& other) noexcept
  1387. {
  1388. if (this != &other)
  1389. {
  1390. detail::any_executor_base::swap(
  1391. static_cast<detail::any_executor_base&>(other));
  1392. const prop_fns<any_executor>* tmp_prop_fns = other.prop_fns_;
  1393. other.prop_fns_ = prop_fns_;
  1394. prop_fns_ = tmp_prop_fns;
  1395. }
  1396. }
  1397. using detail::any_executor_base::execute;
  1398. using detail::any_executor_base::target;
  1399. using detail::any_executor_base::target_type;
  1400. using detail::any_executor_base::operator unspecified_bool_type;
  1401. using detail::any_executor_base::operator!;
  1402. bool equality_helper(const any_executor& other) const noexcept
  1403. {
  1404. return any_executor_base::equality_helper(other);
  1405. }
  1406. template <typename AnyExecutor1, typename AnyExecutor2>
  1407. friend enable_if_t<
  1408. is_base_of<any_executor, AnyExecutor1>::value
  1409. || is_base_of<any_executor, AnyExecutor2>::value,
  1410. bool
  1411. > operator==(const AnyExecutor1& a,
  1412. const AnyExecutor2& b) noexcept
  1413. {
  1414. return static_cast<const any_executor&>(a).equality_helper(b);
  1415. }
  1416. template <typename AnyExecutor>
  1417. friend enable_if_t<
  1418. is_same<AnyExecutor, any_executor>::value,
  1419. bool
  1420. > operator==(const AnyExecutor& a, nullptr_t) noexcept
  1421. {
  1422. return !a;
  1423. }
  1424. template <typename AnyExecutor>
  1425. friend enable_if_t<
  1426. is_same<AnyExecutor, any_executor>::value,
  1427. bool
  1428. > operator==(nullptr_t, const AnyExecutor& b) noexcept
  1429. {
  1430. return !b;
  1431. }
  1432. template <typename AnyExecutor1, typename AnyExecutor2>
  1433. friend enable_if_t<
  1434. is_base_of<any_executor, AnyExecutor1>::value
  1435. || is_base_of<any_executor, AnyExecutor2>::value,
  1436. bool
  1437. > operator!=(const AnyExecutor1& a,
  1438. const AnyExecutor2& b) noexcept
  1439. {
  1440. return !static_cast<const any_executor&>(a).equality_helper(b);
  1441. }
  1442. template <typename AnyExecutor>
  1443. friend enable_if_t<
  1444. is_same<AnyExecutor, any_executor>::value,
  1445. bool
  1446. > operator!=(const AnyExecutor& a, nullptr_t) noexcept
  1447. {
  1448. return !!a;
  1449. }
  1450. template <typename AnyExecutor>
  1451. friend enable_if_t<
  1452. is_same<AnyExecutor, any_executor>::value,
  1453. bool
  1454. > operator!=(nullptr_t, const AnyExecutor& b) noexcept
  1455. {
  1456. return !!b;
  1457. }
  1458. template <typename T>
  1459. struct find_convertible_property :
  1460. detail::supportable_properties<
  1461. 0, void(SupportableProperties...)>::template
  1462. find_convertible_property<T> {};
  1463. template <typename Property>
  1464. void query(const Property& p,
  1465. enable_if_t<
  1466. is_same<
  1467. typename find_convertible_property<Property>::query_result_type,
  1468. void
  1469. >::value
  1470. >* = 0) const
  1471. {
  1472. if (!target_)
  1473. {
  1474. bad_executor ex;
  1475. boost::asio::detail::throw_exception(ex);
  1476. }
  1477. typedef find_convertible_property<Property> found;
  1478. prop_fns_[found::index].query(0, object_fns_->target(*this),
  1479. &static_cast<const typename found::type&>(p));
  1480. }
  1481. template <typename Property>
  1482. typename find_convertible_property<Property>::query_result_type
  1483. query(const Property& p,
  1484. enable_if_t<
  1485. !is_same<
  1486. typename find_convertible_property<Property>::query_result_type,
  1487. void
  1488. >::value
  1489. &&
  1490. is_reference<
  1491. typename find_convertible_property<Property>::query_result_type
  1492. >::value
  1493. >* = 0) const
  1494. {
  1495. if (!target_)
  1496. {
  1497. bad_executor ex;
  1498. boost::asio::detail::throw_exception(ex);
  1499. }
  1500. typedef find_convertible_property<Property> found;
  1501. remove_reference_t<typename found::query_result_type>* result = 0;
  1502. prop_fns_[found::index].query(&result, object_fns_->target(*this),
  1503. &static_cast<const typename found::type&>(p));
  1504. return *result;
  1505. }
  1506. template <typename Property>
  1507. typename find_convertible_property<Property>::query_result_type
  1508. query(const Property& p,
  1509. enable_if_t<
  1510. !is_same<
  1511. typename find_convertible_property<Property>::query_result_type,
  1512. void
  1513. >::value
  1514. &&
  1515. is_scalar<
  1516. typename find_convertible_property<Property>::query_result_type
  1517. >::value
  1518. >* = 0) const
  1519. {
  1520. if (!target_)
  1521. {
  1522. bad_executor ex;
  1523. boost::asio::detail::throw_exception(ex);
  1524. }
  1525. typedef find_convertible_property<Property> found;
  1526. typename found::query_result_type result;
  1527. prop_fns_[found::index].query(&result, object_fns_->target(*this),
  1528. &static_cast<const typename found::type&>(p));
  1529. return result;
  1530. }
  1531. template <typename Property>
  1532. typename find_convertible_property<Property>::query_result_type
  1533. query(const Property& p,
  1534. enable_if_t<
  1535. !is_same<
  1536. typename find_convertible_property<Property>::query_result_type,
  1537. void
  1538. >::value
  1539. &&
  1540. !is_reference<
  1541. typename find_convertible_property<Property>::query_result_type
  1542. >::value
  1543. &&
  1544. !is_scalar<
  1545. typename find_convertible_property<Property>::query_result_type
  1546. >::value
  1547. >* = 0) const
  1548. {
  1549. if (!target_)
  1550. {
  1551. bad_executor ex;
  1552. boost::asio::detail::throw_exception(ex);
  1553. }
  1554. typedef find_convertible_property<Property> found;
  1555. typename found::query_result_type* result;
  1556. prop_fns_[found::index].query(&result, object_fns_->target(*this),
  1557. &static_cast<const typename found::type&>(p));
  1558. return *boost::asio::detail::scoped_ptr<
  1559. typename found::query_result_type>(result);
  1560. }
  1561. template <typename T>
  1562. struct find_convertible_requirable_property :
  1563. detail::supportable_properties<
  1564. 0, void(SupportableProperties...)>::template
  1565. find_convertible_requirable_property<T> {};
  1566. template <typename Property>
  1567. any_executor require(const Property& p,
  1568. enable_if_t<
  1569. find_convertible_requirable_property<Property>::value
  1570. >* = 0) const
  1571. {
  1572. if (!target_)
  1573. {
  1574. bad_executor ex;
  1575. boost::asio::detail::throw_exception(ex);
  1576. }
  1577. typedef find_convertible_requirable_property<Property> found;
  1578. return prop_fns_[found::index].require(object_fns_->target(*this),
  1579. &static_cast<const typename found::type&>(p));
  1580. }
  1581. template <typename T>
  1582. struct find_convertible_preferable_property :
  1583. detail::supportable_properties<
  1584. 0, void(SupportableProperties...)>::template
  1585. find_convertible_preferable_property<T> {};
  1586. template <typename Property>
  1587. any_executor prefer(const Property& p,
  1588. enable_if_t<
  1589. find_convertible_preferable_property<Property>::value
  1590. >* = 0) const
  1591. {
  1592. if (!target_)
  1593. {
  1594. bad_executor ex;
  1595. boost::asio::detail::throw_exception(ex);
  1596. }
  1597. typedef find_convertible_preferable_property<Property> found;
  1598. return prop_fns_[found::index].prefer(object_fns_->target(*this),
  1599. &static_cast<const typename found::type&>(p));
  1600. }
  1601. //private:
  1602. template <typename Ex>
  1603. static const prop_fns<any_executor>* prop_fns_table()
  1604. {
  1605. static const prop_fns<any_executor> fns[] =
  1606. {
  1607. {
  1608. &detail::any_executor_base::query_fn<
  1609. Ex, SupportableProperties>,
  1610. &detail::any_executor_base::require_fn<
  1611. any_executor, Ex, SupportableProperties>,
  1612. &detail::any_executor_base::prefer_fn<
  1613. any_executor, Ex, SupportableProperties>
  1614. }...
  1615. };
  1616. return fns;
  1617. }
  1618. const prop_fns<any_executor>* prop_fns_;
  1619. };
  1620. template <typename... SupportableProperties>
  1621. inline void swap(any_executor<SupportableProperties...>& a,
  1622. any_executor<SupportableProperties...>& b) noexcept
  1623. {
  1624. return a.swap(b);
  1625. }
  1626. } // namespace execution
  1627. namespace traits {
  1628. #if !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  1629. template <typename... SupportableProperties>
  1630. struct equality_comparable<execution::any_executor<SupportableProperties...>>
  1631. {
  1632. static constexpr bool is_valid = true;
  1633. static constexpr bool is_noexcept = true;
  1634. };
  1635. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  1636. #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  1637. template <typename F, typename... SupportableProperties>
  1638. struct execute_member<execution::any_executor<SupportableProperties...>, F>
  1639. {
  1640. static constexpr bool is_valid = true;
  1641. static constexpr bool is_noexcept = false;
  1642. typedef void result_type;
  1643. };
  1644. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  1645. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  1646. template <typename Prop, typename... SupportableProperties>
  1647. struct query_member<
  1648. execution::any_executor<SupportableProperties...>, Prop,
  1649. enable_if_t<
  1650. execution::detail::supportable_properties<
  1651. 0, void(SupportableProperties...)>::template
  1652. find_convertible_property<Prop>::value
  1653. >>
  1654. {
  1655. static constexpr bool is_valid = true;
  1656. static constexpr bool is_noexcept = false;
  1657. typedef typename execution::detail::supportable_properties<
  1658. 0, void(SupportableProperties...)>::template
  1659. find_convertible_property<Prop>::query_result_type result_type;
  1660. };
  1661. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  1662. #if !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  1663. template <typename Prop, typename... SupportableProperties>
  1664. struct require_member<
  1665. execution::any_executor<SupportableProperties...>, Prop,
  1666. enable_if_t<
  1667. execution::detail::supportable_properties<
  1668. 0, void(SupportableProperties...)>::template
  1669. find_convertible_requirable_property<Prop>::value
  1670. >>
  1671. {
  1672. static constexpr bool is_valid = true;
  1673. static constexpr bool is_noexcept = false;
  1674. typedef execution::any_executor<SupportableProperties...> result_type;
  1675. };
  1676. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  1677. #if !defined(BOOST_ASIO_HAS_DEDUCED_PREFER_FREE_TRAIT)
  1678. template <typename Prop, typename... SupportableProperties>
  1679. struct prefer_member<
  1680. execution::any_executor<SupportableProperties...>, Prop,
  1681. enable_if_t<
  1682. execution::detail::supportable_properties<
  1683. 0, void(SupportableProperties...)>::template
  1684. find_convertible_preferable_property<Prop>::value
  1685. >>
  1686. {
  1687. static constexpr bool is_valid = true;
  1688. static constexpr bool is_noexcept = false;
  1689. typedef execution::any_executor<SupportableProperties...> result_type;
  1690. };
  1691. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_PREFER_FREE_TRAIT)
  1692. } // namespace traits
  1693. #endif // defined(GENERATING_DOCUMENTATION)
  1694. } // namespace asio
  1695. } // namespace boost
  1696. #include <boost/asio/detail/pop_options.hpp>
  1697. #endif // BOOST_ASIO_EXECUTION_ANY_EXECUTOR_HPP