| 1 | // PropertyChangeObserver.cxx -- Watch properties for changes |
| 2 | // |
| 3 | // Written by Torsten Dreyer, started April 2014. |
| 4 | // |
| 5 | // Copyright (C) 2014 Torsten Dreyer |
| 6 | // |
| 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. |
| 11 | // |
| 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. |
| 16 | // |
| 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. |
| 20 | |
| 21 | #include "PropertyChangeObserver.hxx" |
| 22 | |
| 23 | #include <Main/fg_props.hxx> |
| 24 | using std::string; |
| 25 | namespace flightgear { |
| 26 | namespace http { |
| 27 | |
| 28 | |
| 29 | |
| 30 | PropertyChangeObserver::PropertyChangeObserver() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | PropertyChangeObserver::~PropertyChangeObserver() |
| 35 | { |
| 36 | // |
| 37 | } |
| 38 | |
| 39 | void PropertyChangeObserver::check() |
| 40 | { |
| 41 | |
| 42 | for (Entries_t::iterator it = _entries.begin(); it != _entries.end(); ++it) { |
| 43 | if (!(*it)->_node.isShared()) { |
| 44 | // node is no longer used but by us - remove the entry |
| 45 | it = _entries.erase(it); |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | if(!(*it)->_changed ) { |
| 50 | (*it)->_changed = (*it)->_prevValue != (*it)->_node->getStringValue(); |
| 51 | if ((*it)->_changed) |
| 52 | (*it)->_prevValue = (*it)->_node->getStringValue(); |
| 53 | |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void PropertyChangeObserver::uncheck() |
| 59 | { |
| 60 | for (Entries_t::iterator it = _entries.begin(); it != _entries.end(); ++it) { |
| 61 | (*it)->_changed = false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | const SGPropertyNode_ptr PropertyChangeObserver::addObservation( const string propertyName) |
| 66 | { |
| 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 |
| 71 | // property. |
| 72 | (*it)->_changed = true; |
| 73 | return (*it)->_node; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | try { |
| 78 | PropertyChangeObserverEntryRef entry = new PropertyChangeObserverEntry(); |
| 79 | entry->_node = fgGetNode( propertyName, true ); |
| 80 | _entries.push_back( entry ); |
| 81 | return entry->_node; |
| 82 | } |
| 83 | catch( string & s ) { |
| 84 | SG_LOG(SG_NETWORK,SG_WARN,"httpd: can't observer '" << propertyName << "'. Invalid name." ); |
| 85 | } |
| 86 | |
| 87 | SGPropertyNode_ptr empty; |
| 88 | return empty; |
| 89 | } |
| 90 | |
| 91 | bool PropertyChangeObserver::isChangedValue(const SGPropertyNode_ptr node) |
| 92 | { |
| 93 | for (Entries_t::iterator it = _entries.begin(); it != _entries.end(); ++it) { |
| 94 | PropertyChangeObserverEntryRef entry = *it; |
| 95 | |
| 96 | if( entry->_node == node && entry->_changed ) { |
| 97 | return true; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | } // namespace http |
| 105 | } // namespace flightgear |