OpenWalnut 1.3.1
WProjectFile.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 WPROJECTFILE_H
00026 #define WPROJECTFILE_H
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 #include <boost/filesystem.hpp>
00032 #include <boost/shared_ptr.hpp>
00033 #include <boost/function.hpp>
00034 #include <boost/signals2/signal.hpp>
00035 
00036 #include "../common/WProjectFileIO.h"
00037 
00038 
00039 /**
00040  * Class loading project files. This class opens an file and reads it line by line. It delegates the actual parsing to each of the known
00041  * WProjectFileIO instances which then do their job.
00042  */
00043 class WProjectFile: public WThreadedRunner,
00044                     public boost::enable_shared_from_this< WProjectFile >
00045 {
00046 public:
00047     /**
00048      * Abbreviation for a shared pointer.
00049      */
00050     typedef boost::shared_ptr< WProjectFile > SPtr;
00051 
00052     /**
00053      * Abbreviation for const shared pointer.
00054      */
00055     typedef boost::shared_ptr< const WProjectFile > ConstSPtr;
00056 
00057     /**
00058      * A callback function type reporting bach a finished load job. The given string vector contains a list of errors if any.
00059      */
00060     typedef boost::function< void( boost::filesystem::path, std::vector< std::string >  ) > ProjectLoadCallback;
00061 
00062     /**
00063      * A callback function signal type reporting bach a finished load job. The given string vector contains a list of errors if any.
00064      */
00065     typedef boost::signals2::signal< void( boost::filesystem::path, std::vector< std::string >  ) > ProjectLoadCallbackSignal;
00066 
00067     /**
00068      * Default constructor. It does NOT parse the file. Parsing is done by using load.
00069      *
00070      * \param project the project file to load or save.
00071      */
00072     explicit WProjectFile( boost::filesystem::path project );
00073 
00074     /**
00075      * Default constructor. It does NOT parse the file. Parsing is done by using load.
00076      *
00077      * \param project the project file to load.
00078      * \param doneCallback gets called whenever the load thread finishes.
00079      */
00080     WProjectFile( boost::filesystem::path project, ProjectLoadCallback doneCallback );
00081 
00082     /**
00083      * Destructor.
00084      */
00085     virtual ~WProjectFile();
00086 
00087     /**
00088      * Parses the project file and applies it. It applies the project file asynchronously!
00089      */
00090     virtual void load();
00091 
00092     /**
00093      * Saves the current state to the file specified in the constructor.
00094      */
00095     virtual void save();
00096 
00097     /**
00098      * Saves the current state to the file specified in the constructor. This also supports a custom list of writers. This is useful to only
00099      * write some parts of the state.
00100      *
00101      * \param writer the list of writers to use.
00102      */
00103     virtual void save( const std::vector< boost::shared_ptr< WProjectFileIO > >& writer );
00104 
00105     /**
00106      * Returns an instance of the Camera writer.
00107      *
00108      * \return the writer able to output the camera configuration to a stream.
00109      */
00110     static boost::shared_ptr< WProjectFileIO > getCameraWriter();
00111 
00112     /**
00113      * Returns an instance of the module writer.
00114      *
00115      * \return the writer able to output the module configuration to a stream.
00116      */
00117     static boost::shared_ptr< WProjectFileIO > getModuleWriter();
00118 
00119     /**
00120      * Returns an instance of the ROI writer.
00121      *
00122      * \return the writer able to output the ROI configuration to a stream.
00123      */
00124     static boost::shared_ptr< WProjectFileIO > getROIWriter();
00125 
00126 protected:
00127     /**
00128      * Function that has to be overwritten for execution. It gets executed in a separate thread after run()
00129      * has been called.
00130      */
00131     virtual void threadMain();
00132 
00133     /**
00134      * The project file to parse.
00135      */
00136     boost::filesystem::path m_project;
00137 
00138     /**
00139      * The parser instances. They are used to parse the file.
00140      */
00141     std::vector< boost::shared_ptr< WProjectFileIO > > m_parsers;
00142 
00143     /**
00144      * Do custom exception handling.
00145      *
00146      * \param e the exception
00147      */
00148     virtual void onThreadException( const WException& e );
00149 private:
00150     /**
00151      * Signal used to callback someone that the loader was finished.
00152      */
00153     ProjectLoadCallbackSignal m_signalLoadDone;
00154 
00155     /**
00156      * Connection managing the signal m_signalLoadDone. This is the one and only connection allowed and will be disconnected upon thread has
00157      * finished.
00158      */
00159     boost::signals2::connection m_signalLoadDoneConnection;
00160 };
00161 
00162 #endif  // WPROJECTFILE_H
00163