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