]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.hxx
Initial revision.
[flightgear.git] / src / Main / fg_props.hxx
1 // fg_props.hxx - Declarations and inline methods for property handling.
2 // Written by David Megginson, started 2000.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #ifndef __FG_PROPS_HXX
7 #define __FG_PROPS_HXX 1
8
9 #include <simgear/debug/logstream.hxx>
10 #include <simgear/misc/props.hxx>
11 #include <simgear/misc/props_io.hxx>
12
13 #include "globals.hxx"
14
15 \f
16 ////////////////////////////////////////////////////////////////////////
17 // Property management.
18 ////////////////////////////////////////////////////////////////////////
19
20
21 /**
22  * Initialize the default FlightGear properties.
23  *
24  * These are mostly properties that haven't been claimed by a
25  * specific module yet.  This function should be invoked once,
26  * while the program is starting (and after the global property
27  * tree has been created).
28  */
29 extern void fgInitProps ();     // fixme: how are they untied?
30
31
32 /**
33  * Update the default FlightGear properties.
34  *
35  * This function should be invoked once in each loop to update all
36  * of the default properties.
37  */
38 extern void fgUpdateProps ();
39
40
41 /**
42  * Save a flight to disk.
43  *
44  * This function saves all of the archivable properties to a stream
45  * so that the current flight can be restored later.
46  *
47  * @param output The output stream to write the XML save file to.
48  * @param write_all If true, write all properties rather than
49  *        just the ones flagged as archivable.
50  * @return true if the flight was saved successfully.
51  */
52 extern bool fgSaveFlight (ostream &output, bool write_all = false);
53
54
55 /**
56  * Load a flight from disk.
57  *
58  * This function loads an XML save file from a stream to restore
59  * a flight.
60  *
61  * @param input The input stream to read the XML from.
62  * @return true if the flight was restored successfully.
63  */
64 extern bool fgLoadFlight (istream &input);
65
66
67 \f
68 ////////////////////////////////////////////////////////////////////////
69 // Convenience functions for getting property values.
70 ////////////////////////////////////////////////////////////////////////
71
72 /**
73  * Get a property node.
74  *
75  * @param path The path of the node, relative to root.
76  * @param create true to create the node if it doesn't exist.
77  * @return The node, or 0 if none exists and none was created.
78  */
79 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
80
81
82 /**
83  * Get a property node with separate index.
84  *
85  * This method separates the index from the path string, to make it
86  * easier to iterate through multiple components without using sprintf
87  * to add indices.  For example, fgGetNode("foo[1]/bar", 3) will
88  * return the same result as fgGetNode("foo[1]/bar[3]").
89  *
90  * @param path The path of the node, relative to root.
91  * @param index The index for the last member of the path (overrides
92  * any given in the string).
93  * @param create true to create the node if it doesn't exist.
94  * @return The node, or 0 if none exists and none was created.
95  */
96 extern SGPropertyNode * fgGetNode (const char * path,
97                                    int index, bool create = false);
98
99
100 /**
101  * Test whether a given node exists.
102  *
103  * @param path The path of the node, relative to root.
104  * @return true if the node exists, false otherwise.
105  */
106 extern bool fgHasNode (const char * path);
107
108
109 /**
110  * Get a bool value for a property.
111  *
112  * This method is convenient but inefficient.  It should be used
113  * infrequently (i.e. for initializing, loading, saving, etc.),
114  * not in the main loop.  If you need to get a value frequently,
115  * it is better to look up the node itself using fgGetNode and then
116  * use the node's getBoolValue() method, to avoid the lookup overhead.
117  *
118  * @param name The property name.
119  * @param defaultValue The default value to return if the property
120  *        does not exist.
121  * @return The property's value as a bool, or the default value provided.
122  */
123 extern bool fgGetBool (const char * name, bool defaultValue = false);
124
125
126 /**
127  * Get an int value for a property.
128  *
129  * This method is convenient but inefficient.  It should be used
130  * infrequently (i.e. for initializing, loading, saving, etc.),
131  * not in the main loop.  If you need to get a value frequently,
132  * it is better to look up the node itself using fgGetNode and then
133  * use the node's getIntValue() method, to avoid the lookup overhead.
134  *
135  * @param name The property name.
136  * @param defaultValue The default value to return if the property
137  *        does not exist.
138  * @return The property's value as an int, or the default value provided.
139  */
140 extern int fgGetInt (const char * name, int defaultValue = 0);
141
142
143 /**
144  * Get a long value for a property.
145  *
146  * This method is convenient but inefficient.  It should be used
147  * infrequently (i.e. for initializing, loading, saving, etc.),
148  * not in the main loop.  If you need to get a value frequently,
149  * it is better to look up the node itself using fgGetNode and then
150  * use the node's getLongValue() method, to avoid the lookup overhead.
151  *
152  * @param name The property name.
153  * @param defaultValue The default value to return if the property
154  *        does not exist.
155  * @return The property's value as a long, or the default value provided.
156  */
157 extern int fgGetLong (const char * name, long defaultValue = 0L);
158
159
160 /**
161  * Get a float value for a property.
162  *
163  * This method is convenient but inefficient.  It should be used
164  * infrequently (i.e. for initializing, loading, saving, etc.),
165  * not in the main loop.  If you need to get a value frequently,
166  * it is better to look up the node itself using fgGetNode and then
167  * use the node's getFloatValue() method, to avoid the lookup overhead.
168  *
169  * @param name The property name.
170  * @param defaultValue The default value to return if the property
171  *        does not exist.
172  * @return The property's value as a float, or the default value provided.
173  */
174 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
175
176
177 /**
178  * Get a double value for a property.
179  *
180  * This method is convenient but inefficient.  It should be used
181  * infrequently (i.e. for initializing, loading, saving, etc.),
182  * not in the main loop.  If you need to get a value frequently,
183  * it is better to look up the node itself using fgGetNode and then
184  * use the node's getDoubleValue() method, to avoid the lookup overhead.
185  *
186  * @param name The property name.
187  * @param defaultValue The default value to return if the property
188  *        does not exist.
189  * @return The property's value as a double, or the default value provided.
190  */
191 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
192
193
194 /**
195  * Get a string value for a property.
196  *
197  * This method is convenient but inefficient.  It should be used
198  * infrequently (i.e. for initializing, loading, saving, etc.),
199  * not in the main loop.  If you need to get a value frequently,
200  * it is better to look up the node itself using fgGetNode and then
201  * use the node's getStringValue() method, to avoid the lookup overhead.
202  *
203  * @param name The property name.
204  * @param defaultValue The default value to return if the property
205  *        does not exist.
206  * @return The property's value as a string, or the default value provided.
207  */
208 extern const char * fgGetString (const char * name,
209                                  const char * defaultValue = "");
210
211
212 /**
213  * Set a bool value for a property.
214  *
215  * Assign a bool value to a property.  If the property does not
216  * yet exist, it will be created and its type will be set to
217  * BOOL; if it has a type of UNKNOWN, the type will also be set to
218  * BOOL; otherwise, the bool value will be converted to the property's
219  * type.
220  *
221  * @param name The property name.
222  * @param val The new value for the property.
223  * @return true if the assignment succeeded, false otherwise.
224  */
225 extern bool fgSetBool (const char * name, bool val);
226
227
228 /**
229  * Set an int value for a property.
230  *
231  * Assign an int value to a property.  If the property does not
232  * yet exist, it will be created and its type will be set to
233  * INT; if it has a type of UNKNOWN, the type will also be set to
234  * INT; otherwise, the bool value will be converted to the property's
235  * type.
236  *
237  * @param name The property name.
238  * @param val The new value for the property.
239  * @return true if the assignment succeeded, false otherwise.
240  */
241 extern bool fgSetInt (const char * name, int val);
242
243
244 /**
245  * Set a long value for a property.
246  *
247  * Assign a long value to a property.  If the property does not
248  * yet exist, it will be created and its type will be set to
249  * LONG; if it has a type of UNKNOWN, the type will also be set to
250  * LONG; otherwise, the bool value will be converted to the property's
251  * type.
252  *
253  * @param name The property name.
254  * @param val The new value for the property.
255  * @return true if the assignment succeeded, false otherwise.
256  */
257 extern bool fgSetLong (const char * name, long val);
258
259
260 /**
261  * Set a float value for a property.
262  *
263  * Assign a float value to a property.  If the property does not
264  * yet exist, it will be created and its type will be set to
265  * FLOAT; if it has a type of UNKNOWN, the type will also be set to
266  * FLOAT; otherwise, the bool value will be converted to the property's
267  * type.
268  *
269  * @param name The property name.
270  * @param val The new value for the property.
271  * @return true if the assignment succeeded, false otherwise.
272  */
273 extern bool fgSetFloat (const char * name, float val);
274
275
276 /**
277  * Set a double value for a property.
278  *
279  * Assign a double value to a property.  If the property does not
280  * yet exist, it will be created and its type will be set to
281  * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
282  * DOUBLE; otherwise, the double value will be converted to the property's
283  * type.
284  *
285  * @param name The property name.
286  * @param val The new value for the property.
287  * @return true if the assignment succeeded, false otherwise.
288  */
289 extern bool fgSetDouble (const char * name, double val);
290
291
292 /**
293  * Set a string value for a property.
294  *
295  * Assign a string value to a property.  If the property does not
296  * yet exist, it will be created and its type will be set to
297  * STRING; if it has a type of UNKNOWN, the type will also be set to
298  * STRING; otherwise, the string value will be converted to the property's
299  * type.
300  *
301  * @param name The property name.
302  * @param val The new value for the property.
303  * @return true if the assignment succeeded, false otherwise.
304  */
305 extern bool fgSetString (const char * name, const char * val);
306
307
308 \f
309 ////////////////////////////////////////////////////////////////////////
310 // Convenience functions for setting property attributes.
311 ////////////////////////////////////////////////////////////////////////
312
313
314 /**
315  * Set the state of the archive attribute for a property.
316  *
317  * If the archive attribute is true, the property will be written
318  * when a flight is saved; if it is false, the property will be
319  * skipped.
320  *
321  * A warning message will be printed if the property does not exist.
322  *
323  * @param name The property name.
324  * @param state The state of the archive attribute (defaults to true).
325  */
326 extern void fgSetArchivable (const char * name, bool state = true);
327
328
329 /**
330  * Set the state of the read attribute for a property.
331  *
332  * If the read attribute is true, the property value will be readable;
333  * if it is false, the property value will always be the default value
334  * for its type.
335  *
336  * A warning message will be printed if the property does not exist.
337  *
338  * @param name The property name.
339  * @param state The state of the read attribute (defaults to true).
340  */
341 extern void fgSetReadable (const char * name, bool state = true);
342
343
344 /**
345  * Set the state of the write attribute for a property.
346  *
347  * If the write attribute is true, the property value may be modified
348  * (depending on how it is tied); if the write attribute is false, the
349  * property value may not be modified.
350  *
351  * A warning message will be printed if the property does not exist.
352  *
353  * @param name The property name.
354  * @param state The state of the write attribute (defaults to true).
355  */
356 extern void fgSetWritable (const char * name, bool state = true);
357
358
359 \f
360 ////////////////////////////////////////////////////////////////////////
361 // Convenience functions for tying properties, with logging.
362 ////////////////////////////////////////////////////////////////////////
363
364
365 /**
366  * Untie a property from an external data source.
367  *
368  * Classes should use this function to release control of any
369  * properties they are managing.
370  */
371 extern void fgUntie (const char * name);
372
373
374 /**
375  * Tie a property to a pair of simple functions.
376  *
377  * Every time the property value is queried, the getter (if any) will
378  * be invoked; every time the property value is modified, the setter
379  * (if any) will be invoked.  The getter can be 0 to make the property
380  * unreadable, and the setter can be 0 to make the property
381  * unmodifiable.
382  *
383  * @param name The property name to tie (full path).
384  * @param getter The getter function, or 0 if the value is unreadable.
385  * @param setter The setter function, or 0 if the value is unmodifiable.
386  * @param useDefault true if the setter should be invoked with any existing 
387  *        property value should be; false if the old value should be
388  *        discarded; defaults to true.
389  */
390 template <class V>
391 inline void
392 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
393        bool useDefault = true)
394 {
395   if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
396                                  useDefault))
397     SG_LOG(SG_GENERAL, SG_WARN,
398            "Failed to tie property " << name << " to functions");
399 }
400
401
402 /**
403  * Tie a property to a pair of indexed functions.
404  *
405  * Every time the property value is queried, the getter (if any) will
406  * be invoked with the index provided; every time the property value
407  * is modified, the setter (if any) will be invoked with the index
408  * provided.  The getter can be 0 to make the property unreadable, and
409  * the setter can be 0 to make the property unmodifiable.
410  *
411  * @param name The property name to tie (full path).
412  * @param index The integer argument to pass to the getter and
413  *        setter functions.
414  * @param getter The getter function, or 0 if the value is unreadable.
415  * @param setter The setter function, or 0 if the value is unmodifiable.
416  * @param useDefault true if the setter should be invoked with any existing 
417  *        property value should be; false if the old value should be
418  *        discarded; defaults to true.
419  */
420 template <class V>
421 inline void
422 fgTie (const char * name, int index, V (*getter)(int),
423        void (*setter)(int, V) = 0, bool useDefault = true)
424 {
425   if (!globals->get_props()->tie(name,
426                                  SGRawValueFunctionsIndexed<V>(index,
427                                                                getter,
428                                                                setter),
429                                  useDefault))
430     SG_LOG(SG_GENERAL, SG_WARN,
431            "Failed to tie property " << name << " to indexed functions");
432 }
433
434
435 /**
436  * Tie a property to a pair of object methods.
437  *
438  * Every time the property value is queried, the getter (if any) will
439  * be invoked; every time the property value is modified, the setter
440  * (if any) will be invoked.  The getter can be 0 to make the property
441  * unreadable, and the setter can be 0 to make the property
442  * unmodifiable.
443  *
444  * @param name The property name to tie (full path).
445  * @param obj The object whose methods should be invoked.
446  * @param getter The object's getter method, or 0 if the value is
447  *        unreadable.
448  * @param setter The object's setter method, or 0 if the value is
449  *        unmodifiable.
450  * @param useDefault true if the setter should be invoked with any existing 
451  *        property value should be; false if the old value should be
452  *        discarded; defaults to true.
453  */
454 template <class T, class V>
455 inline void
456 fgTie (const char * name, T * obj, V (T::*getter)() const,
457        void (T::*setter)(V) = 0, bool useDefault = true)
458 {
459   if (!globals->get_props()->tie(name,
460                                  SGRawValueMethods<T,V>(*obj, getter, setter),
461                                  useDefault))
462     SG_LOG(SG_GENERAL, SG_WARN,
463            "Failed to tie property " << name << " to object methods");
464 }
465
466
467 /**
468  * Tie a property to a pair of indexed object methods.
469  *
470  * Every time the property value is queried, the getter (if any) will
471  * be invoked with the index provided; every time the property value
472  * is modified, the setter (if any) will be invoked with the index
473  * provided.  The getter can be 0 to make the property unreadable, and
474  * the setter can be 0 to make the property unmodifiable.
475  *
476  * @param name The property name to tie (full path).
477  * @param obj The object whose methods should be invoked.
478  * @param index The integer argument to pass to the getter and
479  *        setter methods.
480  * @param getter The getter method, or 0 if the value is unreadable.
481  * @param setter The setter method, or 0 if the value is unmodifiable.
482  * @param useDefault true if the setter should be invoked with any existing 
483  *        property value should be; false if the old value should be
484  *        discarded; defaults to true.
485  */
486 template <class T, class V>
487 inline void 
488 fgTie (const char * name, T * obj, int index,
489        V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
490        bool useDefault = true)
491 {
492   if (!globals->get_props()->tie(name,
493                                  SGRawValueMethodsIndexed<T,V>(*obj,
494                                                                index,
495                                                                getter,
496                                                                setter),
497                                  useDefault))
498     SG_LOG(SG_GENERAL, SG_WARN,
499            "Failed to tie property " << name << " to indexed object methods");
500 }
501
502
503 \f
504 ////////////////////////////////////////////////////////////////////////
505 // Conditions.
506 ////////////////////////////////////////////////////////////////////////
507
508
509 /**
510  * An encoded condition.
511  *
512  * This class encodes a single condition of some sort, possibly
513  * connected with properties.
514  *
515  * This class should migrate to somewhere more general.
516  */
517 class FGCondition
518 {
519 public:
520   FGCondition ();
521   virtual ~FGCondition ();
522   virtual bool test () const = 0;
523 };
524
525
526 /**
527  * Condition for a single property.
528  *
529  * This condition is true only if the property returns a boolean
530  * true value.
531  */
532 class FGPropertyCondition : public FGCondition
533 {
534 public:
535   FGPropertyCondition (const char * propname);
536   virtual ~FGPropertyCondition ();
537   virtual bool test () const { return _node->getBoolValue(); }
538 private:
539   const SGPropertyNode * _node;
540 };
541
542
543 /**
544  * Condition for a 'not' operator.
545  *
546  * This condition is true only if the child condition is false.
547  */
548 class FGNotCondition : public FGCondition
549 {
550 public:
551                                 // transfer pointer ownership
552   FGNotCondition (FGCondition * condition);
553   virtual ~FGNotCondition ();
554   virtual bool test () const;
555 private:
556   FGCondition * _condition;
557 };
558
559
560 /**
561  * Condition for an 'and' group.
562  *
563  * This condition is true only if all of the conditions
564  * in the group are true.
565  */
566 class FGAndCondition : public FGCondition
567 {
568 public:
569   FGAndCondition ();
570   virtual ~FGAndCondition ();
571   virtual bool test () const;
572                                 // transfer pointer ownership
573   virtual void addCondition (FGCondition * condition);
574 private:
575   vector<FGCondition *> _conditions;
576 };
577
578
579 /**
580  * Condition for an 'or' group.
581  *
582  * This condition is true if at least one of the conditions in the
583  * group is true.
584  */
585 class FGOrCondition : public FGCondition
586 {
587 public:
588   FGOrCondition ();
589   virtual ~FGOrCondition ();
590   virtual bool test () const;
591                                 // transfer pointer ownership
592   virtual void addCondition (FGCondition * condition);
593 private:
594   vector<FGCondition *> _conditions;
595 };
596
597
598 /**
599  * Abstract base class for property comparison conditions.
600  */
601 class FGComparisonCondition : public FGCondition
602 {
603 public:
604   enum Type {
605     LESS_THAN,
606     GREATER_THAN,
607     EQUALS
608   };
609   FGComparisonCondition (Type type, bool reverse = false);
610   virtual ~FGComparisonCondition ();
611   virtual bool test () const;
612   virtual void setLeftProperty (const char * propname);
613   virtual void setRightProperty (const char * propname);
614                                 // will make a local copy
615   virtual void setRightValue (const SGPropertyNode * value);
616 private:
617   Type _type;
618   bool _reverse;
619   const SGPropertyNode * _left_property;
620   const SGPropertyNode * _right_property;
621   const SGPropertyNode * _right_value;
622 };
623
624
625 /**
626  * Base class for a conditional components.
627  *
628  * This class manages the conditions and tests; the component should
629  * invoke the test() method whenever it needs to decide whether to
630  * active itself, draw itself, and so on.
631  */
632 class FGConditional
633 {
634 public:
635   FGConditional ();
636   virtual ~FGConditional ();
637                                 // transfer pointer ownership
638   virtual void setCondition (FGCondition * condition);
639   virtual const FGCondition * getCondition () const { return _condition; }
640   virtual bool test () const;
641 private:
642   FGCondition * _condition;
643 };
644
645
646 /**
647  * Global function to make a condition out of properties.
648  *
649  * The top-level is always an implicit 'and' group, whatever the
650  * node's name (it should usually be "condition").
651  *
652  * @param node The top-level condition node (usually named "condition").
653  * @return A pointer to a newly-allocated condition; it is the
654  *         responsibility of the caller to delete the condition when
655  *         it is no longer needed.
656  */
657 FGCondition * fgReadCondition (const SGPropertyNode * node);
658
659
660 #endif // __FG_PROPS_HXX
661