Mountain  1.0.0
Simple C++ 2D Game Framework
Mountain::Coroutine Class Reference

Wrapper around C++20 Coroutines. More...

#include <coroutine.hpp>

Classes

struct  promise_type
 Promise type for C++20 coroutines. More...
 

Public Types

using AwaitType = std::chrono::duration< double_t >
 Default duration type for Coroutine wait, equivalent to floating-point seconds.
 
using HandleType = std::coroutine_handle< promise_type >
 The coroutine handle type.
 

Public Member Functions

MOUNTAIN_API Coroutine (HandleType handle)
 Constructs a new Coroutine from the given handle.
 
MOUNTAIN_API Coroutineoperator= (HandleType handle) noexcept
 
 Coroutine (const Coroutine &)=delete
 
Coroutineoperator= (const Coroutine &)=delete
 
MOUNTAIN_API Coroutine (Coroutine &&other) noexcept
 
MOUNTAIN_API Coroutineoperator= (Coroutine &&other) noexcept
 
MOUNTAIN_API void Resume () const
 Resumes the Coroutine. More...
 
MOUNTAIN_API void ResumeSafe () const
 Safely resumes the Coroutine by first checking if Valid() is true. More...
 
MOUNTAIN_API bool_t Finished () const
 Returns whether the Coroutine finished its execution. More...
 
MOUNTAIN_API bool_t FinishedSafe () const
 Safely returns whether the Coroutine finished its execution by first checking if Valid() is true. More...
 
MOUNTAIN_API void Destroy ()
 Destroys the Coroutine. It can't be resumed afterward. More...
 
MOUNTAIN_API void DestroySafe ()
 Safely destroys the Coroutine by first checking if Valid() is true. More...
 
MOUNTAIN_API bool_t Valid () const
 Gets whether the Coroutine is valid, e.g. if it hasn't been default-initialized. More...
 
MOUNTAIN_API void Reset ()
 Resets the Coroutine. More...
 
MOUNTAIN_API Guid GetId () const
 Returns the Guid of this Coroutine.
 

Static Public Member Functions

static MOUNTAIN_API Guid Start (Coroutine &&coroutine)
 
static MOUNTAIN_API void Start (Coroutine &&coroutine, Guid *coroutineId)
 Starts a coroutine using an existing coroutine Guid. More...
 
static MOUNTAIN_API void UpdateAll ()
 
static MOUNTAIN_API void Stop (const Guid &coroutineId)
 
static MOUNTAIN_API void StopAll ()
 
static MOUNTAIN_API bool_t IsRunning (const Guid &coroutineId)
 
static MOUNTAIN_API bool_t IsRunningAndNotEmpty (const Guid &coroutineId)
 
static MOUNTAIN_API size_t GetRunningCount ()
 

Detailed Description

Wrapper around C++20 Coroutines.

Usage

Example Coroutine usage:

Coroutine Function()
{
using std::chrono_literals;
// Do something
// Wait for 750 milliseconds
co_await 750ms;
// Do something else
// Wait for 0.5 seconds
co_await 0.5f;
// Wait for next resume (most of the time, this is the next frame)
co_yield nullptr;
// Exit the Coroutine at any moment using co_return
if (condition)
co_return;
}
// Somewhere else
Coroutine routine = Function();
// Here you can do whatever you want with the Coroutine
routine.Resume();

Instead of manually resuming the Coroutine, the framework can do it for you:

Guid id = Coroutine::Start(Function());

And the Coroutine will be resumed automatically each frame. Use the returned Guid with the static functions in the Coroutine class.

See also
C++20 Coroutines

Definition at line 61 of file coroutine.hpp.

Member Function Documentation

◆ Destroy()

MOUNTAIN_API void Mountain::Coroutine::Destroy ( )

Destroys the Coroutine. It can't be resumed afterward.

Remarks
If Valid() is false, this is undefined behavior. Consider using DestroySafe() instead.

◆ DestroySafe()

MOUNTAIN_API void Mountain::Coroutine::DestroySafe ( )

Safely destroys the Coroutine by first checking if Valid() is true.

See also
Destroy()

◆ Finished()

MOUNTAIN_API bool_t Mountain::Coroutine::Finished ( ) const

Returns whether the Coroutine finished its execution.

Remarks
If Valid() is false, this is undefined behavior. Consider using FinishedSafe() instead.

◆ FinishedSafe()

MOUNTAIN_API bool_t Mountain::Coroutine::FinishedSafe ( ) const

Safely returns whether the Coroutine finished its execution by first checking if Valid() is true.

If Valid() is false, returns false.

See also
Finished()

◆ Reset()

MOUNTAIN_API void Mountain::Coroutine::Reset ( )

Resets the Coroutine.

Warning
THIS DOES NOT RELEASE THE ALLOCATED MEMORY, make sure to call Destroy() beforehand.

◆ Resume()

MOUNTAIN_API void Mountain::Coroutine::Resume ( ) const

Resumes the Coroutine.

Remarks
If Valid() is false, this is undefined behavior. Consider using ResumeSafe() instead.

◆ ResumeSafe()

MOUNTAIN_API void Mountain::Coroutine::ResumeSafe ( ) const

Safely resumes the Coroutine by first checking if Valid() is true.

See also
Resume()

◆ Start()

static MOUNTAIN_API void Mountain::Coroutine::Start ( Coroutine &&  coroutine,
Guid coroutineId 
)
static

Starts a coroutine using an existing coroutine Guid.

This stops the existing coroutine if it is still running and assigns the guid with a newly created one.

◆ Valid()

MOUNTAIN_API bool_t Mountain::Coroutine::Valid ( ) const

Gets whether the Coroutine is valid, e.g. if it hasn't been default-initialized.

Remarks
This still returns true even when the Coroutine is finished.

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