OpenWalnut  1.3.1
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 /**
33  * 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
34  * 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
35  * error-tolerant way. We want to avoid that small problems in the project file cause the whole file to be useless.
36  */
37 class WProjectFileIO // NOLINT
38 {
39 public:
40  /**
41  * Default constructor.
42  */
44 
45  /**
46  * Destructor.
47  */
48  virtual ~WProjectFileIO();
49 
50  /**
51  * This method parses the specified line and interprets it. It gets called line by line by WProjectFile. You should avoid applying anything
52  * of the loaded information here. You should use \ref done for this.
53  *
54  * \param line the current line as string
55  * \param lineNumber the current line number. Useful for error/warning/debugging output.
56  *
57  * \return true if the line could be parsed.
58  */
59  virtual bool parse( std::string line, unsigned int lineNumber ) = 0;
60 
61  /**
62  * 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
63  * way and apply as most settings as possible even if some other settings are erroneous. Add errors with \ref addError. Try avoiding
64  * exceptions if possible.
65  */
66  virtual void done();
67 
68  /**
69  * Saves the state to the specified stream.
70  *
71  * \param output the stream to print the state to.
72  */
73  virtual void save( std::ostream& output ) = 0; // NOLINT
74 
75  /**
76  * Checks whether there where errors during load or save.
77  *
78  * \return true if there where.
79  */
80  bool hadErrors() const;
81 
82  /**
83  * Get error list.
84  *
85  * \return the list
86  */
87  const std::vector< std::string >& getErrors() const;
88 
89 protected:
90  /**
91  * Add an error. Use this when you encounter some difficulties during parsing or applying settings. Provide useful errors. They will be
92  * presented to the user.
93  *
94  * \param description the error description
95  */
96  void addError( std::string description );
97 
98 private:
99  /**
100  * List of errors if any.
101  */
102  std::vector< std::string > m_errors;
103 };
104 
105 #endif // WPROJECTFILEIO_H
106