any_executor.hpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  1. //
  2. // execution/any_executor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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::require(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