asymmetric_coroutine.hpp 82 KB

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