]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.hxx
More property manager interface updates.
[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 // Loading and saving properties.
11 ////////////////////////////////////////////////////////////////////////
12
13 extern bool fgSaveFlight (ostream &output);
14 extern bool fgLoadFlight (istream &input);
15
16
17 \f
18 ////////////////////////////////////////////////////////////////////////
19 // Convenience functions for getting property values.
20 ////////////////////////////////////////////////////////////////////////
21
22 /**
23  * Get an SGValue pointer that can be queried repeatedly.
24  *
25  * If the property value is going to be accessed within the loop,
26  * it is best to use this method for maximum efficiency.
27  */
28 inline SGValue * fgGetValue (const string &name, bool create = false)
29 {
30   return globals->get_props()->getValue(name, create);
31 }
32
33 inline bool fgHasValue (const string &name)
34 {
35   return globals->get_props()->hasValue(name);
36 }
37
38 inline bool fgGetBool (const string &name, bool defaultValue = false)
39 {
40   return globals->get_props()->getBoolValue(name, defaultValue);
41 }
42
43 inline int fgGetInt (const string &name, int defaultValue = 0)
44 {
45   return globals->get_props()->getIntValue(name, defaultValue);
46 }
47
48 inline float fgGetFloat (const string &name, float defaultValue = 0.0)
49 {
50   return globals->get_props()->getFloatValue(name, defaultValue);
51 }
52
53 inline double fgGetDouble (const string &name, double defaultValue = 0.0)
54 {
55   return globals->get_props()->getDoubleValue(name, defaultValue);
56 }
57
58 inline string fgGetString (const string &name, string defaultValue = "")
59 {
60   return globals->get_props()->getStringValue(name, defaultValue);
61 }
62
63 inline bool fgSetBool (const string &name, bool val)
64 {
65   return globals->get_props()->setBoolValue(name, val);
66 }
67
68 inline bool fgSetInt (const string &name, int val)
69 {
70   return globals->get_props()->setIntValue(name, val);
71 }
72
73 inline bool fgSetFloat (const string &name, float val)
74 {
75   return globals->get_props()->setFloatValue(name, val);
76 }
77
78 inline bool fgSetDouble (const string &name, double val)
79 {
80   return globals->get_props()->setDoubleValue(name, val);
81 }
82
83 inline bool fgSetString (const string &name, const string &val)
84 {
85   return globals->get_props()->setStringValue(name, val);
86 }
87
88
89 \f
90 ////////////////////////////////////////////////////////////////////////
91 // Convenience functions for tying properties, with logging.
92 ////////////////////////////////////////////////////////////////////////
93
94 inline void
95 fgUntie (const string &name)
96 {
97   if (!globals->get_props()->untie(name))
98     FG_LOG(FG_GENERAL, FG_WARN, "Failed to untie property " << name);
99 }
100
101
102                                 // Templates cause ambiguity here
103 inline void
104 fgTie (const string &name, bool *pointer)
105 {
106   if (!globals->get_props()->tie(name, SGRawValuePointer<bool>(pointer)))
107     FG_LOG(FG_GENERAL, FG_WARN,
108            "Failed to tie property " << name << " to a pointer");
109 }
110
111 inline void
112 fgTie (const string &name, int *pointer)
113 {
114   if (!globals->get_props()->tie(name, SGRawValuePointer<int>(pointer)))
115     FG_LOG(FG_GENERAL, FG_WARN,
116            "Failed to tie property " << name << " to a pointer");
117 }
118
119 inline void
120 fgTie (const string &name, float *pointer)
121 {
122   if (!globals->get_props()->tie(name, SGRawValuePointer<float>(pointer)))
123     FG_LOG(FG_GENERAL, FG_WARN,
124            "Failed to tie property " << name << " to a pointer");
125 }
126
127 inline void
128 fgTie (const string &name, double *pointer)
129 {
130   if (!globals->get_props()->tie(name, SGRawValuePointer<double>(pointer)))
131     FG_LOG(FG_GENERAL, FG_WARN,
132            "Failed to tie property " << name << " to a pointer");
133 }
134
135 inline void
136 fgTie (const string &name, string *pointer)
137 {
138   if (!globals->get_props()->tie(name, SGRawValuePointer<string>(pointer)))
139     FG_LOG(FG_GENERAL, FG_WARN,
140            "Failed to tie property " << name << " to a pointer");
141 }
142
143 template <class V>
144 inline void
145 fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0)
146 {
147   if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter)))
148     FG_LOG(FG_GENERAL, FG_WARN,
149            "Failed to tie property " << name << " to functions");
150 }
151
152 template <class V>
153 inline void
154 fgTie (const string &name, int index, V (*getter)(int),
155        void (*setter)(int, V) = 0)
156 {
157   if (!globals->get_props()->tie(name,
158                                  SGRawValueFunctionsIndexed<V>(index,
159                                                                getter,
160                                                                setter)))
161     FG_LOG(FG_GENERAL, FG_WARN,
162            "Failed to tie property " << name << " to indexed functions");
163 }
164
165 template <class T, class V>
166 inline void
167 fgTie (const string &name, T * obj, V (T::*getter)() const,
168        void (T::*setter)(V) = 0)
169 {
170   if (!globals->get_props()->tie(name,
171                                  SGRawValueMethods<T,V>(*obj, getter, setter)))
172     FG_LOG(FG_GENERAL, FG_WARN,
173            "Failed to tie property " << name << " to object methods");
174 }
175
176 template <class T, class V>
177 inline void 
178 fgTie (const string &name, T * obj, int index,
179        V (T::*getter)(int) const, void (T::*setter)(int, V) = 0)
180 {
181   if (!globals->get_props()->tie(name,
182                                  SGRawValueMethodsIndexed<T,V>(*obj,
183                                                                index,
184                                                                getter,
185                                                                setter)))
186     FG_LOG(FG_GENERAL, FG_WARN,
187            "Failed to tie property " << name << " to indexed object methods");
188 }
189
190
191 #endif // __FG_PROPS_HXX
192