38#ifndef __BASE_COROUTINE_HH__
39#define __BASE_COROUTINE_HH__
62template <
typename Arg,
typename Ret>
71 std::is_same_v<Arg, void>,
Empty, std::stack<Arg>>;
74 std::is_same_v<Ret, void>,
Empty, std::stack<Ret>>;
100 template <
typename T = Ret>
103 !std::is_same_v<T, void>, T> param)
118 template <
typename T = Ret>
119 typename std::enable_if_t<std::is_same_v<T, void>,
139 template <
typename T = Arg>
140 typename std::enable_if_t<!std::is_same_v<T, void>, T>
144 while (args_channel.empty()) {
148 auto ret = args_channel.top();
210 template <
typename T = Arg>
212 operator()(
typename std::enable_if_t<!std::is_same_v<T, void>, T> param)
227 template <
typename T = Arg>
228 typename std::enable_if_t<std::is_same_v<T, void>,
Coroutine> &
247 template <
typename T = Ret>
248 typename std::enable_if_t<!std::is_same_v<T, void>, T>
252 while (ret_channel.empty()) {
256 auto ret = ret_channel.top();
266 operator bool()
const {
return !this->
finished(); }
CallerType: A reference to an object of this class will be passed to the coroutine task.
CallerType(Coroutine &_coro)
This template defines a Coroutine wrapper type with a Boost-like interface.
void main() override
Overriding base (Fiber) main.
std::function< void(CallerType &)> task
Coroutine task.
typename std::conditional_t< std::is_same_v< Ret, void >, Empty, std::stack< Ret > > RetChannel
CallerType caller
Coroutine caller.
ArgChannel argsChannel
Arguments for the coroutine.
typename std::conditional_t< std::is_same_v< Arg, void >, Empty, std::stack< Arg > > ArgChannel
This class represents a fiber, which is a light weight sort of thread which is cooperatively schedule...
std::enable_if_t<!std::is_same_v< T, void >, T > get()
get() is the way we can extrapolate return values (yielded) from the coroutine.
Coroutine & operator=(const Coroutine &rhs)=delete
std::enable_if_t< std::is_same_v< T, void >, CallerType > & operator()()
operator() is the way we can jump outside the coroutine
CallerType & operator()(typename std::enable_if_t< !std::is_same_v< T, void >, T > param)
operator() is the way we can jump outside the coroutine and return a value to the caller.
std::enable_if_t<!std::is_same_v< T, void >, T > get()
get() is the way we can extrapolate arguments from the coroutine caller.
Coroutine & operator()(typename std::enable_if_t<!std::is_same_v< T, void >, T > param)
Coroutine interface.
std::enable_if_t< std::is_same_v< T, void >, Coroutine > & operator()()
operator() is the way we can jump inside the coroutine.
Coroutine(const Coroutine &rhs)=delete
Coroutine(std::function< void(CallerType &)> f, bool run_coroutine=true)
Coroutine constructor.
static Fiber * currentFiber()
Get a pointer to the current running Fiber.
bool finished() const
Returns whether the "main" function of this fiber has finished.
void run()
Start executing the fiber represented by this object.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.