-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrategy.cpp
More file actions
230 lines (197 loc) · 5.26 KB
/
strategy.cpp
File metadata and controls
230 lines (197 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*!
* Adapted from "Design Patterns in Modern C++"
* Dmitri Nesteruk (Apress, 2018).
*/
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
#include <vector>
enum class OutputFormat
{
Md,
Html,
Latex,
RawText
};
/*!
* @brief Strategy
* Declares a common interface for all supported algorithms.
* Implemented by ConcreteStrategy.
*/
class ListStrategy
{
public:
virtual ~ListStrategy() = default;
virtual void addItem(std::ostringstream& p_oss, const std::string& p_item) = 0;
/*!
* Some formats actually do not have any start/end markers
* for lists, so the ListStrategy offers default empty
* implementations for those.
*/
virtual void start (std::ostringstream& p_oss) {}
virtual void end (std::ostringstream& p_oss) {}
};
/*!
* @brief ConcreteStrategy
* Implements the algorithms using the Strategy interface.
*
* A Markdown list item is written the following way :
* // No start marker
* * item1
* * item2
* // No end marker
*/
class MdListStrategy : public ListStrategy
{
public:
void addItem(std::ostringstream& p_oss, const std::string& p_item) override
{
p_oss << " * " << p_item << "\n";
}
};
/*!
* @brief ConcreteStrategy
* Implements the algorithms using the Strategy interface.
*
* A html list is written the following way :
* <ul> // Start the list
* <li>item1</li>
* <li>item2</li>
* </ul> // End the list
*/
class HtmlListStrategy : public ListStrategy
{
public:
void start (std::ostringstream& p_oss) override { p_oss << "<ul>\n"; }
void end (std::ostringstream& p_oss) override { p_oss << "</ul>\n"; }
void addItem(std::ostringstream& p_oss, const std::string& p_item) override
{
p_oss << "<li>" << p_item << "</li>\n";
}
};
/*!
* @brief ConcreteStrategy
* Implements the algorithms using the Strategy interface.
*
* A Latex list is written the following way :
* \begin{itemize} // Start the list
* \item item1
* \item item2
* \end{itemize} // End the list
*/
class LatexListStrategy : public ListStrategy
{
public:
void start (std::ostringstream& p_oss) override { p_oss << "\\begin{itemize}\n"; }
void end (std::ostringstream& p_oss) override { p_oss << "\\end{itemize}\n"; }
void addItem(std::ostringstream& p_oss, const std::string& p_item) override
{
p_oss << "\\item " << p_item << "\n";
}
};
/*!
* @brief ConcreteStrategy
* Implements the algorithms using the Strategy interface.
*
* A raw text list is written the following way :
* // No start marker
* - item1
* - item2
* // No end marker
*/
class RawTextListStrategy : public ListStrategy
{
public:
void addItem(std::ostringstream& p_oss, const std::string& p_item) override
{
p_oss << "\t- " << p_item << "\n";
}
};
/*!
* @brief Context
* Configured with a ConcreteStrategy object.
* Maintains a reference to that ConcreteStrategy object.
* Calls its interface to use the algorithm defined
* by the Strategy.
*/
class TextProcessor
{
public:
TextProcessor( const OutputFormat p_format = OutputFormat::RawText ) :
m_list(std::make_unique<RawTextListStrategy>())
{}
void setFormat( const OutputFormat p_format )
{
switch( p_format )
{
case OutputFormat::Html : m_list = std::make_unique<HtmlListStrategy >(); break;
case OutputFormat::Latex : m_list = std::make_unique<LatexListStrategy >(); break;
case OutputFormat::Md : m_list = std::make_unique<MdListStrategy >(); break;
case OutputFormat::RawText: m_list = std::make_unique<RawTextListStrategy>(); break;
}
}
void clear(void)
{
m_oss.str("");
m_oss.clear();
}
void append_list(const std::vector<std::string>& p_items)
{
m_list->start(m_oss);
for ( auto& l_item : p_items )
m_list->addItem(m_oss, l_item);
m_list->end(m_oss);
}
std::string str(void) const { return m_oss.str(); }
private:
std::ostringstream m_oss;
std::unique_ptr<ListStrategy> m_list;
};
int main()
{
TextProcessor myList;
std::vector<std::string> myItems = {"item1", "item2", "item3"};
// Raw text List (default behaviour)
myList.append_list(myItems);
std::cout << "\n----- RAW -----\n" << myList.str() << "\n";
myList.clear();
// Markdown List
myList.setFormat(OutputFormat::Md);
myList.append_list(myItems);
std::cout << "\n----- MARKDOWN -----\n" << myList.str() << "\n";
myList.clear();
// HTML List
myList.setFormat(OutputFormat::Html);
myList.append_list(myItems);
std::cout << "\n----- HTML -----\n" << myList.str() << "\n";
myList.clear();
// Latex List
myList.setFormat(OutputFormat::Latex);
myList.append_list(myItems);
std::cout << "\n----- LATEX -----\n" << myList.str() << "\n";
myList.clear();
return 0;
}
/*
----- RAW -----
- item1
- item2
- item3
----- MARKDOWN -----
* item1
* item2
* item3
----- HTML -----
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
</ul>
----- LATEX -----
\begin{itemize}
\item item1
\item item2
\item item3
\end{itemize}
*/