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