SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
Public Member Functions | List of all members
SDL::InitState Struct Reference

A structure used for thread-safe initialization and shutdown. More...

Inheritance diagram for SDL::InitState:
Inheritance graph
[legend]

Public Member Functions

constexpr InitState ()
 Constructor.
 
bool ShouldInit ()
 Return whether initialization should be done.
 
bool ShouldQuit ()
 Return whether cleanup should be done.
 
void SetInitialized (bool initialized)
 Finish an initialization state transition.
 

Detailed Description

Here is an example of using this:

static SDL::InitState init;
bool InitSystem(void)
{
if (!InitState.ShouldInit(&init)) {
// The system is initialized
return true;
}
// At this point, you should not leave this function without calling
bool initialized = DoInitTasks();
InitState.SetInitialized(&init, initialized);
return initialized;
}
bool UseSubsystem(void)
{
if (InitState.ShouldInit(&init)) {
// Error, the subsystem isn't initialized
InitState.SetInitialized(&init, false);
return false;
}
// Do work using the initialized subsystem
return true;
}
void QuitSystem(void)
{
if (!InitState.ShouldQuit(&init)) {
// The system is not initialized
return;
}
// At this point, you should not leave this function without calling
InitState.SetInitialized()
DoQuitTasks();
InitState.SetInitialized(&init, false);
}
A structure used for thread-safe initialization and shutdown.
Definition SDL3pp_mutex.h:1130
bool ShouldInit()
Return whether initialization should be done.
Definition SDL3pp_mutex.h:1159
constexpr InitState()
Constructor.
Definition SDL3pp_mutex.h:1134
void SetInitialized(bool initialized)
Finish an initialization state transition.
Definition SDL3pp_mutex.h:1197

Note that this doesn't protect any resources created during initialization, or guarantee that nobody is using those resources during cleanup. You should use other mechanisms to protect those, if that's a concern for your code.

Since
This struct is available since SDL 3.2.0.

Member Function Documentation

◆ SetInitialized()

void SDL::InitState::SetInitialized ( bool  initialized)
inline

This function sets the status of the passed in state to INIT_STATUS_INITIALIZED or INIT_STATUS_UNINITIALIZED and allows any threads waiting for the status to proceed.

Parameters
initializedthe new initialization state.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
InitState.ShouldInit
InitState.ShouldQuit

◆ ShouldInit()

bool SDL::InitState::ShouldInit ( )
inline

This function checks the passed in state and if initialization should be done, sets the status to INIT_STATUS_INITIALIZING and returns true. If another thread is already modifying this state, it will wait until that's done before returning.

If this function returns true, the calling code must call InitState.SetInitialized() to complete the initialization.

Returns
true if initialization needs to be done, false otherwise.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
InitState.SetInitialized
InitState.ShouldQuit

◆ ShouldQuit()

bool SDL::InitState::ShouldQuit ( )
inline

This function checks the passed in state and if cleanup should be done, sets the status to INIT_STATUS_UNINITIALIZING and returns true.

If this function returns true, the calling code must call InitState.SetInitialized() to complete the cleanup.

Returns
true if cleanup needs to be done, false otherwise.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
InitState.SetInitialized
InitState.ShouldInit

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