OpenWalnut 1.3.1
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      * Pointer to this. For convenience.
00048      */
00049     typedef boost::shared_ptr< WModuleOutputForwardData< T > > SPtr;
00050 
00051     /**
00052      * Pointer to this. For convenience.
00053      */
00054     typedef boost::shared_ptr< const WModuleOutputForwardData< T > > ConstSPtr;
00055 
00056     /**
00057      * Pointer to this. For convenience.
00058      */
00059     typedef SPtr PtrType;
00060 
00061     /**
00062      * Reference to this type.
00063      */
00064     typedef WModuleOutputForwardData< T >& RefType;
00065 
00066     /**
00067      * Type of the connector.
00068      */
00069     typedef WModuleOutputForwardData< T > Type;
00070 
00071     /**
00072      * Typedef to the contained transferable.
00073      */
00074     typedef T TransferType;
00075 
00076     /**
00077      * Convenience method to create a new instance of this out data connector with proper type.
00078      *
00079      * \param module    the module owning this instance
00080      * \param name      the name of this connector.
00081      * \param description the description of this connector.
00082      *
00083      * \return the pointer to the created connector.
00084      */
00085     static PtrType create( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
00086 
00087     /**
00088      * Convenience method to create a new instance of this out data connector with proper type and add it to the list of connectors of the
00089      * specified module.
00090      *
00091      * \param module    the module owning this instance
00092      * \param name      the name of this connector.
00093      * \param description the description of this connector.
00094      *
00095      * \return the pointer to the created connector.
00096      */
00097     static PtrType createAndAdd( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
00098 
00099     /**
00100      * Constructor. This creates a new output data connector which is able to forward data changes <b>FROM</b> other output data connectors.
00101      *
00102      * \param module the module which is owner of this connector.
00103      * \param name The name of this connector.
00104      * \param description Short description of this connector.
00105      */
00106     WModuleOutputForwardData( boost::shared_ptr< WModule > module, std::string name="", std::string description="" )
00107         :WModuleOutputData< T >( module, name, description )
00108     {
00109         // initialize the output data connector
00110         m_in = boost::shared_ptr< WModuleInputData< T > >( new WModuleInputData< T >( module, "[FWD]" + name, description ) );
00111 
00112         // subscribe both signals
00113         m_in->subscribeSignal( CONNECTION_ESTABLISHED, boost::bind( &WModuleOutputForwardData::inputNotifyDataChange, this, _1, _2 ) );
00114         m_in->subscribeSignal( DATA_CHANGED,           boost::bind( &WModuleOutputForwardData::inputNotifyDataChange, this, _1, _2 ) );
00115     };
00116 
00117     /**
00118      * Destructor.
00119      */
00120     virtual ~WModuleOutputForwardData()
00121     {
00122     }
00123 
00124     /**
00125      * Forward the output to the specified output. The specified output must be compatible with the template parameter of this output.
00126      *
00127      * \param from the output connector whose data should be forwarded.
00128      */
00129     virtual void forward( boost::shared_ptr< WModuleConnector > from )
00130     {
00131         m_in->connect( from );
00132     }
00133 
00134     /**
00135      * Remove the specified connector from the forwarding list.
00136      *
00137      * \param from the output connector to be removed from forwarding list.
00138      */
00139     virtual void unforward( boost::shared_ptr< WModuleConnector > from )
00140     {
00141         m_in->disconnect( from );
00142     }
00143 
00144 protected:
00145     /**
00146      * The output connector which collects data and distributes it to all connectors connected using the forwardTo() method.
00147      */
00148     boost::shared_ptr< WModuleInputData< T > > m_in;
00149 
00150     /**
00151      * Gets called whenever a connected output updates its data. In detail: it is a callback for m_in and waits simply forwards
00152      * new data to this output instance.
00153      */
00154     virtual void inputNotifyDataChange( boost::shared_ptr<WModuleConnector> /*input*/, boost::shared_ptr<WModuleConnector> /*output*/ )
00155     {
00156         // if the input changes its data-> forward the change to this output instance
00157         WModuleOutputData< T >::updateData( m_in->getData() );
00158     }
00159 
00160 private:
00161 };
00162 
00163 template < typename T >
00164 typename WModuleOutputForwardData< T >::PtrType WModuleOutputForwardData< T >::create( boost::shared_ptr< WModule > module, std::string name,
00165                                                                                                               std::string description )
00166 {
00167     typedef typename WModuleOutputForwardData< T >::PtrType PTR;
00168     typedef typename WModuleOutputForwardData< T >::Type TYPE;
00169     return PTR( new TYPE( module, name, description ) );
00170 }
00171 
00172 template < typename T >
00173 typename WModuleOutputForwardData< T >::PtrType WModuleOutputForwardData< T >::createAndAdd( boost::shared_ptr< WModule > module, std::string name,
00174                                                                                                                     std::string description )
00175 {
00176     typename WModuleOutputForwardData< T >::PtrType c = create( module, name, description );
00177     module->addConnector( c );
00178     return c;
00179 }
00180 
00181 #endif  // WMODULEOUTPUTFORWARDDATA_H
00182