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