OpenWalnut  1.4.0
WApplyCombiner.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 WAPPLYCOMBINER_H
26 #define WAPPLYCOMBINER_H
27 
28 #include <list>
29 #include <map>
30 #include <string>
31 #include <utility>
32 
33 #ifndef Q_MOC_RUN
34 #include <boost/shared_ptr.hpp>
35 #endif
36 
37 #include "../WModule.h"
38 #include "../WModuleCombinerTypes.h"
39 #include "WModuleOneToOneCombiner.h"
40 
41 #include "../WModuleInputConnector.h"
42 #include "../WModuleOutputConnector.h"
43 
44 
45 /**
46  * Base class for all combiners which apply one connection between two connectors of two modules.
47  */
49 {
50 public:
51  /**
52  * Creates a combiner which sets up the specified modules and prototype combination. Specifying a NULL pointer to the srcModule parameter
53  * causes the combiner to only add the target module without any connections. This is especially useful for modules which do not provide any
54  * input which must be connected. It is possible to specify prototypes here. The will get created upon apply.
55  *
56  *
57  * \param target the target container
58  * \param srcModule the module whose output should be connected with the prototypes input
59  * \param srcConnector the output connector of the module
60  * \param targetModule the module/prototype to use for connecting the module with
61  * \param targetConnector the input connector of the prototype to connect with srcConnector.
62  */
63  WApplyCombiner( boost::shared_ptr< WModuleContainer > target,
64  WModule::SPtr srcModule, std::string srcConnector,
65  WModule::SPtr targetModule, std::string targetConnector );
66 
67  /**
68  * Creates a combiner which sets up the specified modules and prototype combination. This constructor automatically uses the kernel's root
69  * container as target container. Specifying a NULL pointer to the srcModule parameter
70  * causes the combiner to only add the target module without any connections. This is especially useful for modules which do not provide any
71  * input which must be connected. It is possible to specify prototypes here. The will get created upon apply.
72  *
73  * \param srcModule the module whose output should be connected with the prototypes input
74  * \param srcConnector the output connector of the module
75  * \param targetModule the module/prototype to use for connecting the module with
76  * \param targetConnector the input connector of the prototype to connect with srcConnector.
77  */
78  WApplyCombiner( WModule::SPtr srcModule, std::string srcConnector,
79  WModule::SPtr targetModule, std::string targetConnector );
80 
81  /**
82  * Creates a combiner which only adds the given module. This constructor automatically uses the kernel's root
83  * container as target container. Specifying a NULL pointer to the srcModule parameter
84  * causes the combiner to only add the target module without any connections. This is especially useful for modules which do not provide any
85  * input which must be connected. It is possible to specify prototypes here. The will get created upon apply.
86  *
87  * \param module the module to add
88  */
89  explicit WApplyCombiner( WModule::SPtr module );
90 
91  /**
92  * Destructor.
93  */
94  virtual ~WApplyCombiner();
95 
96  /**
97  * Apply the internal module structure to the target container. Be aware, that this operation might take some time, as modules can be
98  * connected only if they are "ready", which, at least with WMData modules, might take some time. It applies the loaded project file.
99  */
100  virtual void apply();
101 
102  /**
103  * This method creates a list of possible combiners for connections between the specified modules. Both modules can be prototypes. This
104  * method lists only connections from module1's outputs to module2's inputs.
105  *
106  * \param module1 the first module
107  * \param module2 the second module
108  *
109  * \return the list of combiner for one-to-one connections
110  */
111  template < typename T >
112  static WCombinerTypes::WOneToOneCombiners createCombinerList( WModule::SPtr module1, WModule::SPtr module2 )
113  {
114  // this list contains all connections for the current module with the other one
115  WCombinerTypes::WOneToOneCombiners lComp;
116 
117  // get offered outputs
118  WModule::OutputConnectorList cons = module1->getOutputConnectors();
119 
120  // get connectors of this prototype
121  WModule::InputConnectorList pcons = module2->getInputConnectors();
122 
123  // ensure we have 1 connector
124  if( ( pcons.size() == 0 ) || ( cons.size() == 0 ) )
125  {
126  return lComp;
127  }
128 
129  // iterate connector list, first find all matches of the output connectors with all inputs
130  for( WModule::OutputConnectorList::const_iterator outIter = cons.begin(); outIter != cons.end(); ++outIter )
131  {
132  // now go through each input iterator of the current prototype
133  for( WModule::InputConnectorList::const_iterator inIter = pcons.begin(); inIter != pcons.end(); ++inIter )
134  {
135  // compatible?
136  if( ( *outIter )->connectable( *inIter ) && ( *inIter )->connectable( *outIter ) )
137  {
138  // create a apply-prototype combiner
139  lComp.push_back( boost::shared_ptr< WApplyCombiner >(
140  new T( module1, ( *outIter )->getName(), module2, ( *inIter )->getName() ) )
141  );
142 
143  // wlog::debug( "ModuleFactory" ) << ( *outIter )->getCanonicalName() << " -> " << ( *inIter )->getCanonicalName();
144  }
145  }
146  }
147 
148  return lComp;
149  }
150 
151 protected:
152 private:
153 };
154 
155 #endif // WAPPLYCOMBINER_H
156 
std::vector< boost::shared_ptr< WModuleOutputConnector > > OutputConnectorList
The type for the list of output connectors.
Definition: WModule.h:116
WApplyCombiner(boost::shared_ptr< WModuleContainer > target, WModule::SPtr srcModule, std::string srcConnector, WModule::SPtr targetModule, std::string targetConnector)
Creates a combiner which sets up the specified modules and prototype combination. ...
virtual void apply()
Apply the internal module structure to the target container.
Base class for all combiners which apply one connection between two connectors of two modules...
boost::shared_ptr< WModule > SPtr
Shared pointer to a WModule.
Definition: WModule.h:121
std::vector< boost::shared_ptr< WModuleInputConnector > > InputConnectorList
The type for the list of input connectors.
Definition: WModule.h:111
Base class for all combiners which apply one connection between two connectors of two modules...
virtual ~WApplyCombiner()
Destructor.
static WCombinerTypes::WOneToOneCombiners createCombinerList(WModule::SPtr module1, WModule::SPtr module2)
This method creates a list of possible combiners for connections between the specified modules...