]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.hxx
More property node optimizations.
[flightgear.git] / src / Main / fg_props.hxx
1 #ifndef __FG_PROPS_HXX
2 #define __FG_PROPS_HXX 1
3
4 #include <simgear/debug/logstream.hxx>
5 #include <simgear/misc/props.hxx>
6 #include "globals.hxx"
7
8 \f
9 ////////////////////////////////////////////////////////////////////////
10 // Property management.
11 ////////////////////////////////////////////////////////////////////////
12
13 extern void fgInitProps ();     // fixme: how are they untied?
14 extern void fgUpdateProps ();
15 extern bool fgSaveFlight (ostream &output);
16 extern bool fgLoadFlight (istream &input);
17
18
19 \f
20 ////////////////////////////////////////////////////////////////////////
21 // Convenience functions for getting property values.
22 ////////////////////////////////////////////////////////////////////////
23
24
25 /**
26  * Get a property node.
27  *
28  * @param path The path of the node, relative to root.
29  * @param create true to create the node if it doesn't exist.
30  */
31 inline SGPropertyNode * 
32 fgGetNode (const string &path, bool create = false)
33 {
34   return globals->get_props()->getNode(path, create);
35 }
36
37
38 /**
39  * Get an SGValue pointer that can be queried repeatedly.
40  *
41  * If the property value is going to be accessed within the loop,
42  * it is best to use this method for maximum efficiency.
43  */
44 inline SGValue * 
45 fgGetValue (const string &name, bool create = false)
46 {
47   return globals->get_props()->getValue(name, create);
48 }
49
50 inline bool fgHasValue (const string &name)
51 {
52   return globals->get_props()->hasValue(name);
53 }
54
55 inline bool fgGetBool (const string &name, bool defaultValue = false)
56 {
57   return globals->get_props()->getBoolValue(name, defaultValue);
58 }
59
60 inline int fgGetInt (const string &name, int defaultValue = 0)
61 {
62   return globals->get_props()->getIntValue(name, defaultValue);
63 }
64
65 inline int fgGetLong (const string &name, long defaultValue = 0L)
66 {
67   return globals->get_props()->getLongValue(name, defaultValue);
68 }
69
70 inline float fgGetFloat (const string &name, float defaultValue = 0.0)
71 {
72   return globals->get_props()->getFloatValue(name, defaultValue);
73 }
74
75 inline double fgGetDouble (const string &name, double defaultValue = 0.0)
76 {
77   return globals->get_props()->getDoubleValue(name, defaultValue);
78 }
79
80 inline string fgGetString (const string &name, string defaultValue = "")
81 {
82   return globals->get_props()->getStringValue(name, defaultValue);
83 }
84
85 inline bool fgSetBool (const string &name, bool val)
86 {
87   return globals->get_props()->setBoolValue(name, val);
88 }
89
90 inline bool fgSetInt (const string &name, int val)
91 {
92   return globals->get_props()->setIntValue(name, val);
93 }
94
95 inline bool fgSetLong (const string &name, long val)
96 {
97   return globals->get_props()->setLongValue(name, val);
98 }
99
100 inline bool fgSetFloat (const string &name, float val)
101 {
102   return globals->get_props()->setFloatValue(name, val);
103 }
104
105 inline bool fgSetDouble (const string &name, double val)
106 {
107   return globals->get_props()->setDoubleValue(name, val);
108 }
109
110 inline bool fgSetString (const string &name, const string &val)
111 {
112   return globals->get_props()->setStringValue(name, val);
113 }
114
115
116 \f
117 ////////////////////////////////////////////////////////////////////////
118 // Convenience functions for tying properties, with logging.
119 ////////////////////////////////////////////////////////////////////////
120
121 inline void
122 fgUntie (const string &name)
123 {
124   if (!globals->get_props()->untie(name))
125     SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
126 }
127
128
129                                 // Templates cause ambiguity here
130 inline void
131 fgTie (const string &name, bool *pointer, bool useDefault = true)
132 {
133   if (!globals->get_props()->tie(name, SGRawValuePointer<bool>(pointer),
134                                  useDefault))
135     SG_LOG(SG_GENERAL, SG_WARN,
136            "Failed to tie property " << name << " to a pointer");
137 }
138
139 inline void
140 fgTie (const string &name, int *pointer, bool useDefault = true)
141 {
142   if (!globals->get_props()->tie(name, SGRawValuePointer<int>(pointer),
143                                  useDefault))
144     SG_LOG(SG_GENERAL, SG_WARN,
145            "Failed to tie property " << name << " to a pointer");
146 }
147
148 inline void
149 fgTie (const string &name, long *pointer, bool useDefault = true)
150 {
151   if (!globals->get_props()->tie(name, SGRawValuePointer<long>(pointer),
152                                  useDefault))
153     SG_LOG(SG_GENERAL, SG_WARN,
154            "Failed to tie property " << name << " to a pointer");
155 }
156
157 inline void
158 fgTie (const string &name, float *pointer, bool useDefault = true)
159 {
160   if (!globals->get_props()->tie(name, SGRawValuePointer<float>(pointer),
161                                  useDefault))
162     SG_LOG(SG_GENERAL, SG_WARN,
163            "Failed to tie property " << name << " to a pointer");
164 }
165
166 inline void
167 fgTie (const string &name, double *pointer, bool useDefault = true)
168 {
169   if (!globals->get_props()->tie(name, SGRawValuePointer<double>(pointer),
170                                  useDefault))
171     SG_LOG(SG_GENERAL, SG_WARN,
172            "Failed to tie property " << name << " to a pointer");
173 }
174
175 inline void
176 fgTie (const string &name, string *pointer, bool useDefault = true)
177 {
178   if (!globals->get_props()->tie(name, SGRawValuePointer<string>(pointer),
179                                  useDefault))
180     SG_LOG(SG_GENERAL, SG_WARN,
181            "Failed to tie property " << name << " to a pointer");
182 }
183
184 template <class V>
185 inline void
186 fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0,
187        bool useDefault = true)
188 {
189   if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
190                                  useDefault))
191     SG_LOG(SG_GENERAL, SG_WARN,
192            "Failed to tie property " << name << " to functions");
193 }
194
195 template <class V>
196 inline void
197 fgTie (const string &name, int index, V (*getter)(int),
198        void (*setter)(int, V) = 0, bool useDefault = true)
199 {
200   if (!globals->get_props()->tie(name,
201                                  SGRawValueFunctionsIndexed<V>(index,
202                                                                getter,
203                                                                setter),
204                                  useDefault))
205     SG_LOG(SG_GENERAL, SG_WARN,
206            "Failed to tie property " << name << " to indexed functions");
207 }
208
209 template <class T, class V>
210 inline void
211 fgTie (const string &name, T * obj, V (T::*getter)() const,
212        void (T::*setter)(V) = 0, bool useDefault = true)
213 {
214   if (!globals->get_props()->tie(name,
215                                  SGRawValueMethods<T,V>(*obj, getter, setter),
216                                  useDefault))
217     SG_LOG(SG_GENERAL, SG_WARN,
218            "Failed to tie property " << name << " to object methods");
219 }
220
221 template <class T, class V>
222 inline void 
223 fgTie (const string &name, T * obj, int index,
224        V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
225        bool useDefault = true)
226 {
227   if (!globals->get_props()->tie(name,
228                                  SGRawValueMethodsIndexed<T,V>(*obj,
229                                                                index,
230                                                                getter,
231                                                                setter),
232                                  useDefault))
233     SG_LOG(SG_GENERAL, SG_WARN,
234            "Failed to tie property " << name << " to indexed object methods");
235 }
236
237
238 #endif // __FG_PROPS_HXX
239