pull_coroutine.ipp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_IPP
  6. #define BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_IPP
  7. #include <algorithm>
  8. #include <utility>
  9. #include <boost/assert.hpp>
  10. #include <boost/config.hpp>
  11. #include <boost/coroutine2/detail/config.hpp>
  12. #include <boost/coroutine2/detail/create_control_block.ipp>
  13. #include <boost/coroutine2/detail/disable_overload.hpp>
  14. #include <boost/coroutine2/fixedsize_stack.hpp>
  15. #include <boost/coroutine2/segmented_stack.hpp>
  16. #ifdef BOOST_HAS_ABI_HEADERS
  17. # include BOOST_ABI_PREFIX
  18. #endif
  19. namespace boost {
  20. namespace coroutines2 {
  21. namespace detail {
  22. // pull_coroutine< T >
  23. template< typename T >
  24. pull_coroutine< T >::pull_coroutine( control_block * cb) noexcept :
  25. cb_{ cb } {
  26. }
  27. template< typename T >
  28. bool
  29. pull_coroutine< T >::has_result_() const noexcept {
  30. return nullptr != cb_->other->t;
  31. }
  32. template< typename T >
  33. template< typename Fn,
  34. typename
  35. >
  36. pull_coroutine< T >::pull_coroutine( Fn && fn) :
  37. pull_coroutine{ default_stack(), std::forward< Fn >( fn) } {
  38. }
  39. template< typename T >
  40. template< typename StackAllocator, typename Fn >
  41. pull_coroutine< T >::pull_coroutine( StackAllocator && salloc, Fn && fn) :
  42. cb_{ create_control_block< control_block >( std::forward< StackAllocator >( salloc), std::forward< Fn >( fn) ) } {
  43. if ( ! cb_->valid() ) {
  44. cb_->deallocate();
  45. cb_ = nullptr;
  46. }
  47. }
  48. template< typename T >
  49. pull_coroutine< T >::~pull_coroutine() {
  50. if ( nullptr != cb_) {
  51. cb_->deallocate();
  52. }
  53. }
  54. template< typename T >
  55. pull_coroutine< T >::pull_coroutine( pull_coroutine && other) noexcept :
  56. cb_{ nullptr } {
  57. std::swap( cb_, other.cb_);
  58. }
  59. template< typename T >
  60. pull_coroutine< T > &
  61. pull_coroutine< T >::operator()() {
  62. cb_->resume();
  63. return * this;
  64. }
  65. template< typename T >
  66. pull_coroutine< T >::operator bool() const noexcept {
  67. return nullptr != cb_ && cb_->valid();
  68. }
  69. template< typename T >
  70. bool
  71. pull_coroutine< T >::operator!() const noexcept {
  72. return nullptr == cb_ || ! cb_->valid();
  73. }
  74. template< typename T >
  75. T
  76. pull_coroutine< T >::get() noexcept {
  77. return std::move( cb_->get() );
  78. }
  79. // pull_coroutine< T & >
  80. template< typename T >
  81. pull_coroutine< T & >::pull_coroutine( control_block * cb) noexcept :
  82. cb_{ cb } {
  83. }
  84. template< typename T >
  85. bool
  86. pull_coroutine< T & >::has_result_() const noexcept {
  87. return nullptr != cb_->other->t;
  88. }
  89. template< typename T >
  90. template< typename Fn,
  91. typename
  92. >
  93. pull_coroutine< T & >::pull_coroutine( Fn && fn) :
  94. pull_coroutine{ default_stack(), std::forward< Fn >( fn) } {
  95. }
  96. template< typename T >
  97. template< typename StackAllocator, typename Fn >
  98. pull_coroutine< T & >::pull_coroutine( StackAllocator && salloc, Fn && fn) :
  99. cb_{ create_control_block< control_block >( std::forward< StackAllocator >( salloc), std::forward< Fn >( fn) ) } {
  100. if ( ! cb_->valid() ) {
  101. cb_->deallocate();
  102. cb_ = nullptr;
  103. }
  104. }
  105. template< typename T >
  106. pull_coroutine< T & >::~pull_coroutine() {
  107. if ( nullptr != cb_) {
  108. cb_->deallocate();
  109. }
  110. }
  111. template< typename T >
  112. pull_coroutine< T & >::pull_coroutine( pull_coroutine && other) noexcept :
  113. cb_{ nullptr } {
  114. std::swap( cb_, other.cb_);
  115. }
  116. template< typename T >
  117. pull_coroutine< T & > &
  118. pull_coroutine< T & >::operator()() {
  119. cb_->resume();
  120. return * this;
  121. }
  122. template< typename T >
  123. pull_coroutine< T & >::operator bool() const noexcept {
  124. return nullptr != cb_ && cb_->valid();
  125. }
  126. template< typename T >
  127. bool
  128. pull_coroutine< T & >::operator!() const noexcept {
  129. return nullptr == cb_ || ! cb_->valid();
  130. }
  131. template< typename T >
  132. T &
  133. pull_coroutine< T & >::get() noexcept {
  134. return cb_->get();
  135. }
  136. // pull_coroutine< void >
  137. inline
  138. pull_coroutine< void >::pull_coroutine( control_block * cb) noexcept :
  139. cb_{ cb } {
  140. }
  141. template< typename Fn,
  142. typename
  143. >
  144. pull_coroutine< void >::pull_coroutine( Fn && fn) :
  145. pull_coroutine{ default_stack(), std::forward< Fn >( fn) } {
  146. }
  147. template< typename StackAllocator, typename Fn >
  148. pull_coroutine< void >::pull_coroutine( StackAllocator && salloc, Fn && fn) :
  149. cb_{ create_control_block< control_block >( std::forward< StackAllocator >( salloc), std::forward< Fn >( fn) ) } {
  150. if ( ! cb_->valid() ) {
  151. cb_->deallocate();
  152. cb_ = nullptr;
  153. }
  154. }
  155. inline
  156. pull_coroutine< void >::~pull_coroutine() {
  157. if ( nullptr != cb_) {
  158. cb_->deallocate();
  159. }
  160. }
  161. inline
  162. pull_coroutine< void >::pull_coroutine( pull_coroutine && other) noexcept :
  163. cb_{ nullptr } {
  164. std::swap( cb_, other.cb_);
  165. }
  166. inline
  167. pull_coroutine< void > &
  168. pull_coroutine< void >::operator()() {
  169. cb_->resume();
  170. return * this;
  171. }
  172. inline
  173. pull_coroutine< void >::operator bool() const noexcept {
  174. return nullptr != cb_ && cb_->valid();
  175. }
  176. inline
  177. bool
  178. pull_coroutine< void >::operator!() const noexcept {
  179. return nullptr == cb_ || ! cb_->valid();
  180. }
  181. }}}
  182. #ifdef BOOST_HAS_ABI_HEADERS
  183. # include BOOST_ABI_SUFFIX
  184. #endif
  185. #endif // BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_IPP