Memento Header

namespace memento_pattern

Namespace for the Memento pattern.

class Caretaker
#include <memento.hpp>

The Caretaker class manages Memento objects.

Public Functions

inline void addMemento(std::unique_ptr<Memento> memento)

Adds a Memento to the list.

Parameters:

memento – The Memento to add.

inline const Memento &getMemento(size_t index) const

Retrieves a Memento by index.

Parameters:

index – The index of the desired Memento.

Throws:

std::out_of_range – if the index is invalid.

Returns:

A reference to the Memento at the specified index.

Private Members

std::vector<std::unique_ptr<Memento>> mementos

List of saved Mementos.

class Memento
#include <memento.hpp>

The Memento class stores the state of the Originator.

Public Functions

inline explicit Memento(const std::string &state)

Constructor to initialize the Memento with a state.

Parameters:

state – The state to save.

inline std::string getState() const

Retrieves the saved state.

Returns:

The saved state as a string.

Private Members

std::string state

Saved state of the Originator.

class Originator
#include <memento.hpp>

The Originator class creates and restores Memento objects.

Public Functions

inline void setState(const std::string &newState)

Sets the state of the Originator.

Parameters:

newState – The new state.

inline std::string getState() const

Retrieves the current state of the Originator.

Returns:

The current state as a string.

inline std::unique_ptr<Memento> createMemento() const

Creates a Memento containing the current state.

Returns:

A unique pointer to a Memento.

inline void restoreMemento(const Memento &memento)

Restores the state from a given Memento.

Parameters:

memento – The Memento to restore from.

Private Members

std::string state

Current state of the Originator.

Inner Classes

Inner Namespaces