123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- #ifndef BOOST_COROUTINES2_DETAIL_PUSH_COROUTINE_HPP
- #define BOOST_COROUTINES2_DETAIL_PUSH_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 push_coroutine {
- private:
- template< typename X >
- friend class pull_coroutine;
- struct control_block;
- control_block * cb_;
- explicit push_coroutine( control_block *) noexcept;
- public:
- template< typename Fn,
- typename = detail::disable_overload< push_coroutine, Fn >
- >
- explicit push_coroutine( Fn &&);
- template< typename StackAllocator, typename Fn >
- push_coroutine( StackAllocator &&, Fn &&);
- ~push_coroutine();
- push_coroutine( push_coroutine const&) = delete;
- push_coroutine & operator=( push_coroutine const&) = delete;
- push_coroutine( push_coroutine &&) noexcept;
- push_coroutine & operator=( push_coroutine && other) noexcept {
- if ( this == & other) return * this;
- std::swap( cb_, other.cb_);
- return * this;
- }
- push_coroutine & operator()( T const&);
- push_coroutine & operator()( T &&);
- explicit operator bool() const noexcept;
- bool operator!() const noexcept;
- class iterator {
- private:
- push_coroutine< T > * c_{ nullptr };
- public:
- typedef std::output_iterator_tag iterator_category;
- typedef void value_type;
- typedef void difference_type;
- typedef void pointer;
- typedef void reference;
- iterator() noexcept = default;
- explicit iterator( push_coroutine< T > * c) noexcept :
- c_{ c } {
- }
- iterator & operator=( T t) {
- BOOST_ASSERT( nullptr != c_);
- if ( ! ( * c_)( t) ) {
- c_ = nullptr;
- }
- return * this;
- }
- bool operator==( iterator const& other) const noexcept {
- return other.c_ == c_;
- }
- bool operator!=( iterator const& other) const noexcept {
- return other.c_ != c_;
- }
- iterator & operator*() noexcept {
- return * this;
- }
- iterator & operator++() noexcept {
- return * this;
- }
- };
- };
- template< typename T >
- class push_coroutine< T & > {
- private:
- template< typename X >
- friend class pull_coroutine;
- struct control_block;
- control_block * cb_;
- explicit push_coroutine( control_block *) noexcept;
- public:
- template< typename Fn,
- typename = detail::disable_overload< push_coroutine, Fn >
- >
- explicit push_coroutine( Fn &&);
- template< typename StackAllocator, typename Fn >
- push_coroutine( StackAllocator &&, Fn &&);
- ~push_coroutine();
- push_coroutine( push_coroutine const&) = delete;
- push_coroutine & operator=( push_coroutine const&) = delete;
- push_coroutine( push_coroutine &&) noexcept;
- push_coroutine & operator=( push_coroutine && other) noexcept {
- if ( this == & other) return * this;
- std::swap( cb_, other.cb_);
- return * this;
- }
- push_coroutine & operator()( T &);
- explicit operator bool() const noexcept;
- bool operator!() const noexcept;
- class iterator {
- private:
- push_coroutine< T & > * c_{ nullptr };
- public:
- typedef std::output_iterator_tag iterator_category;
- typedef void value_type;
- typedef void difference_type;
- typedef void pointer;
- typedef void reference;
- iterator() noexcept = default;
- explicit iterator( push_coroutine< T & > * c) noexcept :
- c_{ c } {
- }
- iterator & operator=( T & t) {
- BOOST_ASSERT( nullptr != c_);
- if ( ! ( * c_)( t) ) {
- c_ = nullptr;
- }
- return * this;
- }
- bool operator==( iterator const& other) const noexcept {
- return other.c_ == c_;
- }
- bool operator!=( iterator const& other) const noexcept {
- return other.c_ != c_;
- }
- iterator & operator*() noexcept {
- return * this;
- }
- iterator & operator++() noexcept {
- return * this;
- }
- };
- };
- template<>
- class push_coroutine< void > {
- private:
- template< typename X >
- friend class pull_coroutine;
- struct control_block;
- control_block * cb_;
- explicit push_coroutine( control_block *) noexcept;
- public:
- template< typename Fn,
- typename = detail::disable_overload< push_coroutine, Fn >
- >
- explicit push_coroutine( Fn &&);
- template< typename StackAllocator, typename Fn >
- push_coroutine( StackAllocator &&, Fn &&);
- ~push_coroutine();
- push_coroutine( push_coroutine const&) = delete;
- push_coroutine & operator=( push_coroutine const&) = delete;
- push_coroutine( push_coroutine &&) noexcept;
- push_coroutine & operator=( push_coroutine && other) noexcept {
- if ( this == & other) return * this;
- std::swap( cb_, other.cb_);
- return * this;
- }
- push_coroutine & operator()();
- explicit operator bool() const noexcept;
- bool operator!() const noexcept;
- };
- template< typename T >
- typename push_coroutine< T >::iterator
- begin( push_coroutine< T > & c) {
- return typename push_coroutine< T >::iterator( & c);
- }
- template< typename T >
- typename push_coroutine< T >::iterator
- end( push_coroutine< T > &) {
- return typename push_coroutine< T >::iterator();
- }
- }}}
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_SUFFIX
- #endif
- #endif
|