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