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