2 #define __FG_PROPS_HXX 1
4 #include <simgear/debug/logstream.hxx>
5 #include <simgear/misc/props.hxx>
9 ////////////////////////////////////////////////////////////////////////
10 // Property management.
11 ////////////////////////////////////////////////////////////////////////
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);
20 ////////////////////////////////////////////////////////////////////////
21 // Convenience functions for getting property values.
22 ////////////////////////////////////////////////////////////////////////
26 * Get a property node.
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 * @return The node, or 0 if none exists and none was created.
32 inline SGPropertyNode *
33 fgGetNode (const string &path, bool create = false)
35 return globals->get_props()->getNode(path, create);
40 * Test whether a given node exists.
42 * @param path The path of the node, relative to root.
43 * @return true if the node exists, false otherwise.
46 fgHasNode (const string &path)
48 return (fgGetNode(path, false) != 0);
52 inline bool fgGetBool (const string &name, bool defaultValue = false)
54 return globals->get_props()->getBoolValue(name, defaultValue);
57 inline int fgGetInt (const string &name, int defaultValue = 0)
59 return globals->get_props()->getIntValue(name, defaultValue);
62 inline int fgGetLong (const string &name, long defaultValue = 0L)
64 return globals->get_props()->getLongValue(name, defaultValue);
67 inline float fgGetFloat (const string &name, float defaultValue = 0.0)
69 return globals->get_props()->getFloatValue(name, defaultValue);
72 inline double fgGetDouble (const string &name, double defaultValue = 0.0)
74 return globals->get_props()->getDoubleValue(name, defaultValue);
77 inline string fgGetString (const string &name, string defaultValue = "")
79 return globals->get_props()->getStringValue(name, defaultValue);
82 inline bool fgSetBool (const string &name, bool val)
84 return globals->get_props()->setBoolValue(name, val);
87 inline bool fgSetInt (const string &name, int val)
89 return globals->get_props()->setIntValue(name, val);
92 inline bool fgSetLong (const string &name, long val)
94 return globals->get_props()->setLongValue(name, val);
97 inline bool fgSetFloat (const string &name, float val)
99 return globals->get_props()->setFloatValue(name, val);
102 inline bool fgSetDouble (const string &name, double val)
104 return globals->get_props()->setDoubleValue(name, val);
107 inline bool fgSetString (const string &name, const string &val)
109 return globals->get_props()->setStringValue(name, val);
114 ////////////////////////////////////////////////////////////////////////
115 // Convenience functions for tying properties, with logging.
116 ////////////////////////////////////////////////////////////////////////
119 fgUntie (const string &name)
121 if (!globals->get_props()->untie(name))
122 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
126 // Templates cause ambiguity here
128 fgTie (const string &name, bool *pointer, bool useDefault = true)
130 if (!globals->get_props()->tie(name, SGRawValuePointer<bool>(pointer),
132 SG_LOG(SG_GENERAL, SG_WARN,
133 "Failed to tie property " << name << " to a pointer");
137 fgTie (const string &name, int *pointer, bool useDefault = true)
139 if (!globals->get_props()->tie(name, SGRawValuePointer<int>(pointer),
141 SG_LOG(SG_GENERAL, SG_WARN,
142 "Failed to tie property " << name << " to a pointer");
146 fgTie (const string &name, long *pointer, bool useDefault = true)
148 if (!globals->get_props()->tie(name, SGRawValuePointer<long>(pointer),
150 SG_LOG(SG_GENERAL, SG_WARN,
151 "Failed to tie property " << name << " to a pointer");
155 fgTie (const string &name, float *pointer, bool useDefault = true)
157 if (!globals->get_props()->tie(name, SGRawValuePointer<float>(pointer),
159 SG_LOG(SG_GENERAL, SG_WARN,
160 "Failed to tie property " << name << " to a pointer");
164 fgTie (const string &name, double *pointer, bool useDefault = true)
166 if (!globals->get_props()->tie(name, SGRawValuePointer<double>(pointer),
168 SG_LOG(SG_GENERAL, SG_WARN,
169 "Failed to tie property " << name << " to a pointer");
173 fgTie (const string &name, string *pointer, bool useDefault = true)
175 if (!globals->get_props()->tie(name, SGRawValuePointer<string>(pointer),
177 SG_LOG(SG_GENERAL, SG_WARN,
178 "Failed to tie property " << name << " to a pointer");
183 fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0,
184 bool useDefault = true)
186 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
188 SG_LOG(SG_GENERAL, SG_WARN,
189 "Failed to tie property " << name << " to functions");
194 fgTie (const string &name, int index, V (*getter)(int),
195 void (*setter)(int, V) = 0, bool useDefault = true)
197 if (!globals->get_props()->tie(name,
198 SGRawValueFunctionsIndexed<V>(index,
202 SG_LOG(SG_GENERAL, SG_WARN,
203 "Failed to tie property " << name << " to indexed functions");
206 template <class T, class V>
208 fgTie (const string &name, T * obj, V (T::*getter)() const,
209 void (T::*setter)(V) = 0, bool useDefault = true)
211 if (!globals->get_props()->tie(name,
212 SGRawValueMethods<T,V>(*obj, getter, setter),
214 SG_LOG(SG_GENERAL, SG_WARN,
215 "Failed to tie property " << name << " to object methods");
218 template <class T, class V>
220 fgTie (const string &name, T * obj, int index,
221 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
222 bool useDefault = true)
224 if (!globals->get_props()->tie(name,
225 SGRawValueMethodsIndexed<T,V>(*obj,
230 SG_LOG(SG_GENERAL, SG_WARN,
231 "Failed to tie property " << name << " to indexed object methods");
235 #endif // __FG_PROPS_HXX