OpenWalnut  1.5.0dev
WPropertyObserver.cpp
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 #include <map>
26 #include <memory>
27 #include <set>
28 #include <string>
29 
30 #include "WPropertyObserver.h"
31 
33  WCondition(),
34  m_subscriptions(),
35  m_updated( false ),
36  m_properties(),
37  m_propNames(),
38  m_updateConditionConnection()
39 {
40  // initialize members
41 }
42 
44 {
45  // cleanup
46 }
47 
49 {
50  return m_updated;
51 }
52 
54 {
56 
57  // reset everything
58  m_updated = false;
59 
60  // return a copy of the list
61  PropertyNameMap ret( l->get() );
62  l->get().clear();
63 
64  return ret;
65 }
66 
67 void WPropertyObserver::observe( std::shared_ptr< WProperties > properties, std::set< std::string > names )
68 {
69  // something to do?
70  if( m_properties == properties )
71  {
72  return;
73  }
74 
75  // remove old subscriptions to the old properties
76  m_updateConditionConnection.disconnect();
78  m_updated = false;
80  l->get().clear();
81 
82  m_propNames = names;
83 
84  // set new properties and subscribe to all signals
85  m_properties = properties;
86  // we need to get a call if properties get added or removed
87  m_updateConditionConnection = m_properties->getUpdateCondition()->subscribeSignal(
88  boost::bind( &WPropertyObserver::updateSubscriptions, this )
89  );
90 
91  // and subscribe again to the new group's properties
92  // NOTE: it may be possible that the updateSubscriptions method was called already by the above callback. But thats not that evil.
94 }
95 
97 {
98  // NOTE: locking is handled by WSharedAssociativeContainer
99 
100  // unfortunately, scoped_connections can't be used for the container as it requires copy construction which is not allowed by
101  // scoped_connections. So we need to iterate by hand and disconnect
102 
104  for( Subscriptions::Iterator i = subs->get().begin(); i != subs->get().end(); ++i )
105  {
106  ( *i ).second.disconnect();
107  }
108  subs->get().clear();
109 }
110 
112 {
113  // lock m_subscriptions
115 
116  // iterate the properties
118  for( WProperties::PropertyConstIterator i = props->get().begin(); i != props->get().end(); ++i )
119  {
120  // should the property be handled? (empty container ensures handling of all props)
121  if( !m_propNames.size() || ( m_propNames.find( ( *i )->getName() ) != m_propNames.end() ) )
122  {
123  // subscribe to each update signal of the properties
124  subs->get().insert( std::make_pair( *i,
125  ( *i )->getUpdateCondition()->subscribeSignal( boost::bind( boost::mem_fn( &WPropertyObserver::propertyUpdated ), this, *i ) )
126  ) );
127  }
128  }
129 }
130 
131 void WPropertyObserver::propertyUpdated( std::shared_ptr< WPropertyBase > property )
132 {
133  // lock m_lastUpdated
135  m_updated = true;
136  l->get().insert( std::make_pair( property->getName(), property ) );
137  notify();
138 }
139 
140 std::shared_ptr< WPropertyObserver > WPropertyObserver::create()
141 {
142  return std::shared_ptr< WPropertyObserver >( new WPropertyObserver() );
143 }
144 
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
virtual void notify()
Notifies all waiting threads.
Definition: WCondition.cpp:44
WPropertyGroupBase::PropertyConstIterator PropertyConstIterator
The const iterator type of the container.
std::shared_ptr< WProperties > m_properties
The properties handled by this observer.
static std::shared_ptr< WPropertyObserver > create()
Creates a new instance of WPropertyObserver.
void cancelSubscriptions()
Cancels all current subscriptions and cleans m_subscriptions.
WPropertyObserver()
Default constructor.
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.
void propertyUpdated(std::shared_ptr< WPropertyBase > property)
Gets called by the update callback of the property.
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.
T::iterator Iterator
A typedef for the correct iterator to traverse this sequence container.
std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:65
std::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket
Type for write tickets.
Definition: WSharedObject.h:70
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.