OpenWalnut  1.4.0
WApplyCombiner.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 WAPPLYCOMBINER_H
00026 #define WAPPLYCOMBINER_H
00027 
00028 #include <list>
00029 #include <map>
00030 #include <string>
00031 #include <utility>
00032 
00033 #include <boost/shared_ptr.hpp>
00034 
00035 #include "../WModule.h"
00036 #include "../WModuleCombinerTypes.h"
00037 #include "WModuleOneToOneCombiner.h"
00038 
00039 #include "../WModuleInputConnector.h"
00040 #include "../WModuleOutputConnector.h"
00041 
00042 
00043 /**
00044  * Base class for all combiners which apply one connection between two connectors of two modules.
00045  */
00046 class  WApplyCombiner: public WModuleOneToOneCombiner
00047 {
00048 public:
00049     /**
00050      * Creates a combiner which sets up the specified modules and prototype combination. Specifying a NULL pointer to the srcModule parameter
00051      * causes the combiner to only add the target module without any connections. This is especially useful for modules which do not provide any
00052      * input which must be connected. It is possible to specify prototypes here. The will get created upon apply.
00053      *
00054      *
00055      * \param target            the target container
00056      * \param srcModule         the module whose output should be connected with the prototypes input
00057      * \param srcConnector      the output connector of the module
00058      * \param targetModule      the module/prototype to use for connecting the module with
00059      * \param targetConnector   the input connector of the prototype to connect with srcConnector.
00060      */
00061     WApplyCombiner( boost::shared_ptr< WModuleContainer > target,
00062                     WModule::SPtr srcModule, std::string srcConnector,
00063                     WModule::SPtr targetModule, std::string targetConnector );
00064 
00065     /**
00066      * Creates a combiner which sets up the specified modules and prototype combination. This constructor automatically uses the kernel's root
00067      * container as target container. Specifying a NULL pointer to the srcModule parameter
00068      * causes the combiner to only add the target module without any connections. This is especially useful for modules which do not provide any
00069      * input which must be connected. It is possible to specify prototypes here. The will get created upon apply.
00070      *
00071      * \param srcModule         the module whose output should be connected with the prototypes input
00072      * \param srcConnector      the output connector of the module
00073      * \param targetModule      the module/prototype to use for connecting the module with
00074      * \param targetConnector   the input connector of the prototype to connect with srcConnector.
00075      */
00076     WApplyCombiner( WModule::SPtr srcModule, std::string srcConnector,
00077                     WModule::SPtr targetModule, std::string targetConnector );
00078 
00079     /**
00080      * Creates a combiner which only adds the given module. This constructor automatically uses the kernel's root
00081      * container as target container. Specifying a NULL pointer to the srcModule parameter
00082      * causes the combiner to only add the target module without any connections. This is especially useful for modules which do not provide any
00083      * input which must be connected. It is possible to specify prototypes here. The will get created upon apply.
00084      *
00085      * \param module the module to add
00086      */
00087     explicit WApplyCombiner( WModule::SPtr module );
00088 
00089     /**
00090      * Destructor.
00091      */
00092     virtual ~WApplyCombiner();
00093 
00094     /**
00095      * Apply the internal module structure to the target container. Be aware, that this operation might take some time, as modules can be
00096      * connected only if they are "ready", which, at least with WMData modules, might take some time. It applies the loaded project file.
00097      */
00098     virtual void apply();
00099 
00100     /**
00101      * This method creates a list of possible combiners for connections between the specified modules. Both modules can be prototypes. This
00102      * method lists only connections from module1's outputs to module2's inputs.
00103      *
00104      * \param module1 the first module
00105      * \param module2 the second module
00106      *
00107      * \return the list of combiner for one-to-one connections
00108      */
00109     template < typename T >
00110     static WCombinerTypes::WOneToOneCombiners createCombinerList( WModule::SPtr module1, WModule::SPtr module2 )
00111     {
00112         // this list contains all connections for the current module with the other one
00113         WCombinerTypes::WOneToOneCombiners lComp;
00114 
00115         // get offered outputs
00116         WModule::OutputConnectorList cons = module1->getOutputConnectors();
00117 
00118         // get connectors of this prototype
00119         WModule::InputConnectorList pcons = module2->getInputConnectors();
00120 
00121         // ensure we have 1 connector
00122         if( ( pcons.size() == 0 ) || ( cons.size() == 0 ) )
00123         {
00124             return lComp;
00125         }
00126 
00127         // iterate connector list, first find all matches of the output connectors with all inputs
00128         for( WModule::OutputConnectorList::const_iterator outIter = cons.begin(); outIter != cons.end(); ++outIter )
00129         {
00130             // now go through each input iterator of the current prototype
00131             for( WModule::InputConnectorList::const_iterator inIter = pcons.begin(); inIter != pcons.end(); ++inIter )
00132             {
00133                 // compatible?
00134                 if( ( *outIter )->connectable( *inIter ) &&  ( *inIter )->connectable( *outIter ) )
00135                 {
00136                     // create a apply-prototype combiner
00137                     lComp.push_back( boost::shared_ptr< WApplyCombiner >(
00138                         new T( module1, ( *outIter )->getName(), module2, ( *inIter )->getName() ) )
00139                     );
00140 
00141                     // wlog::debug( "ModuleFactory" ) << ( *outIter )->getCanonicalName() << " -> " << ( *inIter )->getCanonicalName();
00142                 }
00143             }
00144         }
00145 
00146         return lComp;
00147     }
00148 
00149 protected:
00150 private:
00151 };
00152 
00153 #endif  // WAPPLYCOMBINER_H
00154