OpenWalnut  1.4.0
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 <ostream>
29 #include <string>
30 #include <vector>
31 
32 #ifndef Q_MOC_RUN
33 #include <boost/shared_ptr.hpp>
34 #endif
35 
36 #include "WProperties.h"
37 
38 class WProjectFile;
39 
40 /**
41  * 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
42  * 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
43  * error-tolerant way. We want to avoid that small problems in the project file cause the whole file to be useless.
44  *
45  * In general, each IO implementation has the chance to parse each line. After parsing all lines, the done method gets called. This method should
46  * contain code to actually apply the settings loaded. You should avoid doing this in the parse method itself.
47  */
48 class WProjectFileIO // NOLINT
49 {
50 public:
51  /**
52  * Abbreviation for a shared pointer.
53  */
54  typedef boost::shared_ptr< WProjectFileIO > SPtr;
55 
56  /**
57  * Abbreviation for const shared pointer.
58  */
59  typedef boost::shared_ptr< const WProjectFileIO > ConstSPtr;
60 
61  /**
62  * Default constructor.
63  */
65 
66  /**
67  * Destructor.
68  */
69  virtual ~WProjectFileIO();
70 
71  /**
72  * This method parses the specified line and interprets it. It gets called line by line by WProjectFile. You should avoid applying anything
73  * of the loaded information here. You should use \ref done for this.
74  *
75  * \param line the current line as string
76  * \param lineNumber the current line number. Useful for error/warning/debugging output.
77  *
78  * \return true if the line could be parsed.
79  */
80  virtual bool parse( std::string line, unsigned int lineNumber ) = 0;
81 
82  /**
83  * 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
84  * way and apply as most settings as possible even if some other settings are erroneous. Add errors with \ref addError. Try avoiding
85  * exceptions if possible.
86  */
87  virtual void done();
88 
89  /**
90  * Saves the state to the specified stream.
91  *
92  * \param output the stream to print the state to.
93  */
94  virtual void save( std::ostream& output ) = 0; // NOLINT
95 
96  /**
97  * Checks whether there where errors during load or save.
98  *
99  * \return true if there where.
100  */
101  bool hadErrors() const;
102 
103  /**
104  * Get error list.
105  *
106  * \return the list
107  */
108  const std::vector< std::string >& getErrors() const;
109 
110  /**
111  * Checks whether there where warnings during load or save.
112  *
113  * \return true if there where.
114  */
115  bool hadWarnings() const;
116 
117  /**
118  * Get warnings list.
119  *
120  * \return the list
121  */
122  const std::vector< std::string >& getWarnings() const;
123 
124  /**
125  * Create a clone of the IO. This is especially useful for custom parsers registered at \ref WProjectFile::registerParser. Implement this
126  * function.
127  *
128  * \param project the project file using this parser instance.
129  *
130  * \return Cloned instance.
131  */
132  virtual SPtr clone( WProjectFile* project ) const = 0;
133 
134  /**
135  * Set the project using this parser
136  *
137  * \param project the project
138  */
139  void setProject( WProjectFile* project );
140 
141  /**
142  * When to apply this parser. This might be important in some cases. Note that you can only decide
143  * whether you want to apply your changes before or after the modules have been added.
144  */
146  {
147  PRE_MODULES = 0,
148  POST_MODULES
149  };
150 
151  /**
152  * Return the apply order of this IO.
153  *
154  * \return the order
155  */
156  ApplyOrder getApplyOrder() const;
157 
158 protected:
159  /**
160  * Add an error. Use this when you encounter some difficulties during parsing or applying settings. Provide useful errors. They will be
161  * presented to the user.
162  *
163  * \param description the error description
164  */
165  void addError( std::string description );
166 
167  /**
168  * Add an warning. Use this when you encounter some difficulties during parsing or applying settings. Provide useful warnings. They will be
169  * presented to the user.
170  *
171  * \param description the error description
172  */
173  void addWarning( std::string description );
174 
175  /**
176  * Recursively prints the properties and nested properties.
177  *
178  * \param output the output stream to print to
179  * \param props the properties to recursively print
180  * \param indent the indentation level
181  * \param prefix the prefix (name prefix of property)
182  * \param index the ID to use
183  * \param indexPrefix use this to add a prefix to the index
184  */
185  void printProperties( std::ostream& output, boost::shared_ptr< WProperties > props, std::string indent, //NOLINT ( non-const ref )
186  std::string prefix, unsigned int index, std::string indexPrefix = "" );
187 
188 
189  /**
190  * Set the order of calls to "done".
191  *
192  * \param order the order.
193  */
194  void setApplyOrder( ApplyOrder order );
195 
196  /**
197  * The project using this parser.
198  *
199  * \return the project
200  */
201  WProjectFile* getProject() const;
202 
203 private:
204  /**
205  * List of errors if any.
206  */
207  std::vector< std::string > m_errors;
208 
209  /**
210  * List of warnings if any.
211  */
212  std::vector< std::string > m_warnings;
213 
214  /**
215  * The project using this parser
216  */
218 
219  /**
220  * The order in which the "done" functions are called.
221  */
223 };
224 
225 #endif // WPROJECTFILEIO_H
226 
virtual void done()
Called whenever the end of the project file has been reached.
void setProject(WProjectFile *project)
Set the project using this parser.
WProjectFileIO()
Default constructor.
void setApplyOrder(ApplyOrder order)
Set the order of calls to "done".
std::vector< std::string > m_warnings
List of warnings if any.
virtual void save(std::ostream &output)=0
Saves the state to the specified stream.
virtual ~WProjectFileIO()
Destructor.
const std::vector< std::string > & getErrors() const
Get error list.
void addWarning(std::string description)
Add an warning.
boost::shared_ptr< const WProjectFileIO > ConstSPtr
Abbreviation for const shared pointer.
Class loading project files.
Definition: WProjectFile.h:57
ApplyOrder m_applyOrder
The order in which the "done" functions are called.
const std::vector< std::string > & getWarnings() const
Get warnings list.
void addError(std::string description)
Add an error.
boost::shared_ptr< WProjectFileIO > SPtr
Abbreviation for a shared pointer.
virtual bool parse(std::string line, unsigned int lineNumber)=0
This method parses the specified line and interprets it.
WProjectFile * m_project
The project using this parser.
bool hadErrors() const
Checks whether there where errors during load or save.
std::vector< std::string > m_errors
List of errors if any.
A base class for all parts of OpenWalnut which can be serialized to a project file.
void printProperties(std::ostream &output, boost::shared_ptr< WProperties > props, std::string indent, std::string prefix, unsigned int index, std::string indexPrefix="")
Recursively prints the properties and nested properties.
bool hadWarnings() const
Checks whether there where warnings during load or save.
ApplyOrder
When to apply this parser.
virtual SPtr clone(WProjectFile *project) const =0
Create a clone of the IO.
ApplyOrder getApplyOrder() const
Return the apply order of this IO.
WProjectFile * getProject() const
The project using this parser.