From 34a434ab53d20b9ce8174c217b4bde89f4f8aef2 Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 5 Mar 2017 23:07:00 +0100 Subject: [PATCH] move helpers to coro_base --- ostd/coroutine.hh | 68 ++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/ostd/coroutine.hh b/ostd/coroutine.hh index ad0689f..4b5f425 100644 --- a/ostd/coroutine.hh +++ b/ostd/coroutine.hh @@ -181,6 +181,19 @@ namespace detail { } protected: + template + void call_helper(F &func, C &coro, std::index_sequence) { + p_result = std::forward( + func(coro, std::forward(std::get(p_args))...) + ); + } + + R call(A ...args) { + p_args = std::forward_as_tuple(std::forward(args)...); + p_ctx.call(); + return std::forward(this->p_result); + } + std::tuple p_args; R p_result; coroutine_context p_ctx; @@ -199,6 +212,16 @@ namespace detail { } protected: + template + void call_helper(F &func, C &coro, std::index_sequence) { + p_result = std::forward(func(coro)); + } + + R call() { + p_ctx.call(); + return std::forward(this->p_result); + } + R p_result; coroutine_context p_ctx; }; @@ -216,6 +239,16 @@ namespace detail { } protected: + template + void call_helper(F &func, C &coro, std::index_sequence) { + func(coro, std::forward(std::get(p_args))...); + } + + void call(A ...args) { + p_args = std::forward_as_tuple(std::forward(args)...); + p_ctx.call(); + } + std::tuple p_args; coroutine_context p_ctx; }; @@ -232,6 +265,15 @@ namespace detail { } protected: + template + void call_helper(F &func, C &coro, std::index_sequence) { + func(coro); + } + + void call() { + p_ctx.call(); + } + coroutine_context p_ctx; }; } /* namespace detail */ @@ -255,33 +297,9 @@ struct coroutine: detail::coro_base { return this->call(std::forward(args)...); } private: - R call(A ...args) { - if constexpr(sizeof...(A) != 0) { - this->p_args = std::forward_as_tuple(std::forward(args)...); - } - this->p_ctx.call(); - if constexpr(!std::is_same_v) { - return std::forward(this->p_result); - } - } - - template - R call_helper(std::index_sequence) { - if constexpr(sizeof...(A) != 0) { - return p_func(*this, std::forward(std::get(this->p_args))...); - } else { - return p_func(*this); - } - } - static void context_call(void *data) { - using indices = std::index_sequence_for; coroutine &self = *(static_cast(data)); - if constexpr(std::is_same_v) { - self.call_helper(indices{}); - } else { - self.p_result = self.call_helper(indices{}); - } + self.call_helper(self.p_func, self, std::index_sequence_for{}); } std::function &, A...)> p_func;