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