Design Pattern Examples
Overview of object-oriented design patterns
State_Class.h
Go to the documentation of this file.
1
7
8#pragma once
9#ifndef __STATE_CLASS_H__
10#define __STATE_CLASS_H__
11
12#include <memory>
13#include <string>
14
15
17{
18
23 {
34 Done
35 };
36
37
38 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
39 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
40 // Interface definitions
41 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
42 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
43
47 const char EOF_CHAR = static_cast<char>(0xff);
48
49
50 //########################################################################
51 //########################################################################
52
53
61 {
65 using shared_ptr_t = std::shared_ptr<IStateContext>;
66
70 virtual ~IStateContext() {}
71
77 virtual char GetNextCharacter() = 0;
78
84 virtual void OutputCharacter(char character) = 0;
85 };
86
87
88 //########################################################################
89 //########################################################################
90
91
103 {
107 using shared_ptr_t = std::shared_ptr<IStateBehavior>;
108
112 virtual ~IStateBehavior() {}
113
114 virtual CurrentState GoNext(IStateContext* context) = 0;
115 };
116
117
118
119
120 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
121 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
122 // State context definition
123 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
124 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125
126
139 {
140 private:
144 std::unique_ptr<IStateContext> _stateContextimpl;
145
146 public:
151
152
153 //--------------------------------------------------------------------
154 // StateContext_Class public entry points.
155 //--------------------------------------------------------------------
156
163 std::string RemoveComments(std::string text);
164 };
165
166} // end namespace
167
168#endif // __STATE_CLASS_H__
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.
std::unique_ptr< IStateContext > _stateContextimpl
Pointer to the actual implementation.
Definition: State_Class.h:144
The namespace containing all Design Pattern Examples implemented in C++.
const char EOF_CHAR
Indicates End-of-file (no more data available).
Definition: State_Class.h:47
CurrentState
Represents the current state of the state machine.
Definition: State_Class.h:23
@ SingleQuotedText
‘’` transitions to EscapedSingleQuoteText, \ transitions to NormalText, EOF_CHAR transitions to Done
Definition: State_Class.h:27
@ DoubleQuotedText
\ transitions to EscapedDoubleQuoteText, " transitions to NormalText, EOF_CHAR transitions to Done
Definition: State_Class.h:26
@ BlockComment
* transitions to EndBlockComment, EOF_CHAR transitions to Done
Definition: State_Class.h:32
@ Initial
State before the state machine actually starts. transitions to NormalText.
Definition: State_Class.h:24
@ EndBlockComment
/ transitions to NormalText, EOF_CHAR transitions to Done, all else transitions to BlockComment
Definition: State_Class.h:33
@ LineComment
\\n transitions to NormalText, EOF_CHAR transitions to Done
Definition: State_Class.h:31
@ Done
Indicates processing is done.
Definition: State_Class.h:34
@ StartComment
/ transitions to LineComment, * transitions to BlockComment, EOF_CHAR transitions to Done,...
Definition: State_Class.h:30
@ NormalText
" transitions to QuotedText, / transitions to StartComment, EOF_CHAR transitions to Done
Definition: State_Class.h:25
@ EscapedDoubleQuoteText
\ transitions to QuotedText, EOF_CHAR transitions to Done
Definition: State_Class.h:28
@ EscapedSingleQuoteText
\ transitions to SingleQuotedText, EOF_CHAR transitions to Done
Definition: State_Class.h:29
Represents a class that implements one state of the state machine.
Definition: State_Class.h:103
std::shared_ptr< IStateBehavior > shared_ptr_t
Alias to make using a shared pointer easier.
Definition: State_Class.h:107
virtual ~IStateBehavior()
Virtual destructor as required for interfaces.
Definition: State_Class.h:112
virtual CurrentState GoNext(IStateContext *context)=0
Represents the context as passed to each state class.
Definition: State_Class.h:61
std::shared_ptr< IStateContext > shared_ptr_t
Alias to make using a shared pointer easier.
Definition: State_Class.h:65
virtual void OutputCharacter(char character)=0
Write the character to the context-> This is how the parser accumulates the filtered text.
virtual char GetNextCharacter()=0
Get the next character from the input.
virtual ~IStateContext()
Virtual destructor as required for interfaces.
Definition: State_Class.h:70