OpenWalnut  1.5.0dev
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 <list>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 
33 #include <boost/filesystem.hpp>
34 #include <boost/function.hpp>
35 #include <boost/signals2/signal.hpp>
36 
37 #include "../common/WProjectFileIO.h"
38 #include "../common/WSharedSequenceContainer.h"
39 #include "../common/WThreadedRunner.h"
40 
42 class WModule;
43 
44 /**
45  * 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
46  * WProjectFileIO instances which then do their job.
47  */
49  public std::enable_shared_from_this< WProjectFile >
50 {
51 public:
52  /**
53  * Abbreviation for a shared pointer.
54  */
55  typedef std::shared_ptr< WProjectFile > SPtr;
56 
57  /**
58  * Abbreviation for const shared pointer.
59  */
60  typedef std::shared_ptr< const WProjectFile > ConstSPtr;
61 
62  /**
63  * A callback function type reporting bach a finished load job. The given string vector contains a list of errors if any.
64  */
65  typedef boost::function< void( boost::filesystem::path, std::vector< std::string >, std::vector< std::string > ) > ProjectLoadCallback;
66 
67  /**
68  * A callback function signal type reporting bach a finished load job. The given string vector contains a list of errors if any.
69  */
70  typedef boost::signals2::signal< void( boost::filesystem::path, std::vector< std::string >, std::vector< std::string > ) >
72 
73  /**
74  * Default constructor. It does NOT parse the file. Parsing is done by using load.
75  *
76  * \param project the project file to load or save.
77  */
78  explicit WProjectFile( boost::filesystem::path project );
79 
80  /**
81  * Default constructor. It does NOT parse the file. Parsing is done by using load.
82  *
83  * \param project the project file to load.
84  * \param doneCallback gets called whenever the load thread finishes.
85  */
86  WProjectFile( boost::filesystem::path project, ProjectLoadCallback doneCallback );
87 
88  /**
89  * Destructor.
90  */
91  virtual ~WProjectFile();
92 
93  /**
94  * Parses the project file and applies it. It applies the project file asynchronously!
95  */
96  virtual void load();
97 
98  /**
99  * Saves the current state to the file specified in the constructor.
100  */
101  virtual void save();
102 
103  /**
104  * Saves the current state to the file specified in the constructor. This also supports a custom list of writers. This is useful to only
105  * write some parts of the state.
106  *
107  * \param writer the list of writers to use.
108  */
109  virtual void save( const std::list< std::shared_ptr< WProjectFileIO > >& writer );
110 
111  /**
112  * Saves the current state to the file specified in the constructor. This also supports a custom list of writers. This is useful to only
113  * write some parts of the state.
114  *
115  * \param writer the list of writers to use.
116  */
117  virtual void save( const std::vector< std::shared_ptr< WProjectFileIO > >& writer );
118 
119  /**
120  * Returns an instance of the Camera writer.
121  *
122  * \return the writer able to output the camera configuration to a stream.
123  */
124  static std::shared_ptr< WProjectFileIO > getCameraWriter();
125 
126  /**
127  * Returns an instance of the module writer.
128  *
129  * \return the writer able to output the module configuration to a stream.
130  */
131  static std::shared_ptr< WProjectFileIO > getModuleWriter();
132 
133  /**
134  * Returns an instance of the ROI writer.
135  *
136  * \return the writer able to output the ROI configuration to a stream.
137  */
138  static std::shared_ptr< WProjectFileIO > getROIWriter();
139 
140  /**
141  * Register a custom project file parser. Use this to add and re-read information from project files. The change takes effect when creating a
142  * new instance of WProjectFile. The custom parser needs to implement \ref WProjectFileIO::clone as this is used for instancing the parser
143  * for each project file.
144  *
145  * \note: See \ref WQtNetworkEditorProjectFileIO for a working example.
146  *
147  * \param parser the parser. Can be added multiple times.
148  */
149  static void registerParser( WProjectFileIO::SPtr parser );
150 
151  /**
152  * Remove parser from registry. The change takes effect when creating a new instance of WProjectFile.
153  *
154  * \param parser the parser to remove. If not existing, nothing happens.
155  */
156  static void deregisterParser( WProjectFileIO::SPtr parser );
157 
158  /**
159  * Map a given project file ID to a module. This method must not be used by WModuleProjectFileCombiner, as it builds this mapping. All other
160  * \ref WProjectFileIO implementations are allowed to use it in their save and apply methods (NOT in parse()).
161  *
162  * \param id the id
163  *
164  * \return the module, or NULL if ID is not known.
165  */
166  std::shared_ptr< WModule > mapToModule( unsigned int id ) const;
167 
168  /**
169  * Map a given module to project file ID. This method must not be used by WModuleProjectFileCombiner, as it builds this mapping. All other
170  * \ref WProjectFileIO implementations are allowed to use it in their save and apply methods (NOT in parse()).
171  *
172  * \param module the module to map
173  *
174  * \return the ID, or numeric_limits< unisigned int >::max() if module not known.*
175  */
176  unsigned int mapFromModule( std::shared_ptr< WModule > module ) const;
177 
178 protected:
179  /**
180  * Function that has to be overwritten for execution. It gets executed in a separate thread after run()
181  * has been called.
182  */
183  virtual void threadMain();
184 
185  /**
186  * The project file to parse.
187  */
188  boost::filesystem::path m_project;
189 
190  /**
191  * The parser instances. They are used to parse the file.
192  */
193  std::list< std::shared_ptr< WProjectFileIO > > m_parsers;
194 
195  /**
196  * The writer instances. They are used to write the file.
197  */
198  std::list< std::shared_ptr< WProjectFileIO > > m_writers;
199 
200  /**
201  * Do custom exception handling.
202  *
203  * \param e the exception
204  */
205  virtual void onThreadException( const WException& e );
206 
207 private:
208  /**
209  * Type used for returning lists of parser prototypes.
210  */
212 
213  /**
214  * Signal used to callback someone that the loader was finished.
215  */
217 
218  /**
219  * Connection managing the signal m_signalLoadDone. This is the one and only connection allowed and will be disconnected upon thread has
220  * finished.
221  */
222  boost::signals2::connection m_signalLoadDoneConnection;
223 
224  /**
225  * List of all additional parser prototypes. Handled as singleton.
226  */
228 
229  /**
230  * This is the only WProjectFileIO instance which is needed. It is used to map ID to module.
231  */
232  std::shared_ptr< WModuleProjectFileCombiner > m_moduleIO;
233 };
234 
235 #endif // WPROJECTFILE_H
236 
Basic exception handler.
Definition: WException.h:39
This class is able to parse project files and create the appropriate module graph inside a specified ...
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
std::shared_ptr< WProjectFileIO > SPtr
Abbreviation for a shared pointer.
Class loading project files.
Definition: WProjectFile.h:50
std::list< std::shared_ptr< WProjectFileIO > > m_writers
The writer instances.
Definition: WProjectFile.h:198
static ParserList m_additionalParsers
List of all additional parser prototypes.
Definition: WProjectFile.h:227
virtual ~WProjectFile()
Destructor.
boost::filesystem::path m_project
The project file to parse.
Definition: WProjectFile.h:188
ProjectLoadCallbackSignal m_signalLoadDone
Signal used to callback someone that the loader was finished.
Definition: WProjectFile.h:216
WSharedSequenceContainer< std::vector< WProjectFileIO::SPtr > > ParserList
Type used for returning lists of parser prototypes.
Definition: WProjectFile.h:211
static void deregisterParser(WProjectFileIO::SPtr parser)
Remove parser from registry.
virtual void load()
Parses the project file and applies it.
boost::function< void(boost::filesystem::path, std::vector< std::string >, std::vector< std::string >) > ProjectLoadCallback
A callback function type reporting bach a finished load job.
Definition: WProjectFile.h:65
virtual void threadMain()
Function that has to be overwritten for execution.
boost::signals2::connection m_signalLoadDoneConnection
Connection managing the signal m_signalLoadDone.
Definition: WProjectFile.h:222
boost::signals2::signal< void(boost::filesystem::path, std::vector< std::string >, std::vector< std::string >) > ProjectLoadCallbackSignal
A callback function signal type reporting bach a finished load job.
Definition: WProjectFile.h:71
std::shared_ptr< const WProjectFile > ConstSPtr
Abbreviation for const shared pointer.
Definition: WProjectFile.h:60
static std::shared_ptr< WProjectFileIO > getModuleWriter()
Returns an instance of the module writer.
virtual void onThreadException(const WException &e)
Do custom exception handling.
std::shared_ptr< WProjectFile > SPtr
Abbreviation for a shared pointer.
Definition: WProjectFile.h:55
virtual void save()
Saves the current state to the file specified in the constructor.
std::list< std::shared_ptr< WProjectFileIO > > m_parsers
The parser instances.
Definition: WProjectFile.h:193
std::shared_ptr< WModuleProjectFileCombiner > m_moduleIO
This is the only WProjectFileIO instance which is needed.
Definition: WProjectFile.h:232
static void registerParser(WProjectFileIO::SPtr parser)
Register a custom project file parser.
WProjectFile(boost::filesystem::path project)
Default constructor.
static std::shared_ptr< WProjectFileIO > getROIWriter()
Returns an instance of the ROI writer.
std::shared_ptr< WModule > mapToModule(unsigned int id) const
Map a given project file ID to a module.
unsigned int mapFromModule(std::shared_ptr< WModule > module) const
Map a given module to project file ID.
static std::shared_ptr< WProjectFileIO > getCameraWriter()
Returns an instance of the Camera writer.
This class provides a common interface for thread-safe access to sequence containers (list,...
Base class for all classes needing to be executed in a separate thread.