From: Torsten Dreyer Date: Sun, 6 Feb 2011 14:05:50 +0000 (+0100) Subject: Move TiedPropertyList from flightgear to simgear X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=2ee87483f958ebf015855202df1dc2e91dd21cc7;p=simgear.git Move TiedPropertyList from flightgear to simgear --- diff --git a/simgear/props/Makefile.am b/simgear/props/Makefile.am index 23666451..7196e8a8 100644 --- a/simgear/props/Makefile.am +++ b/simgear/props/Makefile.am @@ -8,7 +8,8 @@ include_HEADERS = \ props_io.hxx \ AtomicChangeListener.hxx \ ExtendedPropertyAdapter.hxx \ - propertyObject.hxx + propertyObject.hxx \ + tiedpropertylist.hxx libsgprops_a_SOURCES = \ condition.cxx \ diff --git a/simgear/props/tiedpropertylist.hxx b/simgear/props/tiedpropertylist.hxx new file mode 100644 index 00000000..0820d6b6 --- /dev/null +++ b/simgear/props/tiedpropertylist.hxx @@ -0,0 +1,108 @@ +// tiedpropertylist.hxx -- Maintain tied properties +// +// Written by Torsten Dreyer started in 2010 +// +// Copyright (C) 2010 Torsten Dreyer - torsten (at) t3r _dot_ de +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// +#ifndef __TIEDPROPERTYLIST_HXX +#define __TIEDPROPERTYLIST_HXX +#include +using simgear::PropertyList; + +namespace simgear { + +/** + * @brief A list of tied properties that get automatically untied + * This helper class keeps track of tied properties and unties + * each tied property when this class gets destructed. +*/ +class TiedPropertyList : PropertyList { +public: + TiedPropertyList() {} + TiedPropertyList( SGPropertyNode_ptr root ) : _root(root) {} + + void setRoot( SGPropertyNode_ptr root ) { _root = root; } + SGPropertyNode_ptr getRoot() const { return _root; } + + template SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, const SGRawValue &rawValue, bool useDefault = true ) { + bool success = node->tie( rawValue, useDefault ); + if( success ) { + SG_LOG( SG_ALL, SG_INFO, "Tied " << node->getPath() ); + push_back( node ); + } else { +#if PROPS_STANDALONE + cerr << "Failed to tie property " << node->getPath() << endl; +#else + SG_LOG(SG_GENERAL, SG_WARN, "Failed to tie property " << node->getPath() ); +#endif + } + return node; + } + + template SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, V * value, bool useDefault = true ) { + return Tie( node, SGRawValuePointer(value), useDefault ); + } + + template SGPropertyNode_ptr Tie( const char * relative_path, V * value, bool useDefault = true ) { + return Tie( _root->getNode(relative_path,true), SGRawValuePointer(value), useDefault ); + } + + template SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, V (*getter)(), void (*setter)(V) = 0, bool useDefault = true ) { + return Tie(node, SGRawValueFunctions(getter, setter), useDefault ); + } + + template SGPropertyNode_ptr Tie( const char * relative_path, V (*getter)(), void (*setter)(V) = 0, bool useDefault = true ) { + return Tie(_root->getNode(relative_path, true), SGRawValueFunctions(getter, setter), useDefault ); + } + + template SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, int index, V (*getter)(int), void (*setter)(int, V) = 0, bool useDefault = true) { + return Tie( node, SGRawValueFunctionsIndexed(index, getter, setter), useDefault ); + } + + template SGPropertyNode_ptr Tie( const char * relative_path, int index, V (*getter)(int), void (*setter)(int, V) = 0, bool useDefault = true) { + return Tie( _root->getNode( relative_path, true ), SGRawValueFunctionsIndexed(index, getter, setter), useDefault ); + } + + template SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, T * obj, V (T::*getter)() const, void (T::*setter)(V) = 0, bool useDefault = true) { + return Tie( node, SGRawValueMethods(*obj, getter, setter), useDefault ); + } + + template SGPropertyNode_ptr Tie( const char * relative_path, T * obj, V (T::*getter)() const, void (T::*setter)(V) = 0, bool useDefault = true) { + return Tie( _root->getNode( relative_path, true), SGRawValueMethods(*obj, getter, setter), useDefault ); + } + + template SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, T * obj, int index, V (T::*getter)(int) const, void (T::*setter)(int, V) = 0, bool useDefault = true) { + return Tie( node, SGRawValueMethodsIndexed(*obj, index, getter, setter), useDefault); + } + + template SGPropertyNode_ptr Tie( const char * relative_path, T * obj, int index, V (T::*getter)(int) const, void (T::*setter)(int, V) = 0, bool useDefault = true) { + return Tie( _root->getNode( relative_path, true ), SGRawValueMethodsIndexed(*obj, index, getter, setter), useDefault); + } + + void Untie() { + while( size() > 0 ) { + SG_LOG( SG_ALL, SG_INFO, "untie of " << back()->getPath() ); + back()->untie(); + pop_back(); + } + } +private: + SGPropertyNode_ptr _root; +}; + +} // namespace +#endif