asymmetric_coroutine.hpp 82 KB

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