galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
galaxy::StateMachine< Stored > Class Template Referenceabstract

A finite state machine. More...

#include <StateMachine.hpp>

+ Inheritance diagram for galaxy::StateMachine< Stored >:
+ Collaboration diagram for galaxy::StateMachine< Stored >:

Public Member Functions

 StateMachine () noexcept
 Constructor.
 
 StateMachine (StateMachine< Stored > &&)
 Move constructor.
 
StateMachine< Stored > & operator= (StateMachine< Stored > &&)
 Move assignment operator.
 
virtual ~StateMachine ()
 Destructor.
 
template<typename... Args>
std::shared_ptr< Stored > add (const std::string &key, Args &&... args)
 Add a new state.
 
std::shared_ptr< Stored > get (const std::string &key) noexcept
 Get a specific state.
 
bool has (const std::string &key) noexcept
 Does a state exist.
 
void remove (const std::string &key)
 Remove a specific state.
 
void push (const std::string &key) noexcept
 Push a state onto the top of stack.
 
void pop () noexcept
 Remove state on top of stack.
 
void pop_all () noexcept
 Remove all states in stack.
 
std::shared_ptr< Stored > top () const noexcept
 Get top state in stack.
 
virtual void update ()=0
 Process events and updates.
 
Stackstack () noexcept
 Get stack.
 
Mapstorage () noexcept
 Get storage.
 

Protected Attributes

Stack m_stack
 State stack.
 
Map m_storage
 State cache.
 

Private Types

using Stack = std::vector<std::shared_ptr<Stored>>
 We specifically use a std::vector for our stack.
 
using Map = ankerl::unordered_dense::map<std::uint64_t, std::shared_ptr<Stored>>
 Use a dense map for storage.
 

Private Member Functions

 StateMachine (const StateMachine< Stored > &)=delete
 Copy constructor.
 
StateMachine< Stored > & operator= (const StateMachine< Stored > &)=delete
 Copy assignment operator.
 

Detailed Description

template<std::derived_from< State > Stored>
class galaxy::StateMachine< Stored >

A finite state machine.

https://gameprogrammingpatterns.com/state.html

Template Parameters
StoredObject type being used in the state machine.

Definition at line 27 of file StateMachine.hpp.

Member Typedef Documentation

◆ Stack

template<std::derived_from< State > Stored>
using galaxy::StateMachine< Stored >::Stack = std::vector<std::shared_ptr<Stored>>
private

We specifically use a std::vector for our stack.

Definition at line 32 of file StateMachine.hpp.

◆ Map

template<std::derived_from< State > Stored>
using galaxy::StateMachine< Stored >::Map = ankerl::unordered_dense::map<std::uint64_t, std::shared_ptr<Stored>>
private

Use a dense map for storage.

Definition at line 37 of file StateMachine.hpp.

Constructor & Destructor Documentation

◆ StateMachine() [1/3]

template<std::derived_from< State > Stored>
galaxy::StateMachine< Stored >::StateMachine ( )
noexcept

Constructor.

Definition at line 173 of file StateMachine.hpp.

◆ StateMachine() [2/3]

template<std::derived_from< State > Stored>
galaxy::StateMachine< Stored >::StateMachine ( StateMachine< Stored > && sm)
inline

Move constructor.

Definition at line 178 of file StateMachine.hpp.

◆ ~StateMachine()

template<std::derived_from< State > Stored>
galaxy::StateMachine< Stored >::~StateMachine ( )
inlinevirtual

Destructor.

Definition at line 197 of file StateMachine.hpp.

+ Here is the call graph for this function:

◆ StateMachine() [3/3]

template<std::derived_from< State > Stored>
galaxy::StateMachine< Stored >::StateMachine ( const StateMachine< Stored > & )
privatedelete

Copy constructor.

Member Function Documentation

◆ operator=() [1/2]

template<std::derived_from< State > Stored>
StateMachine< Stored > & galaxy::StateMachine< Stored >::operator= ( StateMachine< Stored > && sm)
inline

