OpenWalnut  1.5.0dev
WPropertyObserver.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 WPROPERTYOBSERVER_H
26 #define WPROPERTYOBSERVER_H
27 
28 #include <map>
29 #include <memory>
30 #include <set>
31 #include <string>
32 
33 #include <boost/signals2/signal.hpp>
34 #include <boost/thread.hpp>
35 
36 #include "WCondition.h"
37 #include "WProperties.h"
38 
39 
40 /**
41  * This class can observe properties inside a property group. The property group to observer can simply be set and replaced comfortably. Whenever
42  * one of the child properties updates, the observer fires too. If the observed property group itself
43  * changes (added properties, removed properties and so on), the observer gets updated automatically.
44  */
46 {
47 public:
48  /**
49  * Convenience type for a set of property instances.
50  */
51  typedef std::map< std::string, std::shared_ptr< WPropertyBase > > PropertyNameMap;
52 
53  /**
54  * Convenience type for a shared pointer on property observers.
55  */
56  typedef std::shared_ptr< WPropertyObserver > SPtr;
57 
58  /**
59  * Default constructor.
60  */
62 
63  /**
64  * Destructor.
65  */
66  virtual ~WPropertyObserver();
67 
68  /**
69  * Defines the property group whose children should be watched. You can define a list of names manually if you are not interested in updates
70  * of ALL properties.
71  * \note this also resets the updated flag and the list of the last fired properties.
72  *
73  * \param properties the group whose children should be watched.
74  * \param names list of names. If specified, only these properties are observed.
75  */
76  void observe( std::shared_ptr< WProperties > properties, std::set< std::string > names = std::set< std::string >() );
77 
78  /**
79  * Is true if one observed property fired. This is reset by the \ref handled method.
80  *
81  * \return true if one property fired.
82  */
83  bool updated() const;
84 
85  /**
86  * Resets the update flag and the list of fired properties.
87  *
88  * \return the set of properties fired until the last call of \ref handled.
89  */
91 
92  /**
93  * Creates a new instance of WPropertyObserver. Useful to save some typing as it creates an shared pointer for you.
94  *
95  * \return the new instance.
96  */
97  static std::shared_ptr< WPropertyObserver > create();
98 
99 protected:
100 private:
101  /**
102  * Disallow copy construction.
103  *
104  * \param rhs the other threaded runner.
105  */
107 
108  /**
109  * Disallow copy assignment.
110  *
111  * \param rhs the other threaded runner.
112  * \return this.
113  */
115 
116  /**
117  * Cancels all current subscriptions and cleans m_subscriptions.
118  */
119  void cancelSubscriptions();
120 
121  /**
122  * Subscribes each property update condition which matches an entry in m_propNames.
123  */
124  void updateSubscriptions();
125 
126  /**
127  * Gets called by the update callback of the property. The property given as parameter was the property that fired.
128  *
129  * \param property the property that fired.
130  */
131  void propertyUpdated( std::shared_ptr< WPropertyBase > property );
132 
133  /**
134  * Type for shared container with signal connections.
135  */
137 
138  /**
139  * The subscription to each property which was subscribed.
140  */
142 
143  /**
144  * True if a property fired.
145  */
146  bool m_updated;
147 
148  /**
149  * The properties handled by this observer.
150  */
151  std::shared_ptr< WProperties > m_properties;
152 
153  /**
154  * The names of the properties which shall be observed if they are or become available.
155  */
156  std::set< std::string > m_propNames;
157 
158  /**
159  * The connection used to get notified about m_properties updates.
160  */
161  boost::signals2::scoped_connection m_updateConditionConnection;
162 
163  /**
164  * Type of shared container for the list of last updated properties.
165  */
167 
168  /**
169  * The queue of properties that fired before handled() is called.
170  */
172 };
173 
174 #endif // WPROPERTYOBSERVER_H
175 
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
This class can observe properties inside a property group.
std::shared_ptr< WProperties > m_properties
The properties handled by this observer.
static std::shared_ptr< WPropertyObserver > create()
Creates a new instance of WPropertyObserver.
std::shared_ptr< WPropertyObserver > SPtr
Convenience type for a shared pointer on property observers.
void cancelSubscriptions()
Cancels all current subscriptions and cleans m_subscriptions.
WPropertyObserver()
Default constructor.
WPropertyObserver & operator=(const WPropertyObserver &rhs)
Disallow copy assignment.
WSharedAssociativeContainer< std::map< std::shared_ptr< WPropertyBase >, boost::signals2::connection > > Subscriptions
Type for shared container with signal connections.
Subscriptions m_subscriptions
The subscription to each property which was subscribed.
std::set< std::string > m_propNames
The names of the properties which shall be observed if they are or become available.
bool updated() const
Is true if one observed property fired.
WSharedAssociativeContainer< PropertyNameMap > LastUpdated
Type of shared container for the list of last updated properties.
void propertyUpdated(std::shared_ptr< WPropertyBase > property)
Gets called by the update callback of the property.
WPropertyObserver(const WPropertyObserver &rhs)
Disallow copy construction.
bool m_updated
True if a property fired.
void observe(std::shared_ptr< WProperties > properties, std::set< std::string > names=std::set< std::string >())
Defines the property group whose children should be watched.
std::map< std::string, std::shared_ptr< WPropertyBase > > PropertyNameMap
Convenience type for a set of property instances.
PropertyNameMap handled()
Resets the update flag and the list of fired properties.
virtual ~WPropertyObserver()
Destructor.
void updateSubscriptions()
Subscribes each property update condition which matches an entry in m_propNames.
LastUpdated m_lastUpdated
The queue of properties that fired before handled() is called.
boost::signals2::scoped_connection m_updateConditionConnection
The connection used to get notified about m_properties updates.
This class provides a common interface for thread-safe access to associative containers (set,...