CG-Project2
Loading...
Searching...
No Matches
StateManager Class Reference

This class manages the current State running in the application and also provides a simple cache system. More...

#include <State.hpp>

Public Member Functions

 StateManager (StateManager &other)=delete
void operator= (const StateManager &other)=delete
bool shouldExit () const
 Retrieves true if the current state should exit.
void execUpdate ()
 Execute State::onUpdate() method of the current state.
void execRender ()
 Execute State::onRender() method of the current state.
void sync ()
 This method works like a barrier: after calling StateManager::changeState(), detach the current state; clear the EventManager, attach the queued State, defines new event listeners. It needs to be called after every StateManager::changeState() or at the end of a loop before calling StateManager::shouldExit().
void clean ()
 Cleans all the current state cache system.
void changeState (const std::string &name, const Shared< State > &state)
 Works as the other StateManager::changeState(const Shared<State>&) but this will use the given name while caching.
void changeState (const Shared< State > &state)
 This method queue the given State and tells the StateManager that the current state needs to be changed. If the given State is not in the cache system, the StateManager will cache the State with the state's name. To make it work the method StateManager::sync() needs to be called (read StateManager::sync() for further informations).
void changeState (const std::string &name)
 This method search in the cache the State with the given name. If the name is not found in the cache, the current state will not change.
bool isCurrentStateDefined () const
 Retrieves if the current state is defined or not.
void cacheState (const std::string &name, const Shared< State > &state)
 This method caches the State given with the given name.
void cacheState (const Shared< State > &state)
 Caches the given State, his name will be used in the cached states.
void uncacheState (const std::string &name)
 Removes the State with the given name if contained in the cache.
std::map< std::string, Shared< State > > getCachedStates ()
 Retrieves the current cache of the StateManager.

Static Public Member Functions

static Shared< StateManagerinstance ()
 Retrieves the instance of the StateManager if it's not created. This function is thread safe using a simple std::mutex.

Detailed Description

This class manages the current State running in the application and also provides a simple cache system.

It allow to change state while executing for one iteration or in a loop using a simple barrier system.

It's implemented with singleton so there only be one instance of this class.

Member Function Documentation

◆ cacheState() [1/2]

void StateManager::cacheState ( const Shared< State > & state)
inline

Caches the given State, his name will be used in the cached states.

Parameters
statestate to be cached

◆ cacheState() [2/2]

void StateManager::cacheState ( const std::string & name,
const Shared< State > & state )

This method caches the State given with the given name.

Parameters
namestate's name
Statea shared pointer to the State

◆ changeState() [1/3]

void StateManager::changeState ( const Shared< State > & state)
inline

This method queue the given State and tells the StateManager that the current state needs to be changed. If the given State is not in the cache system, the StateManager will cache the State with the state's name. To make it work the method StateManager::sync() needs to be called (read StateManager::sync() for further informations).

Parameters
Statea shared pointer to the State

◆ changeState() [2/3]

void StateManager::changeState ( const std::string & name)

This method search in the cache the State with the given name. If the name is not found in the cache, the current state will not change.

Parameters
namethe state's name

◆ changeState() [3/3]

void StateManager::changeState ( const std::string & name,
const Shared< State > & state )

Works as the other StateManager::changeState(const Shared<State>&) but this will use the given name while caching.

See also
StateManager::changeState(const Shared<State>&)
Parameters
namestate's name
Statea shared pointer to the State

◆ getCachedStates()

std::map< std::string, Shared< State > > StateManager::getCachedStates ( )
inline

Retrieves the current cache of the StateManager.

Returns
cachedStates map of (std::string, Shared<State>)

◆ instance()

Shared< StateManager > StateManager::instance ( )
inlinestatic

Retrieves the instance of the StateManager if it's not created. This function is thread safe using a simple std::mutex.

Returns
StateManager unique object.

◆ isCurrentStateDefined()

bool StateManager::isCurrentStateDefined ( ) const
inline

Retrieves if the current state is defined or not.

Returns
true if the current state is initialized

◆ shouldExit()

bool StateManager::shouldExit ( ) const
inline

Retrieves true if the current state should exit.

Returns
true if the current state is finished or changed

◆ sync()

void StateManager::sync ( )

This method works like a barrier: after calling StateManager::changeState(), detach the current state; clear the EventManager, attach the queued State, defines new event listeners. It needs to be called after every StateManager::changeState() or at the end of a loop before calling StateManager::shouldExit().

Note
It uses a bool variable to check if there is a new State in the queue.

◆ uncacheState()

void StateManager::uncacheState ( const std::string & name)

Removes the State with the given name if contained in the cache.

Parameters
namestate's name

The documentation for this class was generated from the following files:
  • App/include/State/State.hpp
  • App/src/State/State.cpp