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