OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
WModuleOutputForwardData.h
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 #ifndef WMODULEOUTPUTFORWARDDATA_H
00026 #define WMODULEOUTPUTFORWARDDATA_H
00027 
00028 #include <iostream>
00029 #include <string>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 
00033 #include "../common/WLogger.h"
00034 
00035 #include "WModuleInputData.h"
00036 #include "WModuleOutputData.h"
00037 
00038 /**
00039  * This is a simple class which forwards output data to output data connectors. It itself is a output data connector and can be used
00040  * as one, but also provides the possibility to forward data changes to other output data connectors.
00041  */
00042 template< typename T >
00043 class WModuleOutputForwardData: public WModuleOutputData< T >
00044 {
00045 public:
00046 
00047     /**
00048      * Constructor. This creates a new output data connector which is able to forward data changes <b>FROM</b> other output data connectors.
00049      *
00050      * \param module the module which is owner of this connector.
00051      * \param name The name of this connector.
00052      * \param description Short description of this connector.
00053      */
00054     WModuleOutputForwardData( boost::shared_ptr< WModule > module, std::string name="", std::string description="" )
00055         :WModuleOutputData< T >( module, name, description )
00056     {
00057         // initialize the output data connector
00058         m_in = boost::shared_ptr< WModuleInputData< T > >( new WModuleInputData< T >( module, "[FWD]" + name, description ) );
00059 
00060         // subscribe both signals
00061         m_in->subscribeSignal( CONNECTION_ESTABLISHED, boost::bind( &WModuleOutputForwardData::inputNotifyDataChange, this, _1, _2 ) );
00062         m_in->subscribeSignal( DATA_CHANGED,           boost::bind( &WModuleOutputForwardData::inputNotifyDataChange, this, _1, _2 ) );
00063     };
00064 
00065     /**
00066      * Destructor.
00067      */
00068     virtual ~WModuleOutputForwardData()
00069     {
00070     }
00071 
00072     /**
00073      * Forward the output to the specified output. The specified output must be compatible with the template parameter of this output.
00074      *
00075      * \param from the output connector whose data should be forwarded.
00076      */
00077     virtual void forward( boost::shared_ptr< WModuleConnector > from )
00078     {
00079         m_in->connect( from );
00080     }
00081 
00082     /**
00083      * Remove the specified connector from the forwarding list.
00084      *
00085      * \param from the output connector to be removed from forwarding list.
00086      */
00087     virtual void unforward( boost::shared_ptr< WModuleConnector > from )
00088     {
00089         m_in->disconnect( from );
00090     }
00091 
00092 protected:
00093 
00094     /**
00095      * The output connector which collects data and distributes it to all connectors connected using the forwardTo() method.
00096      */
00097     boost::shared_ptr< WModuleInputData< T > > m_in;
00098 
00099     /**
00100      * Gets called whenever a connected output updates its data. In detail: it is a callback for m_in and waits simply forwards
00101      * new data to this output instance.
00102      */
00103     virtual void inputNotifyDataChange( boost::shared_ptr<WModuleConnector> /*input*/, boost::shared_ptr<WModuleConnector> /*output*/ )
00104     {
00105         // if the input changes its data-> forward the change to this output instance
00106         this->updateData( m_in->getData() );
00107     }
00108 
00109 private:
00110 };
00111 
00112 #endif  // WMODULEOUTPUTFORWARDDATA_H
00113 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends