OpenWalnut  1.5.0dev
WProjectFileIO.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WPROJECTFILEIO_H
26 #define WPROJECTFILEIO_H
27 
28 #include <memory>
29 #include <ostream>
30 #include <string>
31 #include <vector>
32 
33 
34 #include "WProperties.h"
35 
36 class WProjectFile;
37 
38 /**
39  * A base class for all parts of OpenWalnut which can be serialized to a project file. It is used by WProjectFile to actually parse the file line
40  * by line. Derive from this class if you write your own parser and use it to fill your internal data structures. But write it in a very
41  * error-tolerant way. We want to avoid that small problems in the project file cause the whole file to be useless.
42  *
43  * In general, each IO implementation has the chance to parse each line. After parsing all lines, the done method gets called. This method should
44  * contain code to actually apply the settings loaded. You should avoid doing this in the parse method itself.
45  */
46 class WProjectFileIO // NOLINT
47 {
48 public:
49  /**
50  * Abbreviation for a shared pointer.
51  */
52  typedef std::shared_ptr< WProjectFileIO > SPtr;
53 
54  /**
55  * Abbreviation for const shared pointer.
56  */
57  typedef std::shared_ptr< const WProjectFileIO > ConstSPtr;
58 
59  /**
60  * Default constructor.
61  */
63 
64  /**
65  * Destructor.
66  */
67  virtual ~WProjectFileIO();
68 
69  /**
70  * This method parses the specified line and interprets it. It gets called line by line by WProjectFile. You should avoid applying anything
71  * of the loaded information here. You should use \ref done for this.
72  *
73  * \param line the current line as string
74  * \param lineNumber the current line number. Useful for error/warning/debugging output.
75  *
76  * \return true if the line could be parsed.
77  */
78  virtual bool parse( std::string line, unsigned int lineNumber ) = 0;
79 
80  /**
81  * Called whenever the end of the project file has been reached. Use this to actually apply your loaded settings. Do this in a error-tolerant
82  * way and apply as most settings as possible even if some other settings are erroneous. Add errors with \ref addError. Try avoiding
83  * exceptions if possible.
84  */
85  virtual void done();
86 
87  /**
88  * Saves the state to the specified stream.
89  *
90  * \param output the stream to print the state to.
91  */
92  virtual void save( std::ostream& output ) = 0; // NOLINT
93 
94  /**
95  * Checks whether there where errors during load or save.
96  *
97  * \return true if there where.
98  */
99  bool hadErrors() const;
100 
101  /**
102  * Get error list.
103  *
104  * \return the list
105  */
106  const std::vector< std::string >& getErrors() const;
107 
108  /**
109  * Checks whether there where warnings during load or save.
110  *
111  * \return true if there where.
112  */
113  bool hadWarnings() const;
114 
115  /**
116  * Get warnings list.
117  *
118  * \return the list
119  */
120  const std::vector< std::string >& getWarnings() const;
121 
122  /**
123  * Create a clone of the IO. This is especially useful for custom parsers registered at \ref WProjectFile::registerParser. Implement this
124  * function.
125  *
126  * \param project the project file using this parser instance.
127  *
128  * \return Cloned instance.
129  */
130  virtual SPtr clone( WProjectFile* project ) const = 0;
131 
132  /**
133  * Set the project using this parser
134  *
135  * \param project the project
136  */
137  void setProject( WProjectFile* project );
138 
139  /**
140  * When to apply this parser. This might be important in some cases. Note that you can only decide
141  * whether you want to apply your changes before or after the modules have been added.
142  */
144  {
145  PRE_MODULES = 0,
146  POST_MODULES
147  };
148 
149  /**
150  * Return the apply order of this IO.
151  *
152  * \return the order
153  */
154  ApplyOrder getApplyOrder() const;
155 
156 protected:
157  /**
158  * Add an error. Use this when you encounter some difficulties during parsing or applying settings. Provide useful errors. They will be
159  * presented to the user.
160  *
161  * \param description the error description
162  */
163  void addError( std::string description );
164 
165  /**
166  * Add an warning. Use this when you encounter some difficulties during parsing or applying settings. Provide useful warnings. They will be
167  * presented to the user.
168  *
169  * \param description the error description
170  */
171  void addWarning( std::string description );
172 
173  /**
174  * Recursively prints the properties and nested properties.
175  *
176  * \param output the output stream to print to
177  * \param props the properties to recursively print
178  * \param indent the indentation level
179  * \param prefix the prefix (name prefix of property)
180  * \param index the ID to use
181  * \param indexPrefix use this to add a prefix to the index
182  */
183  void printProperties( std::ostream& output, std::shared_ptr< WProperties > props, std::string indent, //NOLINT ( non-const ref )
184  std::string prefix, unsigned int index, std::string indexPrefix = "" );
185 
186 
187  /**
188  * Set the order of calls to "done".
189  *
190  * \param order the order.
191  */
192  void setApplyOrder( ApplyOrder order );
193 
194  /**
195  * The project using this parser.
196  *
197  * \return the project
198  */
199  WProjectFile* getProject() const;
200 
201 private:
202  /**
203  * List of errors if any.
204  */
205  std::vector< std::string > m_errors;
206 
207  /**
208  * List of warnings if any.
209  */
210  std::vector< std::string > m_warnings;
211 
212  /**
213  * The project using this parser
214  */
216 
217  /**
218  * The order in which the "done" functions are called.
219  */
221 };
222 
223 #endif // WPROJECTFILEIO_H
224 
A base class for all parts of OpenWalnut which can be serialized to a project file.
std::vector< std::string > m_errors
List of errors if any.
virtual SPtr clone(WProjectFile *project) const =0
Create a clone of the IO.
void setProject(WProjectFile *project)
Set the project using this parser.
std::vector< std::string > m_warnings
List of warnings if any.
WProjectFile * m_project
The project using this parser.
ApplyOrder getApplyOrder() const
Return the apply order of this IO.
bool hadWarnings() const
Checks whether there where warnings during load or save.
WProjectFileIO()
Default constructor.
virtual void done()
Called whenever the end of the project file has been reached.
const std::vector< std::string > & getErrors() const
Get error list.
bool hadErrors() const
Checks whether there where errors during load or save.
virtual ~WProjectFileIO()
Destructor.
ApplyOrder
When to apply this parser.
void setApplyOrder(ApplyOrder order)
Set the order of calls to "done".
virtual void save(std::ostream &output)=0
Saves the state to the specified stream.
WProjectFile * getProject() const
The project using this parser.
ApplyOrder m_applyOrder
The order in which the "done" functions are called.
virtual bool parse(std::string line, unsigned int lineNumber)=0
This method parses the specified line and interprets it.
std::shared_ptr< const WProjectFileIO > ConstSPtr
Abbreviation for const shared pointer.
std::shared_ptr< WProjectFileIO > SPtr
Abbreviation for a shared pointer.
void addError(std::string description)
Add an error.
void addWarning(std::string description)
Add an warning.
void printProperties(std::ostream &output, std::shared_ptr< WProperties > props, std::string indent, std::string prefix, unsigned int index, std::string indexPrefix="")
Recursively prints the properties and nested properties.
const std::vector< std::string > & getWarnings() const
Get warnings list.
Class loading project files.
Definition: WProjectFile.h:50