SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
Loading...
Searching...
No Matches
swt::WorkAwaitable< T > Class Template Reference

Awaitable for executing work on background thread. More...

#include <Awaitable.h>

Collaboration diagram for swt::WorkAwaitable< T >:
Collaboration graph

Public Member Functions

template<typename Func >
 WorkAwaitable (std::shared_ptr< SLLooper > looper, Func &&func)
 Construct WorkAwaitable with function.
 
bool await_ready () const noexcept
 Check if result is ready (coroutine interface)
 
void await_suspend (std::coroutine_handle<> handle) const noexcept
 Suspend coroutine and execute work on background thread.
 
auto await_resume () const -> T
 Resume coroutine and return result.
 
std::shared_ptr< SLLoopergetLooper () const
 Get the SLLooper instance.
 
std::function< T()> getFunc () const
 Get the work function.
 
void setResult (T result) const
 Set successful result (internal use)
 
void setException (std::exception_ptr ex) const
 Set exception result (internal use)
 

Detailed Description

template<typename T>
class swt::WorkAwaitable< T >

Awaitable for executing work on background thread.

Template Parameters
TReturn type of the background work function

WorkAwaitable executes a function on a background thread and suspends the coroutine until the work completes. The result is then returned to the coroutine when it resumes.

auto result = co_await looper->awaitWork([]() -> int {
// Heavy computation on background thread
return 42;
});

Definition at line 43 of file Awaitable.h.

Constructor & Destructor Documentation

◆ WorkAwaitable()

template<typename T >
template<typename Func >
swt::WorkAwaitable< T >::WorkAwaitable ( std::shared_ptr< SLLooper looper,
Func &&  func 
)
inline

Construct WorkAwaitable with function.

Template Parameters
FuncFunction type (auto-deduced)
Parameters
looperShared pointer to SLLooper
funcFunction to execute on background thread

Definition at line 59 of file Awaitable.h.

Member Function Documentation

◆ await_ready()

template<typename T >
bool swt::WorkAwaitable< T >::await_ready ( ) const
inlinenoexcept

Check if result is ready (coroutine interface)

Returns
false - always suspend initially

Definition at line 66 of file Awaitable.h.

◆ await_resume()

template<typename T >
auto swt::WorkAwaitable< T >::await_resume ( ) const -> T
inline

Resume coroutine and return result.

Returns
Result from background work
Exceptions
std::exceptionif work failed

Definition at line 79 of file Awaitable.h.

◆ await_suspend()

template<typename T >
void swt::WorkAwaitable< T >::await_suspend ( std::coroutine_handle<>  handle) const
noexcept

Suspend coroutine and execute work on background thread.

Parameters
handleCoroutine handle for resumption

Definition at line 258 of file SLLooper.cpp.

References SLLOOPER_DEBUG, SLLOOPER_ERROR, and SLLOOPER_ERROR_STREAM.

◆ getFunc()

template<typename T >
std::function< T()> swt::WorkAwaitable< T >::getFunc ( ) const
inline

Get the work function.

Returns
Function to execute

Definition at line 95 of file Awaitable.h.

◆ getLooper()

template<typename T >
std::shared_ptr< SLLooper > swt::WorkAwaitable< T >::getLooper ( ) const
inline

Get the SLLooper instance.

Returns
Shared pointer to looper

Definition at line 89 of file Awaitable.h.

◆ setException()

template<typename T >
void swt::WorkAwaitable< T >::setException ( std::exception_ptr  ex) const
inline

Set exception result (internal use)

Parameters
exException pointer from work function

Definition at line 110 of file Awaitable.h.

◆ setResult()

template<typename T >
void swt::WorkAwaitable< T >::setResult ( result) const
inline

Set successful result (internal use)

Parameters
resultResult value from work function

Definition at line 101 of file Awaitable.h.


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