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 #include <boost/shared_ptr.hpp>
29 #include <boost/signals2/signal.hpp>
30 
31 #include "WFlag.h"
32 
33 
34 /**
35  * This class helps especially container module programmers to easily synchronize the value of one flag with several other
36  * flag. Assume the following scenario: you have a container module with two isosurface modules whose isovalue-properties
37  * need to be in sync with the isovalue-property your container module provides to the outside world. Here, WFlagForwaderd
38  * comes into play. Add the first isosurface's isovalue-property to the container modules m_properties list and forward it to
39  * the other isovalue-property of the second isosurface module. Now they are in sync.
40  * Be aware, that this is not a property itself!
41  *
42  * \note this class is not thread-safe for performance reasons. It is possible to add further flags to the forward-list but
43  * be yourself aware that you might miss value changes if you add the flag right between two value changes of the source flag.
44  *
45  * \note Also be aware that this class only forwards changes in the flag value! Changes of "hidden" in PropertyVariables
46  * are not propagated.
47  *
48  * \note The template parameter is the type encapsulated inside the flag. I.e. for WFlag< bool > use T=bool
49  *
50  * \param T the encapsulated type inside the flag. I.e. for WFlag< int32_t > use T=int32_t
51  */
52 template < typename T >
53 class WFlagForwarder // NOLINT
54 {
55 public:
56  /**
57  * Default constructor.
58  *
59  * \param source the property to be used as reference. All other properties will be synced with this one.
60  */
61  explicit WFlagForwarder( boost::shared_ptr< WFlag< T > > source );
62 
63  /**
64  * Destructor.
65  */
66  virtual ~WFlagForwarder();
67 
68  /**
69  * Forward the source property to the specified one. This ensures that the flag in "to" always has the value of the source flag.
70  * There is no remove method.
71  *
72  * \param to the property to sync with source.
73  */
74  void forward( boost::shared_ptr< WFlag< T > > to );
75 
76 protected:
77  /**
78  * The source property to which all other properties are synced to.
79  */
80  boost::shared_ptr< WFlag< T > > m_source;
81 
82  /**
83  * The signal fired by m_source upon value change
84  */
85  boost::signals2::connection m_sourceConnection;
86 
87  /**
88  * Signal forwarding the new value.
89  */
90  boost::signals2::signal< void( T ) > signal_forward;
91 
92  /**
93  * This is a callback and gets called whenever the source property has changed.
94  */
95  void sourceChanged();
96 
97 private:
98  /**
99  * Disallow copy construction.
100  *
101  * \param rhs the other forwarder.
102  */
103  WFlagForwarder( const WFlagForwarder& rhs );
104 
105  /**
106  * Disallow copy assignment.
107  *
108  * \param rhs the other forwarder.
109  * \return this.
110  */
111  WFlagForwarder& operator=( const WFlagForwarder& rhs );
112 };
113 
114 template < typename T >
115 WFlagForwarder< T >::WFlagForwarder( boost::shared_ptr< WFlag< T > > source ):
116  m_source( source )
117 {
118  // connect the source's change signal
119  m_sourceConnection = source->getValueChangeCondition()->subscribeSignal( boost::bind( &WFlagForwarder::sourceChanged, this ) );
120 }
121 
122 template < typename T >
124 {
125  // cleanup (disconnect all)
126  m_sourceConnection.disconnect();
127  signal_forward.disconnect_all_slots();
128 }
129 
130 template < typename T >
131 void WFlagForwarder< T >::forward( boost::shared_ptr< WFlag< T > > to )
132 {
133  to->set( m_source->get() );
134 
135  // NOTE: we do not need to store the signals2::connection here as the destructor disconnects ALL slots
136  signal_forward.connect( boost::bind( &WFlag< T >::set, to.get(), _1, false ) );
137 }
138 
139 template < typename T >
141 {
142  signal_forward( m_source->get() );
143 }
144 
145 #endif // WFLAGFORWARDER_H
146 
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:37
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:256
virtual ~WFlagForwarder()
Destructor.