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