| 1 | |
| 2 | // Copyright Oliver Kowalke 2014. |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #ifndef BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_IPP |
| 8 | #define BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_IPP |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <utility> |
| 12 | |
| 13 | #include <boost/assert.hpp> |
| 14 | #include <boost/config.hpp> |
| 15 | |
| 16 | #include <boost/coroutine2/detail/config.hpp> |
| 17 | #include <boost/coroutine2/detail/create_control_block.ipp> |
| 18 | #include <boost/coroutine2/detail/disable_overload.hpp> |
| 19 | #include <boost/coroutine2/fixedsize_stack.hpp> |
| 20 | #include <boost/coroutine2/segmented_stack.hpp> |
| 21 | |
| 22 | #ifdef BOOST_HAS_ABI_HEADERS |
| 23 | # include BOOST_ABI_PREFIX |
| 24 | #endif |
| 25 | |
| 26 | namespace boost { |
| 27 | namespace coroutines2 { |
| 28 | namespace detail { |
| 29 | |
| 30 | // pull_coroutine< T > |
| 31 | |
| 32 | template< typename T > |
| 33 | pull_coroutine< T >::pull_coroutine( control_block * cb) noexcept : |
| 34 | cb_{ cb } { |
| 35 | } |
| 36 | |
| 37 | template< typename T > |
| 38 | bool |
| 39 | pull_coroutine< T >::has_result_() const noexcept { |
| 40 | return nullptr != cb_->other->t; |
| 41 | } |
| 42 | |
| 43 | template< typename T > |
| 44 | template< typename Fn, |
| 45 | typename |
| 46 | > |
| 47 | pull_coroutine< T >::pull_coroutine( Fn && fn) : |
| 48 | pull_coroutine{ default_stack(), std::forward< Fn >( fn) } { |
| 49 | } |
| 50 | |
| 51 | template< typename T > |
| 52 | template< typename StackAllocator, typename Fn > |
| 53 | pull_coroutine< T >::pull_coroutine( StackAllocator && salloc, Fn && fn) : |
| 54 | cb_{ create_control_block< control_block >( std::forward< StackAllocator >( salloc), std::forward< Fn >( fn) ) } { |
| 55 | if ( ! cb_->valid() ) { |
| 56 | cb_->deallocate(); |
| 57 | cb_ = nullptr; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | template< typename T > |
| 62 | pull_coroutine< T >::~pull_coroutine() { |
| 63 | if ( nullptr != cb_) { |
| 64 | cb_->deallocate(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | template< typename T > |
| 69 | pull_coroutine< T >::pull_coroutine( pull_coroutine && other) noexcept : |
| 70 | cb_{ nullptr } { |
| 71 | std::swap( cb_, other.cb_); |
| 72 | } |
| 73 | |
| 74 | template< typename T > |
| 75 | pull_coroutine< T > & |
| 76 | pull_coroutine< T >::operator()() { |
| 77 | cb_->resume(); |
| 78 | return * this; |
| 79 | } |
| 80 | |
| 81 | template< typename T > |
| 82 | pull_coroutine< T >::operator bool() const noexcept { |
| 83 | return nullptr != cb_ && cb_->valid(); |
| 84 | } |
| 85 | |
| 86 | template< typename T > |
| 87 | bool |
| 88 | pull_coroutine< T >::operator!() const noexcept { |
| 89 | return nullptr == cb_ || ! cb_->valid(); |
| 90 | } |
| 91 | |
| 92 | template< typename T > |
| 93 | T |
| 94 | pull_coroutine< T >::get() noexcept { |
| 95 | return std::move( cb_->get() ); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | // pull_coroutine< T & > |
| 100 | |
| 101 | template< typename T > |
| 102 | pull_coroutine< T & >::pull_coroutine( control_block * cb) noexcept : |
| 103 | cb_{ cb } { |
| 104 | } |
| 105 | |
| 106 | template< typename T > |
| 107 | bool |
| 108 | pull_coroutine< T & >::has_result_() const noexcept { |
| 109 | return nullptr != cb_->other->t; |
| 110 | } |
| 111 | |
| 112 | template< typename T > |
| 113 | template< typename Fn, |
| 114 | typename |
| 115 | > |
| 116 | pull_coroutine< T & >::pull_coroutine( Fn && fn) : |
| 117 | pull_coroutine{ default_stack(), std::forward< Fn >( fn) } { |
| 118 | } |
| 119 | |
| 120 | template< typename T > |
| 121 | template< typename StackAllocator, typename Fn > |
| 122 | pull_coroutine< T & >::pull_coroutine( StackAllocator && salloc, Fn && fn) : |
| 123 | cb_{ create_control_block< control_block >( std::forward< StackAllocator >( salloc), std::forward< Fn >( fn) ) } { |
| 124 | if ( ! cb_->valid() ) { |
| 125 | cb_->deallocate(); |
| 126 | cb_ = nullptr; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | template< typename T > |
| 131 | pull_coroutine< T & >::~pull_coroutine() { |
| 132 | if ( nullptr != cb_) { |
| 133 | cb_->deallocate(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | template< typename T > |
| 138 | pull_coroutine< T & >::pull_coroutine( pull_coroutine && other) noexcept : |
| 139 | cb_{ nullptr } { |
| 140 | std::swap( cb_, other.cb_); |
| 141 | } |
| 142 | |
| 143 | template< typename T > |
| 144 | pull_coroutine< T & > & |
| 145 | pull_coroutine< T & >::operator()() { |
| 146 | cb_->resume(); |
| 147 | return * this; |
| 148 | } |
| 149 | |
| 150 | template< typename T > |
| 151 | pull_coroutine< T & >::operator bool() const noexcept { |
| 152 | return nullptr != cb_ && cb_->valid(); |
| 153 | } |
| 154 | |
| 155 | template< typename T > |
| 156 | bool |
| 157 | pull_coroutine< T & >::operator!() const noexcept { |
| 158 | return nullptr == cb_ || ! cb_->valid(); |
| 159 | } |
| 160 | |
| 161 | template< typename T > |
| 162 | T & |
| 163 | pull_coroutine< T & >::get() noexcept { |
| 164 | return cb_->get(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | // pull_coroutine< void > |
| 169 | |
| 170 | inline |
| 171 | pull_coroutine< void >::pull_coroutine( control_block * cb) noexcept : |
| 172 | cb_{ cb } { |
| 173 | } |
| 174 | |
| 175 | template< typename Fn, |
| 176 | typename |
| 177 | > |
| 178 | pull_coroutine< void >::pull_coroutine( Fn && fn) : |
| 179 | pull_coroutine{ default_stack(), std::forward< Fn >( fn) } { |
| 180 | } |
| 181 | |
| 182 | template< typename StackAllocator, typename Fn > |
| 183 | pull_coroutine< void >::pull_coroutine( StackAllocator && salloc, Fn && fn) : |
| 184 | cb_{ create_control_block< control_block >( std::forward< StackAllocator >( salloc), std::forward< Fn >( fn) ) } { |
| 185 | if ( ! cb_->valid() ) { |
| 186 | cb_->deallocate(); |
| 187 | cb_ = nullptr; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | inline |
| 192 | pull_coroutine< void >::~pull_coroutine() { |
| 193 | if ( nullptr != cb_) { |
| 194 | cb_->deallocate(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | inline |
| 199 | pull_coroutine< void >::pull_coroutine( pull_coroutine && other) noexcept : |
| 200 | cb_{ nullptr } { |
| 201 | std::swap( a&: cb_, b&: other.cb_); |
| 202 | } |
| 203 | |
| 204 | inline |
| 205 | pull_coroutine< void > & |
| 206 | pull_coroutine< void >::operator()() { |
| 207 | cb_->resume(); |
| 208 | return * this; |
| 209 | } |
| 210 | |
| 211 | inline |
| 212 | pull_coroutine< void >::operator bool() const noexcept { |
| 213 | return nullptr != cb_ && cb_->valid(); |
| 214 | } |
| 215 | |
| 216 | inline |
| 217 | bool |
| 218 | pull_coroutine< void >::operator!() const noexcept { |
| 219 | return nullptr == cb_ || ! cb_->valid(); |
| 220 | } |
| 221 | |
| 222 | }}} |
| 223 | |
| 224 | #ifdef BOOST_HAS_ABI_HEADERS |
| 225 | # include BOOST_ABI_SUFFIX |
| 226 | #endif |
| 227 | |
| 228 | #endif // BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_IPP |
| 229 | |