]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.hxx
Updated from JSBSim cvs.
[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 int fgGetLong (const string &name, long defaultValue = 0L)
49 {
50   return globals->get_props()->getLongValue(name, defaultValue);
51 }
52
53 inline float fgGetFloat (const string &name, float defaultValue = 0.0)
54 {
55   return globals->get_props()->getFloatValue(name, defaultValue);
56 }
57
58 inline double fgGetDouble (const string &name, double defaultValue = 0.0)
59 {
60   return globals->get_props()->getDoubleValue(name, defaultValue);
61 }
62
63 inline string fgGetString (const string &name, string defaultValue = "")
64 {
65   return globals->get_props()->getStringValue(name, defaultValue);
66 }
67
68 inline bool fgSetBool (const string &name, bool val)
69 {
70   return globals->get_props()->setBoolValue(name, val);
71 }
72
73 inline bool fgSetInt (const string &name, int val)
74 {
75   return globals->get_props()->setIntValue(name, val);
76 }
77
78 inline bool fgSetLong (const string &name, long val)
79 {
80   return globals->get_props()->setLongValue(name, val);
81 }
82
83 inline bool fgSetFloat (const string &name, float val)
84 {
85   return globals->get_props()->setFloatValue(name, val);
86 }
87
88 inline bool fgSetDouble (const string &name, double val)
89 {
90   return globals->get_props()->setDoubleValue(name, val);
91 }
92
93 inline bool fgSetString (const string &name, const string &val)
94 {
95   return globals->get_props()->setStringValue(name, val);
96 }
97
98
99 \f
100 ////////////////////////////////////////////////////////////////////////
101 // Convenience functions for tying properties, with logging.
102 ////////////////////////////////////////////////////////////////////////
103
104 inline void
105 fgUntie (const string &name)
106 {
107   if (!globals->get_props()->untie(name))
108     SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
109 }
110
111
112                                 // Templates cause ambiguity here
113 inline void
114 fgTie (const string &name, bool *pointer, bool useDefault = true)
115 {
116   if (!globals->get_props()->tie(name, SGRawValuePointer<bool>(pointer),
117                                  useDefault))
118     SG_LOG(SG_GENERAL, SG_WARN,
119            "Failed to tie property " << name << " to a pointer");
120 }
121
122 inline void
123 fgTie (const string &name, int *pointer, bool useDefault = true)
124 {
125   if (!globals->get_props()->tie(name, SGRawValuePointer<int>(pointer),
126                                  useDefault))
127     SG_LOG(SG_GENERAL, SG_WARN,
128            "Failed to tie property " << name << " to a pointer");
129 }
130
131 inline void
132 fgTie (const string &name, long *pointer, bool useDefault = true)
133 {
134   if (!globals->get_props()->tie(name, SGRawValuePointer<long>(pointer),
135                                  useDefault))
136     SG_LOG(SG_GENERAL, SG_WARN,
137            "Failed to tie property " << name << " to a pointer");
138 }
139
140 inline void
141 fgTie (const string &name, float *pointer, bool useDefault = true)
142 {
143   if (!globals->get_props()->tie(name, SGRawValuePointer<float>(pointer),
144                                  useDefault))
145     SG_LOG(SG_GENERAL, SG_WARN,
146            "Failed to tie property " << name << " to a pointer");
147 }
148
149 inline void
150 fgTie (const string &name, double *pointer, bool useDefault = true)
151 {
152   if (!globals->get_props()->tie(name, SGRawValuePointer<double>(pointer),
153                                  useDefault))
154     SG_LOG(SG_GENERAL, SG_WARN,
155            "Failed to tie property " << name << " to a pointer");
156 }
157
158 inline void
159 fgTie (const string &name, string *pointer, bool useDefault = true)
160 {
161   if (!globals->get_props()->tie(name, SGRawValuePointer<string>(pointer),
162                                  useDefault))
163     SG_LOG(SG_GENERAL, SG_WARN,
164            "Failed to tie property " << name << " to a pointer");
165 }
166
167 template <class V>
168 inline void
169 fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0,
170        bool useDefault = true)
171 {
172   if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
173                                  useDefault))
174     SG_LOG(SG_GENERAL, SG_WARN,
175            "Failed to tie property " << name << " to functions");
176 }
177
178 template <class V>
179 inline void
180 fgTie (const string &name, int index, V (*getter)(int),
181        void (*setter)(int, V) = 0, bool useDefault = true)
182 {
183   if (!globals->get_props()->tie(name,
184                                  SGRawValueFunctionsIndexed<V>(index,
185                                                                getter,
186                                                                setter),
187                                  useDefault))
188     SG_LOG(SG_GENERAL, SG_WARN,
189            "Failed to tie property " << name << " to indexed functions");
190 }
191
192 template <class T, class V>
193 inline void
194 fgTie (const string &name, T * obj, V (T::*getter)() const,
195        void (T::*setter)(V) = 0, bool useDefault = true)
196 {
197   if (!globals->get_props()->tie(name,
198                                  SGRawValueMethods<T,V>(*obj, getter, setter),
199                                  useDefault))
200     SG_LOG(SG_GENERAL, SG_WARN,
201            "Failed to tie property " << name << " to object methods");
202 }
203
204 template <class T, class V>
205 inline void 
206 fgTie (const string &name, T * obj, int index,
207        V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
208        bool useDefault = true)
209 {
210   if (!globals->get_props()->tie(name,
211                                  SGRawValueMethodsIndexed<T,V>(*obj,
212                                                                index,
213                                                                getter,
214                                                                setter),
215                                  useDefault))
216     SG_LOG(SG_GENERAL, SG_WARN,
217            "Failed to tie property " << name << " to indexed object methods");
218 }
219
220
221 #endif // __FG_PROPS_HXX
222