State Header

namespace state_pattern

Namespace for the State pattern.

class ConcreteStateA : public state_pattern::State
#include <state.hpp>

Public Functions

virtual void handle(Context &context) const override

Handle the request and optionally change the state.

Parameters:

context – Reference to the context object.

Public Static Functions

static std::string getName()

Get the name of this state.

Returns:

State name as a string.

class ConcreteStateB : public state_pattern::State
#include <state.hpp>

Public Functions

virtual void handle(Context &context) const override

Handle the request and optionally change the state.

Parameters:

context – Reference to the context object.

Public Static Functions

static std::string getName()

Get the name of this state.

Returns:

State name as a string.

class Context
#include <state.hpp>

Public Functions

explicit Context(std::unique_ptr<State> initialState)

Constructor that initializes the context with an initial state.

Parameters:

initialState – Initial state of the context.

void setState(std::unique_ptr<State> newState)

Sets the state of the context.

Parameters:

newState – New state to transition to.

void request()

Triggers the handle method of the current state.

std::string getStateName() const

Gets the name of the current state (for testing/logging purposes).

Returns:

Name of the current state.

Private Members

std::unique_ptr<State> state

Current state of the context.

class State
#include <state.hpp>

Subclassed by state_pattern::ConcreteStateA, state_pattern::ConcreteStateB

Public Functions

virtual ~State() = default
virtual void handle(Context &context) const = 0

Handle the request and optionally change the state.

Parameters:

context – Reference to the context object.

Inner Classes

Inner Namespaces