Design Pattern Examples
Overview of object-oriented design patterns
Memento.h
Go to the documentation of this file.
1
5#pragma once
6#ifndef __MEMENTO_H__
7#define __MEMENTO_H__
8
9#include <memory>
10#include <string>
11
13{
14
20 struct IMemento
21 {
25 using shared_ptr_t = std::shared_ptr<IMemento>;
26
30 virtual ~IMemento() {}
31
38 virtual std::string Name() = 0;
39 };
40
41
42 //########################################################################
43 //########################################################################
44
45
54 {
55 private:
56 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57 // Inner class Memento
58
64 class Memento : public IMemento
65 {
70 std::string _name;
71
76 std::string _text;
77
78 public:
84 Memento(std::string name, std::string text)
85 {
86 _name = name;
87 _text = text;
88 }
89
90
96 std::string Text()
97 {
98 return _text;
99 }
100
105 std::string Name() override
106 {
107 return _name;
108 }
109 };
110
111 // End of Inner class Memento
112 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
113 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
114
115
116 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
117 // Private data of the Memento_TextObject class.
118
122 std::string _text;
123
124 // End private data of the Memento_TextObject class.
125 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
126 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
127
128 public:
129
134 Memento_TextObject(std::string text)
135 {
136 _text = text;
137 }
138
142 std::string Text()
143 {
144 return _text;
145 }
146
150 void SetText(const std::string& value)
151 {
152 _text = value;
153 }
154
159 IMemento::shared_ptr_t GetMemento(std::string operationName)
160 {
161 return std::make_shared<Memento>(operationName, _text);
162 }
163
164
171 {
172 if (memento != nullptr)
173 {
174 _text = dynamic_cast<Memento*>(memento.get())->Text();
175 }
176 }
177
178
184 std::string ToString()
185 {
186 return _text;
187 }
188 };
189
190} // end namespace
191
192#endif // __MEMENTO_H__
193
Represents a single memento (snapshot) of the text state before an operation is applied....
Definition: Memento.h:65
std::string Text()
The saved text in this memento. This is accessible only by the Memento_TextObject class since it is t...
Definition: Memento.h:96
Memento(std::string name, std::string text)
Constructor.
Definition: Memento.h:84
std::string _name
The name of this memento (really just the name of the operation that triggered the need for this meme...
Definition: Memento.h:70
std::string Name() override
The name of this memento. This is seen as the operation that triggered the need for the memento.
Definition: Memento.h:105
std::string _text
The snapshot of the text data as stored in the Memento_TextObject class instance.
Definition: Memento.h:76
Container for a string. Need to use a class that allows the text to be changed while the container (t...
Definition: Memento.h:54
Memento_TextObject(std::string text)
Constructs a text object with an initial string.
Definition: Memento.h:134
std::string Text()
Gets the text in this TextObject.
Definition: Memento.h:142
std::string ToString()
Converts the Memento_TextObject to a string (makes it easier to use the class in string formatting).
Definition: Memento.h:184
void RestoreMemento(IMemento::shared_ptr_t memento)
Sets the text in this class instance to the snapshot stored in the given IMemento object (which is as...
Definition: Memento.h:170
void SetText(const std::string &value)
Sets the text in this TextObject.
Definition: Memento.h:150
IMemento::shared_ptr_t GetMemento(std::string operationName)
Returns an IMemento object containing a snapshot of the text stored in this class instance.
Definition: Memento.h:159
std::string _text
The text that can change in this Memento_TextObject class.
Definition: Memento.h:122
The namespace containing all Design Pattern Examples implemented in C++.
Represents a single memento, a single snapshot of the state of the Memento_TextObject class as repres...
Definition: Memento.h:21
std::shared_ptr< IMemento > shared_ptr_t
Alias to make working with a shared pointer easier to type.
Definition: Memento.h:25
virtual ~IMemento()
Virtual destructor.
Definition: Memento.h:30
virtual std::string Name()=0
The name of the memento (snapshot). Useful for displaying a list of mementos in an undo list....