OpenWalnut 1.3.1
WProjectFileIO.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WPROJECTFILEIO_H
00026 #define WPROJECTFILEIO_H
00027 
00028 #include <ostream>
00029 #include <string>
00030 #include <vector>
00031 
00032 /**
00033  * 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
00034  * 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
00035  * error-tolerant way. We want to avoid that small problems in the project file cause the whole file to be useless.
00036  */
00037 class WProjectFileIO // NOLINT
00038 {
00039 public:
00040     /**
00041      * Default constructor.
00042      */
00043     WProjectFileIO();
00044 
00045     /**
00046      * Destructor.
00047      */
00048     virtual ~WProjectFileIO();
00049 
00050     /**
00051      * This method parses the specified line and interprets it. It gets called line by line by WProjectFile. You should avoid applying anything
00052      * of the loaded information here. You should use \ref done for this.
00053      *
00054      * \param line the current line as string
00055      * \param lineNumber the current line number. Useful for error/warning/debugging output.
00056      *
00057      * \return true if the line could be parsed.
00058      */
00059     virtual bool parse( std::string line, unsigned int lineNumber ) = 0;
00060 
00061     /**
00062      * 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
00063      * way and apply as most settings as possible even if some other settings are erroneous. Add errors with \ref addError. Try avoiding
00064      * exceptions if possible.
00065      */
00066     virtual void done();
00067 
00068     /**
00069      * Saves the state to the specified stream.
00070      *
00071      * \param output the stream to print the state to.
00072      */
00073     virtual void save( std::ostream& output ) = 0;   // NOLINT
00074 
00075     /**
00076      * Checks whether there where errors during load or save.
00077      *
00078      * \return true if there where.
00079      */
00080     bool hadErrors() const;
00081 
00082     /**
00083      * Get error list.
00084      *
00085      * \return the list
00086      */
00087     const std::vector< std::string >& getErrors() const;
00088 
00089 protected:
00090     /**
00091      * Add an error. Use this when you encounter some difficulties during parsing or applying settings. Provide useful errors. They will be
00092      * presented to the user.
00093      *
00094      * \param description the error description
00095      */
00096     void addError( std::string description );
00097 
00098 private:
00099     /**
00100      * List of errors if any.
00101      */
00102     std::vector< std::string > m_errors;
00103 };
00104 
00105 #endif  // WPROJECTFILEIO_H
00106