OpenWalnut  1.4.0
WFlagForwarder.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WFLAGFORWARDER_H
26 #define WFLAGFORWARDER_H
27 
28 #ifndef Q_MOC_RUN
29 #include <boost/shared_ptr.hpp>
30 #endif
31 #ifndef Q_MOC_RUN
32 #include <boost/signals2/signal.hpp>
33 #endif
34 
35 #include "WFlag.h"
36 
37 
38 /**
39  * This class helps especially container module programmers to easily synchronize the value of one flag with several other
40  * flag. Assume the following scenario: you have a container module with two isosurface modules whose isovalue-properties
41  * need to be in sync with the isovalue-property your container module provides to the outside world. Here, WFlagForwaderd
42  * comes into play. Add the first isosurface's isovalue-property to the container modules m_properties list and forward it to
43  * the other isovalue-property of the second isosurface module. Now they are in sync.
44  * Be aware, that this is not a property itself!
45  *
46  * \note this class is not thread-safe for performance reasons. It is possible to add further flags to the forward-list but
47  * be yourself aware that you might miss value changes if you add the flag right between two value changes of the source flag.
48  *
49  * \note Also be aware that this class only forwards changes in the flag value! Changes of "hidden" in PropertyVariables
50  * are not propagated.
51  *
52  * \note The template parameter is the type encapsulated inside the flag. I.e. for WFlag< bool > use T=bool
53  *
54  * \param T the encapsulated type inside the flag. I.e. for WFlag< int32_t > use T=int32_t
55  */
56 template < typename T >
57 class WFlagForwarder // NOLINT
58 {
59 public:
60  /**
61  * Default constructor.
62  *
63  * \param source the property to be used as reference. All other properties will be synced with this one.
64  */
65  explicit WFlagForwarder( boost::shared_ptr< WFlag< T > > source );
66 
67  /**
68  * Destructor.
69  */
70  virtual ~WFlagForwarder();
71 
72  /**
73  * Forward the source property to the specified one. This ensures that the flag in "to" always has the value of the source flag.
74  * There is no remove method.
75  *
76  * \param to the property to sync with source.
77  */
78  void forward( boost::shared_ptr< WFlag< T > > to );
79 
80 protected:
81  /**
82  * The source property to which all other properties are synced to.
83  */
84  boost::shared_ptr< WFlag< T > > m_source;
85 
86  /**
87  * The signal fired by m_source upon value change
88  */
89  boost::signals2::connection m_sourceConnection;
90 
91  /**
92  * Signal forwarding the new value.
93  */
94  boost::signals2::signal< void( T ) > signal_forward;
95 
96  /**
97  * This is a callback and gets called whenever the source property has changed.
98  */
99  void sourceChanged();
100 
101 private:
102  /**
103  * Disallow copy construction.
104  *
105  * \param rhs the other forwarder.
106  */
107  WFlagForwarder( const WFlagForwarder& rhs );
108 
109  /**
110  * Disallow copy assignment.
111  *
112  * \param rhs the other forwarder.
113  * \return this.
114  */
115  WFlagForwarder& operator=( const WFlagForwarder& rhs );
116 };
117 
118 template < typename T >
119 WFlagForwarder< T >::WFlagForwarder( boost::shared_ptr< WFlag< T > > source ):
120  m_source( source )
121 {
122  // connect the source's change signal
123  m_sourceConnection = source->getValueChangeCondition()->subscribeSignal( boost::bind( &WFlagForwarder::sourceChanged, this ) );
124 }
125 
126 template < typename T >
128 {
129  // cleanup (disconnect all)
130  m_sourceConnection.disconnect();
131  signal_forward.disconnect_all_slots();
132 }
133 
134 template < typename T >
135 void WFlagForwarder< T >::forward( boost::shared_ptr< WFlag< T > > to )
136 {
137  to->set( m_source->get() );
138 
139  // NOTE: we do not need to store the signals2::connection here as the destructor disconnects ALL slots
140  signal_forward.connect( boost::bind( &WFlag< T >::set, to.get(), _1, false ) );
141 }
142 
143 template < typename T >
145 {
146  signal_forward( m_source->get() );
147 }
148 
149 #endif // WFLAGFORWARDER_H
150 
boost::shared_ptr< WFlag< T > > m_source
The source property to which all other properties are synced to.
WFlagForwarder & operator=(const WFlagForwarder &rhs)
Disallow copy assignment.
Class to have a simple notification/condition system for simple flags.
Definition: WFlag.h:39
boost::signals2::connection m_sourceConnection
The signal fired by m_source upon value change.
void forward(boost::shared_ptr< WFlag< T > > to)
Forward the source property to the specified one.
WFlagForwarder(boost::shared_ptr< WFlag< T > > source)
Default constructor.
This class helps especially container module programmers to easily synchronize the value of one flag ...
void sourceChanged()
This is a callback and gets called whenever the source property has changed.
boost::signals2::signal< void(T) > signal_forward
Signal forwarding the new value.
virtual const T & get(bool resetChangeState=false)
Operator returns value of the flag.
Definition: WFlag.h:258
virtual ~WFlagForwarder()
Destructor.