OpenWalnut  1.3.1
WProjectFile.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 WPROJECTFILE_H
26 #define WPROJECTFILE_H
27 
28 #include <string>
29 #include <vector>
30 
31 #include <boost/filesystem.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <boost/function.hpp>
34 #include <boost/signals2/signal.hpp>
35 
36 #include "../common/WProjectFileIO.h"
37 
38 
39 /**
40  * 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
41  * WProjectFileIO instances which then do their job.
42  */
44  public boost::enable_shared_from_this< WProjectFile >
45 {
46 public:
47  /**
48  * Abbreviation for a shared pointer.
49  */
50  typedef boost::shared_ptr< WProjectFile > SPtr;
51 
52  /**
53  * Abbreviation for const shared pointer.
54  */
55  typedef boost::shared_ptr< const WProjectFile > ConstSPtr;
56 
57  /**
58  * A callback function type reporting bach a finished load job. The given string vector contains a list of errors if any.
59  */
60  typedef boost::function< void( boost::filesystem::path, std::vector< std::string > ) > ProjectLoadCallback;
61 
62  /**
63  * A callback function signal type reporting bach a finished load job. The given string vector contains a list of errors if any.
64  */
65  typedef boost::signals2::signal< void( boost::filesystem::path, std::vector< std::string > ) > ProjectLoadCallbackSignal;
66 
67  /**
68  * Default constructor. It does NOT parse the file. Parsing is done by using load.
69  *
70  * \param project the project file to load or save.
71  */
72  explicit WProjectFile( boost::filesystem::path project );
73 
74  /**
75  * Default constructor. It does NOT parse the file. Parsing is done by using load.
76  *
77  * \param project the project file to load.
78  * \param doneCallback gets called whenever the load thread finishes.
79  */
80  WProjectFile( boost::filesystem::path project, ProjectLoadCallback doneCallback );
81 
82  /**
83  * Destructor.
84  */
85  virtual ~WProjectFile();
86 
87  /**
88  * Parses the project file and applies it. It applies the project file asynchronously!
89  */
90  virtual void load();
91 
92  /**
93  * Saves the current state to the file specified in the constructor.
94  */
95  virtual void save();
96 
97  /**
98  * Saves the current state to the file specified in the constructor. This also supports a custom list of writers. This is useful to only
99  * write some parts of the state.
100  *
101  * \param writer the list of writers to use.
102  */
103  virtual void save( const std::vector< boost::shared_ptr< WProjectFileIO > >& writer );
104 
105  /**
106  * Returns an instance of the Camera writer.
107  *
108  * \return the writer able to output the camera configuration to a stream.
109  */
110  static boost::shared_ptr< WProjectFileIO > getCameraWriter();
111 
112  /**
113  * Returns an instance of the module writer.
114  *
115  * \return the writer able to output the module configuration to a stream.
116  */
117  static boost::shared_ptr< WProjectFileIO > getModuleWriter();
118 
119  /**
120  * Returns an instance of the ROI writer.
121  *
122  * \return the writer able to output the ROI configuration to a stream.
123  */
124  static boost::shared_ptr< WProjectFileIO > getROIWriter();
125 
126 protected:
127  /**
128  * Function that has to be overwritten for execution. It gets executed in a separate thread after run()
129  * has been called.
130  */
131  virtual void threadMain();
132 
133  /**
134  * The project file to parse.
135  */
136  boost::filesystem::path m_project;
137 
138  /**
139  * The parser instances. They are used to parse the file.
140  */
141  std::vector< boost::shared_ptr< WProjectFileIO > > m_parsers;
142 
143  /**
144  * Do custom exception handling.
145  *
146  * \param e the exception
147  */
148  virtual void onThreadException( const WException& e );
149 private:
150  /**
151  * Signal used to callback someone that the loader was finished.
152  */
154 
155  /**
156  * Connection managing the signal m_signalLoadDone. This is the one and only connection allowed and will be disconnected upon thread has
157  * finished.
158  */
159  boost::signals2::connection m_signalLoadDoneConnection;
160 };
161 
162 #endif // WPROJECTFILE_H
163