OpenWalnut 1.2.5

WFlagForwarder.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 WFLAGFORWARDER_H
00026 #define WFLAGFORWARDER_H
00027 
00028 #include <boost/shared_ptr.hpp>
00029 #include <boost/signals2/signal.hpp>
00030 
00031 #include "WFlag.h"
00032 #include "WExportCommon.h"
00033 
00034 /**
00035  * This class helps especially container module programmers to easily synchronize the value of one flag with several other
00036  * flag. Assume the following scenario: you have a container module with two isosurface modules whose isovalue-properties
00037  * need to be in sync with the isovalue-property your container module provides to the outside world. Here, WFlagForwaderd
00038  * comes into play. Add the first isosurface's isovalue-property to the container modules m_properties list and forward it to
00039  * the other isovalue-property of the second isosurface module. Now they are in sync.
00040  * Be aware, that this is not a property itself!
00041  *
00042  * \note this class is not thread-safe for performance reasons. It is possible to add further flags to the forward-list but
00043  * be yourself aware that you might miss value changes if you add the flag right between two value changes of the source flag.
00044  *
00045  * \note Also be aware that this class only forwards changes in the flag value! Changes of "hidden" in PropertyVariables
00046  * are not propagated.
00047  *
00048  * \note The template parameter is the type encapsulated inside the flag. I.e. for WFlag< bool > use T=bool
00049  *
00050  * \param T the encapsulated type inside the flag. I.e. for WFlag< int32_t > use T=int32_t
00051  */
00052 template < typename T >
00053 class OWCOMMON_EXPORT WFlagForwarder // NOLINT
00054 {
00055 public:
00056 
00057     /**
00058      * Default constructor.
00059      *
00060      * \param source the property to be used as reference. All other properties will be synced with this one.
00061      */
00062     explicit WFlagForwarder( boost::shared_ptr< WFlag< T > > source );
00063 
00064     /**
00065      * Destructor.
00066      */
00067     virtual ~WFlagForwarder();
00068 
00069     /**
00070      * Forward the source property to the specified one. This ensures that the flag in "to" always has the value of the source flag.
00071      * There is no remove method.
00072      *
00073      * \param to the property to sync with source.
00074      */
00075     void forward( boost::shared_ptr< WFlag< T > > to );
00076 
00077 protected:
00078 
00079     /**
00080      * The source property to which all other properties are synced to.
00081      */
00082     boost::shared_ptr< WFlag< T > > m_source;
00083 
00084     /**
00085      * The signal fired by m_source upon value change
00086      */
00087     boost::signals2::connection m_sourceConnection;
00088 
00089     /**
00090      * Signal forwarding the new value.
00091      */
00092     boost::signals2::signal< void( T ) > signal_forward;
00093 
00094     /**
00095      * This is a callback and gets called whenever the source property has changed.
00096      */
00097     void sourceChanged();
00098 
00099 private:
00100 
00101     /**
00102      * Disallow copy construction.
00103      *
00104      * \param rhs the other forwarder.
00105      */
00106     WFlagForwarder( const WFlagForwarder& rhs );
00107 
00108     /**
00109      * Disallow copy assignment.
00110      *
00111      * \param rhs the other forwarder.
00112      * \return this.
00113      */
00114     WFlagForwarder& operator=( const WFlagForwarder& rhs );
00115 };
00116 
00117 template < typename T >
00118 WFlagForwarder< T >::WFlagForwarder( boost::shared_ptr< WFlag< T > > source ):
00119     m_source( source )
00120 {
00121     // connect the source's change signal
00122     m_sourceConnection = source->getValueChangeCondition()->subscribeSignal( boost::bind( &WFlagForwarder::sourceChanged, this ) );
00123 }
00124 
00125 template < typename T >
00126 WFlagForwarder< T >::~WFlagForwarder()
00127 {
00128     // cleanup (disconnect all)
00129     m_sourceConnection.disconnect();
00130     signal_forward.disconnect_all_slots();
00131 }
00132 
00133 template < typename T >
00134 void WFlagForwarder< T >::forward( boost::shared_ptr< WFlag< T > >  to )
00135 {
00136     to->set( m_source->get() );
00137 
00138     // NOTE: we do not need to store the signals2::connection here as the destructor disconnects ALL slots
00139     signal_forward.connect( boost::bind( &WFlag< T >::set, to.get(), _1, false ) );
00140 }
00141 
00142 template < typename T >
00143 void WFlagForwarder< T >::sourceChanged()
00144 {
00145     signal_forward( m_source->get() );
00146 }
00147 
00148 #endif  // WFLAGFORWARDER_H
00149 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends