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