Design Pattern Examples
Overview of object-oriented design patterns
Memento_Exercise.cpp
Go to the documentation of this file.
1
6
7#include <iostream>
8#include <sstream>
9#include <vector>
10
11#include "helpers/formatstring.h"
12#include "helpers/replace.h"
13
14#include "Memento_Exercise.h"
15#include "Memento.h"
16
17namespace // Anonymous
18{
19 using namespace DesignPatternExamples_cpp;
20
25 std::vector<IMemento::shared_ptr_t> _mementoUndoList;
26
34 void Memento_SaveForUndo(Memento_TextObject& text, std::string operation)
35 {
36 IMemento::shared_ptr_t memento = text.GetMemento(operation);
37 _mementoUndoList.push_back(memento);
38 }
39
40
47 void Memento_Operation_Replace(Memento_TextObject& source, std::string searchPattern, std::string replaceText)
48 {
49 source.SetText(Helpers::Replace(source.Text(), searchPattern.c_str(), replaceText.c_str()));
50 }
51
57 {
58 std::ostringstream output;
59 std::string text = source.Text();
60 for (size_t index = 0; index < text.size(); ++index)
61 {
62 output << text[text.size() - 1 - index];
63 }
64 source.SetText(output.str());
65 }
66
73 {
74 if (!_mementoUndoList.empty())
75 {
76 IMemento::shared_ptr_t lastMemento = _mementoUndoList.back();
77 _mementoUndoList.pop_back();
78 text.RestoreMemento(lastMemento);
79
80 // Show off what we (un)did.
81 std::cout
82 << Helpers::formatstring(" undoing operation %-31s: \"%s\"",
83 lastMemento->Name().c_str(), text.ToString().c_str())
84 << std::endl;
85 }
86 }
87
96 void Memento_ApplyReplaceOperation(Memento_TextObject& text, std::string searchPattern, std::string replaceText)
97 {
98 std::string operationName = Helpers::formatstring("Replace '%s' with '%s'",
99 searchPattern.c_str(), replaceText.c_str());
100 Memento_SaveForUndo(text, operationName);
101 Memento_Operation_Replace(text, searchPattern, replaceText);
102 std::cout
103 << Helpers::formatstring(" operation %-31s: \"%s\"", operationName.c_str(), text.ToString().c_str())
104 << std::endl;
105 }
106
107
115 {
116 std::string operationName = "Reverse";
117 Memento_SaveForUndo(text, operationName);
119 std::cout
120 << Helpers::formatstring(" operation %-31s: \"%s\"", operationName.c_str(), text.ToString().c_str())
121 << std::endl;
122 }
123
124} // end namespace Anonmyous
125
126
128{
129
154 // ! [Using Memento in C++]
156 {
157 std::cout << std::endl;
158 std::cout << "Memento Exercise" << std::endl;
159
160 // Start with a fresh undo list.
161 _mementoUndoList.clear();
162
163 // The base text object to work from.
164 Memento_TextObject text("This is a line of text on which to experiment.");
165
166 std::cout
167 << Helpers::formatstring(" Starting text: \"%s\"", text.ToString().c_str())
168 << std::endl;
169
170 // Apply four operations to the text.
171 Memento_ApplyReplaceOperation(text, "text", "painting");
172 Memento_ApplyReplaceOperation(text, "on", "off");
174 Memento_ApplyReplaceOperation(text, "i", "!");
175
176 std::cout << " Now perform undo until back to original" << std::endl;
177
178 // Now undo the four operations.
179 Memento_Undo(text);
180 Memento_Undo(text);
181 Memento_Undo(text);
182 Memento_Undo(text);
183
184 std::cout
185 << Helpers::formatstring(" Final text : \"%s\"", text.ToString().c_str())
186 << std::endl;
187
188 std::cout << " Done." << std::endl;
189 }
190 // ! [Using Memento in C++]
191
192} // end namespace
Implementation of the Memento_TextObject class used in the Memento Pattern.
static void Memento_Undo(Memento_TextObject *text)
Perform an undo on the given Command_TextObject, using the mementos in the "global" undo list....
static void Memento_ApplyReverseOperation(Memento_TextObject *text)
Helper function to reverse the order of the characters in the given Memento_TextObject after adding a...
static void Memento_Operation_Replace(Memento_TextObject *source, const char *searchPattern, const char *replaceText)
An operation to search and replace text in a Memento_TextObject.
static void Memento_SaveForUndo(Memento_TextObject *text, const char *operation)
Take a snapshot of the given text object associated with the name of given operation.
static void Memento_ApplyReplaceOperation(Memento_TextObject *text, const char *searchPattern, const char *replaceText)
Helper function to replace a pattern with another string in the given Memento_TextObject after adding...
static StackEntry * _mementoUndoList
The list of memento objects that form a series of snapshots in time of a Memento_TextObject.
static void Memento_Operation_Reverse(Memento_TextObject *source)
An operation to reverse the characters in the given Memento_TextObject.
Container for a string. Need to use a class that allows the text to be changed while the container (t...
Definition: Memento.h:54
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
Declaration of the Memento_Exercise() function as used in the Memento Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void Memento_Exercise()
Example of using the Memento design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
std::string Replace(const std::string &s, const char *str1, const char *str2, bool bCaseInsensitive)
Replace all occurrences of narrow string str1 with narrow string str2 in s. If str2 is empty then all...
Definition: replace.cpp:47
std::shared_ptr< IMemento > shared_ptr_t
Alias to make working with a shared pointer easier to type.
Definition: Memento.h:25