1 // PropertyChangeObserver.cxx -- Watch properties for changes
3 // Written by Torsten Dreyer, started April 2014.
5 // Copyright (C) 2014 Torsten Dreyer
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "PropertyChangeObserver.hxx"
23 #include <Main/fg_props.hxx>
25 namespace flightgear {
30 PropertyChangeObserver::PropertyChangeObserver()
34 PropertyChangeObserver::~PropertyChangeObserver()
39 void PropertyChangeObserver::check()
42 for (Entries_t::iterator it = _entries.begin(); it != _entries.end(); ++it) {
43 if (false == (*it)->_node.isShared()) {
44 // node is no longer used but by us - remove the entry
45 it = _entries.erase(it);
49 if( false == (*it)->_changed ) {
50 (*it)->_changed = (*it)->_prevValue != (*it)->_node->getStringValue();
52 (*it)->_prevValue = (*it)->_node->getStringValue();
58 void PropertyChangeObserver::uncheck()
60 for (Entries_t::iterator it = _entries.begin(); it != _entries.end(); ++it) {
61 (*it)->_changed = false;
65 const SGPropertyNode_ptr PropertyChangeObserver::addObservation( const string propertyName)
67 for (Entries_t::iterator it = _entries.begin(); it != _entries.end(); ++it) {
68 if (propertyName == (*it)->_node->getPath(true) ) {
69 // if a new observer is added to a property, mark it as changed to ensure the observer
70 // gets notified on initial call. This also causes a notification for all other observers of this
72 (*it)->_changed = true;
78 PropertyChangeObserverEntryRef entry = new PropertyChangeObserverEntry();
79 entry->_node = fgGetNode( propertyName, true );
80 _entries.push_back( entry );
84 SG_LOG(SG_NETWORK,SG_WARN,"httpd: can't observer '" << propertyName << "'. Invalid name." );
87 SGPropertyNode_ptr empty;
91 bool PropertyChangeObserver::isChangedValue(const SGPropertyNode_ptr node)
93 for (Entries_t::iterator it = _entries.begin(); it != _entries.end(); ++it) {
94 PropertyChangeObserverEntryRef entry = *it;
96 if( entry->_node == node && entry->_changed ) {
105 } // namespace flightgear