Design Pattern Examples
Overview of object-oriented design patterns
State_Exercise.cpp
Go to the documentation of this file.
1
6
7#include <iostream>
8
9#include "helpers/formatstring.h"
10#include "helpers/split.h"
11#include "helpers/stringlist.h"
12
13#include "State_Exercise.h"
14#include "State_Class.h"
15
16
17namespace // Anonymous
18{
19 using namespace DesignPatternExamples_cpp;
20
26 void _State_DisplayText(const std::string& textToDisplay)
27 {
28 StringList lines = Helpers::split(textToDisplay, "\n");
29 int lineNumber = 1;
30 for (std::string line : lines)
31 {
32 std::cout << Helpers::formatstring(" %2d) %s", lineNumber, line.c_str()) << std::endl;
33 ++lineNumber;
34 }
35 }
36
37} // end namespace Anonmyous
38
39
41{
42
56 // ! [Using State in C++]
58 {
59 std::cout << std::endl;
60 std::cout << "State Exercise" << std::endl;
61
62 StateContext_Class filterContext;
63 std::string textToFilter =
64 "/*#################### Block Comment #################################*/\n"
65 "//#################### Line Comment ####################################\n"
66 "// A comment. /* A nested comment */\n"
67 "\n"
68 "void State_Exercise() // An exercise in state machines\n"
69 "{\n"
70 " char character = '\\\"';\n"
71 " std::cout << std::endl;\n"
72 " std::cout << \"\\\"State\\\" /*Exercise*/\" << std::endl;\n"
73 "\n"
74 " StateContext_Class filterContext;\n"
75 "\n"
76 " std::cout << \"\\t\\tDone. //(No, really)//\" << std::endl;\n"
77 "}";
78
79 std::cout << " Text to filter:" << std::endl;
81
82 std::cout << " Filtering text..." << std::endl;
83 std::string filteredText = filterContext.RemoveComments(textToFilter);
84
85 std::cout << " Filtered text:" << std::endl;
86 _State_DisplayText(filteredText);
87
88 std::cout << " Done." << std::endl;
89 }
90 // ! [Using State in C++]
91
92} // end namespace
Declaration of the IStateContext and IStateBehavior interfaces, as well as the StateContext_Class cla...
static const char * textToFilter
static void _State_DisplayText(const char *textToDisplay)
Helper function to display text from the State exercise. Text is displayed with line numbers.
Wraps a private implementation of the state machine. The implementation maintains the context in whic...
Definition: State_Class.h:139
std::string RemoveComments(std::string text)
Entry point for callers to filter text. Removes C++-style line and block comments from the text.
Declaration of the State_Exercise() function as used in the State Pattern.
std::vector< std::string > StringList
Typedef for a vector of std::string.
The namespace containing all Design Pattern Examples implemented in C++.
void State_Exercise()
Example of using the State design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
std::vector< std::string > split(const char *pszString, const char *splitChars)
Split the given string into a list of strings given the character on which to split....
Definition: split.cpp:13