symmetric_coroutine_yield.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_DETAIL_SYMMETRIC_COROUTINE_YIELD_H
  6. #define BOOST_COROUTINES_DETAIL_SYMMETRIC_COROUTINE_YIELD_H
  7. #include <algorithm>
  8. #include <boost/assert.hpp>
  9. #include <boost/config.hpp>
  10. #include <boost/move/move.hpp>
  11. #include <boost/throw_exception.hpp>
  12. #include <boost/type_traits/is_same.hpp>
  13. #include <boost/utility/enable_if.hpp>
  14. #include <boost/utility/explicit_operator_bool.hpp>
  15. #include <boost/coroutine/detail/config.hpp>
  16. #include <boost/coroutine/exceptions.hpp>
  17. #ifdef BOOST_HAS_ABI_HEADERS
  18. # include BOOST_ABI_PREFIX
  19. #endif
  20. namespace boost {
  21. namespace coroutines {
  22. namespace detail {
  23. template< typename R >
  24. class symmetric_coroutine_yield
  25. {
  26. private:
  27. template< typename X, typename Y, typename Z >
  28. friend class symmetric_coroutine_object;
  29. typedef symmetric_coroutine_impl< R > impl_type;
  30. struct dummy {};
  31. BOOST_MOVABLE_BUT_NOT_COPYABLE( symmetric_coroutine_yield)
  32. impl_type * impl_;
  33. R * result_;
  34. symmetric_coroutine_yield( impl_type * impl, R * result) BOOST_NOEXCEPT :
  35. impl_( impl),
  36. result_( result)
  37. {
  38. BOOST_ASSERT( 0 != impl_);
  39. BOOST_ASSERT( 0 != result_);
  40. }
  41. public:
  42. symmetric_coroutine_yield() BOOST_NOEXCEPT :
  43. impl_( 0),
  44. result_( 0)
  45. {}
  46. symmetric_coroutine_yield( BOOST_RV_REF( symmetric_coroutine_yield) other) BOOST_NOEXCEPT :
  47. impl_( 0),
  48. result_( 0)
  49. { swap( other); }
  50. symmetric_coroutine_yield & operator=( BOOST_RV_REF( symmetric_coroutine_yield) other) BOOST_NOEXCEPT
  51. {
  52. symmetric_coroutine_yield tmp( boost::move( other) );
  53. swap( tmp);
  54. return * this;
  55. }
  56. BOOST_EXPLICIT_OPERATOR_BOOL();
  57. bool operator!() const BOOST_NOEXCEPT
  58. { return 0 == impl_; }
  59. void swap( symmetric_coroutine_yield & other) BOOST_NOEXCEPT
  60. {
  61. std::swap( impl_, other.impl_);
  62. std::swap( result_, other.result_);
  63. }
  64. symmetric_coroutine_yield & operator()()
  65. {
  66. result_ = impl_->yield();
  67. return * this;
  68. }
  69. template< typename Coro >
  70. symmetric_coroutine_yield & operator()( Coro & other, typename Coro::value_type x,
  71. typename disable_if<
  72. is_same< typename Coro::value_type, void >,
  73. dummy*
  74. >::type = 0)
  75. {
  76. BOOST_ASSERT( other);
  77. result_ = impl_->yield_to( other.impl_, x);
  78. return * this;
  79. }
  80. template< typename Coro >
  81. symmetric_coroutine_yield & operator()( Coro & other,
  82. typename enable_if<
  83. is_same< typename Coro::value_type, void >,
  84. dummy*
  85. >::type = 0)
  86. {
  87. BOOST_ASSERT( other);
  88. result_ = impl_->yield_to( other.impl_);
  89. return * this;
  90. }
  91. R get() const
  92. {
  93. if ( 0 == result_)
  94. boost::throw_exception(
  95. invalid_result() );
  96. return * result_;
  97. }
  98. };
  99. template< typename R >
  100. class symmetric_coroutine_yield< R & >
  101. {
  102. private:
  103. template< typename X, typename Y, typename Z >
  104. friend class symmetric_coroutine_object;
  105. typedef symmetric_coroutine_impl< R & > impl_type;
  106. struct dummy {};
  107. BOOST_MOVABLE_BUT_NOT_COPYABLE( symmetric_coroutine_yield)
  108. impl_type * impl_;
  109. R * result_;
  110. symmetric_coroutine_yield( impl_type * impl, R * result) BOOST_NOEXCEPT :
  111. impl_( impl),
  112. result_( result)
  113. {
  114. BOOST_ASSERT( 0 != impl_);
  115. BOOST_ASSERT( 0 != result_);
  116. }
  117. public:
  118. symmetric_coroutine_yield() BOOST_NOEXCEPT :
  119. impl_( 0),
  120. result_( 0)
  121. {}
  122. symmetric_coroutine_yield( BOOST_RV_REF( symmetric_coroutine_yield) other) BOOST_NOEXCEPT :
  123. impl_( 0),
  124. result_( 0)
  125. { swap( other); }
  126. symmetric_coroutine_yield & operator=( BOOST_RV_REF( symmetric_coroutine_yield) other) BOOST_NOEXCEPT
  127. {
  128. symmetric_coroutine_yield tmp( boost::move( other) );
  129. swap( tmp);
  130. return * this;
  131. }
  132. BOOST_EXPLICIT_OPERATOR_BOOL();
  133. bool operator!() const BOOST_NOEXCEPT
  134. { return 0 == impl_; }
  135. void swap( symmetric_coroutine_yield & other) BOOST_NOEXCEPT
  136. {
  137. std::swap( impl_, other.impl_);
  138. std::swap( result_, other.result_);
  139. }
  140. symmetric_coroutine_yield & operator()()
  141. {
  142. result_ = impl_->yield();
  143. return * this;
  144. }
  145. template< typename Coro >
  146. symmetric_coroutine_yield & operator()( Coro & other, typename Coro::value_type & x,
  147. typename disable_if<
  148. is_same< typename Coro::value_type, void >,
  149. dummy*
  150. >::type = 0)
  151. {
  152. BOOST_ASSERT( other);
  153. result_ = impl_->yield_to( other.impl_, x);
  154. return * this;
  155. }
  156. template< typename Coro >
  157. symmetric_coroutine_yield & operator()( Coro & other,
  158. typename enable_if<
  159. is_same< typename Coro::value_type, void >,
  160. dummy*
  161. >::type = 0)
  162. {
  163. BOOST_ASSERT( other);
  164. result_ = impl_->yield_to( other.impl_);
  165. return * this;
  166. }
  167. R & get() const
  168. {
  169. if ( 0 == result_)
  170. boost::throw_exception(
  171. invalid_result() );
  172. return * result_;
  173. }
  174. };
  175. template<>
  176. class symmetric_coroutine_yield< void >
  177. {
  178. private:
  179. template< typename X, typename Y, typename Z >
  180. friend class symmetric_coroutine_object;
  181. typedef symmetric_coroutine_impl< void > impl_type;
  182. struct dummy {};
  183. BOOST_MOVABLE_BUT_NOT_COPYABLE( symmetric_coroutine_yield)
  184. impl_type * impl_;
  185. symmetric_coroutine_yield( impl_type * impl) BOOST_NOEXCEPT :
  186. impl_( impl)
  187. { BOOST_ASSERT( 0 != impl_); }
  188. public:
  189. symmetric_coroutine_yield() BOOST_NOEXCEPT :
  190. impl_( 0)
  191. {}
  192. symmetric_coroutine_yield( BOOST_RV_REF( symmetric_coroutine_yield) other) BOOST_NOEXCEPT :
  193. impl_( 0)
  194. { swap( other); }
  195. symmetric_coroutine_yield & operator=( BOOST_RV_REF( symmetric_coroutine_yield) other) BOOST_NOEXCEPT
  196. {
  197. symmetric_coroutine_yield tmp( boost::move( other) );
  198. swap( tmp);
  199. return * this;
  200. }
  201. BOOST_EXPLICIT_OPERATOR_BOOL();
  202. inline bool operator!() const BOOST_NOEXCEPT
  203. { return 0 == impl_; }
  204. inline void swap( symmetric_coroutine_yield & other) BOOST_NOEXCEPT
  205. { std::swap( impl_, other.impl_); }
  206. inline symmetric_coroutine_yield & operator()()
  207. {
  208. impl_->yield();
  209. return * this;
  210. }
  211. template< typename Coro >
  212. symmetric_coroutine_yield & operator()( Coro & other, typename Coro::value_type & x,
  213. typename disable_if<
  214. is_same< typename Coro::value_type, void >,
  215. dummy*
  216. >::type = 0)
  217. {
  218. BOOST_ASSERT( other);
  219. impl_->yield_to( other.impl_, x);
  220. return * this;
  221. }
  222. template< typename Coro >
  223. symmetric_coroutine_yield & operator()( Coro & other,
  224. typename enable_if<
  225. is_same< typename Coro::value_type, void >,
  226. dummy*
  227. >::type = 0)
  228. {
  229. BOOST_ASSERT( other);
  230. impl_->yield_to( other.impl_);
  231. return * this;
  232. }
  233. };
  234. template< typename R >
  235. void swap( symmetric_coroutine_yield< R > & l, symmetric_coroutine_yield< R > & r)
  236. { l.swap( r); }
  237. }}}
  238. #ifdef BOOST_HAS_ABI_HEADERS
  239. # include BOOST_ABI_SUFFIX
  240. #endif
  241. #endif // BOOST_COROUTINES_DETAIL_SYMMETRIC_COROUTINE_YIELD_H