Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
kcenon::container::async::task< T > Class Template Reference

Forward declaration of task. More...

#include <task.h>

Collaboration diagram for kcenon::container::async::task< T >:
Collaboration graph

Classes

struct  awaiter
 Awaitable interface. More...
 

Public Types

using promise_type = detail::promise_type<T>
 
using handle_type = std::coroutine_handle<promise_type>
 

Public Member Functions

 task () noexcept
 Default constructor - creates empty task.
 
 task (handle_type handle) noexcept
 Construct from coroutine handle.
 
 task (task &&other) noexcept
 Move constructor.
 
taskoperator= (task &&other) noexcept
 Move assignment operator.
 
 ~task ()
 Destructor - destroys the coroutine if owned.
 
 task (const task &)=delete
 
taskoperator= (const task &)=delete
 
bool valid () const noexcept
 Check if task is valid (has a coroutine)
 
 operator bool () const noexcept
 Check if task is valid (has a coroutine)
 
bool done () const noexcept
 Check if the coroutine is done.
 
decltype(auto) get () &
 Get the result (blocking, for testing)
 
decltype(auto) get () &&
 Get the result (rvalue, blocking, for testing)
 
awaiter operator co_await () noexcept
 Get awaiter for co_await.
 
void resume () const
 Resume the coroutine (for manual execution)
 

Private Attributes

handle_type handle_
 

Detailed Description

template<typename T>
class kcenon::container::async::task< T >

Forward declaration of task.

Coroutine task type for async operations.

This is a lazy coroutine type that starts executing when awaited. It supports:

  • Value-returning coroutines (task<T>)
  • Void-returning coroutines (task<void>)
  • Exception propagation
  • Proper RAII resource management

Properties:

  • Move-only (non-copyable)
  • Lazy execution (starts when awaited)
  • Symmetric transfer for efficient chaining
Template Parameters
TThe return type of the coroutine
Examples
async_coroutine_example.cpp.

Definition at line 208 of file task.h.

Member Typedef Documentation

◆ handle_type

template<typename T >
using kcenon::container::async::task< T >::handle_type = std::coroutine_handle<promise_type>

Definition at line 212 of file task.h.

◆ promise_type

template<typename T >
using kcenon::container::async::task< T >::promise_type = detail::promise_type<T>

Definition at line 211 of file task.h.

Constructor & Destructor Documentation

◆ task() [1/4]

template<typename T >
kcenon::container::async::task< T >::task ( )
inlinenoexcept

Default constructor - creates empty task.

Definition at line 217 of file task.h.

217: handle_(nullptr) {}

◆ task() [2/4]

template<typename T >
kcenon::container::async::task< T >::task ( handle_type handle)
inlineexplicitnoexcept

Construct from coroutine handle.

Definition at line 222 of file task.h.

222: handle_(handle) {}

◆ task() [3/4]

template<typename T >
kcenon::container::async::task< T >::task ( task< T > && other)
inlinenoexcept

Move constructor.

Definition at line 227 of file task.h.

227: handle_(std::exchange(other.handle_, nullptr)) {}

◆ ~task()

template<typename T >
kcenon::container::async::task< T >::~task ( )
inline

Destructor - destroys the coroutine if owned.

Definition at line 248 of file task.h.

249 {
250 if (handle_)
251 {
252 handle_.destroy();
253 }
254 }

References kcenon::container::async::task< T >::handle_.

◆ task() [4/4]

template<typename T >
kcenon::container::async::task< T >::task ( const task< T > & )
delete

Member Function Documentation

◆ done()

template<typename T >
bool kcenon::container::async::task< T >::done ( ) const
inlinenodiscardnoexcept

Check if the coroutine is done.

Uses atomic flag for thread-safe completion checking. This avoids data races when polling from a different thread than the one executing the coroutine.

Definition at line 283 of file task.h.

284 {
285 return handle_ && handle_.promise().completed_.load(std::memory_order_acquire);
286 }

References kcenon::container::async::task< T >::handle_.

◆ get() [1/2]

template<typename T >
decltype(auto) kcenon::container::async::task< T >::get ( ) &
inlinenodiscard

Get the result (blocking, for testing)

Warning
This assumes the coroutine has already completed

Definition at line 293 of file task.h.

294 {
295 return handle_.promise().result();
296 }

References kcenon::container::async::task< T >::handle_.

◆ get() [2/2]

template<typename T >
decltype(auto) kcenon::container::async::task< T >::get ( ) &&
inlinenodiscard

Get the result (rvalue, blocking, for testing)

Definition at line 301 of file task.h.

302 {
303 return std::move(handle_.promise()).result();
304 }

References kcenon::container::async::task< T >::handle_.

◆ operator bool()

template<typename T >
kcenon::container::async::task< T >::operator bool ( ) const
inlineexplicitnodiscardnoexcept

Check if task is valid (has a coroutine)

Definition at line 271 of file task.h.

272 {
273 return valid();
274 }
bool valid() const noexcept
Check if task is valid (has a coroutine)
Definition task.h:263

References kcenon::container::async::task< T >::valid().

Here is the call graph for this function:

◆ operator co_await()

template<typename T >
awaiter kcenon::container::async::task< T >::operator co_await ( )
inlinenodiscardnoexcept

Get awaiter for co_await.

Definition at line 343 of file task.h.

344 {
345 return awaiter{handle_};
346 }

References kcenon::container::async::task< T >::handle_.

◆ operator=() [1/2]

template<typename T >
task & kcenon::container::async::task< T >::operator= ( const task< T > & )
delete

◆ operator=() [2/2]

template<typename T >
task & kcenon::container::async::task< T >::operator= ( task< T > && other)
inlinenoexcept

Move assignment operator.

Definition at line 232 of file task.h.

233 {
234 if (this != &other)
235 {
236 if (handle_)
237 {
238 handle_.destroy();
239 }
240 handle_ = std::exchange(other.handle_, nullptr);
241 }
242 return *this;
243 }

References kcenon::container::async::task< T >::handle_.

◆ resume()

template<typename T >
void kcenon::container::async::task< T >::resume ( ) const
inline

Resume the coroutine (for manual execution)

Definition at line 351 of file task.h.

352 {
353 if (handle_ && !handle_.done())
354 {
355 handle_.resume();
356 }
357 }

References kcenon::container::async::task< T >::handle_.

◆ valid()

template<typename T >
bool kcenon::container::async::task< T >::valid ( ) const
inlinenodiscardnoexcept

Check if task is valid (has a coroutine)

Definition at line 263 of file task.h.

264 {
265 return handle_ != nullptr;
266 }

References kcenon::container::async::task< T >::handle_.

Referenced by kcenon::container::async::task< T >::operator bool().

Here is the caller graph for this function:

Member Data Documentation

◆ handle_


The documentation for this class was generated from the following file: