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