Skip to content

Commit

Permalink
Async factory
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Dec 11, 2024
1 parent 668da87 commit bf5c98d
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions nano/lib/async.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ concept async_task = std::same_as<T, asio::awaitable<void>>;

// Concept for callables that return an awaitable
template <typename T>
concept async_callable = requires (T t) {
concept async_factory = requires (T t) {
{
t ()
} -> std::same_as<asio::awaitable<void>>;
};

// Concept for tasks that take a condition and return an awaitable
// Concept for callables that take a condition and return an awaitable
template <typename T>
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<asio::awaitable<void>>;
Expand All @@ -192,8 +192,7 @@ class task
{
}

template <typename Func>
requires async_task<Func> || async_callable<Func>
template <async_task Func>
task (nano::async::strand & strand, Func && func) :
strand{ strand },
cancellation{ strand }
Expand All @@ -204,7 +203,18 @@ class task
asio::bind_cancellation_slot (cancellation.slot (), asio::use_future));
}

template <async_callable_with_condition Func>
template <async_factory Func>
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 <async_factory_with_condition Func>
task (nano::async::strand & strand, Func && func) :
strand{ strand },
cancellation{ strand },
Expand Down

0 comments on commit bf5c98d

Please sign in to comment.