From 31ca9039997095e6682cbd57fc5279293befebfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 19 Nov 2024 00:24:27 +0100 Subject: [PATCH] Async factory --- nano/lib/async.hpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/nano/lib/async.hpp b/nano/lib/async.hpp index c7a28a856..1cc8b0bfc 100644 --- a/nano/lib/async.hpp +++ b/nano/lib/async.hpp @@ -162,15 +162,15 @@ concept async_task = std::same_as>; // Concept for callables that return an awaitable template -concept async_callable = requires (T t) { +concept async_factory = requires (T t) { { t () } -> std::same_as>; }; -// Concept for tasks that take a condition and return an awaitable +// Concept for callables that take a condition and return an awaitable template -concept async_callable_with_condition = requires (T t, condition & c) { +concept async_factory_with_condition = requires (T t, condition & c) { { t (c) } -> std::same_as>; @@ -192,8 +192,7 @@ public: { } - template - requires async_task || async_callable + template task (nano::async::strand & strand, Func && func) : strand{ strand }, cancellation{ strand } @@ -204,7 +203,18 @@ public: asio::bind_cancellation_slot (cancellation.slot (), asio::use_future)); } - template + template + task (nano::async::strand & strand, Func && func) : + strand{ strand }, + cancellation{ strand } + { + future = asio::co_spawn ( + strand, + func (), + asio::bind_cancellation_slot (cancellation.slot (), asio::use_future)); + } + + template task (nano::async::strand & strand, Func && func) : strand{ strand }, cancellation{ strand },