pull_coroutine.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. };
  98. template< typename T >
  99. class pull_coroutine< T & > {
  100. private:
  101. template< typename X >
  102. friend class push_coroutine;
  103. struct control_block;
  104. control_block * cb_;
  105. explicit pull_coroutine( control_block *) noexcept;
  106. bool has_result_() const noexcept;
  107. public:
  108. template< typename Fn,
  109. typename = detail::disable_overload< pull_coroutine, Fn >
  110. >
  111. explicit pull_coroutine( Fn &&);
  112. template< typename StackAllocator, typename Fn >
  113. pull_coroutine( StackAllocator &&, Fn &&);
  114. ~pull_coroutine();
  115. pull_coroutine( pull_coroutine const&) = delete;
  116. pull_coroutine & operator=( pull_coroutine const&) = delete;
  117. pull_coroutine( pull_coroutine &&) noexcept;
  118. pull_coroutine & operator=( pull_coroutine && other) noexcept {
  119. std::swap( cb_, other.cb_);
  120. return * this;
  121. }
  122. pull_coroutine & operator()();
  123. explicit operator bool() const noexcept;
  124. bool operator!() const noexcept;
  125. T & get() noexcept;
  126. class iterator {
  127. private:
  128. pull_coroutine< T & > * c_{ nullptr };
  129. void fetch_() noexcept {
  130. BOOST_ASSERT( nullptr != c_);
  131. if ( ! ( * c_) ) {
  132. c_ = nullptr;
  133. return;
  134. }
  135. }
  136. void increment_() {
  137. BOOST_ASSERT( nullptr != c_);
  138. BOOST_ASSERT( * c_);
  139. ( * c_)();
  140. fetch_();
  141. }
  142. public:
  143. typedef std::input_iterator_tag iterator_category;
  144. typedef typename std::remove_reference< T >::type value_type;
  145. typedef std::ptrdiff_t difference_type;
  146. typedef value_type * pointer;
  147. typedef value_type & reference;
  148. typedef pointer pointer_t;
  149. typedef reference reference_t;
  150. iterator() noexcept = default;
  151. explicit iterator( pull_coroutine< T & > * c) noexcept :
  152. c_{ c } {
  153. fetch_();
  154. }
  155. bool operator==( iterator const& other) const noexcept {
  156. return other.c_ == c_;
  157. }
  158. bool operator!=( iterator const& other) const noexcept {
  159. return other.c_ != c_;
  160. }
  161. iterator & operator++() {
  162. increment_();
  163. return * this;
  164. }
  165. void operator++( int) {
  166. increment_();
  167. }
  168. reference_t operator*() const noexcept {
  169. return c_->cb_->get();
  170. }
  171. pointer_t operator->() const noexcept {
  172. return std::addressof( c_->cb_->get() );
  173. }
  174. };
  175. friend class iterator;
  176. };
  177. template<>
  178. class pull_coroutine< void > {
  179. private:
  180. template< typename X >
  181. friend class push_coroutine;
  182. struct control_block;
  183. control_block * cb_;
  184. explicit pull_coroutine( control_block *) noexcept;
  185. public:
  186. template< typename Fn,
  187. typename = detail::disable_overload< pull_coroutine, Fn >
  188. >
  189. explicit pull_coroutine( Fn &&);
  190. template< typename StackAllocator, typename Fn >
  191. pull_coroutine( StackAllocator &&, Fn &&);
  192. ~pull_coroutine();
  193. pull_coroutine( pull_coroutine const&) = delete;
  194. pull_coroutine & operator=( pull_coroutine const&) = delete;
  195. pull_coroutine( pull_coroutine &&) noexcept;
  196. pull_coroutine & operator=( pull_coroutine && other) noexcept {
  197. std::swap( cb_, other.cb_);
  198. return * this;
  199. }
  200. pull_coroutine & operator()();
  201. explicit operator bool() const noexcept;
  202. bool operator!() const noexcept;
  203. };
  204. template< typename T >
  205. typename pull_coroutine< T >::iterator
  206. begin( pull_coroutine< T > & c) {
  207. return typename pull_coroutine< T >::iterator( & c);
  208. }
  209. template< typename T >
  210. typename pull_coroutine< T >::iterator
  211. end( pull_coroutine< T > &) {
  212. return typename pull_coroutine< T >::iterator();
  213. }
  214. }}}
  215. #ifdef BOOST_HAS_ABI_HEADERS
  216. # include BOOST_ABI_SUFFIX
  217. #endif
  218. #endif // BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_HPP