7 #ifndef __MERCURY_COROUTINE_ENGINE_COROUTINE__
8 #define __MERCURY_COROUTINE_ENGINE_COROUTINE__
15 #include <boost/coroutine2/all.hpp>
30 coroutine*& tl_this_coroutine();
50 template <
typename Callable,
typename... As>
51 static std::unique_ptr<coroutine>
make(Callable&& cb, As&&... as)
53 return std::unique_ptr<coroutine>(
56 std::forward<Callable>(cb),
57 std::forward<As>(as)...)));
61 template <
typename THUNK>
64 co_(co_t::pull_type(wrapper_functor{ yield_, std::forward<THUNK>(th) }))
68 template <
typename StackAllocator,
typename THUNK>
71 co_(std::forward<StackAllocator>(sa),
72 co_t::pull_type(wrapper_functor{ yield_, std::forward<THUNK>(th) }))
80 yield_(std::move(rhs.yield_)),
81 co_(std::move(rhs.co_))
90 yield_ = std::move(rhs.yield_);
91 co_ = std::move(rhs.co_);
109 auto& tl_co = detail::tl_this_coroutine();
112 auto parent_co = tl_co;
123 std::rethrow_exception(std::current_exception());
145 return !(co_.operator bool());
149 typedef boost::coroutines2::coroutine<void> co_t;
151 struct wrapper_functor
153 inline void operator()(coroutine::co_t::push_type&
yield)
160 co_t::push_type*& yield_;
164 co_t::push_type* yield_;
171 return detail::tl_this_coroutine() ? true :
false;
180 return detail::tl_this_coroutine();
188 coroutine* c = detail::tl_this_coroutine();
189 if(c) { c->
yield(); }
void yield()
Definition: coroutine.hpp:186
coroutine * this_coroutine()
Definition: coroutine.hpp:178
bool in_coroutine()
Returns true if executing in a coroutine, else false.
Definition: coroutine.hpp:169
std::function< void()> thunk
thunk type definition. Also known as a nullary function
Definition: function_utility.hpp:72
Definition: coroutine.hpp:43
void yield()
Pause execution and return to run() caller.
Definition: coroutine.hpp:137
virtual void run()
Execute until thunk completes or yield() is called.
Definition: coroutine.hpp:105
static std::unique_ptr< coroutine > make(Callable &&cb, As &&... as)
construct an allocated coroutine from a Callable and arguments
Definition: coroutine.hpp:51
coroutine(coroutine &&rhs) noexcept
Move Constructor.
Definition: coroutine.hpp:79
coroutine(THUNK &&th)
construct a coroutine from a thunk
Definition: coroutine.hpp:62
bool complete()
Returns true if thunk is complete, else false since the coroutine yielded early.
Definition: coroutine.hpp:143
coroutine(StackAllocator &&sa, THUNK &&th)
construct a coroutine from a stack allocator and thunk
Definition: coroutine.hpp:69
coroutine & operator=(coroutine &&rhs) noexcept
Move Assignment.
Definition: coroutine.hpp:88