2017-03-04 17:25:09 +00:00
|
|
|
/* Coroutines for OctaSTD.
|
|
|
|
*
|
|
|
|
* This file is part of OctaSTD. See COPYING.md for further information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSTD_COROUTINE_HH
|
|
|
|
#define OSTD_COROUTINE_HH
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <utility>
|
|
|
|
#include <tuple>
|
2017-03-05 22:48:51 +00:00
|
|
|
#include <type_traits>
|
2017-03-05 23:47:15 +00:00
|
|
|
#include <optional>
|
2017-03-04 17:25:09 +00:00
|
|
|
|
|
|
|
#include "ostd/types.hh"
|
2017-03-05 23:47:15 +00:00
|
|
|
#include "ostd/range.hh"
|
2017-03-05 15:48:44 +00:00
|
|
|
|
2017-03-07 20:29:12 +00:00
|
|
|
#include "ostd/internal/context.hh"
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
namespace ostd {
|
|
|
|
|
|
|
|
struct coroutine_error: std::runtime_error {
|
|
|
|
using std::runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
2017-03-07 01:18:22 +00:00
|
|
|
template<typename T>
|
|
|
|
struct coroutine;
|
|
|
|
|
2017-03-05 15:48:44 +00:00
|
|
|
namespace detail {
|
2017-03-05 22:48:51 +00:00
|
|
|
/* like reference_wrapper but for any value */
|
|
|
|
template<typename T>
|
|
|
|
struct arg_wrapper {
|
|
|
|
arg_wrapper() = default;
|
|
|
|
arg_wrapper(T arg): p_arg(std::move(arg)) {}
|
|
|
|
|
|
|
|
void operator=(T arg) {
|
|
|
|
p_arg = std::move(arg);
|
|
|
|
}
|
|
|
|
operator T &&() {
|
|
|
|
return std::move(p_arg);
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(arg_wrapper &other) {
|
2017-03-07 16:33:56 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(p_arg, other.p_arg);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
private:
|
|
|
|
T p_arg = T{};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct arg_wrapper<T &&> {
|
|
|
|
arg_wrapper() = default;
|
|
|
|
arg_wrapper(T &&arg): p_arg(&arg) {}
|
|
|
|
|
|
|
|
void operator=(T &&arg) {
|
|
|
|
p_arg = &arg;
|
|
|
|
}
|
|
|
|
operator T &&() {
|
|
|
|
return *p_arg;
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(arg_wrapper &other) {
|
2017-03-07 16:33:56 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(p_arg, other.p_arg);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
private:
|
|
|
|
T *p_arg = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct arg_wrapper<T &> {
|
|
|
|
arg_wrapper() = default;
|
|
|
|
arg_wrapper(T &arg): p_arg(&arg) {}
|
|
|
|
|
|
|
|
void operator=(T &arg) {
|
|
|
|
p_arg = &arg;
|
|
|
|
}
|
|
|
|
operator T &() {
|
|
|
|
return *p_arg;
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(arg_wrapper &other) {
|
2017-03-07 16:33:56 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(p_arg, other.p_arg);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
private:
|
|
|
|
T *p_arg = nullptr;
|
|
|
|
};
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
template<typename T>
|
|
|
|
inline void swap(arg_wrapper<T> &a, arg_wrapper<T> &b) {
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2017-03-05 18:29:44 +00:00
|
|
|
template<typename ...A>
|
|
|
|
struct coro_types {
|
2017-03-05 22:48:51 +00:00
|
|
|
using yield_type = std::tuple<A...>;
|
2017-03-11 02:53:41 +00:00
|
|
|
|
|
|
|
static yield_type get(std::tuple<arg_wrapper<A>...> &args) {
|
|
|
|
return std::move(args);
|
|
|
|
}
|
2017-03-05 18:29:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename A>
|
|
|
|
struct coro_types<A> {
|
2017-03-05 22:48:51 +00:00
|
|
|
using yield_type = A;
|
2017-03-11 02:53:41 +00:00
|
|
|
|
|
|
|
static yield_type get(std::tuple<arg_wrapper<A>> &args) {
|
|
|
|
return std::forward<A>(std::get<0>(args));
|
|
|
|
}
|
2017-03-05 18:29:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
|
|
|
struct coro_types<A, B> {
|
2017-03-05 22:48:51 +00:00
|
|
|
using yield_type = std::pair<A, B>;
|
2017-03-11 02:53:41 +00:00
|
|
|
|
|
|
|
static yield_type get(std::tuple<arg_wrapper<A>, arg_wrapper<B>> &args) {
|
|
|
|
return std::make_pair(
|
|
|
|
std::forward<A>(std::get<0>(args)),
|
|
|
|
std::forward<B>(std::get<1>(args))
|
|
|
|
);
|
|
|
|
}
|
2017-03-05 18:29:44 +00:00
|
|
|
};
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<>
|
|
|
|
struct coro_types<> {
|
|
|
|
using yield_type = void;
|
|
|
|
};
|
|
|
|
|
2017-03-05 18:29:44 +00:00
|
|
|
template<typename ...A>
|
|
|
|
using coro_args = typename coro_types<A...>::yield_type;
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<typename R, typename ...A>
|
|
|
|
struct coro_yielder;
|
2017-03-05 18:29:44 +00:00
|
|
|
|
2017-03-05 19:11:39 +00:00
|
|
|
/* default case, yield returns args and takes a value */
|
2017-03-04 17:25:09 +00:00
|
|
|
template<typename R, typename ...A>
|
2017-03-07 01:18:22 +00:00
|
|
|
struct coro_base: coroutine_context {
|
2017-03-06 17:37:33 +00:00
|
|
|
protected:
|
2017-03-11 01:12:43 +00:00
|
|
|
friend struct coro_yielder<R, A...>;
|
|
|
|
|
|
|
|
template<size_t ...I>
|
|
|
|
coro_args<A...> get_args(std::index_sequence<I...>) {
|
2017-03-11 02:53:41 +00:00
|
|
|
return coro_types<A...>::get(p_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_args(A ...args) {
|
|
|
|
p_args = std::make_tuple(arg_wrapper<A>(std::forward<A>(args))...);
|
|
|
|
}
|
|
|
|
|
|
|
|
R get_result() {
|
|
|
|
return std::forward<R>(p_result);
|
2017-03-11 01:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Y, typename F, size_t ...I>
|
|
|
|
void call_helper(Y &&yielder, F &func, std::index_sequence<I...>) {
|
|
|
|
p_result = std::forward<R>(func(
|
|
|
|
std::forward<Y>(yielder),
|
|
|
|
std::forward<A>(std::get<I>(p_args))...
|
|
|
|
));
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(coro_base &other) {
|
2017-03-07 16:33:56 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(p_args, other.p_args);
|
|
|
|
swap(p_result, other.p_result);
|
2017-03-07 01:18:22 +00:00
|
|
|
coroutine_context::swap(other);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
std::tuple<arg_wrapper<A>...> p_args;
|
|
|
|
arg_wrapper<R> p_result;
|
2017-03-04 17:25:09 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 19:11:39 +00:00
|
|
|
/* yield takes a value but doesn't return any args */
|
|
|
|
template<typename R>
|
2017-03-07 01:18:22 +00:00
|
|
|
struct coro_base<R>: coroutine_context {
|
2017-03-06 17:37:33 +00:00
|
|
|
protected:
|
2017-03-11 01:12:43 +00:00
|
|
|
friend struct coro_yielder<R>;
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<size_t ...I>
|
|
|
|
void get_args(std::index_sequence<I...>) {}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-11 02:53:41 +00:00
|
|
|
void set_args() {}
|
|
|
|
|
|
|
|
R get_result() {
|
|
|
|
return std::forward<R>(p_result);
|
|
|
|
}
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<typename Y, typename F, size_t ...I>
|
|
|
|
void call_helper(Y &&yielder, F &func, std::index_sequence<I...>) {
|
|
|
|
p_result = std::forward<R>(func(std::forward<Y>(yielder)));
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(coro_base &other) {
|
2017-03-07 16:33:56 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(p_result, other.p_result);
|
2017-03-07 01:18:22 +00:00
|
|
|
coroutine_context::swap(other);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
arg_wrapper<R> p_result;
|
2017-03-05 19:11:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* yield doesn't take a value and returns args */
|
2017-03-04 17:25:09 +00:00
|
|
|
template<typename ...A>
|
2017-03-07 01:18:22 +00:00
|
|
|
struct coro_base<void, A...>: coroutine_context {
|
2017-03-06 17:37:33 +00:00
|
|
|
protected:
|
2017-03-11 01:12:43 +00:00
|
|
|
friend struct coro_yielder<void, A...>;
|
|
|
|
|
|
|
|
template<size_t ...I>
|
|
|
|
coro_args<A...> get_args(std::index_sequence<I...>) {
|
2017-03-11 02:53:41 +00:00
|
|
|
return coro_types<A...>::get(p_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_args(A ...args) {
|
|
|
|
p_args = std::make_tuple(arg_wrapper<A>(std::forward<A>(args))...);
|
2017-03-11 01:12:43 +00:00
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-11 02:53:41 +00:00
|
|
|
void get_result() {}
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<typename Y, typename F, size_t ...I>
|
|
|
|
void call_helper(Y &&yielder, F &func, std::index_sequence<I...>) {
|
|
|
|
func(
|
|
|
|
std::forward<Y>(yielder),
|
|
|
|
std::forward<A>(std::get<I>(p_args))...
|
|
|
|
);
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(coro_base &other) {
|
2017-03-07 16:33:56 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(p_args, other.p_args);
|
2017-03-07 01:18:22 +00:00
|
|
|
coroutine_context::swap(other);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 22:48:51 +00:00
|
|
|
std::tuple<arg_wrapper<A>...> p_args;
|
2017-03-04 17:25:09 +00:00
|
|
|
};
|
2017-03-05 19:11:39 +00:00
|
|
|
|
|
|
|
/* yield doesn't take a value or return any args */
|
|
|
|
template<>
|
2017-03-07 01:18:22 +00:00
|
|
|
struct coro_base<void>: coroutine_context {
|
2017-03-06 17:37:33 +00:00
|
|
|
protected:
|
2017-03-11 01:12:43 +00:00
|
|
|
friend struct coro_yielder<void>;
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<size_t ...I>
|
|
|
|
void get_args(std::index_sequence<I...>) {}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-11 02:53:41 +00:00
|
|
|
void set_args() {}
|
|
|
|
|
|
|
|
void get_result() {}
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<typename Y, typename F, size_t ...I>
|
|
|
|
void call_helper(Y &&yielder, F &func, std::index_sequence<I...>) {
|
|
|
|
func(std::forward<Y>(yielder));
|
2017-03-05 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
void swap(coro_base &other) {
|
2017-03-07 01:18:22 +00:00
|
|
|
coroutine_context::swap(other);
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
2017-03-05 19:11:39 +00:00
|
|
|
};
|
2017-03-11 01:12:43 +00:00
|
|
|
|
|
|
|
template<typename R, typename ...A>
|
|
|
|
struct coro_yielder {
|
|
|
|
coro_yielder(coro_base<R, A...> &coro): p_coro(coro) {}
|
|
|
|
|
|
|
|
coro_args<A...> operator()(R &&ret) {
|
|
|
|
p_coro.p_result = std::forward<R>(ret);
|
|
|
|
p_coro.yield_jump();
|
|
|
|
return p_coro.get_args(std::make_index_sequence<sizeof...(A)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
coro_args<A...> operator()(R const &ret) {
|
|
|
|
p_coro.p_result = ret;
|
|
|
|
p_coro.yield_jump();
|
|
|
|
return p_coro.get_args(std::make_index_sequence<sizeof...(A)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
coro_base<R, A...> &p_coro;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename R, typename ...A>
|
|
|
|
struct coro_yielder<R &, A...> {
|
|
|
|
coro_yielder(coro_base<R &, A...> &coro): p_coro(coro) {}
|
|
|
|
|
|
|
|
coro_args<A...> operator()(R &ret) {
|
|
|
|
p_coro.p_result = ret;
|
|
|
|
p_coro.yield_jump();
|
|
|
|
return p_coro.get_args(std::make_index_sequence<sizeof...(A)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
coro_base<R &, A...> &p_coro;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename R, typename ...A>
|
|
|
|
struct coro_yielder<R &&, A...> {
|
|
|
|
coro_yielder(coro_base<R &&, A...> &coro): p_coro(coro) {}
|
|
|
|
|
|
|
|
coro_args<A...> operator()(R &&ret) {
|
|
|
|
p_coro.p_result = std::forward<R>(ret);
|
|
|
|
p_coro.yield_jump();
|
|
|
|
return p_coro.get_args(std::make_index_sequence<sizeof...(A)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
coro_base<R &&, A...> &p_coro;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ...A>
|
|
|
|
struct coro_yielder<void, A...> {
|
|
|
|
coro_yielder(coro_base<void, A...> &coro): p_coro(coro) {}
|
|
|
|
|
|
|
|
coro_args<A...> operator()() {
|
|
|
|
p_coro.yield_jump();
|
|
|
|
return p_coro.get_args(std::make_index_sequence<sizeof...(A)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
coro_base<void, A...> &p_coro;
|
|
|
|
};
|
2017-03-04 17:25:09 +00:00
|
|
|
} /* namespace detail */
|
|
|
|
|
|
|
|
template<typename R, typename ...A>
|
|
|
|
struct coroutine<R(A...)>: detail::coro_base<R, A...> {
|
2017-03-06 17:37:33 +00:00
|
|
|
private:
|
|
|
|
using base_t = detail::coro_base<R, A...>;
|
|
|
|
|
|
|
|
public:
|
2017-03-11 01:12:43 +00:00
|
|
|
using yield_type = detail::coro_yielder<R, A...>;
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-07 16:33:56 +00:00
|
|
|
/* we have no way to assign a function anyway... */
|
|
|
|
coroutine() = delete;
|
|
|
|
|
2017-03-07 21:22:22 +00:00
|
|
|
/* 0 means default size decided by the stack allocator */
|
2017-03-08 01:16:24 +00:00
|
|
|
template<typename F, typename SA = default_stack>
|
2017-03-11 17:39:46 +00:00
|
|
|
coroutine(F func, SA sa = SA{}):
|
2017-03-08 01:06:56 +00:00
|
|
|
base_t(), p_func(std::move(func))
|
2017-03-07 01:18:22 +00:00
|
|
|
{
|
2017-03-07 16:33:56 +00:00
|
|
|
/* that way there is no context creation/stack allocation */
|
2017-03-07 01:18:22 +00:00
|
|
|
if (!p_func) {
|
2017-03-11 16:15:39 +00:00
|
|
|
this->set_dead();
|
2017-03-07 01:18:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-08 01:06:56 +00:00
|
|
|
this->make_context(sa, &context_call<SA>);
|
2017-03-07 01:18:22 +00:00
|
|
|
}
|
2017-03-04 17:25:09 +00:00
|
|
|
|
2017-03-11 16:15:39 +00:00
|
|
|
template<typename SA = default_stack>
|
|
|
|
coroutine(std::nullptr_t, SA = SA{0}):
|
|
|
|
base_t(), p_func(nullptr)
|
|
|
|
{
|
|
|
|
this->set_dead();
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
coroutine(coroutine const &) = delete;
|
2017-03-11 17:30:08 +00:00
|
|
|
coroutine(coroutine &&c) noexcept:
|
2017-03-11 16:15:39 +00:00
|
|
|
base_t(std::move(c)), p_func(std::move(c.p_func))
|
2017-03-07 00:12:52 +00:00
|
|
|
{
|
|
|
|
c.p_func = nullptr;
|
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
|
|
|
coroutine &operator=(coroutine const &) = delete;
|
2017-03-11 17:30:08 +00:00
|
|
|
coroutine &operator=(coroutine &&c) noexcept {
|
2017-03-07 00:12:52 +00:00
|
|
|
base_t::operator=(std::move(c));
|
|
|
|
p_func = std::move(c.p_func);
|
|
|
|
c.p_func = nullptr;
|
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-05 15:48:44 +00:00
|
|
|
~coroutine() {
|
2017-03-07 01:07:13 +00:00
|
|
|
this->unwind();
|
2017-03-05 15:48:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 17:30:08 +00:00
|
|
|
explicit operator bool() const noexcept {
|
2017-03-11 16:15:39 +00:00
|
|
|
return !this->is_dead();
|
2017-03-04 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
R resume(A ...args) {
|
2017-03-11 16:15:39 +00:00
|
|
|
if (this->is_dead()) {
|
2017-03-07 00:12:52 +00:00
|
|
|
throw coroutine_error{"dead coroutine"};
|
|
|
|
}
|
2017-03-11 02:53:41 +00:00
|
|
|
this->set_args(std::forward<A>(args)...);
|
|
|
|
detail::coroutine_context::call();
|
|
|
|
return this->get_result();
|
2017-03-06 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
R operator()(A ...args) {
|
2017-03-07 00:12:52 +00:00
|
|
|
return resume(std::forward<A>(args)...);
|
2017-03-04 17:25:09 +00:00
|
|
|
}
|
2017-03-06 17:37:33 +00:00
|
|
|
|
2017-03-11 17:30:08 +00:00
|
|
|
void swap(coroutine &other) noexcept {
|
2017-03-06 17:37:33 +00:00
|
|
|
std::swap(p_func, other.p_func);
|
|
|
|
base_t::swap(other);
|
|
|
|
}
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
private:
|
2017-03-07 16:33:56 +00:00
|
|
|
/* the main entry point of the coroutine */
|
2017-03-08 01:06:56 +00:00
|
|
|
template<typename SA>
|
2017-03-07 01:18:22 +00:00
|
|
|
static void context_call(detail::transfer_t t) {
|
|
|
|
auto &self = *(static_cast<coroutine *>(t.data));
|
|
|
|
self.p_orig = t.ctx;
|
2017-03-11 16:15:39 +00:00
|
|
|
if (self.is_hold()) {
|
|
|
|
/* we never got to execute properly, we're HOLD because we
|
|
|
|
* jumped here without setting the state to EXEC before that
|
|
|
|
*/
|
2017-03-08 00:06:15 +00:00
|
|
|
goto release;
|
|
|
|
}
|
2017-03-07 01:18:22 +00:00
|
|
|
try {
|
2017-03-11 01:12:43 +00:00
|
|
|
self.call_helper(
|
|
|
|
yield_type{self}, self.p_func, std::index_sequence_for<A...>{}
|
|
|
|
);
|
2017-03-07 16:33:56 +00:00
|
|
|
} catch (detail::coroutine_context::forced_unwind v) {
|
|
|
|
/* forced_unwind is unique */
|
2017-03-07 01:18:22 +00:00
|
|
|
self.p_orig = v.ctx;
|
|
|
|
} catch (...) {
|
2017-03-07 16:33:56 +00:00
|
|
|
/* some other exception, will be rethrown later */
|
2017-03-07 01:18:22 +00:00
|
|
|
self.p_except = std::current_exception();
|
|
|
|
}
|
2017-03-08 00:06:15 +00:00
|
|
|
/* switch back, release stack */
|
|
|
|
release:
|
2017-03-11 17:22:01 +00:00
|
|
|
self.template finish<SA>();
|
2017-03-04 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
std::function<R(yield_type, A...)> p_func;
|
2017-03-04 17:25:09 +00:00
|
|
|
};
|
|
|
|
|
2017-03-06 17:37:33 +00:00
|
|
|
template<typename R, typename ...A>
|
2017-03-11 17:30:08 +00:00
|
|
|
inline void swap(coroutine<R(A...)> &a, coroutine<R(A...)> &b) noexcept {
|
2017-03-06 17:37:33 +00:00
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<typename T> struct generator_range;
|
|
|
|
|
2017-03-11 13:16:32 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename T> struct generator_iterator;
|
|
|
|
}
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<typename T>
|
|
|
|
struct generator: detail::coroutine_context {
|
|
|
|
private:
|
2017-03-11 16:15:39 +00:00
|
|
|
using base_t = detail::coroutine_context;
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
struct yielder {
|
|
|
|
yielder(generator<T> &g): p_gen(g) {}
|
|
|
|
|
|
|
|
void operator()(T &&ret) {
|
|
|
|
p_gen.p_result = &ret;
|
|
|
|
p_gen.yield_jump();
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()(T &ret) {
|
|
|
|
p_gen.p_result = &ret;
|
|
|
|
p_gen.yield_jump();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
generator<T> &p_gen;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
using range = generator_range<T>;
|
|
|
|
|
|
|
|
using yield_type = yielder;
|
|
|
|
|
|
|
|
generator() = delete;
|
|
|
|
|
|
|
|
template<typename F, typename SA = default_stack>
|
2017-03-11 17:39:46 +00:00
|
|
|
generator(F func, SA sa = SA{}):
|
2017-03-11 16:15:39 +00:00
|
|
|
base_t(), p_func(std::move(func))
|
|
|
|
{
|
2017-03-11 01:12:43 +00:00
|
|
|
/* that way there is no context creation/stack allocation */
|
|
|
|
if (!p_func) {
|
2017-03-11 16:15:39 +00:00
|
|
|
this->set_dead();
|
2017-03-11 01:12:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->make_context(sa, &context_call<SA>);
|
2017-03-11 01:17:59 +00:00
|
|
|
/* generate an initial value */
|
|
|
|
resume();
|
2017-03-11 01:12:43 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 16:15:39 +00:00
|
|
|
template<typename SA = default_stack>
|
|
|
|
generator(std::nullptr_t, SA = SA{0}):
|
|
|
|
base_t(), p_func(nullptr)
|
|
|
|
{
|
|
|
|
this->set_dead();
|
|
|
|
}
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
generator(generator const &) = delete;
|
2017-03-11 17:30:08 +00:00
|
|
|
generator(generator &&c) noexcept:
|
2017-03-11 16:15:39 +00:00
|
|
|
base_t(std::move(c)), p_func(std::move(c.p_func)), p_result(c.p_result)
|
2017-03-11 01:12:43 +00:00
|
|
|
{
|
|
|
|
c.p_func = nullptr;
|
|
|
|
c.p_result = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
generator &operator=(generator const &) = delete;
|
2017-03-11 17:30:08 +00:00
|
|
|
generator &operator=(generator &&c) noexcept {
|
2017-03-11 16:15:39 +00:00
|
|
|
base_t::operator=(std::move(c));
|
2017-03-11 01:12:43 +00:00
|
|
|
p_func = std::move(c.p_func);
|
|
|
|
p_result = c.p_result;
|
|
|
|
c.p_func = nullptr;
|
|
|
|
c.p_result = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~generator() {
|
|
|
|
this->unwind();
|
|
|
|
}
|
|
|
|
|
2017-03-11 17:30:08 +00:00
|
|
|
explicit operator bool() const noexcept {
|
2017-03-11 16:15:39 +00:00
|
|
|
return !this->is_dead();
|
2017-03-11 01:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void resume() {
|
2017-03-11 16:15:39 +00:00
|
|
|
if (this->is_dead()) {
|
2017-03-11 01:12:43 +00:00
|
|
|
throw coroutine_error{"dead generator"};
|
|
|
|
}
|
|
|
|
detail::coroutine_context::call();
|
|
|
|
}
|
|
|
|
|
|
|
|
T &value() {
|
|
|
|
if (!p_result) {
|
|
|
|
throw coroutine_error{"no value"};
|
|
|
|
}
|
|
|
|
return *p_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
T const &value() const {
|
|
|
|
if (!p_result) {
|
|
|
|
throw coroutine_error{"no value"};
|
|
|
|
}
|
|
|
|
return *p_result;
|
|
|
|
}
|
|
|
|
|
2017-03-11 17:30:08 +00:00
|
|
|
bool empty() const noexcept {
|
2017-03-11 16:15:39 +00:00
|
|
|
return (!p_result || this->is_dead());
|
2017-03-11 01:12:43 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 17:30:08 +00:00
|
|
|
generator_range<T> iter() noexcept;
|
2017-03-11 01:12:43 +00:00
|
|
|
|
2017-03-11 13:16:32 +00:00
|
|
|
/* for range for loop; they're the same, operator!= bypasses comparing */
|
2017-03-11 17:30:08 +00:00
|
|
|
detail::generator_iterator<T> begin() noexcept;
|
|
|
|
detail::generator_iterator<T> end() noexcept;
|
2017-03-11 13:16:32 +00:00
|
|
|
|
2017-03-11 17:30:08 +00:00
|
|
|
void swap(generator &other) noexcept {
|
2017-03-11 01:12:43 +00:00
|
|
|
using std::swap;
|
|
|
|
swap(p_func, other.p_func);
|
|
|
|
swap(p_result, other.p_result);
|
|
|
|
detail::coroutine_context::swap(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/* the main entry point of the generator */
|
|
|
|
template<typename SA>
|
|
|
|
static void context_call(detail::transfer_t t) {
|
|
|
|
auto &self = *(static_cast<generator *>(t.data));
|
|
|
|
self.p_orig = t.ctx;
|
2017-03-11 16:15:39 +00:00
|
|
|
if (self.is_hold()) {
|
2017-03-11 01:12:43 +00:00
|
|
|
goto release;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
self.p_func(yield_type{self});
|
|
|
|
} catch (detail::coroutine_context::forced_unwind v) {
|
|
|
|
self.p_orig = v.ctx;
|
|
|
|
} catch (...) {
|
|
|
|
self.p_except = std::current_exception();
|
|
|
|
}
|
|
|
|
release:
|
|
|
|
self.p_result = nullptr;
|
2017-03-11 17:22:01 +00:00
|
|
|
self.template finish<SA>();
|
2017-03-11 01:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(yield_type)> p_func;
|
|
|
|
/* we can use a pointer because even stack values are alive
|
|
|
|
* as long as the coroutine is alive (and it is on every yield)
|
|
|
|
*/
|
|
|
|
T *p_result = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2017-03-11 17:30:08 +00:00
|
|
|
inline void swap(generator<T> &a, generator<T> &b) noexcept {
|
2017-03-11 01:12:43 +00:00
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2017-03-11 13:51:34 +00:00
|
|
|
namespace detail {
|
|
|
|
template<typename T>
|
|
|
|
struct yield_type_base {
|
|
|
|
using type = typename generator<T>::yield_type;
|
|
|
|
};
|
|
|
|
template<typename R, typename ...A>
|
|
|
|
struct yield_type_base<R(A...)> {
|
|
|
|
using type = typename coroutine<R(A...)>::yield_type;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
using yield_type = typename detail::yield_type_base<T>::type;
|
|
|
|
|
2017-03-05 23:47:15 +00:00
|
|
|
template<typename T>
|
2017-03-11 01:12:43 +00:00
|
|
|
struct generator_range: input_range<generator_range<T>> {
|
2017-03-05 23:47:15 +00:00
|
|
|
using range_category = input_range_tag;
|
|
|
|
using value_type = T;
|
|
|
|
using reference = T &;
|
|
|
|
using size_type = size_t;
|
2017-03-11 13:16:32 +00:00
|
|
|
using difference_type = ptrdiff_t;
|
2017-03-05 23:47:15 +00:00
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
generator_range() = delete;
|
2017-03-11 01:17:59 +00:00
|
|
|
generator_range(generator<T> &g): p_gen(&g) {}
|
2017-03-05 23:47:15 +00:00
|
|
|
|
2017-03-11 17:30:08 +00:00
|
|
|
bool empty() const noexcept {
|
2017-03-11 01:12:43 +00:00
|
|
|
return p_gen->empty();
|
2017-03-05 23:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pop_front() {
|
2017-03-11 01:12:43 +00:00
|
|
|
p_gen->resume();
|
2017-03-05 23:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reference front() const {
|
2017-03-11 01:12:43 +00:00
|
|
|
return p_gen->value();
|
2017-03-05 23:47:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 17:30:08 +00:00
|
|
|
bool equals_front(generator_range const &g) const noexcept {
|
2017-03-11 01:12:43 +00:00
|
|
|
return p_gen == g.p_gen;
|
2017-03-05 23:47:15 +00:00
|
|
|
}
|
|
|
|
private:
|
2017-03-11 01:12:43 +00:00
|
|
|
generator<T> *p_gen;
|
2017-03-05 23:47:15 +00:00
|
|
|
};
|
|
|
|
|
2017-03-11 01:12:43 +00:00
|
|
|
template<typename T>
|
2017-03-11 17:30:08 +00:00
|
|
|
generator_range<T> generator<T>::iter() noexcept {
|
2017-03-11 01:12:43 +00:00
|
|
|
return generator_range<T>{*this};
|
2017-03-06 18:01:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 13:16:32 +00:00
|
|
|
namespace detail {
|
|
|
|
/* deliberately incomplete, only for range for loop */
|
|
|
|
template<typename T>
|
|
|
|
struct generator_iterator {
|
|
|
|
generator_iterator() = delete;
|
|
|
|
generator_iterator(generator<T> &g): p_gen(&g) {}
|
|
|
|
|
2017-03-11 17:30:08 +00:00
|
|
|
bool operator!=(generator_iterator const &) noexcept {
|
2017-03-11 13:16:32 +00:00
|
|
|
return !p_gen->empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
T &operator*() const {
|
|
|
|
return p_gen->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
generator_iterator &operator++() {
|
|
|
|
p_gen->resume();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
generator<T> *p_gen;
|
|
|
|
};
|
|
|
|
} /* namespace detail */
|
|
|
|
|
|
|
|
template<typename T>
|
2017-03-11 17:30:08 +00:00
|
|
|
detail::generator_iterator<T> generator<T>::begin() noexcept {
|
2017-03-11 13:16:32 +00:00
|
|
|
return detail::generator_iterator<T>{*this};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-03-11 17:30:08 +00:00
|
|
|
detail::generator_iterator<T> generator<T>::end() noexcept {
|
2017-03-11 13:16:32 +00:00
|
|
|
return detail::generator_iterator<T>{*this};
|
|
|
|
}
|
|
|
|
|
2017-03-04 17:25:09 +00:00
|
|
|
} /* namespace ostd */
|
|
|
|
|
|
|
|
#endif
|