OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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      * Reference to this type.
00057      */
00058     typedef WModuleOutputData< T >& RefType;
00059 
00060     /**
00061      * Type of the connector.
00062      */
00063     typedef WModuleOutputData< T > Type;
00064 
00065     /**
00066      * Typedef to the contained transferable.
00067      */
00068     typedef T TransferType;
00069 
00070     /**
00071      * Convenience method to create a new instance of this out data connector with proper type.
00072      *
00073      * \param module    the module owning this instance
00074      * \param name      the name of this connector.
00075      * \param description the description of this connector.
00076      *
00077      * \return the pointer to the created connector.
00078      */
00079     static PtrType create( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
00080 
00081     /**
00082      * 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
00083      * specified module.
00084      *
00085      * \param module    the module owning this instance
00086      * \param name      the name of this connector.
00087      * \param description the description of this connector.
00088      *
00089      * \return the pointer to the created connector.
00090      */
00091     static PtrType createAndAdd( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
00092 
00093     /**
00094      * Constructor.
00095      *
00096      * \param module the module which is owner of this connector.
00097      * \param name The name of this connector.
00098      * \param description Short description of this connector.
00099      */
00100     WModuleOutputData( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" )
00101         :WModuleOutputConnector( module, name, description )
00102     {
00103         m_data = boost::shared_ptr< T >();
00104     };
00105 
00106     /**
00107      * Destructor.
00108      */
00109     virtual ~WModuleOutputData()
00110     {
00111     };
00112 
00113     /**
00114      * Update the data associated
00115      *
00116      * \param data the data do send
00117      */
00118     virtual void updateData( boost::shared_ptr< T > data )
00119     {
00120         m_data = data;
00121 
00122         // broadcast this event
00123         triggerUpdate();
00124     };
00125 
00126     /**
00127      * Resets the data on this output. It actually sets NULL and triggers an update.
00128      */
00129     virtual void reset()
00130     {
00131         updateData( boost::shared_ptr< T >() );
00132     }
00133 
00134     /**
00135      * This method simply propagates an update but does not actually change the data.
00136      */
00137     virtual void triggerUpdate()
00138     {
00139         // broadcast this event
00140         propagateDataChange();
00141     };
00142 
00143     /**
00144      * Gives back the currently set data as WTransferable.
00145      *
00146      * \return the data. If no data has been set: a NULL pointer is returned.
00147      */
00148     virtual const boost::shared_ptr< WTransferable > getRawData() const
00149     {
00150         return m_data;
00151     };
00152 
00153     /**
00154      * Gives back the currently set data.
00155      *
00156      * \return the data. If no data has been set: a NULL pointer is returned.
00157      */
00158     const boost::shared_ptr< T > getData() const
00159     {
00160         return m_data;
00161     };
00162 
00163     /**
00164      * Checks whether the specified connector is an input connector and compatible with T.
00165      *
00166      * \param con the connector to check against.
00167      *
00168      * \return true if compatible.
00169      */
00170     virtual bool connectable( boost::shared_ptr<WModuleConnector> con )
00171     {
00172         // since WModuleInputData::connectable already does all the type checking, we simply forward the call
00173         return WModuleOutputConnector::connectable( con );
00174     };
00175 
00176     /**
00177      * Returns the prototype of the Type T used in this connector.
00178      *
00179      * \return the prototype of the transfered type.
00180      */
00181     virtual boost::shared_ptr< WPrototyped > getTransferPrototype()
00182     {
00183         // get prototype or the data pointer currently set
00184         return ( m_data == boost::shared_ptr< T >() ) ? T::getPrototype() : boost::shared_static_cast< WPrototyped >( m_data );
00185     };
00186 
00187 protected:
00188 
00189 private:
00190 
00191     /**
00192      * The data associated with this connector.
00193      */
00194     boost::shared_ptr< T > m_data;
00195 };
00196 
00197 template < typename T >
00198 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::create( boost::shared_ptr< WModule > module, std::string name,
00199                                                                                                               std::string description )
00200 {
00201     typedef typename WModuleOutputData< T >::PtrType PTR;
00202     typedef typename WModuleOutputData< T >::Type TYPE;
00203     return PTR( new TYPE( module, name, description ) );
00204 }
00205 
00206 template < typename T >
00207 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::createAndAdd( boost::shared_ptr< WModule > module, std::string name,
00208                                                                                                                     std::string description )
00209 {
00210     typename WModuleOutputData< T >::PtrType c = create( module, name, description );
00211     module->addConnector( c );
00212     return c;
00213 }
00214 
00215 #endif  // WMODULEOUTPUTDATA_H
00216 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends