OpenWalnut  1.5.0dev
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 <memory>
26 #include <string>
27 
28 #include "../WModuleFactory.h"
29 #include "WApplyCombiner.h"
30 
31 WApplyCombiner::WApplyCombiner( std::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  std::shared_ptr< WModule > srcModule = m_srcModule;
58  std::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 
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.
virtual ~WApplyCombiner()
Destructor.
std::shared_ptr< WModuleContainer > m_container
The module container to use for the modules.
static SPtr getModuleFactory()
Returns instance of the module factory to use to create modules.
static bool isPrototype(std::shared_ptr< WModule > module)
Checks whether the specified module is a prototype or an instantiated module.
Base class for all combiners which apply one connection between two connectors of two modules.
std::shared_ptr< WModule > m_srcModule
The source module to connect with the target.
std::string m_targetConnector
The input connector the target module to connect with m_srcConnector.
std::string m_srcConnector
The output connector of m_srcModule to connect with m_targetConnector.
std::shared_ptr< WModule > m_targetModule
The module/prototype to connect with m_srcMdodule.
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
std::shared_ptr< WModule > SPtr
Shared pointer to a WModule.
Definition: WModule.h:106
std::shared_ptr< WThreadedRunner > SPtr
Abbreviation to a shared_ptr to this type.
WStreamedLogger error(const std::string &source)
Logging an error message.
Definition: WLogger.h:298