any_executor.hpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  1. //
  2. // execution/any_executor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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 ASIO_EXECUTION_ANY_EXECUTOR_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include <new>
  17. #include <typeinfo>
  18. #include "asio/detail/assert.hpp"
  19. #include "asio/detail/cstddef.hpp"
  20. #include "asio/detail/executor_function.hpp"
  21. #include "asio/detail/memory.hpp"
  22. #include "asio/detail/non_const_lvalue.hpp"
  23. #include "asio/detail/scoped_ptr.hpp"
  24. #include "asio/detail/type_traits.hpp"
  25. #include "asio/detail/throw_exception.hpp"
  26. #include "asio/detail/variadic_templates.hpp"
  27. #include "asio/execution/bad_executor.hpp"
  28. #include "asio/execution/blocking.hpp"
  29. #include "asio/execution/execute.hpp"
  30. #include "asio/execution/executor.hpp"
  31. #include "asio/prefer.hpp"
  32. #include "asio/query.hpp"
  33. #include "asio/require.hpp"
  34. #include "asio/detail/push_options.hpp"
  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 a polymorphic wrapper for the specified executor.
  55. template <typename Executor>
  56. any_executor(Executor e);
  57. /// Assignment operator.
  58. any_executor& operator=(const any_executor& e) noexcept;
  59. /// Move assignment operator.
  60. any_executor& operator=(any_executor&& e) noexcept;
  61. /// Assignment operator that sets the polymorphic wrapper to the empty state.
  62. any_executor& operator=(nullptr_t);
  63. /// Assignment operator to create a polymorphic wrapper for the specified
  64. /// executor.
  65. template <typename Executor>
  66. any_executor& operator=(Executor e);
  67. /// Destructor.
  68. ~any_executor();
  69. /// Swap targets with another polymorphic wrapper.
  70. void swap(any_executor& other) noexcept;
  71. /// Obtain a polymorphic wrapper with the specified property.
  72. /**
  73. * Do not call this function directly. It is intended for use with the
  74. * asio::require and asio::prefer customisation points.
  75. *
  76. * For example:
  77. * @code execution::any_executor<execution::blocking_t::possibly_t> ex = ...;
  78. * auto ex2 = asio::requre(ex, execution::blocking.possibly); @endcode
  79. */
  80. template <typename Property>
  81. any_executor require(Property) const;
  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. * asio::prefer customisation point.
  86. *
  87. * For example:
  88. * @code execution::any_executor<execution::blocking_t::possibly_t> ex = ...;
  89. * auto ex2 = asio::prefer(ex, execution::blocking.possibly); @endcode
  90. */
  91. template <typename Property>
  92. any_executor prefer(Property) const;
  93. /// Obtain the value associated with the specified property.
  94. /**
  95. * Do not call this function directly. It is intended for use with the
  96. * asio::query customisation point.
  97. *
  98. * For example:
  99. * @code execution::any_executor<execution::occupancy_t> ex = ...;
  100. * size_t n = asio::query(ex, execution::occupancy); @endcode
  101. */
  102. template <typename Property>
  103. typename Property::polymorphic_query_result_type query(Property) const;
  104. /// Execute the function on the target executor.
  105. /**
  106. * Do not call this function directly. It is intended for use with the
  107. * execution::execute customisation point.
  108. *
  109. * For example:
  110. * @code execution::any_executor<> ex = ...;
  111. * execution::execute(ex, my_function_object); @endcode
  112. *
  113. * Throws asio::bad_executor if the polymorphic wrapper has no target.
  114. */
  115. template <typename Function>
  116. void execute(Function&& f) const;
  117. /// Obtain the underlying execution context.
  118. /**
  119. * This function is provided for backward compatibility. It is automatically
  120. * defined when the @c SupportableProperties... list includes a property of
  121. * type <tt>execution::context_as<U></tt>, for some type <tt>U</tt>.
  122. */
  123. automatically_determined context() const;
  124. /// Determine whether the wrapper has a target executor.
  125. /**
  126. * @returns @c true if the polymorphic wrapper has a target executor,
  127. * otherwise false.
  128. */
  129. explicit operator bool() const noexcept;
  130. /// Get the type of the target executor.
  131. const type_info& target_type() const noexcept;
  132. /// Get a pointer to the target executor.
  133. template <typename Executor> Executor* target() noexcept;
  134. /// Get a pointer to the target executor.
  135. template <typename Executor> const Executor* target() const noexcept;
  136. };
  137. /// Equality operator.
  138. /**
  139. * @relates any_executor
  140. */
  141. template <typename... SupportableProperties>
  142. bool operator==(const any_executor<SupportableProperties...>& a,
  143. const any_executor<SupportableProperties...>& b) noexcept;
  144. /// Equality operator.
  145. /**
  146. * @relates any_executor
  147. */
  148. template <typename... SupportableProperties>
  149. bool operator==(const any_executor<SupportableProperties...>& a,
  150. nullptr_t) noexcept;
  151. /// Equality operator.
  152. /**
  153. * @relates any_executor
  154. */
  155. template <typename... SupportableProperties>
  156. bool operator==(nullptr_t,
  157. const any_executor<SupportableProperties...>& b) noexcept;
  158. /// Inequality operator.
  159. /**
  160. * @relates any_executor
  161. */
  162. template <typename... SupportableProperties>
  163. bool operator!=(const any_executor<SupportableProperties...>& a,
  164. const any_executor<SupportableProperties...>& b) noexcept;
  165. /// Inequality operator.
  166. /**
  167. * @relates any_executor
  168. */
  169. template <typename... SupportableProperties>
  170. bool operator!=(const any_executor<SupportableProperties...>& a,
  171. nullptr_t) noexcept;
  172. /// Inequality operator.
  173. /**
  174. * @relates any_executor
  175. */
  176. template <typename... SupportableProperties>
  177. bool operator!=(nullptr_t,
  178. const any_executor<SupportableProperties...>& b) noexcept;
  179. } // namespace execution
  180. #else // defined(GENERATING_DOCUMENTATION)
  181. namespace execution {
  182. #if !defined(ASIO_EXECUTION_ANY_EXECUTOR_FWD_DECL)
  183. #define ASIO_EXECUTION_ANY_EXECUTOR_FWD_DECL
  184. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  185. template <typename... SupportableProperties>
  186. class any_executor;
  187. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  188. template <typename = void, typename = void, typename = void,
  189. typename = void, typename = void, typename = void,
  190. typename = void, typename = void, typename = void>
  191. class any_executor;
  192. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  193. #endif // !defined(ASIO_EXECUTION_ANY_EXECUTOR_FWD_DECL)
  194. template <typename U>
  195. struct context_as_t;
  196. namespace detail {
  197. // Traits used to detect whether a property is requirable or preferable, taking
  198. // into account that T::is_requirable or T::is_preferable may not not be well
  199. // formed.
  200. template <typename T, typename = void>
  201. struct is_requirable : false_type {};
  202. template <typename T>
  203. struct is_requirable<T, typename enable_if<T::is_requirable>::type> :
  204. true_type {};
  205. template <typename T, typename = void>
  206. struct is_preferable : false_type {};
  207. template <typename T>
  208. struct is_preferable<T, typename enable_if<T::is_preferable>::type> :
  209. true_type {};
  210. // Trait used to detect context_as property, for backward compatibility.
  211. template <typename T>
  212. struct is_context_as : false_type {};
  213. template <typename U>
  214. struct is_context_as<context_as_t<U> > : true_type {};
  215. // Helper template to:
  216. // - Check if a target can supply the supportable properties.
  217. // - Find the first convertible-from-T property in the list.
  218. template <std::size_t I, typename Props>
  219. struct supportable_properties;
  220. template <std::size_t I, typename Prop>
  221. struct supportable_properties<I, void(Prop)>
  222. {
  223. template <typename T>
  224. struct is_valid_target : integral_constant<bool,
  225. (
  226. is_requirable<Prop>::value
  227. ? can_require<T, Prop>::value
  228. : true
  229. )
  230. &&
  231. (
  232. is_preferable<Prop>::value
  233. ? can_prefer<T, Prop>::value
  234. : true
  235. )
  236. &&
  237. (
  238. !is_requirable<Prop>::value && !is_preferable<Prop>::value
  239. ? can_query<T, Prop>::value
  240. : true
  241. )
  242. >
  243. {
  244. };
  245. struct found
  246. {
  247. ASIO_STATIC_CONSTEXPR(bool, value = true);
  248. typedef Prop type;
  249. typedef typename Prop::polymorphic_query_result_type query_result_type;
  250. ASIO_STATIC_CONSTEXPR(std::size_t, index = I);
  251. };
  252. struct not_found
  253. {
  254. ASIO_STATIC_CONSTEXPR(bool, value = false);
  255. };
  256. template <typename T>
  257. struct find_convertible_property :
  258. conditional<
  259. is_same<T, Prop>::value || is_convertible<T, Prop>::value,
  260. found,
  261. not_found
  262. >::type {};
  263. template <typename T>
  264. struct find_convertible_requirable_property :
  265. conditional<
  266. is_requirable<Prop>::value
  267. && (is_same<T, Prop>::value || is_convertible<T, Prop>::value),
  268. found,
  269. not_found
  270. >::type {};
  271. template <typename T>
  272. struct find_convertible_preferable_property :
  273. conditional<
  274. is_preferable<Prop>::value
  275. && (is_same<T, Prop>::value || is_convertible<T, Prop>::value),
  276. found,
  277. not_found
  278. >::type {};
  279. struct find_context_as_property :
  280. conditional<
  281. is_context_as<Prop>::value,
  282. found,
  283. not_found
  284. >::type {};
  285. };
  286. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  287. template <std::size_t I, typename Head, typename... Tail>
  288. struct supportable_properties<I, void(Head, Tail...)>
  289. {
  290. template <typename T>
  291. struct is_valid_target : integral_constant<bool,
  292. (
  293. supportable_properties<I,
  294. void(Head)>::template is_valid_target<T>::value
  295. &&
  296. supportable_properties<I + 1,
  297. void(Tail...)>::template is_valid_target<T>::value
  298. )
  299. >
  300. {
  301. };
  302. template <typename T>
  303. struct find_convertible_property :
  304. conditional<
  305. is_convertible<T, Head>::value,
  306. typename supportable_properties<I, void(Head)>::found,
  307. typename supportable_properties<I + 1,
  308. void(Tail...)>::template find_convertible_property<T>
  309. >::type {};
  310. template <typename T>
  311. struct find_convertible_requirable_property :
  312. conditional<
  313. is_requirable<Head>::value
  314. && is_convertible<T, Head>::value,
  315. typename supportable_properties<I, void(Head)>::found,
  316. typename supportable_properties<I + 1,
  317. void(Tail...)>::template find_convertible_requirable_property<T>
  318. >::type {};
  319. template <typename T>
  320. struct find_convertible_preferable_property :
  321. conditional<
  322. is_preferable<Head>::value
  323. && is_convertible<T, Head>::value,
  324. typename supportable_properties<I, void(Head)>::found,
  325. typename supportable_properties<I + 1,
  326. void(Tail...)>::template find_convertible_preferable_property<T>
  327. >::type {};
  328. struct find_context_as_property :
  329. conditional<
  330. is_context_as<Head>::value,
  331. typename supportable_properties<I, void(Head)>::found,
  332. typename supportable_properties<I + 1,
  333. void(Tail...)>::find_context_as_property
  334. >::type {};
  335. };
  336. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  337. #define ASIO_PRIVATE_ANY_EXECUTOR_PROPS_BASE_DEF(n) \
  338. template <std::size_t I, \
  339. typename Head, ASIO_VARIADIC_TPARAMS(n)> \
  340. struct supportable_properties<I, \
  341. void(Head, ASIO_VARIADIC_TARGS(n))> \
  342. { \
  343. template <typename T> \
  344. struct is_valid_target : integral_constant<bool, \
  345. ( \
  346. supportable_properties<I, \
  347. void(Head)>::template is_valid_target<T>::value \
  348. && \
  349. supportable_properties<I + 1, \
  350. void(ASIO_VARIADIC_TARGS(n))>::template \
  351. is_valid_target<T>::value \
  352. ) \
  353. > \
  354. { \
  355. }; \
  356. \
  357. template <typename T> \
  358. struct find_convertible_property : \
  359. conditional< \
  360. is_convertible<T, Head>::value, \
  361. typename supportable_properties<I, void(Head)>::found, \
  362. typename supportable_properties<I + 1, \
  363. void(ASIO_VARIADIC_TARGS(n))>::template \
  364. find_convertible_property<T> \
  365. >::type {}; \
  366. \
  367. template <typename T> \
  368. struct find_convertible_requirable_property : \
  369. conditional< \
  370. is_requirable<Head>::value \
  371. && is_convertible<T, Head>::value, \
  372. typename supportable_properties<I, void(Head)>::found, \
  373. typename supportable_properties<I + 1, \
  374. void(ASIO_VARIADIC_TARGS(n))>::template \
  375. find_convertible_requirable_property<T> \
  376. >::type {}; \
  377. \
  378. template <typename T> \
  379. struct find_convertible_preferable_property : \
  380. conditional< \
  381. is_preferable<Head>::value \
  382. && is_convertible<T, Head>::value, \
  383. typename supportable_properties<I, void(Head)>::found, \
  384. typename supportable_properties<I + 1, \
  385. void(ASIO_VARIADIC_TARGS(n))>::template \
  386. find_convertible_preferable_property<T> \
  387. >::type {}; \
  388. \
  389. struct find_context_as_property : \
  390. conditional< \
  391. is_context_as<Head>::value, \
  392. typename supportable_properties<I, void(Head)>::found, \
  393. typename supportable_properties<I + 1, void( \
  394. ASIO_VARIADIC_TARGS(n))>::find_context_as_property \
  395. >::type {}; \
  396. }; \
  397. /**/
  398. ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ANY_EXECUTOR_PROPS_BASE_DEF)
  399. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROPS_BASE_DEF
  400. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  401. template <typename T, typename Props>
  402. struct is_valid_target_executor :
  403. conditional<
  404. is_executor<T>::value,
  405. typename supportable_properties<0, Props>::template is_valid_target<T>,
  406. false_type
  407. >::type
  408. {
  409. };
  410. template <typename Props>
  411. struct is_valid_target_executor<int, Props> : false_type
  412. {
  413. };
  414. class any_executor_base
  415. {
  416. public:
  417. any_executor_base() ASIO_NOEXCEPT
  418. : object_fns_(object_fns_table<void>()),
  419. target_(0),
  420. target_fns_(target_fns_table<void>())
  421. {
  422. }
  423. template <ASIO_EXECUTION_EXECUTOR Executor>
  424. any_executor_base(Executor ex, false_type)
  425. : target_fns_(target_fns_table<Executor>(
  426. any_executor_base::query_blocking(ex,
  427. can_query<const Executor&, const execution::blocking_t&>())
  428. == execution::blocking.always))
  429. {
  430. any_executor_base::construct_object(ex,
  431. integral_constant<bool,
  432. sizeof(Executor) <= sizeof(object_type)
  433. && alignment_of<Executor>::value <= alignment_of<object_type>::value
  434. >());
  435. }
  436. template <ASIO_EXECUTION_EXECUTOR Executor>
  437. any_executor_base(Executor other, true_type)
  438. : object_fns_(object_fns_table<asio::detail::shared_ptr<void> >()),
  439. target_fns_(other.target_fns_)
  440. {
  441. asio::detail::shared_ptr<Executor> p =
  442. asio::detail::make_shared<Executor>(
  443. ASIO_MOVE_CAST(Executor)(other));
  444. target_ = p->template target<void>();
  445. new (&object_) asio::detail::shared_ptr<void>(
  446. ASIO_MOVE_CAST(asio::detail::shared_ptr<Executor>)(p));
  447. }
  448. any_executor_base(const any_executor_base& other) ASIO_NOEXCEPT
  449. : object_fns_(other.object_fns_),
  450. target_fns_(other.target_fns_)
  451. {
  452. object_fns_->copy(*this, other);
  453. }
  454. ~any_executor_base() ASIO_NOEXCEPT
  455. {
  456. object_fns_->destroy(*this);
  457. }
  458. any_executor_base& operator=(
  459. const any_executor_base& other) ASIO_NOEXCEPT
  460. {
  461. if (this != &other)
  462. {
  463. object_fns_->destroy(*this);
  464. object_fns_ = other.object_fns_;
  465. target_fns_ = other.target_fns_;
  466. object_fns_->copy(*this, other);
  467. }
  468. return *this;
  469. }
  470. any_executor_base& operator=(nullptr_t) ASIO_NOEXCEPT
  471. {
  472. object_fns_->destroy(*this);
  473. target_ = 0;
  474. object_fns_ = object_fns_table<void>();
  475. target_fns_ = target_fns_table<void>();
  476. return *this;
  477. }
  478. #if defined(ASIO_HAS_MOVE)
  479. any_executor_base(any_executor_base&& other) ASIO_NOEXCEPT
  480. : object_fns_(other.object_fns_),
  481. target_fns_(other.target_fns_)
  482. {
  483. other.object_fns_ = object_fns_table<void>();
  484. other.target_fns_ = target_fns_table<void>();
  485. object_fns_->move(*this, other);
  486. other.target_ = 0;
  487. }
  488. any_executor_base& operator=(
  489. any_executor_base&& other) ASIO_NOEXCEPT
  490. {
  491. if (this != &other)
  492. {
  493. object_fns_->destroy(*this);
  494. object_fns_ = other.object_fns_;
  495. other.object_fns_ = object_fns_table<void>();
  496. target_fns_ = other.target_fns_;
  497. other.target_fns_ = target_fns_table<void>();
  498. object_fns_->move(*this, other);
  499. other.target_ = 0;
  500. }
  501. return *this;
  502. }
  503. #endif // defined(ASIO_HAS_MOVE)
  504. void swap(any_executor_base& other) ASIO_NOEXCEPT
  505. {
  506. if (this != &other)
  507. {
  508. any_executor_base tmp(ASIO_MOVE_CAST(any_executor_base)(other));
  509. other = ASIO_MOVE_CAST(any_executor_base)(*this);
  510. *this = ASIO_MOVE_CAST(any_executor_base)(tmp);
  511. }
  512. }
  513. template <typename F>
  514. void execute(ASIO_MOVE_ARG(F) f) const
  515. {
  516. if (target_fns_->blocking_execute != 0)
  517. {
  518. asio::detail::non_const_lvalue<F> f2(f);
  519. target_fns_->blocking_execute(*this, function_view(f2.value));
  520. }
  521. else
  522. {
  523. target_fns_->execute(*this,
  524. function(ASIO_MOVE_CAST(F)(f), std::allocator<void>()));
  525. }
  526. }
  527. template <typename Executor>
  528. Executor* target()
  529. {
  530. return static_cast<Executor*>(target_);
  531. }
  532. template <typename Executor>
  533. const Executor* target() const
  534. {
  535. return static_cast<Executor*>(target_);
  536. }
  537. #if !defined(ASIO_NO_TYPEID)
  538. const std::type_info& target_type() const
  539. #else // !defined(ASIO_NO_TYPEID)
  540. const void* target_type() const
  541. #endif // !defined(ASIO_NO_TYPEID)
  542. {
  543. return target_fns_->target_type();
  544. }
  545. struct unspecified_bool_type_t {};
  546. typedef void (*unspecified_bool_type)(unspecified_bool_type_t);
  547. static void unspecified_bool_true(unspecified_bool_type_t) {}
  548. operator unspecified_bool_type() const ASIO_NOEXCEPT
  549. {
  550. return target_ ? &any_executor_base::unspecified_bool_true : 0;
  551. }
  552. bool operator!() const ASIO_NOEXCEPT
  553. {
  554. return target_ == 0;
  555. }
  556. protected:
  557. bool equality_helper(const any_executor_base& other) const ASIO_NOEXCEPT
  558. {
  559. if (target_ == other.target_)
  560. return true;
  561. if (target_ && !other.target_)
  562. return false;
  563. if (!target_ && other.target_)
  564. return false;
  565. if (target_fns_ != other.target_fns_)
  566. return false;
  567. return target_fns_->equal(*this, other);
  568. }
  569. template <typename Ex>
  570. Ex& object()
  571. {
  572. return *static_cast<Ex*>(static_cast<void*>(&object_));
  573. }
  574. template <typename Ex>
  575. const Ex& object() const
  576. {
  577. return *static_cast<const Ex*>(static_cast<const void*>(&object_));
  578. }
  579. struct object_fns
  580. {
  581. void (*destroy)(any_executor_base&);
  582. void (*copy)(any_executor_base&, const any_executor_base&);
  583. void (*move)(any_executor_base&, any_executor_base&);
  584. const void* (*target)(const any_executor_base&);
  585. };
  586. static void destroy_void(any_executor_base&)
  587. {
  588. }
  589. static void copy_void(any_executor_base& ex1, const any_executor_base&)
  590. {
  591. ex1.target_ = 0;
  592. }
  593. static void move_void(any_executor_base& ex1, any_executor_base&)
  594. {
  595. ex1.target_ = 0;
  596. }
  597. static const void* target_void(const any_executor_base&)
  598. {
  599. return 0;
  600. }
  601. template <typename Obj>
  602. static const object_fns* object_fns_table(
  603. typename enable_if<
  604. is_same<Obj, void>::value
  605. >::type* = 0)
  606. {
  607. static const object_fns fns =
  608. {
  609. &any_executor_base::destroy_void,
  610. &any_executor_base::copy_void,
  611. &any_executor_base::move_void,
  612. &any_executor_base::target_void
  613. };
  614. return &fns;
  615. }
  616. static void destroy_shared(any_executor_base& ex)
  617. {
  618. typedef asio::detail::shared_ptr<void> type;
  619. ex.object<type>().~type();
  620. }
  621. static void copy_shared(any_executor_base& ex1, const any_executor_base& ex2)
  622. {
  623. typedef asio::detail::shared_ptr<void> type;
  624. new (&ex1.object_) type(ex2.object<type>());
  625. ex1.target_ = ex2.target_;
  626. }
  627. static void move_shared(any_executor_base& ex1, any_executor_base& ex2)
  628. {
  629. typedef asio::detail::shared_ptr<void> type;
  630. new (&ex1.object_) type(ASIO_MOVE_CAST(type)(ex2.object<type>()));
  631. ex1.target_ = ex2.target_;
  632. ex2.object<type>().~type();
  633. }
  634. static const void* target_shared(const any_executor_base& ex)
  635. {
  636. typedef asio::detail::shared_ptr<void> type;
  637. return ex.object<type>().get();
  638. }
  639. template <typename Obj>
  640. static const object_fns* object_fns_table(
  641. typename enable_if<
  642. is_same<Obj, asio::detail::shared_ptr<void> >::value
  643. >::type* = 0)
  644. {
  645. static const object_fns fns =
  646. {
  647. &any_executor_base::destroy_shared,
  648. &any_executor_base::copy_shared,
  649. &any_executor_base::move_shared,
  650. &any_executor_base::target_shared
  651. };
  652. return &fns;
  653. }
  654. template <typename Obj>
  655. static void destroy_object(any_executor_base& ex)
  656. {
  657. ex.object<Obj>().~Obj();
  658. }
  659. template <typename Obj>
  660. static void copy_object(any_executor_base& ex1, const any_executor_base& ex2)
  661. {
  662. new (&ex1.object_) Obj(ex2.object<Obj>());
  663. ex1.target_ = &ex1.object<Obj>();
  664. }
  665. template <typename Obj>
  666. static void move_object(any_executor_base& ex1, any_executor_base& ex2)
  667. {
  668. new (&ex1.object_) Obj(ASIO_MOVE_CAST(Obj)(ex2.object<Obj>()));
  669. ex1.target_ = &ex1.object<Obj>();
  670. ex2.object<Obj>().~Obj();
  671. }
  672. template <typename Obj>
  673. static const void* target_object(const any_executor_base& ex)
  674. {
  675. return &ex.object<Obj>();
  676. }
  677. template <typename Obj>
  678. static const object_fns* object_fns_table(
  679. typename enable_if<
  680. !is_same<Obj, void>::value
  681. && !is_same<Obj, asio::detail::shared_ptr<void> >::value
  682. >::type* = 0)
  683. {
  684. static const object_fns fns =
  685. {
  686. &any_executor_base::destroy_object<Obj>,
  687. &any_executor_base::copy_object<Obj>,
  688. &any_executor_base::move_object<Obj>,
  689. &any_executor_base::target_object<Obj>
  690. };
  691. return &fns;
  692. }
  693. typedef asio::detail::executor_function function;
  694. typedef asio::detail::executor_function_view function_view;
  695. struct target_fns
  696. {
  697. #if !defined(ASIO_NO_TYPEID)
  698. const std::type_info& (*target_type)();
  699. #else // !defined(ASIO_NO_TYPEID)
  700. const void* (*target_type)();
  701. #endif // !defined(ASIO_NO_TYPEID)
  702. bool (*equal)(const any_executor_base&, const any_executor_base&);
  703. void (*execute)(const any_executor_base&, ASIO_MOVE_ARG(function));
  704. void (*blocking_execute)(const any_executor_base&, function_view);
  705. };
  706. #if !defined(ASIO_NO_TYPEID)
  707. static const std::type_info& target_type_void()
  708. {
  709. return typeid(void);
  710. }
  711. #else // !defined(ASIO_NO_TYPEID)
  712. static const void* target_type_void()
  713. {
  714. return 0;
  715. }
  716. #endif // !defined(ASIO_NO_TYPEID)
  717. static bool equal_void(const any_executor_base&, const any_executor_base&)
  718. {
  719. return true;
  720. }
  721. static void execute_void(const any_executor_base&,
  722. ASIO_MOVE_ARG(function))
  723. {
  724. bad_executor ex;
  725. asio::detail::throw_exception(ex);
  726. }
  727. static void blocking_execute_void(const any_executor_base&, function_view)
  728. {
  729. bad_executor ex;
  730. asio::detail::throw_exception(ex);
  731. }
  732. template <typename Ex>
  733. static const target_fns* target_fns_table(
  734. typename enable_if<
  735. is_same<Ex, void>::value
  736. >::type* = 0)
  737. {
  738. static const target_fns fns =
  739. {
  740. &any_executor_base::target_type_void,
  741. &any_executor_base::equal_void,
  742. &any_executor_base::execute_void,
  743. &any_executor_base::blocking_execute_void
  744. };
  745. return &fns;
  746. }
  747. #if !defined(ASIO_NO_TYPEID)
  748. template <typename Ex>
  749. static const std::type_info& target_type_ex()
  750. {
  751. return typeid(Ex);
  752. }
  753. #else // !defined(ASIO_NO_TYPEID)
  754. template <typename Ex>
  755. static const void* target_type_ex()
  756. {
  757. static int unique_id;
  758. return &unique_id;
  759. }
  760. #endif // !defined(ASIO_NO_TYPEID)
  761. template <typename Ex>
  762. static bool equal_ex(const any_executor_base& ex1,
  763. const any_executor_base& ex2)
  764. {
  765. return *ex1.target<Ex>() == *ex2.target<Ex>();
  766. }
  767. template <typename Ex>
  768. static void execute_ex(const any_executor_base& ex,
  769. ASIO_MOVE_ARG(function) f)
  770. {
  771. execution::execute(*ex.target<Ex>(), ASIO_MOVE_CAST(function)(f));
  772. }
  773. template <typename Ex>
  774. static void blocking_execute_ex(const any_executor_base& ex, function_view f)
  775. {
  776. execution::execute(*ex.target<Ex>(), f);
  777. }
  778. template <typename Ex>
  779. static const target_fns* target_fns_table(bool is_always_blocking,
  780. typename enable_if<
  781. !is_same<Ex, void>::value
  782. >::type* = 0)
  783. {
  784. static const target_fns fns_with_execute =
  785. {
  786. &any_executor_base::target_type_ex<Ex>,
  787. &any_executor_base::equal_ex<Ex>,
  788. &any_executor_base::execute_ex<Ex>,
  789. 0
  790. };
  791. static const target_fns fns_with_blocking_execute =
  792. {
  793. &any_executor_base::target_type_ex<Ex>,
  794. &any_executor_base::equal_ex<Ex>,
  795. 0,
  796. &any_executor_base::blocking_execute_ex<Ex>
  797. };
  798. return is_always_blocking ? &fns_with_blocking_execute : &fns_with_execute;
  799. }
  800. #if defined(ASIO_MSVC)
  801. # pragma warning (push)
  802. # pragma warning (disable:4702)
  803. #endif // defined(ASIO_MSVC)
  804. static void query_fn_void(void*, const void*, const void*)
  805. {
  806. bad_executor ex;
  807. asio::detail::throw_exception(ex);
  808. }
  809. template <typename Ex, class Prop>
  810. static void query_fn_non_void(void*, const void* ex, const void* prop,
  811. typename enable_if<
  812. asio::can_query<const Ex&, const Prop&>::value
  813. && is_same<typename Prop::polymorphic_query_result_type, void>::value
  814. >::type*)
  815. {
  816. asio::query(*static_cast<const Ex*>(ex),
  817. *static_cast<const Prop*>(prop));
  818. }
  819. template <typename Ex, class Prop>
  820. static void query_fn_non_void(void*, const void*, const void*,
  821. typename enable_if<
  822. !asio::can_query<const Ex&, const Prop&>::value
  823. && is_same<typename Prop::polymorphic_query_result_type, void>::value
  824. >::type*)
  825. {
  826. }
  827. template <typename Ex, class Prop>
  828. static void query_fn_non_void(void* result, const void* ex, const void* prop,
  829. typename enable_if<
  830. asio::can_query<const Ex&, const Prop&>::value
  831. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  832. && is_reference<typename Prop::polymorphic_query_result_type>::value
  833. >::type*)
  834. {
  835. *static_cast<typename remove_reference<
  836. typename Prop::polymorphic_query_result_type>::type**>(result)
  837. = &static_cast<typename Prop::polymorphic_query_result_type>(
  838. asio::query(*static_cast<const Ex*>(ex),
  839. *static_cast<const Prop*>(prop)));
  840. }
  841. template <typename Ex, class Prop>
  842. static void query_fn_non_void(void*, const void*, const void*,
  843. typename enable_if<
  844. !asio::can_query<const Ex&, const Prop&>::value
  845. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  846. && is_reference<typename Prop::polymorphic_query_result_type>::value
  847. >::type*)
  848. {
  849. std::terminate(); // Combination should not be possible.
  850. }
  851. template <typename Ex, class Prop>
  852. static void query_fn_non_void(void* result, const void* ex, const void* prop,
  853. typename enable_if<
  854. asio::can_query<const Ex&, const Prop&>::value
  855. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  856. && is_scalar<typename Prop::polymorphic_query_result_type>::value
  857. >::type*)
  858. {
  859. *static_cast<typename Prop::polymorphic_query_result_type*>(result)
  860. = static_cast<typename Prop::polymorphic_query_result_type>(
  861. asio::query(*static_cast<const Ex*>(ex),
  862. *static_cast<const Prop*>(prop)));
  863. }
  864. template <typename Ex, class Prop>
  865. static void query_fn_non_void(void* result, const void*, const void*,
  866. typename enable_if<
  867. !asio::can_query<const Ex&, const Prop&>::value
  868. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  869. && is_scalar<typename Prop::polymorphic_query_result_type>::value
  870. >::type*)
  871. {
  872. *static_cast<typename Prop::polymorphic_query_result_type*>(result)
  873. = typename Prop::polymorphic_query_result_type();
  874. }
  875. template <typename Ex, class Prop>
  876. static void query_fn_non_void(void* result, const void* ex, const void* prop,
  877. typename enable_if<
  878. asio::can_query<const Ex&, const Prop&>::value
  879. && !is_same<typename Prop::polymorphic_query_result_type, void>::value
  880. && !is_reference<typename Prop::polymorphic_query_result_type>::value
  881. && !is_scalar<typename Prop::polymorphic_query_result_type>::value
  882. >::type*)
  883. {
  884. *static_cast<typename Prop::polymorphic_query_result_type**>(result)
  885. = new typename Prop::polymorphic_query_result_type(
  886. asio::query(*static_cast<const Ex*>(ex),
  887. *static_cast<const Prop*>(prop)));
  888. }
  889. template <typename Ex, class Prop>
  890. static void query_fn_non_void(void* result, const void*, const void*, ...)
  891. {
  892. *static_cast<typename Prop::polymorphic_query_result_type**>(result)
  893. = new typename Prop::polymorphic_query_result_type();
  894. }
  895. template <typename Ex, class Prop>
  896. static void query_fn_impl(void* result, const void* ex, const void* prop,
  897. typename enable_if<
  898. is_same<Ex, void>::value
  899. >::type*)
  900. {
  901. query_fn_void(result, ex, prop);
  902. }
  903. template <typename Ex, class Prop>
  904. static void query_fn_impl(void* result, const void* ex, const void* prop,
  905. typename enable_if<
  906. !is_same<Ex, void>::value
  907. >::type*)
  908. {
  909. query_fn_non_void<Ex, Prop>(result, ex, prop, 0);
  910. }
  911. template <typename Ex, class Prop>
  912. static void query_fn(void* result, const void* ex, const void* prop)
  913. {
  914. query_fn_impl<Ex, Prop>(result, ex, prop, 0);
  915. }
  916. template <typename Poly, typename Ex, class Prop>
  917. static Poly require_fn_impl(const void*, const void*,
  918. typename enable_if<
  919. is_same<Ex, void>::value
  920. >::type*)
  921. {
  922. bad_executor ex;
  923. asio::detail::throw_exception(ex);
  924. return Poly();
  925. }
  926. template <typename Poly, typename Ex, class Prop>
  927. static Poly require_fn_impl(const void* ex, const void* prop,
  928. typename enable_if<
  929. !is_same<Ex, void>::value && Prop::is_requirable
  930. >::type*)
  931. {
  932. return asio::require(*static_cast<const Ex*>(ex),
  933. *static_cast<const Prop*>(prop));
  934. }
  935. template <typename Poly, typename Ex, class Prop>
  936. static Poly require_fn_impl(const void*, const void*, ...)
  937. {
  938. return Poly();
  939. }
  940. template <typename Poly, typename Ex, class Prop>
  941. static Poly require_fn(const void* ex, const void* prop)
  942. {
  943. return require_fn_impl<Poly, Ex, Prop>(ex, prop, 0);
  944. }
  945. template <typename Poly, typename Ex, class Prop>
  946. static Poly prefer_fn_impl(const void*, const void*,
  947. typename enable_if<
  948. is_same<Ex, void>::value
  949. >::type*)
  950. {
  951. bad_executor ex;
  952. asio::detail::throw_exception(ex);
  953. return Poly();
  954. }
  955. template <typename Poly, typename Ex, class Prop>
  956. static Poly prefer_fn_impl(const void* ex, const void* prop,
  957. typename enable_if<
  958. !is_same<Ex, void>::value && Prop::is_preferable
  959. >::type*)
  960. {
  961. return asio::prefer(*static_cast<const Ex*>(ex),
  962. *static_cast<const Prop*>(prop));
  963. }
  964. template <typename Poly, typename Ex, class Prop>
  965. static Poly prefer_fn_impl(const void*, const void*, ...)
  966. {
  967. return Poly();
  968. }
  969. template <typename Poly, typename Ex, class Prop>
  970. static Poly prefer_fn(const void* ex, const void* prop)
  971. {
  972. return prefer_fn_impl<Poly, Ex, Prop>(ex, prop, 0);
  973. }
  974. template <typename Poly>
  975. struct prop_fns
  976. {
  977. void (*query)(void*, const void*, const void*);
  978. Poly (*require)(const void*, const void*);
  979. Poly (*prefer)(const void*, const void*);
  980. };
  981. #if defined(ASIO_MSVC)
  982. # pragma warning (pop)
  983. #endif // defined(ASIO_MSVC)
  984. private:
  985. template <typename Executor>
  986. static execution::blocking_t query_blocking(const Executor& ex, true_type)
  987. {
  988. return asio::query(ex, execution::blocking);
  989. }
  990. template <typename Executor>
  991. static execution::blocking_t query_blocking(const Executor&, false_type)
  992. {
  993. return execution::blocking_t();
  994. }
  995. template <typename Executor>
  996. void construct_object(Executor& ex, true_type)
  997. {
  998. object_fns_ = object_fns_table<Executor>();
  999. target_ = new (&object_) Executor(ASIO_MOVE_CAST(Executor)(ex));
  1000. }
  1001. template <typename Executor>
  1002. void construct_object(Executor& ex, false_type)
  1003. {
  1004. object_fns_ = object_fns_table<asio::detail::shared_ptr<void> >();
  1005. asio::detail::shared_ptr<Executor> p =
  1006. asio::detail::make_shared<Executor>(
  1007. ASIO_MOVE_CAST(Executor)(ex));
  1008. target_ = p.get();
  1009. new (&object_) asio::detail::shared_ptr<void>(
  1010. ASIO_MOVE_CAST(asio::detail::shared_ptr<Executor>)(p));
  1011. }
  1012. /*private:*/public:
  1013. // template <typename...> friend class any_executor;
  1014. typedef aligned_storage<
  1015. sizeof(asio::detail::shared_ptr<void>) + sizeof(void*),
  1016. alignment_of<asio::detail::shared_ptr<void> >::value
  1017. >::type object_type;
  1018. object_type object_;
  1019. const object_fns* object_fns_;
  1020. void* target_;
  1021. const target_fns* target_fns_;
  1022. };
  1023. template <typename Derived, typename Property, typename = void>
  1024. struct any_executor_context
  1025. {
  1026. };
  1027. #if !defined(ASIO_NO_TS_EXECUTORS)
  1028. template <typename Derived, typename Property>
  1029. struct any_executor_context<Derived, Property,
  1030. typename enable_if<Property::value>::type>
  1031. {
  1032. typename Property::query_result_type context() const
  1033. {
  1034. return static_cast<const Derived*>(this)->query(typename Property::type());
  1035. }
  1036. };
  1037. #endif // !defined(ASIO_NO_TS_EXECUTORS)
  1038. } // namespace detail
  1039. template <>
  1040. class any_executor<> : public detail::any_executor_base
  1041. {
  1042. public:
  1043. any_executor() ASIO_NOEXCEPT
  1044. : detail::any_executor_base()
  1045. {
  1046. }
  1047. any_executor(nullptr_t) ASIO_NOEXCEPT
  1048. : detail::any_executor_base()
  1049. {
  1050. }
  1051. template <typename Executor>
  1052. any_executor(Executor ex,
  1053. typename enable_if<
  1054. conditional<
  1055. !is_same<Executor, any_executor>::value
  1056. && !is_base_of<detail::any_executor_base, Executor>::value,
  1057. is_executor<Executor>,
  1058. false_type
  1059. >::type::value
  1060. >::type* = 0)
  1061. : detail::any_executor_base(
  1062. ASIO_MOVE_CAST(Executor)(ex), false_type())
  1063. {
  1064. }
  1065. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1066. template <typename... OtherSupportableProperties>
  1067. any_executor(any_executor<OtherSupportableProperties...> other)
  1068. : detail::any_executor_base(
  1069. static_cast<const detail::any_executor_base&>(other))
  1070. {
  1071. }
  1072. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1073. template <typename U0, typename U1, typename U2, typename U3,
  1074. typename U4, typename U5, typename U6, typename U7>
  1075. any_executor(any_executor<U0, U1, U2, U3, U4, U5, U6, U7> other)
  1076. : detail::any_executor_base(
  1077. static_cast<const detail::any_executor_base&>(other))
  1078. {
  1079. }
  1080. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1081. any_executor(const any_executor& other) ASIO_NOEXCEPT
  1082. : detail::any_executor_base(
  1083. static_cast<const detail::any_executor_base&>(other))
  1084. {
  1085. }
  1086. any_executor& operator=(const any_executor& other) ASIO_NOEXCEPT
  1087. {
  1088. if (this != &other)
  1089. {
  1090. detail::any_executor_base::operator=(
  1091. static_cast<const detail::any_executor_base&>(other));
  1092. }
  1093. return *this;
  1094. }
  1095. any_executor& operator=(nullptr_t p) ASIO_NOEXCEPT
  1096. {
  1097. detail::any_executor_base::operator=(p);
  1098. return *this;
  1099. }
  1100. #if defined(ASIO_HAS_MOVE)
  1101. any_executor(any_executor&& other) ASIO_NOEXCEPT
  1102. : detail::any_executor_base(
  1103. static_cast<any_executor_base&&>(
  1104. static_cast<any_executor_base&>(other)))
  1105. {
  1106. }
  1107. any_executor& operator=(any_executor&& other) ASIO_NOEXCEPT
  1108. {
  1109. if (this != &other)
  1110. {
  1111. detail::any_executor_base::operator=(
  1112. static_cast<detail::any_executor_base&&>(
  1113. static_cast<detail::any_executor_base&>(other)));
  1114. }
  1115. return *this;
  1116. }
  1117. #endif // defined(ASIO_HAS_MOVE)
  1118. void swap(any_executor& other) ASIO_NOEXCEPT
  1119. {
  1120. detail::any_executor_base::swap(
  1121. static_cast<detail::any_executor_base&>(other));
  1122. }
  1123. using detail::any_executor_base::execute;
  1124. using detail::any_executor_base::target;
  1125. using detail::any_executor_base::target_type;
  1126. using detail::any_executor_base::operator unspecified_bool_type;
  1127. using detail::any_executor_base::operator!;
  1128. bool equality_helper(const any_executor& other) const ASIO_NOEXCEPT
  1129. {
  1130. return any_executor_base::equality_helper(other);
  1131. }
  1132. template <typename AnyExecutor1, typename AnyExecutor2>
  1133. friend typename enable_if<
  1134. is_base_of<any_executor, AnyExecutor1>::value
  1135. || is_base_of<any_executor, AnyExecutor2>::value,
  1136. bool
  1137. >::type operator==(const AnyExecutor1& a,
  1138. const AnyExecutor2& b) ASIO_NOEXCEPT
  1139. {
  1140. return static_cast<const any_executor&>(a).equality_helper(b);
  1141. }
  1142. template <typename AnyExecutor>
  1143. friend typename enable_if<
  1144. is_same<AnyExecutor, any_executor>::value,
  1145. bool
  1146. >::type operator==(const AnyExecutor& a, nullptr_t) ASIO_NOEXCEPT
  1147. {
  1148. return !a;
  1149. }
  1150. template <typename AnyExecutor>
  1151. friend typename enable_if<
  1152. is_same<AnyExecutor, any_executor>::value,
  1153. bool
  1154. >::type operator==(nullptr_t, const AnyExecutor& b) ASIO_NOEXCEPT
  1155. {
  1156. return !b;
  1157. }
  1158. template <typename AnyExecutor1, typename AnyExecutor2>
  1159. friend typename enable_if<
  1160. is_base_of<any_executor, AnyExecutor1>::value
  1161. || is_base_of<any_executor, AnyExecutor2>::value,
  1162. bool
  1163. >::type operator!=(const AnyExecutor1& a,
  1164. const AnyExecutor2& b) ASIO_NOEXCEPT
  1165. {
  1166. return !static_cast<const any_executor&>(a).equality_helper(b);
  1167. }
  1168. template <typename AnyExecutor>
  1169. friend typename enable_if<
  1170. is_same<AnyExecutor, any_executor>::value,
  1171. bool
  1172. >::type operator!=(const AnyExecutor& a, nullptr_t) ASIO_NOEXCEPT
  1173. {
  1174. return !!a;
  1175. }
  1176. template <typename AnyExecutor>
  1177. friend typename enable_if<
  1178. is_same<AnyExecutor, any_executor>::value,
  1179. bool
  1180. >::type operator!=(nullptr_t, const AnyExecutor& b) ASIO_NOEXCEPT
  1181. {
  1182. return !!b;
  1183. }
  1184. };
  1185. inline void swap(any_executor<>& a, any_executor<>& b) ASIO_NOEXCEPT
  1186. {
  1187. return a.swap(b);
  1188. }
  1189. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1190. template <typename... SupportableProperties>
  1191. class any_executor :
  1192. public detail::any_executor_base,
  1193. public detail::any_executor_context<
  1194. any_executor<SupportableProperties...>,
  1195. typename detail::supportable_properties<
  1196. 0, void(SupportableProperties...)>::find_context_as_property>
  1197. {
  1198. public:
  1199. any_executor() ASIO_NOEXCEPT
  1200. : detail::any_executor_base(),
  1201. prop_fns_(prop_fns_table<void>())
  1202. {
  1203. }
  1204. any_executor(nullptr_t) ASIO_NOEXCEPT
  1205. : detail::any_executor_base(),
  1206. prop_fns_(prop_fns_table<void>())
  1207. {
  1208. }
  1209. template <typename Executor>
  1210. any_executor(Executor ex,
  1211. typename enable_if<
  1212. conditional<
  1213. !is_same<Executor, any_executor>::value
  1214. && !is_base_of<detail::any_executor_base, Executor>::value,
  1215. detail::is_valid_target_executor<
  1216. Executor, void(SupportableProperties...)>,
  1217. false_type
  1218. >::type::value
  1219. >::type* = 0)
  1220. : detail::any_executor_base(
  1221. ASIO_MOVE_CAST(Executor)(ex), false_type()),
  1222. prop_fns_(prop_fns_table<Executor>())
  1223. {
  1224. }
  1225. template <typename... OtherSupportableProperties>
  1226. any_executor(any_executor<OtherSupportableProperties...> other,
  1227. typename enable_if<
  1228. conditional<
  1229. !is_same<
  1230. any_executor<OtherSupportableProperties...>,
  1231. any_executor
  1232. >::value,
  1233. typename detail::supportable_properties<
  1234. 0, void(SupportableProperties...)>::template is_valid_target<
  1235. any_executor<OtherSupportableProperties...> >,
  1236. false_type
  1237. >::type::value
  1238. >::type* = 0)
  1239. : detail::any_executor_base(ASIO_MOVE_CAST(
  1240. any_executor<OtherSupportableProperties...>)(other), true_type()),
  1241. prop_fns_(prop_fns_table<any_executor<OtherSupportableProperties...> >())
  1242. {
  1243. }
  1244. any_executor(const any_executor& other) ASIO_NOEXCEPT
  1245. : detail::any_executor_base(
  1246. static_cast<const detail::any_executor_base&>(other)),
  1247. prop_fns_(other.prop_fns_)
  1248. {
  1249. }
  1250. any_executor& operator=(const any_executor& other) ASIO_NOEXCEPT
  1251. {
  1252. if (this != &other)
  1253. {
  1254. prop_fns_ = other.prop_fns_;
  1255. detail::any_executor_base::operator=(
  1256. static_cast<const detail::any_executor_base&>(other));
  1257. }
  1258. return *this;
  1259. }
  1260. any_executor& operator=(nullptr_t p) ASIO_NOEXCEPT
  1261. {
  1262. prop_fns_ = prop_fns_table<void>();
  1263. detail::any_executor_base::operator=(p);
  1264. return *this;
  1265. }
  1266. #if defined(ASIO_HAS_MOVE)
  1267. any_executor(any_executor&& other) ASIO_NOEXCEPT
  1268. : detail::any_executor_base(
  1269. static_cast<any_executor_base&&>(
  1270. static_cast<any_executor_base&>(other))),
  1271. prop_fns_(other.prop_fns_)
  1272. {
  1273. other.prop_fns_ = prop_fns_table<void>();
  1274. }
  1275. any_executor& operator=(any_executor&& other) ASIO_NOEXCEPT
  1276. {
  1277. if (this != &other)
  1278. {
  1279. prop_fns_ = other.prop_fns_;
  1280. detail::any_executor_base::operator=(
  1281. static_cast<detail::any_executor_base&&>(
  1282. static_cast<detail::any_executor_base&>(other)));
  1283. }
  1284. return *this;
  1285. }
  1286. #endif // defined(ASIO_HAS_MOVE)
  1287. void swap(any_executor& other) ASIO_NOEXCEPT
  1288. {
  1289. if (this != &other)
  1290. {
  1291. detail::any_executor_base::swap(
  1292. static_cast<detail::any_executor_base&>(other));
  1293. const prop_fns<any_executor>* tmp_prop_fns = other.prop_fns_;
  1294. other.prop_fns_ = prop_fns_;
  1295. prop_fns_ = tmp_prop_fns;
  1296. }
  1297. }
  1298. using detail::any_executor_base::execute;
  1299. using detail::any_executor_base::target;
  1300. using detail::any_executor_base::target_type;
  1301. using detail::any_executor_base::operator unspecified_bool_type;
  1302. using detail::any_executor_base::operator!;
  1303. bool equality_helper(const any_executor& other) const ASIO_NOEXCEPT
  1304. {
  1305. return any_executor_base::equality_helper(other);
  1306. }
  1307. template <typename AnyExecutor1, typename AnyExecutor2>
  1308. friend typename enable_if<
  1309. is_base_of<any_executor, AnyExecutor1>::value
  1310. || is_base_of<any_executor, AnyExecutor2>::value,
  1311. bool
  1312. >::type operator==(const AnyExecutor1& a,
  1313. const AnyExecutor2& b) ASIO_NOEXCEPT
  1314. {
  1315. return static_cast<const any_executor&>(a).equality_helper(b);
  1316. }
  1317. template <typename AnyExecutor>
  1318. friend typename enable_if<
  1319. is_same<AnyExecutor, any_executor>::value,
  1320. bool
  1321. >::type operator==(const AnyExecutor& a, nullptr_t) ASIO_NOEXCEPT
  1322. {
  1323. return !a;
  1324. }
  1325. template <typename AnyExecutor>
  1326. friend typename enable_if<
  1327. is_same<AnyExecutor, any_executor>::value,
  1328. bool
  1329. >::type operator==(nullptr_t, const AnyExecutor& b) ASIO_NOEXCEPT
  1330. {
  1331. return !b;
  1332. }
  1333. template <typename AnyExecutor1, typename AnyExecutor2>
  1334. friend typename enable_if<
  1335. is_base_of<any_executor, AnyExecutor1>::value
  1336. || is_base_of<any_executor, AnyExecutor2>::value,
  1337. bool
  1338. >::type operator!=(const AnyExecutor1& a,
  1339. const AnyExecutor2& b) ASIO_NOEXCEPT
  1340. {
  1341. return !static_cast<const any_executor&>(a).equality_helper(b);
  1342. }
  1343. template <typename AnyExecutor>
  1344. friend typename enable_if<
  1345. is_same<AnyExecutor, any_executor>::value,
  1346. bool
  1347. >::type operator!=(const AnyExecutor& a, nullptr_t) ASIO_NOEXCEPT
  1348. {
  1349. return !!a;
  1350. }
  1351. template <typename AnyExecutor>
  1352. friend typename enable_if<
  1353. is_same<AnyExecutor, any_executor>::value,
  1354. bool
  1355. >::type operator!=(nullptr_t, const AnyExecutor& b) ASIO_NOEXCEPT
  1356. {
  1357. return !!b;
  1358. }
  1359. template <typename T>
  1360. struct find_convertible_property :
  1361. detail::supportable_properties<
  1362. 0, void(SupportableProperties...)>::template
  1363. find_convertible_property<T> {};
  1364. template <typename Property>
  1365. void query(const Property& p,
  1366. typename enable_if<
  1367. is_same<
  1368. typename find_convertible_property<Property>::query_result_type,
  1369. void
  1370. >::value
  1371. >::type* = 0) const
  1372. {
  1373. typedef find_convertible_property<Property> found;
  1374. prop_fns_[found::index].query(0, object_fns_->target(*this),
  1375. &static_cast<const typename found::type&>(p));
  1376. }
  1377. template <typename Property>
  1378. typename find_convertible_property<Property>::query_result_type
  1379. query(const Property& p,
  1380. typename enable_if<
  1381. !is_same<
  1382. typename find_convertible_property<Property>::query_result_type,
  1383. void
  1384. >::value
  1385. &&
  1386. is_reference<
  1387. typename find_convertible_property<Property>::query_result_type
  1388. >::value
  1389. >::type* = 0) const
  1390. {
  1391. typedef find_convertible_property<Property> found;
  1392. typename remove_reference<
  1393. typename found::query_result_type>::type* result = 0;
  1394. prop_fns_[found::index].query(&result, object_fns_->target(*this),
  1395. &static_cast<const typename found::type&>(p));
  1396. return *result;
  1397. }
  1398. template <typename Property>
  1399. typename find_convertible_property<Property>::query_result_type
  1400. query(const Property& p,
  1401. typename enable_if<
  1402. !is_same<
  1403. typename find_convertible_property<Property>::query_result_type,
  1404. void
  1405. >::value
  1406. &&
  1407. is_scalar<
  1408. typename find_convertible_property<Property>::query_result_type
  1409. >::value
  1410. >::type* = 0) const
  1411. {
  1412. typedef find_convertible_property<Property> found;
  1413. typename found::query_result_type result;
  1414. prop_fns_[found::index].query(&result, object_fns_->target(*this),
  1415. &static_cast<const typename found::type&>(p));
  1416. return result;
  1417. }
  1418. template <typename Property>
  1419. typename find_convertible_property<Property>::query_result_type
  1420. query(const Property& p,
  1421. typename enable_if<
  1422. !is_same<
  1423. typename find_convertible_property<Property>::query_result_type,
  1424. void
  1425. >::value
  1426. &&
  1427. !is_reference<
  1428. typename find_convertible_property<Property>::query_result_type
  1429. >::value
  1430. &&
  1431. !is_scalar<
  1432. typename find_convertible_property<Property>::query_result_type
  1433. >::value
  1434. >::type* = 0) const
  1435. {
  1436. typedef find_convertible_property<Property> found;
  1437. typename found::query_result_type* result;
  1438. prop_fns_[found::index].query(&result, object_fns_->target(*this),
  1439. &static_cast<const typename found::type&>(p));
  1440. return *asio::detail::scoped_ptr<
  1441. typename found::query_result_type>(result);
  1442. }
  1443. template <typename T>
  1444. struct find_convertible_requirable_property :
  1445. detail::supportable_properties<
  1446. 0, void(SupportableProperties...)>::template
  1447. find_convertible_requirable_property<T> {};
  1448. template <typename Property>
  1449. any_executor require(const Property& p,
  1450. typename enable_if<
  1451. find_convertible_requirable_property<Property>::value
  1452. >::type* = 0) const
  1453. {
  1454. typedef find_convertible_requirable_property<Property> found;
  1455. return prop_fns_[found::index].require(object_fns_->target(*this),
  1456. &static_cast<const typename found::type&>(p));
  1457. }
  1458. template <typename T>
  1459. struct find_convertible_preferable_property :
  1460. detail::supportable_properties<
  1461. 0, void(SupportableProperties...)>::template
  1462. find_convertible_preferable_property<T> {};
  1463. template <typename Property>
  1464. any_executor prefer(const Property& p,
  1465. typename enable_if<
  1466. find_convertible_preferable_property<Property>::value
  1467. >::type* = 0) const
  1468. {
  1469. typedef find_convertible_preferable_property<Property> found;
  1470. return prop_fns_[found::index].prefer(object_fns_->target(*this),
  1471. &static_cast<const typename found::type&>(p));
  1472. }
  1473. //private:
  1474. template <typename Ex>
  1475. static const prop_fns<any_executor>* prop_fns_table()
  1476. {
  1477. static const prop_fns<any_executor> fns[] =
  1478. {
  1479. {
  1480. &detail::any_executor_base::query_fn<
  1481. Ex, SupportableProperties>,
  1482. &detail::any_executor_base::require_fn<
  1483. any_executor, Ex, SupportableProperties>,
  1484. &detail::any_executor_base::prefer_fn<
  1485. any_executor, Ex, SupportableProperties>
  1486. }...
  1487. };
  1488. return fns;
  1489. }
  1490. const prop_fns<any_executor>* prop_fns_;
  1491. };
  1492. template <typename... SupportableProperties>
  1493. inline void swap(any_executor<SupportableProperties...>& a,
  1494. any_executor<SupportableProperties...>& b) ASIO_NOEXCEPT
  1495. {
  1496. return a.swap(b);
  1497. }
  1498. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1499. #define ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS(n) \
  1500. ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_##n
  1501. #define ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_1 \
  1502. { \
  1503. &detail::any_executor_base::query_fn<Ex, T1>, \
  1504. &detail::any_executor_base::require_fn<any_executor, Ex, T1>, \
  1505. &detail::any_executor_base::prefer_fn<any_executor, Ex, T1> \
  1506. }
  1507. #define ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_2 \
  1508. ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_1, \
  1509. { \
  1510. &detail::any_executor_base::query_fn<Ex, T2>, \
  1511. &detail::any_executor_base::require_fn<any_executor, Ex, T2>, \
  1512. &detail::any_executor_base::prefer_fn<any_executor, Ex, T2> \
  1513. }
  1514. #define ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_3 \
  1515. ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_2, \
  1516. { \
  1517. &detail::any_executor_base::query_fn<Ex, T3>, \
  1518. &detail::any_executor_base::require_fn<any_executor, Ex, T3>, \
  1519. &detail::any_executor_base::prefer_fn<any_executor, Ex, T3> \
  1520. }
  1521. #define ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_4 \
  1522. ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_3, \
  1523. { \
  1524. &detail::any_executor_base::query_fn<Ex, T4>, \
  1525. &detail::any_executor_base::require_fn<any_executor, Ex, T4>, \
  1526. &detail::any_executor_base::prefer_fn<any_executor, Ex, T4> \
  1527. }
  1528. #define ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_5 \
  1529. ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_4, \
  1530. { \
  1531. &detail::any_executor_base::query_fn<Ex, T5>, \
  1532. &detail::any_executor_base::require_fn<any_executor, Ex, T5>, \
  1533. &detail::any_executor_base::prefer_fn<any_executor, Ex, T5> \
  1534. }
  1535. #define ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_6 \
  1536. ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_5, \
  1537. { \
  1538. &detail::any_executor_base::query_fn<Ex, T6>, \
  1539. &detail::any_executor_base::require_fn<any_executor, Ex, T6>, \
  1540. &detail::any_executor_base::prefer_fn<any_executor, Ex, T6> \
  1541. }
  1542. #define ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_7 \
  1543. ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_6, \
  1544. { \
  1545. &detail::any_executor_base::query_fn<Ex, T7>, \
  1546. &detail::any_executor_base::require_fn<any_executor, Ex, T7>, \
  1547. &detail::any_executor_base::prefer_fn<any_executor, Ex, T7> \
  1548. }
  1549. #define ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_8 \
  1550. ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_7, \
  1551. { \
  1552. &detail::any_executor_base::query_fn<Ex, T8>, \
  1553. &detail::any_executor_base::require_fn<any_executor, Ex, T8>, \
  1554. &detail::any_executor_base::prefer_fn<any_executor, Ex, T8> \
  1555. }
  1556. #if defined(ASIO_HAS_MOVE)
  1557. # define ASIO_PRIVATE_ANY_EXECUTOR_MOVE_OPS \
  1558. any_executor(any_executor&& other) ASIO_NOEXCEPT \
  1559. : detail::any_executor_base( \
  1560. static_cast<any_executor_base&&>( \
  1561. static_cast<any_executor_base&>(other))), \
  1562. prop_fns_(other.prop_fns_) \
  1563. { \
  1564. other.prop_fns_ = prop_fns_table<void>(); \
  1565. } \
  1566. \
  1567. any_executor& operator=(any_executor&& other) ASIO_NOEXCEPT \
  1568. { \
  1569. if (this != &other) \
  1570. { \
  1571. prop_fns_ = other.prop_fns_; \
  1572. detail::any_executor_base::operator=( \
  1573. static_cast<detail::any_executor_base&&>( \
  1574. static_cast<detail::any_executor_base&>(other))); \
  1575. } \
  1576. return *this; \
  1577. } \
  1578. /**/
  1579. #else // defined(ASIO_HAS_MOVE)
  1580. # define ASIO_PRIVATE_ANY_EXECUTOR_MOVE_OPS
  1581. #endif // defined(ASIO_HAS_MOVE)
  1582. #define ASIO_PRIVATE_ANY_EXECUTOR_DEF(n) \
  1583. template <ASIO_VARIADIC_TPARAMS(n)> \
  1584. class any_executor<ASIO_VARIADIC_TARGS(n)> : \
  1585. public detail::any_executor_base, \
  1586. public detail::any_executor_context< \
  1587. any_executor<ASIO_VARIADIC_TARGS(n)>, \
  1588. typename detail::supportable_properties< \
  1589. 0, void(ASIO_VARIADIC_TARGS(n))>::find_context_as_property> \
  1590. { \
  1591. public: \
  1592. any_executor() ASIO_NOEXCEPT \
  1593. : detail::any_executor_base(), \
  1594. prop_fns_(prop_fns_table<void>()) \
  1595. { \
  1596. } \
  1597. \
  1598. any_executor(nullptr_t) ASIO_NOEXCEPT \
  1599. : detail::any_executor_base(), \
  1600. prop_fns_(prop_fns_table<void>()) \
  1601. { \
  1602. } \
  1603. \
  1604. template <ASIO_EXECUTION_EXECUTOR Executor> \
  1605. any_executor(Executor ex, \
  1606. typename enable_if< \
  1607. conditional< \
  1608. !is_same<Executor, any_executor>::value \
  1609. && !is_base_of<detail::any_executor_base, Executor>::value, \
  1610. detail::is_valid_target_executor< \
  1611. Executor, void(ASIO_VARIADIC_TARGS(n))>, \
  1612. false_type \
  1613. >::type::value \
  1614. >::type* = 0) \
  1615. : detail::any_executor_base(ASIO_MOVE_CAST( \
  1616. Executor)(ex), false_type()), \
  1617. prop_fns_(prop_fns_table<Executor>()) \
  1618. { \
  1619. } \
  1620. \
  1621. any_executor(const any_executor& other) ASIO_NOEXCEPT \
  1622. : detail::any_executor_base( \
  1623. static_cast<const detail::any_executor_base&>(other)), \
  1624. prop_fns_(other.prop_fns_) \
  1625. { \
  1626. } \
  1627. \
  1628. any_executor(any_executor<> other) \
  1629. : detail::any_executor_base(ASIO_MOVE_CAST( \
  1630. any_executor<>)(other), true_type()), \
  1631. prop_fns_(prop_fns_table<any_executor<> >()) \
  1632. { \
  1633. } \
  1634. \
  1635. template <typename OtherAnyExecutor> \
  1636. any_executor(OtherAnyExecutor other, \
  1637. typename enable_if< \
  1638. conditional< \
  1639. !is_same<OtherAnyExecutor, any_executor>::value \
  1640. && is_base_of<detail::any_executor_base, \
  1641. OtherAnyExecutor>::value, \
  1642. typename detail::supportable_properties< \
  1643. 0, void(ASIO_VARIADIC_TARGS(n))>::template \
  1644. is_valid_target<OtherAnyExecutor>, \
  1645. false_type \
  1646. >::type::value \
  1647. >::type* = 0) \
  1648. : detail::any_executor_base(ASIO_MOVE_CAST( \
  1649. OtherAnyExecutor)(other), true_type()), \
  1650. prop_fns_(prop_fns_table<OtherAnyExecutor>()) \
  1651. { \
  1652. } \
  1653. \
  1654. any_executor& operator=(const any_executor& other) ASIO_NOEXCEPT \
  1655. { \
  1656. if (this != &other) \
  1657. { \
  1658. prop_fns_ = other.prop_fns_; \
  1659. detail::any_executor_base::operator=( \
  1660. static_cast<const detail::any_executor_base&>(other)); \
  1661. } \
  1662. return *this; \
  1663. } \
  1664. \
  1665. any_executor& operator=(nullptr_t p) ASIO_NOEXCEPT \
  1666. { \
  1667. prop_fns_ = prop_fns_table<void>(); \
  1668. detail::any_executor_base::operator=(p); \
  1669. return *this; \
  1670. } \
  1671. \
  1672. ASIO_PRIVATE_ANY_EXECUTOR_MOVE_OPS \
  1673. \
  1674. void swap(any_executor& other) ASIO_NOEXCEPT \
  1675. { \
  1676. if (this != &other) \
  1677. { \
  1678. detail::any_executor_base::swap( \
  1679. static_cast<detail::any_executor_base&>(other)); \
  1680. const prop_fns<any_executor>* tmp_prop_fns = other.prop_fns_; \
  1681. other.prop_fns_ = prop_fns_; \
  1682. prop_fns_ = tmp_prop_fns; \
  1683. } \
  1684. } \
  1685. \
  1686. using detail::any_executor_base::execute; \
  1687. using detail::any_executor_base::target; \
  1688. using detail::any_executor_base::target_type; \
  1689. using detail::any_executor_base::operator unspecified_bool_type; \
  1690. using detail::any_executor_base::operator!; \
  1691. \
  1692. bool equality_helper(const any_executor& other) const ASIO_NOEXCEPT \
  1693. { \
  1694. return any_executor_base::equality_helper(other); \
  1695. } \
  1696. \
  1697. template <typename AnyExecutor1, typename AnyExecutor2> \
  1698. friend typename enable_if< \
  1699. is_base_of<any_executor, AnyExecutor1>::value \
  1700. || is_base_of<any_executor, AnyExecutor2>::value, \
  1701. bool \
  1702. >::type operator==(const AnyExecutor1& a, \
  1703. const AnyExecutor2& b) ASIO_NOEXCEPT \
  1704. { \
  1705. return static_cast<const any_executor&>(a).equality_helper(b); \
  1706. } \
  1707. \
  1708. template <typename AnyExecutor> \
  1709. friend typename enable_if< \
  1710. is_same<AnyExecutor, any_executor>::value, \
  1711. bool \
  1712. >::type operator==(const AnyExecutor& a, nullptr_t) ASIO_NOEXCEPT \
  1713. { \
  1714. return !a; \
  1715. } \
  1716. \
  1717. template <typename AnyExecutor> \
  1718. friend typename enable_if< \
  1719. is_same<AnyExecutor, any_executor>::value, \
  1720. bool \
  1721. >::type operator==(nullptr_t, const AnyExecutor& b) ASIO_NOEXCEPT \
  1722. { \
  1723. return !b; \
  1724. } \
  1725. \
  1726. template <typename AnyExecutor1, typename AnyExecutor2> \
  1727. friend typename enable_if< \
  1728. is_base_of<any_executor, AnyExecutor1>::value \
  1729. || is_base_of<any_executor, AnyExecutor2>::value, \
  1730. bool \
  1731. >::type operator!=(const AnyExecutor1& a, \
  1732. const AnyExecutor2& b) ASIO_NOEXCEPT \
  1733. { \
  1734. return !static_cast<const any_executor&>(a).equality_helper(b); \
  1735. } \
  1736. \
  1737. template <typename AnyExecutor> \
  1738. friend typename enable_if< \
  1739. is_same<AnyExecutor, any_executor>::value, \
  1740. bool \
  1741. >::type operator!=(const AnyExecutor& a, nullptr_t) ASIO_NOEXCEPT \
  1742. { \
  1743. return !!a; \
  1744. } \
  1745. \
  1746. template <typename AnyExecutor> \
  1747. friend typename enable_if< \
  1748. is_same<AnyExecutor, any_executor>::value, \
  1749. bool \
  1750. >::type operator!=(nullptr_t, const AnyExecutor& b) ASIO_NOEXCEPT \
  1751. { \
  1752. return !!b; \
  1753. } \
  1754. \
  1755. template <typename T> \
  1756. struct find_convertible_property : \
  1757. detail::supportable_properties< \
  1758. 0, void(ASIO_VARIADIC_TARGS(n))>::template \
  1759. find_convertible_property<T> {}; \
  1760. \
  1761. template <typename Property> \
  1762. void query(const Property& p, \
  1763. typename enable_if< \
  1764. is_same< \
  1765. typename find_convertible_property<Property>::query_result_type, \
  1766. void \
  1767. >::value \
  1768. >::type* = 0) const \
  1769. { \
  1770. typedef find_convertible_property<Property> found; \
  1771. prop_fns_[found::index].query(0, object_fns_->target(*this), \
  1772. &static_cast<const typename found::type&>(p)); \
  1773. } \
  1774. \
  1775. template <typename Property> \
  1776. typename find_convertible_property<Property>::query_result_type \
  1777. query(const Property& p, \
  1778. typename enable_if< \
  1779. !is_same< \
  1780. typename find_convertible_property<Property>::query_result_type, \
  1781. void \
  1782. >::value \
  1783. && \
  1784. is_reference< \
  1785. typename find_convertible_property<Property>::query_result_type \
  1786. >::value \
  1787. >::type* = 0) const \
  1788. { \
  1789. typedef find_convertible_property<Property> found; \
  1790. typename remove_reference< \
  1791. typename found::query_result_type>::type* result; \
  1792. prop_fns_[found::index].query(&result, object_fns_->target(*this), \
  1793. &static_cast<const typename found::type&>(p)); \
  1794. return *result; \
  1795. } \
  1796. \
  1797. template <typename Property> \
  1798. typename find_convertible_property<Property>::query_result_type \
  1799. query(const Property& p, \
  1800. typename enable_if< \
  1801. !is_same< \
  1802. typename find_convertible_property<Property>::query_result_type, \
  1803. void \
  1804. >::value \
  1805. && \
  1806. is_scalar< \
  1807. typename find_convertible_property<Property>::query_result_type \
  1808. >::value \
  1809. >::type* = 0) const \
  1810. { \
  1811. typedef find_convertible_property<Property> found; \
  1812. typename found::query_result_type result; \
  1813. prop_fns_[found::index].query(&result, object_fns_->target(*this), \
  1814. &static_cast<const typename found::type&>(p)); \
  1815. return result; \
  1816. } \
  1817. \
  1818. template <typename Property> \
  1819. typename find_convertible_property<Property>::query_result_type \
  1820. query(const Property& p, \
  1821. typename enable_if< \
  1822. !is_same< \
  1823. typename find_convertible_property<Property>::query_result_type, \
  1824. void \
  1825. >::value \
  1826. && \
  1827. !is_reference< \
  1828. typename find_convertible_property<Property>::query_result_type \
  1829. >::value \
  1830. && \
  1831. !is_scalar< \
  1832. typename find_convertible_property<Property>::query_result_type \
  1833. >::value \
  1834. >::type* = 0) const \
  1835. { \
  1836. typedef find_convertible_property<Property> found; \
  1837. typename found::query_result_type* result; \
  1838. prop_fns_[found::index].query(&result, object_fns_->target(*this), \
  1839. &static_cast<const typename found::type&>(p)); \
  1840. return *asio::detail::scoped_ptr< \
  1841. typename found::query_result_type>(result); \
  1842. } \
  1843. \
  1844. template <typename T> \
  1845. struct find_convertible_requirable_property : \
  1846. detail::supportable_properties< \
  1847. 0, void(ASIO_VARIADIC_TARGS(n))>::template \
  1848. find_convertible_requirable_property<T> {}; \
  1849. \
  1850. template <typename Property> \
  1851. any_executor require(const Property& p, \
  1852. typename enable_if< \
  1853. find_convertible_requirable_property<Property>::value \
  1854. >::type* = 0) const \
  1855. { \
  1856. typedef find_convertible_requirable_property<Property> found; \
  1857. return prop_fns_[found::index].require(object_fns_->target(*this), \
  1858. &static_cast<const typename found::type&>(p)); \
  1859. } \
  1860. \
  1861. template <typename T> \
  1862. struct find_convertible_preferable_property : \
  1863. detail::supportable_properties< \
  1864. 0, void(ASIO_VARIADIC_TARGS(n))>::template \
  1865. find_convertible_preferable_property<T> {}; \
  1866. \
  1867. template <typename Property> \
  1868. any_executor prefer(const Property& p, \
  1869. typename enable_if< \
  1870. find_convertible_preferable_property<Property>::value \
  1871. >::type* = 0) const \
  1872. { \
  1873. typedef find_convertible_preferable_property<Property> found; \
  1874. return prop_fns_[found::index].prefer(object_fns_->target(*this), \
  1875. &static_cast<const typename found::type&>(p)); \
  1876. } \
  1877. \
  1878. template <typename Ex> \
  1879. static const prop_fns<any_executor>* prop_fns_table() \
  1880. { \
  1881. static const prop_fns<any_executor> fns[] = \
  1882. { \
  1883. ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS(n) \
  1884. }; \
  1885. return fns; \
  1886. } \
  1887. \
  1888. const prop_fns<any_executor>* prop_fns_; \
  1889. typedef detail::supportable_properties<0, \
  1890. void(ASIO_VARIADIC_TARGS(n))> supportable_properties_type; \
  1891. }; \
  1892. \
  1893. template <ASIO_VARIADIC_TPARAMS(n)> \
  1894. inline void swap(any_executor<ASIO_VARIADIC_TARGS(n)>& a, \
  1895. any_executor<ASIO_VARIADIC_TARGS(n)>& b) ASIO_NOEXCEPT \
  1896. { \
  1897. return a.swap(b); \
  1898. } \
  1899. /**/
  1900. ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ANY_EXECUTOR_DEF)
  1901. #undef ASIO_PRIVATE_ANY_EXECUTOR_DEF
  1902. #undef ASIO_PRIVATE_ANY_EXECUTOR_MOVE_OPS
  1903. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS
  1904. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_1
  1905. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_2
  1906. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_3
  1907. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_4
  1908. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_5
  1909. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_6
  1910. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_7
  1911. #undef ASIO_PRIVATE_ANY_EXECUTOR_PROP_FNS_8
  1912. #endif // if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1913. } // namespace execution
  1914. namespace traits {
  1915. #if !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  1916. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1917. template <typename... SupportableProperties>
  1918. struct equality_comparable<execution::any_executor<SupportableProperties...> >
  1919. {
  1920. static const bool is_valid = true;
  1921. static const bool is_noexcept = true;
  1922. };
  1923. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1924. template <>
  1925. struct equality_comparable<execution::any_executor<> >
  1926. {
  1927. static const bool is_valid = true;
  1928. static const bool is_noexcept = true;
  1929. };
  1930. #define ASIO_PRIVATE_ANY_EXECUTOR_EQUALITY_COMPARABLE_DEF(n) \
  1931. template <ASIO_VARIADIC_TPARAMS(n)> \
  1932. struct equality_comparable< \
  1933. execution::any_executor<ASIO_VARIADIC_TARGS(n)> > \
  1934. { \
  1935. static const bool is_valid = true; \
  1936. static const bool is_noexcept = true; \
  1937. }; \
  1938. /**/
  1939. ASIO_VARIADIC_GENERATE(
  1940. ASIO_PRIVATE_ANY_EXECUTOR_EQUALITY_COMPARABLE_DEF)
  1941. #undef ASIO_PRIVATE_ANY_EXECUTOR_EQUALITY_COMPARABLE_DEF
  1942. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1943. #endif // !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  1944. #if !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  1945. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1946. template <typename F, typename... SupportableProperties>
  1947. struct execute_member<execution::any_executor<SupportableProperties...>, F>
  1948. {
  1949. static const bool is_valid = true;
  1950. static const bool is_noexcept = false;
  1951. typedef void result_type;
  1952. };
  1953. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1954. template <typename F>
  1955. struct execute_member<execution::any_executor<>, F>
  1956. {
  1957. static const bool is_valid = true;
  1958. static const bool is_noexcept = false;
  1959. typedef void result_type;
  1960. };
  1961. #define ASIO_PRIVATE_ANY_EXECUTOR_EXECUTE_MEMBER_DEF(n) \
  1962. template <typename F, ASIO_VARIADIC_TPARAMS(n)> \
  1963. struct execute_member< \
  1964. execution::any_executor<ASIO_VARIADIC_TARGS(n)>, F> \
  1965. { \
  1966. static const bool is_valid = true; \
  1967. static const bool is_noexcept = false; \
  1968. typedef void result_type; \
  1969. }; \
  1970. /**/
  1971. ASIO_VARIADIC_GENERATE(
  1972. ASIO_PRIVATE_ANY_EXECUTOR_EXECUTE_MEMBER_DEF)
  1973. #undef ASIO_PRIVATE_ANY_EXECUTOR_EXECUTE_MEMBER_DEF
  1974. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1975. #endif // !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  1976. #if !defined(ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  1977. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1978. template <typename Prop, typename... SupportableProperties>
  1979. struct query_member<
  1980. execution::any_executor<SupportableProperties...>, Prop,
  1981. typename enable_if<
  1982. execution::detail::supportable_properties<
  1983. 0, void(SupportableProperties...)>::template
  1984. find_convertible_property<Prop>::value
  1985. >::type>
  1986. {
  1987. static const bool is_valid = true;
  1988. static const bool is_noexcept = false;
  1989. typedef typename execution::detail::supportable_properties<
  1990. 0, void(SupportableProperties...)>::template
  1991. find_convertible_property<Prop>::query_result_type result_type;
  1992. };
  1993. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  1994. #define ASIO_PRIVATE_ANY_EXECUTOR_QUERY_MEMBER_DEF(n) \
  1995. template <typename Prop, ASIO_VARIADIC_TPARAMS(n)> \
  1996. struct query_member< \
  1997. execution::any_executor<ASIO_VARIADIC_TARGS(n)>, Prop, \
  1998. typename enable_if< \
  1999. execution::detail::supportable_properties< \
  2000. 0, void(ASIO_VARIADIC_TARGS(n))>::template \
  2001. find_convertible_property<Prop>::value \
  2002. >::type> \
  2003. { \
  2004. static const bool is_valid = true; \
  2005. static const bool is_noexcept = false; \
  2006. typedef typename execution::detail::supportable_properties< \
  2007. 0, void(ASIO_VARIADIC_TARGS(n))>::template \
  2008. find_convertible_property<Prop>::query_result_type result_type; \
  2009. }; \
  2010. /**/
  2011. ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ANY_EXECUTOR_QUERY_MEMBER_DEF)
  2012. #undef ASIO_PRIVATE_ANY_EXECUTOR_QUERY_MEMBER_DEF
  2013. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  2014. #endif // !defined(ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  2015. #if !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  2016. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  2017. template <typename Prop, typename... SupportableProperties>
  2018. struct require_member<
  2019. execution::any_executor<SupportableProperties...>, Prop,
  2020. typename enable_if<
  2021. execution::detail::supportable_properties<
  2022. 0, void(SupportableProperties...)>::template
  2023. find_convertible_requirable_property<Prop>::value
  2024. >::type>
  2025. {
  2026. static const bool is_valid = true;
  2027. static const bool is_noexcept = false;
  2028. typedef execution::any_executor<SupportableProperties...> result_type;
  2029. };
  2030. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  2031. #define ASIO_PRIVATE_ANY_EXECUTOR_REQUIRE_MEMBER_DEF(n) \
  2032. template <typename Prop, ASIO_VARIADIC_TPARAMS(n)> \
  2033. struct require_member< \
  2034. execution::any_executor<ASIO_VARIADIC_TARGS(n)>, Prop, \
  2035. typename enable_if< \
  2036. execution::detail::supportable_properties< \
  2037. 0, void(ASIO_VARIADIC_TARGS(n))>::template \
  2038. find_convertible_requirable_property<Prop>::value \
  2039. >::type> \
  2040. { \
  2041. static const bool is_valid = true; \
  2042. static const bool is_noexcept = false; \
  2043. typedef execution::any_executor<ASIO_VARIADIC_TARGS(n)> result_type; \
  2044. }; \
  2045. /**/
  2046. ASIO_VARIADIC_GENERATE(
  2047. ASIO_PRIVATE_ANY_EXECUTOR_REQUIRE_MEMBER_DEF)
  2048. #undef ASIO_PRIVATE_ANY_EXECUTOR_REQUIRE_MEMBER_DEF
  2049. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  2050. #endif // !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  2051. #if !defined(ASIO_HAS_DEDUCED_PREFER_FREE_TRAIT)
  2052. #if defined(ASIO_HAS_VARIADIC_TEMPLATES)
  2053. template <typename Prop, typename... SupportableProperties>
  2054. struct prefer_member<
  2055. execution::any_executor<SupportableProperties...>, Prop,
  2056. typename enable_if<
  2057. execution::detail::supportable_properties<
  2058. 0, void(SupportableProperties...)>::template
  2059. find_convertible_preferable_property<Prop>::value
  2060. >::type>
  2061. {
  2062. static const bool is_valid = true;
  2063. static const bool is_noexcept = false;
  2064. typedef execution::any_executor<SupportableProperties...> result_type;
  2065. };
  2066. #else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  2067. #define ASIO_PRIVATE_ANY_EXECUTOR_PREFER_FREE_DEF(n) \
  2068. template <typename Prop, ASIO_VARIADIC_TPARAMS(n)> \
  2069. struct prefer_member< \
  2070. execution::any_executor<ASIO_VARIADIC_TARGS(n)>, Prop, \
  2071. typename enable_if< \
  2072. execution::detail::supportable_properties< \
  2073. 0, void(ASIO_VARIADIC_TARGS(n))>::template \
  2074. find_convertible_preferable_property<Prop>::value \
  2075. >::type> \
  2076. { \
  2077. static const bool is_valid = true; \
  2078. static const bool is_noexcept = false; \
  2079. typedef execution::any_executor<ASIO_VARIADIC_TARGS(n)> result_type; \
  2080. }; \
  2081. /**/
  2082. ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ANY_EXECUTOR_PREFER_FREE_DEF)
  2083. #undef ASIO_PRIVATE_ANY_EXECUTOR_PREFER_FREE_DEF
  2084. #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
  2085. #endif // !defined(ASIO_HAS_DEDUCED_PREFER_FREE_TRAIT)
  2086. } // namespace traits
  2087. #endif // defined(GENERATING_DOCUMENTATION)
  2088. } // namespace asio
  2089. #include "asio/detail/pop_options.hpp"
  2090. #endif // ASIO_EXECUTION_ANY_EXECUTOR_HPP