OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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                                 boost::shared_ptr< WModule > srcModule, std::string srcConnector,
00033                                 boost::shared_ptr< WModule > targetModule, std::string targetConnector ):
00034     WModuleOneToOneCombiner( target, srcModule, srcConnector, targetModule, targetConnector )
00035 {
00036 }
00037 
00038 WApplyCombiner::WApplyCombiner( boost::shared_ptr< WModule > srcModule, std::string srcConnector,
00039                                 boost::shared_ptr< WModule > targetModule, std::string targetConnector ):
00040     WModuleOneToOneCombiner( srcModule, srcConnector, targetModule, targetConnector )
00041 {
00042 }
00043 
00044 WApplyCombiner::~WApplyCombiner()
00045 {
00046     // cleanup
00047 }
00048 
00049 void WApplyCombiner::apply()
00050 {
00051     // create the modules from the prototypes if needed
00052     boost::shared_ptr< WModule > srcModule = m_srcModule;
00053     boost::shared_ptr< WModule > targetModule = m_targetModule;
00054 
00055     // create module instance if src is a prototype
00056     if( srcModule && WModuleFactory::isPrototype( srcModule ) )
00057     {
00058         srcModule = WModuleFactory::getModuleFactory()->create( m_srcModule );
00059     }
00060 
00061     // create module instance if target is a prototype
00062     if( targetModule && WModuleFactory::isPrototype( targetModule ) )
00063     {
00064         targetModule = WModuleFactory::getModuleFactory()->create( m_targetModule );
00065     }
00066 
00067     // add the src and target module to the container
00068     // NOTE: the container does nothing if a NULL pointer has been specified and it also does nothing if the module already is associated with
00069     // the container
00070     m_container->add( srcModule );
00071     m_container->add( targetModule );
00072 
00073     // wait for the source module if there is any
00074     if( srcModule )
00075     {
00076         srcModule->isReadyOrCrashed().wait();
00077         if( srcModule->isCrashed()() )
00078         {
00079             // NOTE: throwing an exception here should not be needed as the module container already has forwarded the exception
00080             wlog::error( "Prototype Combiner" ) << "The source module \"" << srcModule->getName() << "\" has crashed. Abort.";
00081             return;
00082         }
00083     }
00084 
00085     // wait for the source module if there is any
00086     if( targetModule )
00087     {
00088         targetModule->isReadyOrCrashed().wait();
00089         if( targetModule->isCrashed()() )
00090         {
00091             // NOTE: throwing an exception here should not be needed as the module container already has forwarded the exception
00092             wlog::error( "Prototype Combiner" ) << "The target module \"" << targetModule->getName() << "\" has crashed. Abort.";
00093             return;
00094         }
00095     }
00096 
00097     // if the connector is an empty string -> do not connect, just add
00098     if( ( m_srcConnector.empty() ) || ( m_targetConnector.empty() ) )
00099     {
00100         return;
00101     }
00102 
00103     // and connect them finally:
00104     if( srcModule && targetModule )
00105     {
00106         targetModule->getInputConnector( m_targetConnector )->disconnectAll(); // before connecting, remove existing connection on input
00107         targetModule->getInputConnector( m_targetConnector )->connect( srcModule->getOutputConnector( m_srcConnector ) );
00108     }
00109 }
00110 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends