-
Notifications
You must be signed in to change notification settings - Fork 0
/
l_parser.h
330 lines (283 loc) · 7.95 KB
/
l_parser.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* l_parser.h
* Copyright (C) 2011 Daniel van den Akker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef L_PARSER_INCLUDED
#define L_PARSER_INCLUDED
#include <map>
#include <string>
#include <set>
#include <exception>
/**
* \brief The namespace used by the LParser
*/
namespace LParser
{
/**
* \brief The exception thrown when an invalid file is read
*/
class ParserException: public std::exception
{
private:
/**
* \brief The message explaining what went wrong
*/
std::string message;
public:
/**
* \brief Constructor
*
* \param msg String explaining what went wrong
* \param line The line in the file at which the parser failed
* \param pos The position on the line at which the parser failed
*/
ParserException(std::string const& msg, unsigned int line, unsigned int pos);
/**
* \brief Copy Constructor
*
* \param original The exception to be copied
*/
ParserException(const ParserException &original);
/**
* \brief Destructor
*/
virtual ~ParserException() throw ();
/**
* \brief Assignment operator
*
* \param original The original exception to be assigned to this one
*/
ParserException& operator=(const ParserException &original);
/**
* \brief Returns a description of the error hat occurred.
*
* \return A description of the error hat occurred.
*/
virtual const char *what() const throw ();
};
/**
* \brief This is the Base Class used by LParser2D and LParser3D
*/
class LSystem
{
protected:
/**
* \brief Constructor: creates an empty LSystem
*/
LSystem();
/**
* \brief Copy-constructor: creates a new L-System from an existing L-System
*
* \param system The L-System to be copied
*/
LSystem(LSystem const& system);
/**
* \brief Destructor
*/
virtual ~LSystem();
/**
*\brief Assignment operator
*
* \param system the L-System to be copied
*/
LSystem& operator=(LSystem const& system);
public:
/**
* \brief returns the Alphabet of the L-System
*
* \return a const reference to the vector containing the alphabet
*/
std::set<char> const& get_alphabet() const;
/**
* \brief Draw function. Returns true if a line needs to be drawn for this character
*
* \param c the character of the alphabet
*
* \return whether a line needs to be drawn for the character
*/
bool draw(char c) const;
/**
* \brief Replacement function. Returns the replacement string for a given character of the Alphabet
*
* \param c the character of the alphabet
*
* \return replacement string
*/
std::string const& get_replacement(char c) const;
/**
* \brief Returns the angle of the L-System.
*
* \returns the angle used by the LSystem
*/
double get_angle() const;
/**
* \brief Returns the initiator string of the L-System
*
* \return the inititor string of the L-System
*/
std::string const& get_initiator() const;
/**
* \brief Retrurns the number of times a symbol must be replaced by it's replacement string
*
* \return the number of replacements;
*/
unsigned int get_nr_iterations() const;
protected:
/**
* \brief the alphabet of the l-system
*/
std::set<char> alphabet;
/**
* \brief the draw function mapping of the l-system
*/
std::map<char, bool> drawfunction;
/**
* \brief the initiator stringof the l-system
*/
std::string initiator;
/**
* \brief the angle of the l-system
*/
double angle;
/**
* \brief the replacement rules of the l-system
*/
std::map<char, std::string> replacementrules;
/**
* \brief the number of replacements of the l-system
*/
unsigned int nrIterations;
};
class LSystem2D;
/**
* \brief Writes an LSystem2D to an output stream.
*
* \param out The outputstream to write the LSystem2D to
* \param system The L-System to be written
*
* \return The outputstream the L-System was written to
*/
std::ostream& operator<<(std::ostream& out, LSystem2D const& system);
/**
* \brief Reads an LSystem2D from an output stream.
*
* \param in The input stream to read the LSystem2D from
* \param system The L-System object in which the parsed LSystem is to be stored
*
* \return The input stream from which the L-System was read
*/
std::istream& operator>>(std::istream& in, LSystem2D& system);
/**
* \brief This class represents a 2D-LSystem
*/
class LSystem2D: public LSystem
{
public:
/**
* \brief Constructor
* */
LSystem2D();
/**
* \brief Copy Constructor
*
* \param system The L-System to be copied
*/
LSystem2D(LSystem2D const& system);
/**
* \brief Constructor: reads the LSystem from an input stream
*
* \param in The input stream from which the L-System is to be read
*/
LSystem2D(std::istream& in);
/*
* \brief Destructor
*/
virtual ~LSystem2D();
/**
* \brief Assignment operator. Assigns another LSystem to this object
*
* \param system The L-System to be assigned to this object
*/
LSystem2D& operator=(LSystem2D const& system);
/**
* \brief Returns the starting angle of the 2D L-System
*
* \return the starting angle of the L-System
* */
double get_starting_angle() const;
protected:
friend std::istream& operator>>(std::istream& in, LSystem2D& system);
/**
* \brief the starting angle of the 2D-LSystem
*/
double startingAngle;
};
class LSystem3D;
/**
* \brief Writes an LSystem3D to an output stream.
*
* \param out The outputstream to write the LSystem2D to
* \param system The L-System to be written
*
* \return The outputstream the L-System was written to
*/
std::ostream& operator<<(std::ostream& out, LSystem3D const& system);
/**
* \brief Reads an LSystem3D from an output stream.
*
* \param in The input stream to read the LSystem2D from
* \param system The L-System object in which the parsed LSystem is to be stored
*
* \return The input stream from which the L-System was read
*/
std::istream& operator>>(std::istream& in, LSystem3D& system);
/**
* \brief This class represents a 3D-LSystem
*/
class LSystem3D: public LSystem
{
public:
/**
* \brief Constructor
* */
LSystem3D();
/**
* \brief Copy Constructor
*
* \param system The L-System to be assigned to this object
*/
LSystem3D(LSystem3D const& system);
/**
* \brief Constructor: reads the LSystem from an input stream
*
* \param in The input stream from which the LSystem is to be read
*/
LSystem3D(std::istream& in);
/**
* \brief Destructor
*/
virtual ~LSystem3D();
/**
* \brief Assignment operator
*
* \param system The L-System to be assigned to this object
*/
LSystem3D& operator=(LSystem3D const& system);
private:
friend std::istream& operator>>(std::istream& in, LSystem3D& system);
};
}
#endif //L_PARSER_INCLUDED