Pacman Torben Petré
Loading...
Searching...
No Matches
StateManager Class Reference

#include <StateManager.h>

Public Member Functions

 StateManager ()
Statetop ()
 Returns the top of the manager stack.
bool empty () const
 Returns whether the manager stack is empty.
GameContextgetGameContext () const
 Returns the GameContext struct.
void swap (const std::shared_ptr< State > &state)
 Helper function for swapping the top state with a new state. Does this by updating the pending command.
void push (const std::shared_ptr< State > &state)
 Pushes the state to the stack. Does this by updating pending command.
void pop ()
 Pops the top state from the stack. Does this by updating pending command.
void clear (const std::shared_ptr< State > &state)
 Clears the state stack and pushes the new state. Does this by updating pending command.
void executeCommand ()
 Applies any pending command, if there is any pending command.

Detailed Description

StateManager class holds a stack with States. It makes use of the Command pattern to assure that at no point in time, states are deleting themselves instantly when calling StateManager methods.

Stack influencing methods update the StateManager::pendingCommand. The dedicated StateManager::executeCommand() function will execute the pending command. Implementation wise a State can now request to be popped/swapped or replaced, but it does not control when exactly happens.

// Example implementation
stateManager->top().update(dt);
// The top state now can finish it's update function before any Stack
// changing commands will be executed.
stateManager->executeCommand();

Constructor & Destructor Documentation

◆ StateManager()

StateManager::StateManager ( )
explicit

Member Function Documentation

◆ clear()

void StateManager::clear ( const std::shared_ptr< State > & state)

Clears the state stack and pushes the new state. Does this by updating pending command.

Parameters
stateState

◆ empty()

bool StateManager::empty ( ) const
nodiscard

Returns whether the manager stack is empty.

Returns
Whether stack is empty

◆ executeCommand()

void StateManager::executeCommand ( )

Applies any pending command, if there is any pending command.

◆ getGameContext()

GameContext & StateManager::getGameContext ( ) const
nodiscard

Returns the GameContext struct.

Returns
GameContext reference

◆ pop()

void StateManager::pop ( )

Pops the top state from the stack. Does this by updating pending command.

Exceptions
std::runtime_errorWhen the manager stack is empty

◆ push()

void StateManager::push ( const std::shared_ptr< State > & state)

Pushes the state to the stack. Does this by updating pending command.

Parameters
stateState

◆ swap()

void StateManager::swap ( const std::shared_ptr< State > & state)

Helper function for swapping the top state with a new state. Does this by updating the pending command.

This function is for when the top state wants to switch itself with a different state. Having the StateManager do the swapping itself, avoids calling push or pop function on already destructed objects.

Parameters
stateState

◆ top()

State & StateManager::top ( )
nodiscard

Returns the top of the manager stack.

Exceptions
std::runtime_errorWhen the manager stack is empty
Returns
State reference

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