]> git.mxchange.org Git - simgear.git/blob - simgear/props/tiedpropertylist.hxx
Catch dangling property ties.
[simgear.git] / simgear / props / tiedpropertylist.hxx
1 // tiedpropertylist.hxx -- Maintain tied properties
2 //
3 // Written by Torsten Dreyer started in 2010
4 //
5 // Copyright (C) 2010  Torsten Dreyer - torsten (at) t3r _dot_ de
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 #ifndef __TIEDPROPERTYLIST_HXX
22 #define  __TIEDPROPERTYLIST_HXX
23 #include <simgear/props/props.hxx>
24 #include <assert.h>
25 using simgear::PropertyList;
26
27 namespace simgear {
28
29 /** 
30  * @brief A list of tied properties that get automatically untied
31  * This helper class keeps track of tied properties and unties
32  * each tied property when this class gets destructed.
33 */
34 class TiedPropertyList : PropertyList {
35 public:
36     TiedPropertyList() {}
37     TiedPropertyList( SGPropertyNode_ptr root ) : _root(root) {}
38     virtual ~TiedPropertyList()
39     { 
40         _root = 0;
41         if (size()>0)
42         {
43             SG_LOG(SG_GENERAL, SG_ALERT, "Detected properties with dangling ties. Use 'Untie' before removing a TiedPropertyList.");
44             // running debug mode: go, fix it!
45             assert(size() == 0);
46         }
47     }
48
49     void setRoot( SGPropertyNode_ptr root ) { _root = root; }
50     SGPropertyNode_ptr getRoot() const { return _root; }
51
52     template<typename T> SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, const SGRawValue<T> &rawValue, bool useDefault = true  ) {
53         bool success = node->tie( rawValue, useDefault );
54         if( success ) {
55             SG_LOG( SG_ALL, SG_INFO, "Tied " << node->getPath() );
56             push_back( node );
57         } else {
58 #if PROPS_STANDALONE
59             cerr << "Failed to tie property " << node->getPath() << endl;
60 #else
61             SG_LOG(SG_GENERAL, SG_WARN, "Failed to tie property " << node->getPath() );
62 #endif
63         }
64         return node;
65     }
66
67     template <class V> SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, V * value, bool useDefault = true ) {
68         return Tie( node, SGRawValuePointer<V>(value), useDefault );
69     }
70
71     template <class V> SGPropertyNode_ptr Tie( const char * relative_path, V * value, bool useDefault = true ) {
72         return Tie( _root->getNode(relative_path,true), SGRawValuePointer<V>(value), useDefault );
73     }
74
75     template <class V> SGPropertyNode_ptr Tie( const char * relative_path, int prop_index, V * value, bool useDefault = true ) {
76         return Tie( _root->getNode(relative_path,prop_index,true), SGRawValuePointer<V>(value), useDefault );
77     }
78
79     template <class V> SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, V (*getter)(), void (*setter)(V) = 0, bool useDefault = true ) {
80         return Tie(node, SGRawValueFunctions<V>(getter, setter), useDefault );
81     }
82
83     template <class V> SGPropertyNode_ptr Tie( const char * relative_path, V (*getter)(), void (*setter)(V) = 0, bool useDefault = true ) {
84         return Tie(_root->getNode(relative_path,true), SGRawValueFunctions<V>(getter, setter), useDefault );
85     }
86
87     template <class V> SGPropertyNode_ptr Tie( const char * relative_path, int prop_index, V (*getter)(), void (*setter)(V) = 0, bool useDefault = true ) {
88         return Tie(_root->getNode(relative_path,prop_index,true), SGRawValueFunctions<V>(getter, setter), useDefault );
89     }
90
91     template <class V> SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, int index, V (*getter)(int), void (*setter)(int, V) = 0, bool useDefault = true) {
92         return Tie( node, SGRawValueFunctionsIndexed<V>(index, getter, setter), useDefault );
93     }
94
95     template <class V> SGPropertyNode_ptr Tie( const char * relative_path, int index, V (*getter)(int), void (*setter)(int, V) = 0, bool useDefault = true) {
96         return Tie( _root->getNode( relative_path, true ), SGRawValueFunctionsIndexed<V>(index, getter, setter), useDefault );
97     }
98
99     template <class V> SGPropertyNode_ptr Tie( const char * relative_path, int prop_index, int index, V (*getter)(int), void (*setter)(int, V) = 0, bool useDefault = true) {
100         return Tie( _root->getNode( relative_path,prop_index,true ), SGRawValueFunctionsIndexed<V>(index, getter, setter), useDefault );
101     }
102
103     template <class T, class V> SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, T * obj, V (T::*getter)() const, void (T::*setter)(V) = 0, bool useDefault = true) {
104         return Tie( node, SGRawValueMethods<T,V>(*obj, getter, setter), useDefault );
105     }
106
107     template <class T, class V> SGPropertyNode_ptr Tie( const char * relative_path, T * obj, V (T::*getter)() const, void (T::*setter)(V) = 0, bool useDefault = true) {
108         return Tie( _root->getNode( relative_path, true), SGRawValueMethods<T,V>(*obj, getter, setter), useDefault );
109     }
110
111     template <class T, class V> SGPropertyNode_ptr Tie( const char * relative_path, int prop_index, T * obj, V (T::*getter)() const, void (T::*setter)(V) = 0, bool useDefault = true) {
112         return Tie( _root->getNode( relative_path,prop_index,true), SGRawValueMethods<T,V>(*obj, getter, setter), useDefault );
113     }
114
115     template <class T, class V> SGPropertyNode_ptr Tie( SGPropertyNode_ptr node, T * obj, int index, V (T::*getter)(int) const, void (T::*setter)(int, V) = 0, bool useDefault = true) {
116         return Tie( node, SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter), useDefault);
117     }
118
119     template <class T, class V> 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) {
120         return Tie( _root->getNode( relative_path, true ), SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter), useDefault);
121     }
122
123     template <class T, class V> SGPropertyNode_ptr Tie( const char * relative_path, int prop_index, T * obj, int index, V (T::*getter)(int) const, void (T::*setter)(int, V) = 0, bool useDefault = true) {
124         return Tie( _root->getNode( relative_path,prop_index,true ), SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter), useDefault);
125     }
126
127     void Untie() {
128         while( size() > 0 ) {
129             SG_LOG( SG_ALL, SG_INFO, "untie of " << back()->getPath() );
130             back()->untie();
131             pop_back();
132         }
133     }
134 private:
135     SGPropertyNode_ptr _root;
136 };
137
138 } // namespace
139 #endif