123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- // Copyright Oliver Kowalke 2014.
- // Distributed under the Boost Software License, Version 1.0.
- // (See accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- #ifndef BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_HPP
- #define BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_HPP
- #include <iterator>
- #include <type_traits>
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/coroutine2/detail/config.hpp>
- #include <boost/coroutine2/detail/disable_overload.hpp>
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_PREFIX
- #endif
- namespace boost {
- namespace coroutines2 {
- namespace detail {
- template< typename T >
- class pull_coroutine {
- private:
- template< typename X >
- friend class push_coroutine;
- struct control_block;
- control_block * cb_;
- explicit pull_coroutine( control_block *) noexcept;
- bool has_result_() const noexcept;
- public:
- template< typename Fn,
- typename = detail::disable_overload< pull_coroutine, Fn >
- >
- explicit pull_coroutine( Fn &&);
- template< typename StackAllocator, typename Fn >
- pull_coroutine( StackAllocator &&, Fn &&);
- ~pull_coroutine();
- pull_coroutine( pull_coroutine const&) = delete;
- pull_coroutine & operator=( pull_coroutine const&) = delete;
- pull_coroutine( pull_coroutine &&) noexcept;
- pull_coroutine & operator=( pull_coroutine && other) noexcept {
- std::swap( cb_, other.cb_);
- return * this;
- }
- pull_coroutine & operator()();
- explicit operator bool() const noexcept;
- bool operator!() const noexcept;
- T get() noexcept;
- class iterator {
- private:
- pull_coroutine< T > * c_{ nullptr };
- void fetch_() noexcept {
- BOOST_ASSERT( nullptr != c_);
- if ( ! ( * c_) ) {
- c_ = nullptr;
- return;
- }
- }
- void increment_() {
- BOOST_ASSERT( nullptr != c_);
- BOOST_ASSERT( * c_);
- ( * c_)();
- fetch_();
- }
- public:
- typedef std::input_iterator_tag iterator_category;
- typedef typename std::remove_reference< T >::type value_type;
- typedef std::ptrdiff_t difference_type;
- typedef value_type * pointer;
- typedef value_type & reference;
- typedef pointer pointer_t;
- typedef reference reference_t;
- iterator() noexcept = default;
- explicit iterator( pull_coroutine< T > * c) noexcept :
- c_{ c } {
- fetch_();
- }
- bool operator==( iterator const& other) const noexcept {
- return other.c_ == c_;
- }
- bool operator!=( iterator const& other) const noexcept {
- return other.c_ != c_;
- }
- iterator & operator++() {
- increment_();
- return * this;
- }
- void operator++( int) {
- increment_();
- }
- reference_t operator*() const noexcept {
- return c_->cb_->get();
- }
- pointer_t operator->() const noexcept {
- return std::addressof( c_->cb_->get() );
- }
- };
- friend class iterator;
- iterator begin() { return iterator (this); }
- iterator end() { return iterator(); }
- };
- template< typename T >
- class pull_coroutine< T & > {
- private:
- template< typename X >
- friend class push_coroutine;
- struct control_block;
- control_block * cb_;
- explicit pull_coroutine( control_block *) noexcept;
- bool has_result_() const noexcept;
- public:
- template< typename Fn,
- typename = detail::disable_overload< pull_coroutine, Fn >
- >
- explicit pull_coroutine( Fn &&);
- template< typename StackAllocator, typename Fn >
- pull_coroutine( StackAllocator &&, Fn &&);
- ~pull_coroutine();
- pull_coroutine( pull_coroutine const&) = delete;
- pull_coroutine & operator=( pull_coroutine const&) = delete;
- pull_coroutine( pull_coroutine &&) noexcept;
- pull_coroutine & operator=( pull_coroutine && other) noexcept {
- std::swap( cb_, other.cb_);
- return * this;
- }
- pull_coroutine & operator()();
- explicit operator bool() const noexcept;
- bool operator!() const noexcept;
- T & get() noexcept;
- class iterator {
- private:
- pull_coroutine< T & > * c_{ nullptr };
- void fetch_() noexcept {
- BOOST_ASSERT( nullptr != c_);
- if ( ! ( * c_) ) {
- c_ = nullptr;
- return;
- }
- }
- void increment_() {
- BOOST_ASSERT( nullptr != c_);
- BOOST_ASSERT( * c_);
- ( * c_)();
- fetch_();
- }
- public:
- typedef std::input_iterator_tag iterator_category;
- typedef typename std::remove_reference< T >::type value_type;
- typedef std::ptrdiff_t difference_type;
- typedef value_type * pointer;
- typedef value_type & reference;
- typedef pointer pointer_t;
- typedef reference reference_t;
- iterator() noexcept = default;
- explicit iterator( pull_coroutine< T & > * c) noexcept :
- c_{ c } {
- fetch_();
- }
- bool operator==( iterator const& other) const noexcept {
- return other.c_ == c_;
- }
- bool operator!=( iterator const& other) const noexcept {
- return other.c_ != c_;
- }
- iterator & operator++() {
- increment_();
- return * this;
- }
- void operator++( int) {
- increment_();
- }
- reference_t operator*() const noexcept {
- return c_->cb_->get();
- }
- pointer_t operator->() const noexcept {
- return std::addressof( c_->cb_->get() );
- }
- };
- friend class iterator;
- iterator begin() { return iterator (this); }
- iterator end() { return iterator(); }
- };
- template<>
- class pull_coroutine< void > {
- private:
- template< typename X >
- friend class push_coroutine;
- struct control_block;
- control_block * cb_;
- explicit pull_coroutine( control_block *) noexcept;
- public:
- template< typename Fn,
- typename = detail::disable_overload< pull_coroutine, Fn >
- >
- explicit pull_coroutine( Fn &&);
- template< typename StackAllocator, typename Fn >
- pull_coroutine( StackAllocator &&, Fn &&);
- ~pull_coroutine();
- pull_coroutine( pull_coroutine const&) = delete;
- pull_coroutine & operator=( pull_coroutine const&) = delete;
- pull_coroutine( pull_coroutine &&) noexcept;
- pull_coroutine & operator=( pull_coroutine && other) noexcept {
- std::swap( cb_, other.cb_);
- return * this;
- }
- pull_coroutine & operator()();
- explicit operator bool() const noexcept;
- bool operator!() const noexcept;
- };
- template< typename T >
- typename pull_coroutine< T >::iterator
- begin( pull_coroutine< T > & c) {
- return typename pull_coroutine< T >::iterator( & c);
- }
- template< typename T >
- typename pull_coroutine< T >::iterator
- end( pull_coroutine< T > &) {
- return typename pull_coroutine< T >::iterator();
- }
- }}}
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_SUFFIX
- #endif
- #endif // BOOST_COROUTINES2_DETAIL_PULL_COROUTINE_HPP
|