OpenWalnut  1.4.0
WApplyCombiner.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <string>
00026 
00027 #include "../WModuleFactory.h"
00028 
00029 #include "WApplyCombiner.h"
00030 
00031 WApplyCombiner::WApplyCombiner( boost::shared_ptr< WModuleContainer > target,
00032                                 WModule::SPtr srcModule, std::string srcConnector,
00033                                 WModule::SPtr targetModule, std::string targetConnector ):
00034     WModuleOneToOneCombiner( target, srcModule, srcConnector, targetModule, targetConnector )
00035 {
00036 }
00037 
00038 WApplyCombiner::WApplyCombiner( WModule::SPtr srcModule, std::string srcConnector,
00039                                 WModule::SPtr targetModule, std::string targetConnector ):
00040     WModuleOneToOneCombiner( srcModule, srcConnector, targetModule, targetConnector )
00041 {
00042 }
00043 
00044 WApplyCombiner::WApplyCombiner( WModule::SPtr module ):
00045     WModuleOneToOneCombiner( WModule::SPtr(), "", module, "" )
00046 {
00047 }
00048 
00049 WApplyCombiner::~WApplyCombiner()
00050 {
00051     // cleanup
00052 }
00053 
00054 void WApplyCombiner::apply()
00055 {
00056     // create the modules from the prototypes if needed
00057     boost::shared_ptr< WModule > srcModule = m_srcModule;
00058     boost::shared_ptr< WModule > targetModule = m_targetModule;
00059 
00060     // create module instance if src is a prototype
00061     if( srcModule && WModuleFactory::isPrototype( srcModule ) )
00062     {
00063         srcModule = WModuleFactory::getModuleFactory()->create( m_srcModule );
00064     }
00065 
00066     // create module instance if target is a prototype
00067     if( targetModule && WModuleFactory::isPrototype( targetModule ) )
00068     {
00069         targetModule = WModuleFactory::getModuleFactory()->create( m_targetModule );
00070     }
00071 
00072     // add the src and target module to the container
00073     // NOTE: the container does nothing if a NULL pointer has been specified and it also does nothing if the module already is associated with
00074     // the container
00075     m_container->add( srcModule );
00076     m_container->add( targetModule );
00077 
00078     // wait for the source module if there is any
00079     if( srcModule )
00080     {
00081         srcModule->isReadyOrCrashed().wait();
00082         if( srcModule->isCrashed()() )
00083         {
00084             // NOTE: throwing an exception here should not be needed as the module container already has forwarded the exception
00085             wlog::error( "Prototype Combiner" ) << "The source module \"" << srcModule->getName() << "\" has crashed. Abort.";
00086             return;
00087         }
00088     }
00089 
00090     // wait for the source module if there is any
00091     if( targetModule )
00092     {
00093         targetModule->isReadyOrCrashed().wait();
00094         if( targetModule->isCrashed()() )
00095         {
00096             // NOTE: throwing an exception here should not be needed as the module container already has forwarded the exception
00097             wlog::error( "Prototype Combiner" ) << "The target module \"" << targetModule->getName() << "\" has crashed. Abort.";
00098             return;
00099         }
00100     }
00101 
00102     // if the connector is an empty string -> do not connect, just add
00103     if( ( m_srcConnector.empty() ) || ( m_targetConnector.empty() ) )
00104     {
00105         return;
00106     }
00107 
00108     // and connect them finally:
00109     if( srcModule && targetModule )
00110     {
00111         targetModule->getInputConnector( m_targetConnector )->disconnectAll(); // before connecting, remove existing connection on input
00112         targetModule->getInputConnector( m_targetConnector )->connect( srcModule->getOutputConnector( m_srcConnector ) );
00113     }
00114 }
00115