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

source code of boost/libs/coroutine2/include/boost/coroutine2/detail/push_coroutine.ipp