11#include "helpers/formatstring.h"
12#include "helpers/replace.h"
58 std::ostringstream output;
59 std::string text = source.
Text();
60 for (
size_t index = 0; index < text.size(); ++index)
62 output << text[text.size() - 1 - index];
83 lastMemento->Name().c_str(), text.
ToString().c_str())
99 searchPattern.c_str(), replaceText.c_str());
116 std::string operationName =
"Reverse";
157 std::cout << std::endl;
158 std::cout <<
"Memento Exercise" << std::endl;
176 std::cout <<
" Now perform undo until back to original" << std::endl;
188 std::cout <<
" Done." << std::endl;
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...
std::string Text()
Gets the text in this TextObject.
std::string ToString()
Converts the Memento_TextObject to a string (makes it easier to use the class in string formatting).
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...
void SetText(const std::string &value)
Sets the text in this TextObject.
IMemento::shared_ptr_t GetMemento(std::string operationName)
Returns an IMemento object containing a snapshot of the text stored in this class instance.
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...
std::shared_ptr< IMemento > shared_ptr_t
Alias to make working with a shared pointer easier to type.