OpenWalnut  1.4.0
WModuleProjectFileCombiner.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WMODULEPROJECTFILECOMBINER_H
00026 #define WMODULEPROJECTFILECOMBINER_H
00027 
00028 #include <ostream>
00029 #include <list>
00030 #include <map>
00031 #include <string>
00032 #include <utility>
00033 
00034 #include <boost/shared_ptr.hpp>
00035 
00036 #include "../../common/WProjectFileIO.h"
00037 
00038 #include "../WModuleCombiner.h"
00039 
00040 class WProjectFile;
00041 class WModule;
00042 
00043 /**
00044  * This class is able to parse project files and create the appropriate module graph inside a specified container. It is also derived from
00045  * WProjectFileIO to allow WProjectFile to fill this combiner.
00046  */
00047 class  WModuleProjectFileCombiner: public WModuleCombiner,
00048                                    public WProjectFileIO
00049 {
00050 public:
00051     /**
00052      * Creates an empty combiner.
00053      *
00054      * \param target the target container where to add the modules to.
00055      */
00056     explicit WModuleProjectFileCombiner( boost::shared_ptr< WModuleContainer > target );
00057 
00058     /**
00059      * Creates an empty combiner. This constructor automatically uses the kernel's root container as target container.
00060      */
00061     WModuleProjectFileCombiner();
00062 
00063     /**
00064      * Destructor.
00065      */
00066     virtual ~WModuleProjectFileCombiner();
00067 
00068     /**
00069      * Apply the internal module structure to the target container. Be aware, that this operation might take some time, as modules can be
00070      * connected only if they are "ready", which, at least with WDataModule modules, might take some time. It applies the loaded project file.
00071      *
00072      * \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
00073      * thrown whenever the file could not be opened.
00074      *
00075      * \throw WFileNotFound whenever the project file could not be opened.
00076      */
00077     virtual void apply();
00078 
00079     /**
00080      * This method parses the specified line and interprets it to fill the internal module graph structure.
00081      *
00082      * \param line the current line as string
00083      * \param lineNumber the current line number. Useful for error/warning/debugging output.
00084      *
00085      * \return true if the line could be parsed.
00086      */
00087     virtual bool parse( std::string line, unsigned int lineNumber );
00088 
00089     /**
00090      * Called whenever the end of the project file has been reached. This is useful if your specific parser class wants to do some post
00091      * processing after parsing line by line.
00092      */
00093     virtual void done();
00094 
00095     /**
00096      * Saves the state to the specified stream.
00097      *
00098      * \param output the stream to print the state to.
00099      */
00100     virtual void save( std::ostream& output );   // NOLINT
00101 
00102     /**
00103      * Create a clone of the IO. This is especially useful for custom parsers registered at \ref WProjectFile::registerParser. Implement this
00104      * function.
00105      *
00106      * \param project the project file using this parser instance.
00107      *
00108      * \return Cloned instance.
00109      */
00110     virtual SPtr clone( WProjectFile* project ) const;
00111 
00112     /**
00113      * Map a given project file ID to a module. This method must not be used by WModuleProjectFileCombiner, as it builds this mapping. All other
00114      * \ref WProjectFileIO implementations are allowed to use it in their save and apply methods (NOT in parse()).
00115      *
00116      * \param id the id
00117      *
00118      * \return the module, or NULL if ID is not known.
00119      */
00120     virtual boost::shared_ptr< WModule > mapToModule( unsigned int id ) const;
00121 
00122     /**
00123      * Map a given module to project file ID. This method must not be used by WModuleProjectFileCombiner, as it builds this mapping. All other
00124      * \ref WProjectFileIO implementations are allowed to use it in their save and apply methods (NOT in parse()).
00125      *
00126      * \param module the module to map
00127      *
00128      * \return the ID, or numeric_limits< unisigned int >::max() if module not known.
00129      */
00130     virtual unsigned int mapFromModule( boost::shared_ptr< WModule > module ) const;
00131 
00132 protected:
00133     /**
00134      * The module ID type. A pair of ID and pointer to module.
00135      */
00136     typedef std::pair< unsigned int, boost::shared_ptr< WModule > > ModuleID;
00137 
00138     /**
00139      * Map between ID and Module
00140      */
00141     typedef std::map< unsigned int, boost::shared_ptr< WModule > > ModuleIDMap;
00142 
00143     /**
00144      * All Modules.
00145      */
00146     std::map< unsigned int, boost::shared_ptr< WModule > > m_modules;
00147 
00148     /**
00149      * A connector is described by ID and name.
00150      */
00151     typedef std::pair< unsigned int, std::string > Connector;
00152 
00153     /**
00154      * A connection is a pair of connectors.
00155      */
00156     typedef std::pair< Connector, Connector > Connection;
00157 
00158     /**
00159      * All connections.
00160      */
00161     std::list< Connection > m_connections;
00162 
00163     /**
00164      * A property is a pair of ID and name.
00165      */
00166     typedef std::pair< unsigned int, std::string > Property;
00167 
00168     /**
00169      * A property value is a property and the new value as string.
00170      */
00171     typedef std::pair< Property, std::string > PropertyValue;
00172 
00173     /**
00174      * All properties.
00175      */
00176     std::list< PropertyValue > m_properties;
00177 private:
00178 };
00179 
00180 #endif  // WMODULEPROJECTFILECOMBINER_H
00181