OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WApplyCombiner.cpp
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 #include <string>
26 
27 #include "../WModuleFactory.h"
28 
29 #include "WApplyCombiner.h"
30 
31 WApplyCombiner::WApplyCombiner( boost::shared_ptr< WModuleContainer > target,
32  boost::shared_ptr< WModule > srcModule, std::string srcConnector,
33  boost::shared_ptr< WModule > targetModule, std::string targetConnector ):
34  WModuleOneToOneCombiner( target, srcModule, srcConnector, targetModule, targetConnector )
35 {
36 }
37 
38 WApplyCombiner::WApplyCombiner( boost::shared_ptr< WModule > srcModule, std::string srcConnector,
39  boost::shared_ptr< WModule > targetModule, std::string targetConnector ):
40  WModuleOneToOneCombiner( srcModule, srcConnector, targetModule, targetConnector )
41 {
42 }
43 
45 {
46  // cleanup
47 }
48 
50 {
51  // create the modules from the prototypes if needed
52  boost::shared_ptr< WModule > srcModule = m_srcModule;
53  boost::shared_ptr< WModule > targetModule = m_targetModule;
54 
55  // create module instance if src is a prototype
56  if( srcModule && WModuleFactory::isPrototype( srcModule ) )
57  {
58  srcModule = WModuleFactory::getModuleFactory()->create( m_srcModule );
59  }
60 
61  // create module instance if target is a prototype
62  if( targetModule && WModuleFactory::isPrototype( targetModule ) )
63  {
64  targetModule = WModuleFactory::getModuleFactory()->create( m_targetModule );
65  }
66 
67  // add the src and target module to the container
68  // NOTE: the container does nothing if a NULL pointer has been specified and it also does nothing if the module already is associated with
69  // the container
70  m_container->add( srcModule );
71  m_container->add( targetModule );
72 
73  // wait for the source module if there is any
74  if( srcModule )
75  {
76  srcModule->isReadyOrCrashed().wait();
77  if( srcModule->isCrashed()() )
78  {
79  // NOTE: throwing an exception here should not be needed as the module container already has forwarded the exception
80  wlog::error( "Prototype Combiner" ) << "The source module \"" << srcModule->getName() << "\" has crashed. Abort.";
81  return;
82  }
83  }
84 
85  // wait for the source module if there is any
86  if( targetModule )
87  {
88  targetModule->isReadyOrCrashed().wait();
89  if( targetModule->isCrashed()() )
90  {
91  // NOTE: throwing an exception here should not be needed as the module container already has forwarded the exception
92  wlog::error( "Prototype Combiner" ) << "The target module \"" << targetModule->getName() << "\" has crashed. Abort.";
93  return;
94  }
95  }
96 
97  // if the connector is an empty string -> do not connect, just add
98  if( ( m_srcConnector.empty() ) || ( m_targetConnector.empty() ) )
99  {
100  return;
101  }
102 
103  // and connect them finally:
104  if( srcModule && targetModule )
105  {
106  targetModule->getInputConnector( m_targetConnector )->disconnectAll(); // before connecting, remove existing connection on input
107  targetModule->getInputConnector( m_targetConnector )->connect( srcModule->getOutputConnector( m_srcConnector ) );
108  }
109 }
110