OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 #include "WExportCommon.h"
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 OWCOMMON_EXPORT WFlagForwarder // NOLINT
54 {
55 public:
56 
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( boost::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( boost::shared_ptr< WFlag< T > > to );
76 
77 protected:
78 
79  /**
80  * The source property to which all other properties are synced to.
81  */
82  boost::shared_ptr< WFlag< T > > m_source;
83 
84  /**
85  * The signal fired by m_source upon value change
86  */
87  boost::signals2::connection m_sourceConnection;
88 
89  /**
90  * Signal forwarding the new value.
91  */
92  boost::signals2::signal< void( T ) > signal_forward;
93 
94  /**
95  * This is a callback and gets called whenever the source property has changed.
96  */
97  void sourceChanged();
98 
99 private:
100 
101  /**
102  * Disallow copy construction.
103  *
104  * \param rhs the other forwarder.
105  */
106  WFlagForwarder( const WFlagForwarder& rhs );
107 
108  /**
109  * Disallow copy assignment.
110  *
111  * \param rhs the other forwarder.
112  * \return this.
113  */
114  WFlagForwarder& operator=( const WFlagForwarder& rhs );
115 };
116 
117 template < typename T >
118 WFlagForwarder< T >::WFlagForwarder( boost::shared_ptr< WFlag< T > > source ):
119  m_source( source )
120 {
121  // connect the source's change signal
122  m_sourceConnection = source->getValueChangeCondition()->subscribeSignal( boost::bind( &WFlagForwarder::sourceChanged, this ) );
123 }
124 
125 template < typename T >
127 {
128  // cleanup (disconnect all)
129  m_sourceConnection.disconnect();
130  signal_forward.disconnect_all_slots();
131 }
132 
133 template < typename T >
134 void WFlagForwarder< T >::forward( boost::shared_ptr< WFlag< T > > to )
135 {
136  to->set( m_source->get() );
137 
138  // NOTE: we do not need to store the signals2::connection here as the destructor disconnects ALL slots
139  signal_forward.connect( boost::bind( &WFlag< T >::set, to.get(), _1, false ) );
140 }
141 
142 template < typename T >
144 {
145  signal_forward( m_source->get() );
146 }
147 
148 #endif // WFLAGFORWARDER_H
149