Move assignment operator.

Definition at line 185 of file StateMachine.hpp.

+ Here is the caller graph for this function:

◆ add()

template<std::derived_from< State > Stored>
template<typename... Args>
std::shared_ptr< Stored > galaxy::StateMachine< Stored >::add ( const std::string & key,
Args &&... args )
inline

Add a new state.

Template Parameters
ArgsConstructor arguments for state.
Parameters
keyName to assign to this state.
argsVariable arguments for state constructor.
Returns
Shared pointer to added scene.

Definition at line 207 of file StateMachine.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get()

template<std::derived_from< State > Stored>
std::shared_ptr< Stored > galaxy::StateMachine< Stored >::get ( const std::string & key)
inlinenodiscardnoexcept

Get a specific state.

Parameters
keyName of state.
Returns
Shared pointer to scene.

Definition at line 224 of file StateMachine.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ has()

template<std::derived_from< State > Stored>
bool galaxy::StateMachine< Stored >::has ( const std::string & key)
inlinenodiscardnoexcept

Does a state exist.

Parameters
keyName of state.
Returns
True if exists.

Definition at line 240 of file StateMachine.hpp.

+ Here is the call graph for this function:

◆ remove()

template<std::derived_from< State > Stored>
void galaxy::StateMachine< Stored >::remove ( const std::string & key)
inline

Remove a specific state.

Will fail if state is in stack.

Parameters
keyName of state.

Definition at line 247 of file StateMachine.hpp.

+ Here is the call graph for this function:

◆ push()

template<std::derived_from< State > Stored>
void galaxy::StateMachine< Stored >::push ( const std::string & key)
inlinenoexcept

Push a state onto the top of stack.

Parameters
keyName of state.

Definition at line 263 of file StateMachine.hpp.

+ Here is the call graph for this function:

◆ pop()

template<std::derived_from< State > Stored>
void galaxy::StateMachine< Stored >::pop ( )
inlinenoexcept

Remove state on top of stack.

Definition at line 277 of file StateMachine.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pop_all()

template<std::derived_from< State > Stored>
void galaxy::StateMachine< Stored >::pop_all ( )
inlinenoexcept

Remove all states in stack.

Definition at line 292 of file StateMachine.hpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ top()

template<std::derived_from< State > Stored>
std::shared_ptr< Stored > galaxy::StateMachine< Stored >::top ( ) const
inlinenodiscardnoexcept

Get top state in stack.

Returns
Shared pointer to top state.

Definition at line 301 of file StateMachine.hpp.

+ Here is the caller graph for this function:

◆ update()

template<std::derived_from< State > Stored>
virtual void galaxy::StateMachine< Stored >::update ( )
pure virtual

Process events and updates.

Implemented in galaxy::World.

◆ stack()

template<std::derived_from< State > Stored>
StateMachine< Stored >::Stack & galaxy::StateMachine< Stored >::stack ( )
inlinenodiscardnoexcept

Get stack.

Returns
Reference.

Definition at line 312 of file StateMachine.hpp.

◆ storage()

template<std::derived_from< State > Stored>
StateMachine< Stored >::Map & galaxy::StateMachine< Stored >::storage ( )
inlinenodiscardnoexcept

Get storage.

Returns
Reference.

Definition at line 318 of file StateMachine.hpp.

◆ operator=() [2/2]

template<std::derived_from< State > Stored>
StateMachine< Stored > & galaxy::StateMachine< Stored >::operator= ( const StateMachine< Stored > & )
privatedelete

Copy assignment operator.

Member Data Documentation

◆ m_stack

template<std::derived_from< State > Stored>
Stack galaxy::StateMachine< Stored >::m_stack
protected

State stack.

Definition at line 153 of file StateMachine.hpp.

◆ m_storage

template<std::derived_from< State > Stored>
Map galaxy::StateMachine< Stored >::m_storage
protected

State cache.

Definition at line 158 of file StateMachine.hpp.


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