OpenWalnut  1.4.0
WModuleOutputData.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 WMODULEOUTPUTDATA_H
00026 #define WMODULEOUTPUTDATA_H
00027 
00028 #include <string>
00029 
00030 #include <boost/shared_ptr.hpp>
00031 
00032 #include "../common/WLogger.h"
00033 
00034 // this is necessary since we have some kind of cyclic includes
00035 template < typename T > class WModuleInputData;
00036 #include "WModuleInputData.h"
00037 #include "../common/WPrototyped.h"
00038 #include "../common/WTransferable.h"
00039 
00040 #include "WModuleOutputConnector.h"
00041 
00042 /**
00043  * Class offering an instantiate-able data connection between modules.
00044  * Due to is template style it is possible to bind nearly arbitrary data.
00045  */
00046 template < typename T >
00047 class WModuleOutputData: public WModuleOutputConnector
00048 {
00049 public:
00050     /**
00051      * Pointer to this. For convenience.
00052      */
00053     typedef boost::shared_ptr< WModuleOutputData< T > > PtrType;
00054 
00055     /**
00056      * Pointer to this. For convenience.
00057      */
00058     typedef boost::shared_ptr< WModuleOutputData< T > > SPtr;
00059 
00060     /**
00061      * Pointer to this. For convenience.
00062      */
00063     typedef boost::shared_ptr< const WModuleOutputData< T > > ConstSPtr;
00064 
00065     /**
00066      * Reference to this type.
00067      */
00068     typedef WModuleOutputData< T >& RefType;
00069 
00070     /**
00071      * Type of the connector.
00072      */
00073     typedef WModuleOutputData< T > Type;
00074 
00075     /**
00076      * Typedef to the contained transferable.
00077      */
00078     typedef T TransferType;
00079 
00080     /**
00081      * Convenience method to create a new instance of this out data connector with proper type.
00082      *
00083      * \param module    the module owning this instance
00084      * \param name      the name of this connector.
00085      * \param description the description of this connector.
00086      *
00087      * \return the pointer to the created connector.
00088      */
00089     static PtrType create( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
00090 
00091     /**
00092      * 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
00093      * specified module.
00094      *
00095      * \param module    the module owning this instance
00096      * \param name      the name of this connector.
00097      * \param description the description of this connector.
00098      *
00099      * \return the pointer to the created connector.
00100      */
00101     static PtrType createAndAdd( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
00102 
00103     /**
00104      * Constructor.
00105      *
00106      * \param module the module which is owner of this connector.
00107      * \param name The name of this connector.
00108      * \param description Short description of this connector.
00109      */
00110     WModuleOutputData( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" )
00111         :WModuleOutputConnector( module, name, description )
00112     {
00113         m_data = boost::shared_ptr< T >();
00114     };
00115 
00116     /**
00117      * Destructor.
00118      */
00119     virtual ~WModuleOutputData()
00120     {
00121     };
00122 
00123     /**
00124      * Update the data associated
00125      *
00126      * \param data the data do send
00127      */
00128     virtual void updateData( boost::shared_ptr< T > data )
00129     {
00130         m_data = data;
00131 
00132         // broadcast this event
00133         triggerUpdate();
00134     };
00135 
00136     /**
00137      * Resets the data on this output. It actually sets NULL and triggers an update.
00138      */
00139     virtual void reset()
00140     {
00141         updateData( boost::shared_ptr< T >() );
00142     }
00143 
00144     /**
00145      * This method simply propagates an update but does not actually change the data.
00146      */
00147     virtual void triggerUpdate()
00148     {
00149         // broadcast this event
00150         propagateDataChange();
00151     };
00152 
00153     /**
00154      * Gives back the currently set data as WTransferable.
00155      *
00156      * \return the data. If no data has been set: a NULL pointer is returned.
00157      */
00158     virtual const boost::shared_ptr< WTransferable > getRawData() const
00159     {
00160         return m_data;
00161     };
00162 
00163     /**
00164      * Gives back the currently set data.
00165      *
00166      * \return the data. If no data has been set: a NULL pointer is returned.
00167      */
00168     const boost::shared_ptr< T > getData() const
00169     {
00170         return m_data;
00171     };
00172 
00173     /**
00174      * Checks whether the specified connector is an input connector and compatible with T.
00175      *
00176      * \param con the connector to check against.
00177      *
00178      * \return true if compatible.
00179      */
00180     virtual bool connectable( boost::shared_ptr<WModuleConnector> con )
00181     {
00182         // since WModuleInputData::connectable already does all the type checking, we simply forward the call
00183         return WModuleOutputConnector::connectable( con );
00184     };
00185 
00186     /**
00187      * Returns the prototype of the Type T used in this connector.
00188      *
00189      * \return the prototype of the transfered type.
00190      */
00191     virtual boost::shared_ptr< WPrototyped > getTransferPrototype()
00192     {
00193         // get prototype or the data pointer currently set
00194         return ( m_data == boost::shared_ptr< T >() ) ? T::getPrototype() : boost::static_pointer_cast< WPrototyped >( m_data );
00195     };
00196 
00197 protected:
00198 private:
00199     /**
00200      * The data associated with this connector.
00201      */
00202     boost::shared_ptr< T > m_data;
00203 };
00204 
00205 template < typename T >
00206 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::create( boost::shared_ptr< WModule > module, std::string name,
00207                                                                                                               std::string description )
00208 {
00209     typedef typename WModuleOutputData< T >::PtrType PTR;
00210     typedef typename WModuleOutputData< T >::Type TYPE;
00211     return PTR( new TYPE( module, name, description ) );
00212 }
00213 
00214 template < typename T >
00215 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::createAndAdd( boost::shared_ptr< WModule > module, std::string name,
00216                                                                                                                     std::string description )
00217 {
00218     typename WModuleOutputData< T >::PtrType c = create( module, name, description );
00219     module->addConnector( c );
00220     return c;
00221 }
00222 
00223 #endif  // WMODULEOUTPUTDATA_H
00224