14#include "helpers/formatstring.h"
15#include "helpers/replace.h"
43 static void Command_Operation_Replace(Command_TextObject::shared_ptr_t source, std::string searchPattern, std::string replaceText)
45 std::string newText =
Helpers::Replace(source->Text(), searchPattern.c_str(), replaceText.c_str());
46 source->SetText(newText);
55 std::ostringstream output;
56 std::string text = source->Text();
57 for (
size_t index = 0; index < text.size(); ++index)
59 output << text[text.size() - 1 - index];
61 source->SetText(output.str());
85 (*commandIter).Execute();
91 lastCommand.
ToString().c_str(), text->ToString().c_str()) << std::endl;
108 command.
ToString().c_str(), text->ToString().c_str()) << std::endl;
124 command.
ToString().c_str(), text->ToString().c_str()) << std::endl;
147 std::cout << std::endl;
148 std::cout <<
"Command Exercise" << std::endl;
151 Command_TextObject::shared_ptr_t text = std::make_shared<Command_TextObject>(
"This is a line of text on which to experiment.");
161 std::cout <<
" Now perform undo until back to original" << std::endl;
171 std::cout <<
" Done." << std::endl;
Implementation of the Command_TextObject and Command classes used in the Command Pattern.
void Command_Save_And_Execute(Command *command)
Save the given command on the undo list then execute the command on the text object with which the co...
static void Command_ApplyReplaceCommand(Command_TextObject *text, const char *searchPattern, const char *replaceText)
Helper function to create a Command object that replaces text in the given Command_TextObject,...
static StackEntry * _commandUndoList
The stack used to remember the commands for undo.
static void Command_Operation_Replace(Command_TextObject *source, const char *searchPattern, const char *replaceText)
An operation to search and replace text in a Command_TextObject.
static void Command_ApplyReverseCommand(Command_TextObject *text)
Helper function to create a Command object that reverses the order of the characters in the given Com...
static void Command_Undo(Command_TextObject *text)
Perform an undo on the given Command_TextObject, using the commands in the "global" undo list....
static void Command_Operation_Reverse(Command_TextObject *source)
An operation to reverse the characters in the given Command_TextObject.
Represents an operation that can be applied to a TextObject. This class can handle two kinds of opera...
void Execute()
Execute the command on the TextObject.
std::string ToString()
Convert this command to a string representation.
Declaration of the Command_Exercise() function as used in the Command Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void Command_Exercise()
Example of using the Command 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...