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