pull_coroutine.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright Oliver Kowalke 2014.
  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_COROUTINES2_DETAIL_PULL_COROUTINE_HPP
  6. #define BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_HPP
  7. #include <iterator>
  8. #include <type_traits>
  9. #include <boost/assert.hpp>
  10. #include <boost/config.hpp>
  11. #include <boost/coroutine2/detail/config.hpp>
  12. #include <boost/coroutine2/detail/disable_overload.hpp>
  13. #ifdef BOOST_HAS_ABI_HEADERS
  14. # include BOOST_ABI_PREFIX
  15. #endif
  16. namespace boost {
  17. namespace coroutines2 {
  18. namespace detail {
  19. template< typename T >
  20. class pull_coroutine {
  21. private:
  22. template< typename X >
  23. friend class push_coroutine;
  24. struct control_block;
  25. control_block * cb_;
  26. explicit pull_coroutine( control_block *) noexcept;
  27. bool has_result_() const noexcept;
  28. public:
  29. template< typename Fn,
  30. typename = detail::disable_overload< pull_coroutine, Fn >
  31. >
  32. explicit pull_coroutine( Fn &&);
  33. template< typename StackAllocator, typename Fn >
  34. pull_coroutine( StackAllocator &&, Fn &&);
  35. ~pull_coroutine();
  36. pull_coroutine( pull_coroutine const&) = delete;
  37. pull_coroutine & operator=( pull_coroutine const&) = delete;
  38. pull_coroutine( pull_coroutine &&) noexcept;
  39. pull_coroutine & operator=( pull_coroutine && other) noexcept {
  40. std::swap( cb_, other.cb_);
  41. return * this;
  42. }
  43. pull_coroutine & operator()();
  44. explicit operator bool() const noexcept;
  45. bool operator!() const noexcept;
  46. T get() noexcept;
  47. class iterator {
  48. private:
  49. pull_coroutine< T > * c_{ nullptr };
  50. void fetch_() noexcept {
  51. BOOST_ASSERT( nullptr != c_);
  52. if ( ! ( * c_) ) {
  53. c_ = nullptr;
  54. return;
  55. }
  56. }
  57. void increment_() {
  58. BOOST_ASSERT( nullptr != c_);
  59. BOOST_ASSERT( * c_);
  60. ( * c_)();
  61. fetch_();
  62. }
  63. public:
  64. typedef std::input_iterator_tag iterator_category;
  65. typedef typename std::remove_reference< T >::type value_type;
  66. typedef std::ptrdiff_t difference_type;
  67. typedef value_type * pointer;
  68. typedef value_type & reference;
  69. typedef pointer pointer_t;
  70. typedef reference reference_t;
  71. iterator() noexcept = default;
  72. explicit iterator( pull_coroutine< T > * c) noexcept :
  73. c_{ c } {
  74. fetch_();
  75. }
  76. bool operator==( iterator const& other) const noexcept {
  77. return other.c_ == c_;
  78. }
  79. bool operator!=( iterator const& other) const noexcept {
  80. return other.c_ != c_;
  81. }
  82. iterator & operator++() {
  83. increment_();
  84. return * this;
  85. }
  86. void operator++( int) {
  87. increment_();
  88. }
  89. reference_t operator*() const noexcept {
  90. return c_->cb_->get();
  91. }
  92. pointer_t operator->() const noexcept {
  93. return std::addressof( c_->cb_->get() );
  94. }
  95. };
  96. friend class iterator;
  97. iterator begin() { return iterator (this); }
  98. iterator end() { return iterator(); }
  99. };
  100. template< typename T >
  101. class pull_coroutine< T & > {
  102. private:
  103. template< typename X >
  104. friend class push_coroutine;
  105. struct control_block;
  106. control_block * cb_;
  107. explicit pull_coroutine( control_block *) noexcept;
  108. bool has_result_() const noexcept;
  109. public:
  110. template< typename Fn,
  111. typename = detail::disable_overload< pull_coroutine, Fn >
  112. >
  113. explicit pull_coroutine( Fn &&);
  114. template< typename StackAllocator, typename Fn >
  115. pull_coroutine( StackAllocator &&, Fn &&);
  116. ~pull_coroutine();
  117. pull_coroutine( pull_coroutine const&) = delete;
  118. pull_coroutine & operator=( pull_coroutine const&) = delete;
  119. pull_coroutine( pull_coroutine &&) noexcept;
  120. pull_coroutine & operator=( pull_coroutine && other) noexcept {
  121. std::swap( cb_, other.cb_);
  122. return * this;
  123. }
  124. pull_coroutine & operator()();
  125. explicit operator bool() const noexcept;
  126. bool operator!() const noexcept;
  127. T & get() noexcept;
  128. class iterator {
  129. private:
  130. pull_coroutine< T & > * c_{ nullptr };
  131. void fetch_() noexcept {
  132. BOOST_ASSERT( nullptr != c_);
  133. if ( ! ( * c_) ) {
  134. c_ = nullptr;
  135. return;
  136. }
  137. }
  138. void increment_() {
  139. BOOST_ASSERT( nullptr != c_);
  140. BOOST_ASSERT( * c_);
  141. ( * c_)();
  142. fetch_();
  143. }
  144. public:
  145. typedef std::input_iterator_tag iterator_category;
  146. typedef typename std::remove_reference< T >::type value_type;
  147. typedef std::ptrdiff_t difference_type;
  148. typedef value_type * pointer;
  149. typedef value_type & reference;
  150. typedef pointer pointer_t;
  151. typedef reference reference_t;
  152. iterator() noexcept = default;
  153. explicit iterator( pull_coroutine< T & > * c) noexcept :
  154. c_{ c } {
  155. fetch_();
  156. }
  157. bool operator==( iterator const& other) const noexcept {
  158. return other.c_ == c_;
  159. }
  160. bool operator!=( iterator const& other) const noexcept {
  161. return other.c_ != c_;
  162. }
  163. iterator & operator++() {
  164. increment_();
  165. return * this;
  166. }
  167. void operator++( int) {
  168. increment_();
  169. }
  170. reference_t operator*() const noexcept {
  171. return c_->cb_->get();
  172. }
  173. pointer_t operator->() const noexcept {
  174. return std::addressof( c_->cb_->get() );
  175. }
  176. };
  177. friend class iterator;
  178. iterator begin() { return iterator (this); }
  179. iterator end() { return iterator(); }
  180. };
  181. template<>
  182. class pull_coroutine< void > {
  183. private:
  184. template< typename X >
  185. friend class push_coroutine;
  186. struct control_block;
  187. control_block * cb_;
  188. explicit pull_coroutine( control_block *) noexcept;
  189. public:
  190. template< typename Fn,
  191. typename = detail::disable_overload< pull_coroutine, Fn >
  192. >
  193. explicit pull_coroutine( Fn &&);
  194. template< typename StackAllocator, typename Fn >
  195. pull_coroutine( StackAllocator &&, Fn &&);
  196. ~pull_coroutine();
  197. pull_coroutine( pull_coroutine const&) = delete;
  198. pull_coroutine & operator=( pull_coroutine const&) = delete;
  199. pull_coroutine( pull_coroutine &&) noexcept;
  200. pull_coroutine & operator=( pull_coroutine && other) noexcept {
  201. std::swap( cb_, other.cb_);
  202. return * this;
  203. }
  204. pull_coroutine & operator()();
  205. explicit operator bool() const noexcept;
  206. bool operator!() const noexcept;
  207. };
  208. template< typename T >
  209. typename pull_coroutine< T >::iterator
  210. begin( pull_coroutine< T > & c) {
  211. return typename pull_coroutine< T >::iterator( & c);
  212. }
  213. template< typename T >
  214. typename pull_coroutine< T >::iterator
  215. end( pull_coroutine< T > &) {
  216. return typename pull_coroutine< T >::iterator();
  217. }
  218. }}}
  219. #ifdef BOOST_HAS_ABI_HEADERS
  220. # include BOOST_ABI_SUFFIX
  221. #endif
  222. #endif // BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_HPP