OpenWalnut  1.4.0
WModuleProjectFileCombiner.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 WMODULEPROJECTFILECOMBINER_H
26 #define WMODULEPROJECTFILECOMBINER_H
27 
28 #include <ostream>
29 #include <list>
30 #include <map>
31 #include <string>
32 #include <utility>
33 
34 #ifndef Q_MOC_RUN
35 #include <boost/shared_ptr.hpp>
36 #endif
37 
38 #include "../../common/WProjectFileIO.h"
39 
40 #include "../WModuleCombiner.h"
41 
42 class WProjectFile;
43 class WModule;
44 
45 /**
46  * This class is able to parse project files and create the appropriate module graph inside a specified container. It is also derived from
47  * WProjectFileIO to allow WProjectFile to fill this combiner.
48  */
50  public WProjectFileIO
51 {
52 public:
53  /**
54  * Creates an empty combiner.
55  *
56  * \param target the target container where to add the modules to.
57  */
58  explicit WModuleProjectFileCombiner( boost::shared_ptr< WModuleContainer > target );
59 
60  /**
61  * Creates an empty combiner. This constructor automatically uses the kernel's root container as target container.
62  */
64 
65  /**
66  * Destructor.
67  */
69 
70  /**
71  * Apply the internal module structure to the target container. Be aware, that this operation might take some time, as modules can be
72  * connected only if they are "ready", which, at least with WDataModule modules, might take some time. It applies the loaded project file.
73  *
74  * \note the loader for project files is very tolerant. It does not abort. It tries to load as much as possible. The only exception that gets
75  * thrown whenever the file could not be opened.
76  *
77  * \throw WFileNotFound whenever the project file could not be opened.
78  */
79  virtual void apply();
80 
81  /**
82  * This method parses the specified line and interprets it to fill the internal module graph structure.
83  *
84  * \param line the current line as string
85  * \param lineNumber the current line number. Useful for error/warning/debugging output.
86  *
87  * \return true if the line could be parsed.
88  */
89  virtual bool parse( std::string line, unsigned int lineNumber );
90 
91  /**
92  * Called whenever the end of the project file has been reached. This is useful if your specific parser class wants to do some post
93  * processing after parsing line by line.
94  */
95  virtual void done();
96 
97  /**
98  * Saves the state to the specified stream.
99  *
100  * \param output the stream to print the state to.
101  */
102  virtual void save( std::ostream& output ); // NOLINT
103 
104  /**
105  * Create a clone of the IO. This is especially useful for custom parsers registered at \ref WProjectFile::registerParser. Implement this
106  * function.
107  *
108  * \param project the project file using this parser instance.
109  *
110  * \return Cloned instance.
111  */
112  virtual SPtr clone( WProjectFile* project ) const;
113 
114  /**
115  * Map a given project file ID to a module. This method must not be used by WModuleProjectFileCombiner, as it builds this mapping. All other
116  * \ref WProjectFileIO implementations are allowed to use it in their save and apply methods (NOT in parse()).
117  *
118  * \param id the id
119  *
120  * \return the module, or NULL if ID is not known.
121  */
122  virtual boost::shared_ptr< WModule > mapToModule( unsigned int id ) const;
123 
124  /**
125  * Map a given module to project file ID. This method must not be used by WModuleProjectFileCombiner, as it builds this mapping. All other
126  * \ref WProjectFileIO implementations are allowed to use it in their save and apply methods (NOT in parse()).
127  *
128  * \param module the module to map
129  *
130  * \return the ID, or numeric_limits< unisigned int >::max() if module not known.
131  */
132  virtual unsigned int mapFromModule( boost::shared_ptr< WModule > module ) const;
133 
134 protected:
135  /**
136  * The module ID type. A pair of ID and pointer to module.
137  */
138  typedef std::pair< unsigned int, boost::shared_ptr< WModule > > ModuleID;
139 
140  /**
141  * Map between ID and Module
142  */
143  typedef std::map< unsigned int, boost::shared_ptr< WModule > > ModuleIDMap;
144 
145  /**
146  * All Modules.
147  */
148  std::map< unsigned int, boost::shared_ptr< WModule > > m_modules;
149 
150  /**
151  * A connector is described by ID and name.
152  */
153  typedef std::pair< unsigned int, std::string > Connector;
154 
155  /**
156  * A connection is a pair of connectors.
157  */
158  typedef std::pair< Connector, Connector > Connection;
159 
160  /**
161  * All connections.
162  */
163  std::list< Connection > m_connections;
164 
165  /**
166  * A property is a pair of ID and name.
167  */
168  typedef std::pair< unsigned int, std::string > Property;
169 
170  /**
171  * A property value is a property and the new value as string.
172  */
173  typedef std::pair< Property, std::string > PropertyValue;
174 
175  /**
176  * All properties.
177  */
178  std::list< PropertyValue > m_properties;
179 private:
180 };
181 
182 #endif // WMODULEPROJECTFILECOMBINER_H
183 
std::pair< unsigned int, std::string > Connector
A connector is described by ID and name.
std::map< unsigned int, boost::shared_ptr< WModule > > ModuleIDMap
Map between ID and Module.
virtual void apply()
Apply the internal module structure to the target container.
std::pair< unsigned int, boost::shared_ptr< WModule > > ModuleID
The module ID type.
Class representing a single module of OpenWalnut.
Definition: WModule.h:83
virtual SPtr clone(WProjectFile *project) const
Create a clone of the IO.
virtual boost::shared_ptr< WModule > mapToModule(unsigned int id) const
Map a given project file ID to a module.
std::pair< Property, std::string > PropertyValue
A property value is a property and the new value as string.
virtual ~WModuleProjectFileCombiner()
Destructor.
virtual void save(std::ostream &output)
Saves the state to the specified stream.
std::pair< unsigned int, std::string > Property
A property is a pair of ID and name.
Class loading project files.
Definition: WProjectFile.h:57
std::list< PropertyValue > m_properties
All properties.
This is a base class for all module combination classes.
This class is able to parse project files and create the appropriate module graph inside a specified ...
boost::shared_ptr< WProjectFileIO > SPtr
Abbreviation for a shared pointer.
std::list< Connection > m_connections
All connections.
A base class for all parts of OpenWalnut which can be serialized to a project file.
virtual bool parse(std::string line, unsigned int lineNumber)
This method parses the specified line and interprets it to fill the internal module graph structure...
std::pair< Connector, Connector > Connection
A connection is a pair of connectors.
virtual unsigned int mapFromModule(boost::shared_ptr< WModule > module) const
Map a given module to project file ID.
WModuleProjectFileCombiner()
Creates an empty combiner.
virtual void done()
Called whenever the end of the project file has been reached.
std::map< unsigned int, boost::shared_ptr< WModule > > m_modules
All Modules.