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::PostAwaitable< T > Class Template Reference

Awaitable for executing function on main thread. More...

#include <Awaitable.h>

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

Public Member Functions

template<typename Func >
 PostAwaitable (std::shared_ptr< SLLooper > looper, Func &&func)
 Construct PostAwaitable 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 post to main thread.
 
auto await_resume () const -> T
 Resume coroutine and return result.
 
std::function< T()> getFunc () const
 Get the main thread function.
 
std::shared_ptr< SLLoopergetLooper () const
 Get the SLLooper instance.
 
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::PostAwaitable< T >

Awaitable for executing function on main thread.

Template Parameters
TReturn type of the main thread function

PostAwaitable executes a function on the main event loop thread and suspends the coroutine until completion. Useful for UI updates or thread-safe operations.

auto result = co_await looper->awaitPost([]() -> std::string {
// Execute on main thread
return "Main thread result";
});

Definition at line 275 of file Awaitable.h.

Constructor & Destructor Documentation

◆ PostAwaitable()

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

Construct PostAwaitable with function.

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

Definition at line 291 of file Awaitable.h.

Member Function Documentation

◆ await_ready()

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

Check if result is ready (coroutine interface)

Returns
false - always suspend initially

Definition at line 298 of file Awaitable.h.

◆ await_resume()

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

Resume coroutine and return result.

Returns
Result from main thread function
Exceptions
std::exceptionif function failed

Definition at line 311 of file Awaitable.h.

◆ await_suspend()

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

Suspend coroutine and post to main thread.

Parameters
handleCoroutine handle for resumption

Definition at line 305 of file SLLooper.cpp.

References SLLOOPER_DEBUG, SLLOOPER_ERROR, and SLLOOPER_ERROR_STREAM.

◆ getFunc()

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

Get the main thread function.

Returns
Function to execute

Definition at line 321 of file Awaitable.h.

◆ getLooper()

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

Get the SLLooper instance.

Returns
Shared pointer to looper

Definition at line 327 of file Awaitable.h.

◆ setException()

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

Set exception result (internal use)

Parameters
exException pointer from main thread function

Definition at line 342 of file Awaitable.h.

◆ setResult()

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

Set successful result (internal use)

Parameters
resultResult value from main thread function

Definition at line 333 of file Awaitable.h.


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