]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.hxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[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 <iosfwd>
10
11 #include <simgear/structure/subsystem_mgr.hxx>
12 #include <simgear/props/tiedpropertylist.hxx>
13
14 #include <Main/globals.hxx>
15
16 ////////////////////////////////////////////////////////////////////////
17 // Property management.
18 ////////////////////////////////////////////////////////////////////////
19
20 class FGProperties : public SGSubsystem
21 {
22 public:
23     FGProperties ();
24     virtual ~FGProperties ();
25
26     void init ();
27     void bind ();
28     void unbind ();
29     void update (double dt);
30
31 private:
32     simgear::TiedPropertyList _tiedProperties;
33 };
34
35
36 /**
37  * Save a flight to disk.
38  *
39  * This function saves all of the archivable properties to a stream
40  * so that the current flight can be restored later.
41  *
42  * @param output The output stream to write the XML save file to.
43  * @param write_all If true, write all properties rather than
44  *        just the ones flagged as archivable.
45  * @return true if the flight was saved successfully.
46  */
47 extern bool fgSaveFlight (std::ostream &output, bool write_all = false);
48
49
50 /**
51  * Load a flight from disk.
52  *
53  * This function loads an XML save file from a stream to restore
54  * a flight.
55  *
56  * @param input The input stream to read the XML from.
57  * @return true if the flight was restored successfully.
58  */
59 extern bool fgLoadFlight (std::istream &input);
60
61
62 /**
63  * Load properties from a file.
64  *
65  * @param file The relative or absolute filename.
66  * @param props The property node to load the properties into.
67  * @param in_fg_root If true, look for the file relative to
68  *        $FG_ROOT; otherwise, look for the file relative to the
69  *        current working directory.
70  * @return true if the properties loaded successfully, false
71  *         otherwise.
72  */
73 extern bool fgLoadProps (const char * path, SGPropertyNode * props,
74                          bool in_fg_root = true, int default_mode = 0);
75
76 void setLoggingClasses (const char * c);
77 void setLoggingPriority (const char * p);
78
79 \f
80 ////////////////////////////////////////////////////////////////////////
81 // Convenience functions for getting property values.
82 ////////////////////////////////////////////////////////////////////////
83
84 /**
85  * Get a property node.
86  *
87  * @param path The path of the node, relative to root.
88  * @param create true to create the node if it doesn't exist.
89  * @return The node, or 0 if none exists and none was created.
90  */
91 extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
92
93 /**
94  * Get a property node.
95  *
96  * @param path The path of the node, relative to root.
97  * @param create true to create the node if it doesn't exist.
98  * @return The node, or 0 if none exists and none was created.
99  */
100 inline SGPropertyNode * fgGetNode (const std::string & path, bool create = false)
101 {
102     return fgGetNode(path.c_str(), create );
103 }
104
105
106 /**
107  * Get a property node with separate index.
108  *
109  * This method separates the index from the path string, to make it
110  * easier to iterate through multiple components without using sprintf
111  * to add indices.  For example, fgGetNode("foo[1]/bar", 3) will
112  * return the same result as fgGetNode("foo[1]/bar[3]").
113  *
114  * @param path The path of the node, relative to root.
115  * @param index The index for the last member of the path (overrides
116  * any given in the string).
117  * @param create true to create the node if it doesn't exist.
118  * @return The node, or 0 if none exists and none was created.
119  */
120 extern SGPropertyNode * fgGetNode (const char * path,
121                                    int index, bool create = false);
122
123 /**
124  * Get a property node with separate index.
125  *
126  * This method separates the index from the path string, to make it
127  * easier to iterate through multiple components without using sprintf
128  * to add indices.  For example, fgGetNode("foo[1]/bar", 3) will
129  * return the same result as fgGetNode("foo[1]/bar[3]").
130  *
131  * @param path The path of the node, relative to root.
132  * @param index The index for the last member of the path (overrides
133  * any given in the string).
134  * @param create true to create the node if it doesn't exist.
135  * @return The node, or 0 if none exists and none was created.
136  */
137 inline SGPropertyNode * fgGetNode (const std::string & path,
138                                    int index, bool create = false)
139 {
140     return fgGetNode(path.c_str(), index, create );
141 }
142
143
144 /**
145  * Test whether a given node exists.
146  *
147  * @param path The path of the node, relative to root.
148  * @return true if the node exists, false otherwise.
149  */
150 extern bool fgHasNode (const char * path);
151
152 /**
153  * Test whether a given node exists.
154  *
155  * @param path The path of the node, relative to root.
156  * @return true if the node exists, false otherwise.
157  */
158 inline bool fgHasNode (const std::string & path)
159 {
160     return fgHasNode( path.c_str() );
161 }
162
163
164 /**
165  * Add a listener to a node.
166  *
167  * @param listener The listener to add to the node.
168  * @param path The path of the node, relative to root.
169  * @param index The index for the last member of the path (overrides
170  * any given in the string).
171  */
172 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
173                                  const char * path);
174
175 /**
176  * Add a listener to a node.
177  *
178  * @param listener The listener to add to the node.
179  * @param path The path of the node, relative to root.
180  * @param index The index for the last member of the path (overrides
181  * any given in the string).
182  */
183 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
184                                  const std::string & path)
185 {
186     fgAddChangeListener( listener, path.c_str() );
187 }
188
189
190 /**
191  * Add a listener to a node.
192  *
193  * @param listener The listener to add to the node.
194  * @param path The path of the node, relative to root.
195  * @param index The index for the last member of the path (overrides
196  * any given in the string).
197  */
198 extern void fgAddChangeListener (SGPropertyChangeListener * listener,
199                                  const char * path, int index);
200
201 /**
202  * Add a listener to a node.
203  *
204  * @param listener The listener to add to the node.
205  * @param path The path of the node, relative to root.
206  * @param index The index for the last member of the path (overrides
207  * any given in the string).
208  */
209 inline void fgAddChangeListener (SGPropertyChangeListener * listener,
210                                  const std::string & path, int index)
211 {
212     fgAddChangeListener( listener, path.c_str(), index );
213 }
214
215
216 /**
217  * Get a bool value for a property.
218  *
219  * This method is convenient but inefficient.  It should be used
220  * infrequently (i.e. for initializing, loading, saving, etc.),
221  * not in the main loop.  If you need to get a value frequently,
222  * it is better to look up the node itself using fgGetNode and then
223  * use the node's getBoolValue() method, to avoid the lookup overhead.
224  *
225  * @param name The property name.
226  * @param defaultValue The default value to return if the property
227  *        does not exist.
228  * @return The property's value as a bool, or the default value provided.
229  */
230 extern bool fgGetBool (const char * name, bool defaultValue = false);
231
232 /**
233  * Get a bool value for a property.
234  *
235  * This method is convenient but inefficient.  It should be used
236  * infrequently (i.e. for initializing, loading, saving, etc.),
237  * not in the main loop.  If you need to get a value frequently,
238  * it is better to look up the node itself using fgGetNode and then
239  * use the node's getBoolValue() method, to avoid the lookup overhead.
240  *
241  * @param name The property name.
242  * @param defaultValue The default value to return if the property
243  *        does not exist.
244  * @return The property's value as a bool, or the default value provided.
245  */
246 inline bool fgGetBool (const std::string & name, bool defaultValue = false)
247 {
248     return fgGetBool( name.c_str(), defaultValue );
249 }
250
251
252 /**
253  * Get an int value for a property.
254  *
255  * This method is convenient but inefficient.  It should be used
256  * infrequently (i.e. for initializing, loading, saving, etc.),
257  * not in the main loop.  If you need to get a value frequently,
258  * it is better to look up the node itself using fgGetNode and then
259  * use the node's getIntValue() method, to avoid the lookup overhead.
260  *
261  * @param name The property name.
262  * @param defaultValue The default value to return if the property
263  *        does not exist.
264  * @return The property's value as an int, or the default value provided.
265  */
266 extern int fgGetInt (const char * name, int defaultValue = 0);
267
268 /**
269  * Get an int value for a property.
270  *
271  * This method is convenient but inefficient.  It should be used
272  * infrequently (i.e. for initializing, loading, saving, etc.),
273  * not in the main loop.  If you need to get a value frequently,
274  * it is better to look up the node itself using fgGetNode and then
275  * use the node's getIntValue() method, to avoid the lookup overhead.
276  *
277  * @param name The property name.
278  * @param defaultValue The default value to return if the property
279  *        does not exist.
280  * @return The property's value as an int, or the default value provided.
281  */
282 inline int fgGetInt (const std::string & name, int defaultValue = 0)
283 {
284     return fgGetInt( name.c_str(), defaultValue );
285 }
286
287
288 /**
289  * Get a long value for a property.
290  *
291  * This method is convenient but inefficient.  It should be used
292  * infrequently (i.e. for initializing, loading, saving, etc.),
293  * not in the main loop.  If you need to get a value frequently,
294  * it is better to look up the node itself using fgGetNode and then
295  * use the node's getLongValue() method, to avoid the lookup overhead.
296  *
297  * @param name The property name.
298  * @param defaultValue The default value to return if the property
299  *        does not exist.
300  * @return The property's value as a long, or the default value provided.
301  */
302 extern int fgGetLong (const char * name, long defaultValue = 0L);
303
304 /**
305  * Get a long value for a property.
306  *
307  * This method is convenient but inefficient.  It should be used
308  * infrequently (i.e. for initializing, loading, saving, etc.),
309  * not in the main loop.  If you need to get a value frequently,
310  * it is better to look up the node itself using fgGetNode and then
311  * use the node's getLongValue() method, to avoid the lookup overhead.
312  *
313  * @param name The property name.
314  * @param defaultValue The default value to return if the property
315  *        does not exist.
316  * @return The property's value as a long, or the default value provided.
317  */
318 inline int fgGetLong (const std::string & name, long defaultValue = 0L)
319 {
320     return fgGetLong( name.c_str(), defaultValue );
321 }
322
323
324 /**
325  * Get a float value for a property.
326  *
327  * This method is convenient but inefficient.  It should be used
328  * infrequently (i.e. for initializing, loading, saving, etc.),
329  * not in the main loop.  If you need to get a value frequently,
330  * it is better to look up the node itself using fgGetNode and then
331  * use the node's getFloatValue() method, to avoid the lookup overhead.
332  *
333  * @param name The property name.
334  * @param defaultValue The default value to return if the property
335  *        does not exist.
336  * @return The property's value as a float, or the default value provided.
337  */
338 extern float fgGetFloat (const char * name, float defaultValue = 0.0);
339
340 /**
341  * Get a float value for a property.
342  *
343  * This method is convenient but inefficient.  It should be used
344  * infrequently (i.e. for initializing, loading, saving, etc.),
345  * not in the main loop.  If you need to get a value frequently,
346  * it is better to look up the node itself using fgGetNode and then
347  * use the node's getFloatValue() method, to avoid the lookup overhead.
348  *
349  * @param name The property name.
350  * @param defaultValue The default value to return if the property
351  *        does not exist.
352  * @return The property's value as a float, or the default value provided.
353  */
354 inline float fgGetFloat (const std::string & name, float defaultValue = 0.0)
355 {
356     return fgGetFloat( name.c_str(), defaultValue );
357 }
358
359
360 /**
361  * Get a double value for a property.
362  *
363  * This method is convenient but inefficient.  It should be used
364  * infrequently (i.e. for initializing, loading, saving, etc.),
365  * not in the main loop.  If you need to get a value frequently,
366  * it is better to look up the node itself using fgGetNode and then
367  * use the node's getDoubleValue() method, to avoid the lookup overhead.
368  *
369  * @param name The property name.
370  * @param defaultValue The default value to return if the property
371  *        does not exist.
372  * @return The property's value as a double, or the default value provided.
373  */
374 extern double fgGetDouble (const char * name, double defaultValue = 0.0);
375
376 /**
377  * Get a double value for a property.
378  *
379  * This method is convenient but inefficient.  It should be used
380  * infrequently (i.e. for initializing, loading, saving, etc.),
381  * not in the main loop.  If you need to get a value frequently,
382  * it is better to look up the node itself using fgGetNode and then
383  * use the node's getDoubleValue() method, to avoid the lookup overhead.
384  *
385  * @param name The property name.
386  * @param defaultValue The default value to return if the property
387  *        does not exist.
388  * @return The property's value as a double, or the default value provided.
389  */
390 inline double fgGetDouble (const std::string & name, double defaultValue = 0.0)
391 {
392     return fgGetDouble( name.c_str(), defaultValue );
393 }
394
395
396 /**
397  * Get a string value for a property.
398  *
399  * This method is convenient but inefficient.  It should be used
400  * infrequently (i.e. for initializing, loading, saving, etc.),
401  * not in the main loop.  If you need to get a value frequently,
402  * it is better to look up the node itself using fgGetNode and then
403  * use the node's getStringValue() method, to avoid the lookup overhead.
404  *
405  * @param name The property name.
406  * @param defaultValue The default value to return if the property
407  *        does not exist.
408  * @return The property's value as a string, or the default value provided.
409  */
410 extern const char * fgGetString (const char * name,
411                                  const char * defaultValue = "");
412
413 /**
414  * Get a string value for a property.
415  *
416  * This method is convenient but inefficient.  It should be used
417  * infrequently (i.e. for initializing, loading, saving, etc.),
418  * not in the main loop.  If you need to get a value frequently,
419  * it is better to look up the node itself using fgGetNode and then
420  * use the node's getStringValue() method, to avoid the lookup overhead.
421  *
422  * @param name The property name.
423  * @param defaultValue The default value to return if the property
424  *        does not exist.
425  * @return The property's value as a string, or the default value provided.
426  */
427 inline const char * fgGetString (const std::string & name,
428                                  const std::string & defaultValue = std::string(""))
429 {
430     return fgGetString( name.c_str(), defaultValue.c_str() );
431 }
432
433
434 /**
435  * Set a bool value for a property.
436  *
437  * Assign a bool value to a property.  If the property does not
438  * yet exist, it will be created and its type will be set to
439  * BOOL; if it has a type of UNKNOWN, the type will also be set to
440  * BOOL; otherwise, the bool value will be converted to the property's
441  * type.
442  *
443  * @param name The property name.
444  * @param val The new value for the property.
445  * @return true if the assignment succeeded, false otherwise.
446  */
447 extern bool fgSetBool (const char * name, bool val);
448
449 /**
450  * Set a bool value for a property.
451  *
452  * Assign a bool value to a property.  If the property does not
453  * yet exist, it will be created and its type will be set to
454  * BOOL; if it has a type of UNKNOWN, the type will also be set to
455  * BOOL; otherwise, the bool value will be converted to the property's
456  * type.
457  *
458  * @param name The property name.
459  * @param val The new value for the property.
460  * @return true if the assignment succeeded, false otherwise.
461  */
462 inline bool fgSetBool (const std::string & name, bool val)
463 {
464     return fgSetBool( name.c_str(), val );
465 }
466
467
468 /**
469  * Set an int value for a property.
470  *
471  * Assign an int value to a property.  If the property does not
472  * yet exist, it will be created and its type will be set to
473  * INT; if it has a type of UNKNOWN, the type will also be set to
474  * INT; otherwise, the bool value will be converted to the property's
475  * type.
476  *
477  * @param name The property name.
478  * @param val The new value for the property.
479  * @return true if the assignment succeeded, false otherwise.
480  */
481 extern bool fgSetInt (const char * name, int val);
482
483 /**
484  * Set an int value for a property.
485  *
486  * Assign an int value to a property.  If the property does not
487  * yet exist, it will be created and its type will be set to
488  * INT; if it has a type of UNKNOWN, the type will also be set to
489  * INT; otherwise, the bool value will be converted to the property's
490  * type.
491  *
492  * @param name The property name.
493  * @param val The new value for the property.
494  * @return true if the assignment succeeded, false otherwise.
495  */
496 inline bool fgSetInt (const std::string & name, int val)
497 {
498     return fgSetInt( name.c_str(), val );
499 }
500
501 /**
502  * Set a long value for a property.
503  *
504  * Assign a long value to a property.  If the property does not
505  * yet exist, it will be created and its type will be set to
506  * LONG; if it has a type of UNKNOWN, the type will also be set to
507  * LONG; otherwise, the bool value will be converted to the property's
508  * type.
509  *
510  * @param name The property name.
511  * @param val The new value for the property.
512  * @return true if the assignment succeeded, false otherwise.
513  */
514 extern bool fgSetLong (const char * name, long val);
515
516 /**
517  * Set a long value for a property.
518  *
519  * Assign a long value to a property.  If the property does not
520  * yet exist, it will be created and its type will be set to
521  * LONG; if it has a type of UNKNOWN, the type will also be set to
522  * LONG; otherwise, the bool value will be converted to the property's
523  * type.
524  *
525  * @param name The property name.
526  * @param val The new value for the property.
527  * @return true if the assignment succeeded, false otherwise.
528  */
529 inline bool fgSetLong (const std::string & name, long val)
530 {
531     return fgSetLong( name.c_str(), val );
532 }
533
534
535 /**
536  * Set a float value for a property.
537  *
538  * Assign a float value to a property.  If the property does not
539  * yet exist, it will be created and its type will be set to
540  * FLOAT; if it has a type of UNKNOWN, the type will also be set to
541  * FLOAT; otherwise, the bool value will be converted to the property's
542  * type.
543  *
544  * @param name The property name.
545  * @param val The new value for the property.
546  * @return true if the assignment succeeded, false otherwise.
547  */
548 extern bool fgSetFloat (const char * name, float val);
549
550 /**
551  * Set a float value for a property.
552  *
553  * Assign a float value to a property.  If the property does not
554  * yet exist, it will be created and its type will be set to
555  * FLOAT; if it has a type of UNKNOWN, the type will also be set to
556  * FLOAT; otherwise, the bool value will be converted to the property's
557  * type.
558  *
559  * @param name The property name.
560  * @param val The new value for the property.
561  * @return true if the assignment succeeded, false otherwise.
562  */
563 inline bool fgSetFloat (const std::string & name, float val)
564 {
565     return fgSetFloat( name.c_str(), val );
566 }
567
568
569 /**
570  * Set a double value for a property.
571  *
572  * Assign a double value to a property.  If the property does not
573  * yet exist, it will be created and its type will be set to
574  * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
575  * DOUBLE; otherwise, the double value will be converted to the property's
576  * type.
577  *
578  * @param name The property name.
579  * @param val The new value for the property.
580  * @return true if the assignment succeeded, false otherwise.
581  */
582 extern bool fgSetDouble (const char * name, double val);
583
584 /**
585  * Set a double value for a property.
586  *
587  * Assign a double value to a property.  If the property does not
588  * yet exist, it will be created and its type will be set to
589  * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
590  * DOUBLE; otherwise, the double value will be converted to the property's
591  * type.
592  *
593  * @param name The property name.
594  * @param val The new value for the property.
595  * @return true if the assignment succeeded, false otherwise.
596  */
597 inline bool fgSetDouble (const std::string & name, double val)
598 {
599     return fgSetDouble( name.c_str(), val );
600 }
601
602
603 /**
604  * Set a string value for a property.
605  *
606  * Assign a string value to a property.  If the property does not
607  * yet exist, it will be created and its type will be set to
608  * STRING; if it has a type of UNKNOWN, the type will also be set to
609  * STRING; otherwise, the string value will be converted to the property's
610  * type.
611  *
612  * @param name The property name.
613  * @param val The new value for the property.
614  * @return true if the assignment succeeded, false otherwise.
615  */
616 extern bool fgSetString (const char * name, const char * val);
617
618 /**
619  * Set a string value for a property.
620  *
621  * Assign a string value to a property.  If the property does not
622  * yet exist, it will be created and its type will be set to
623  * STRING; if it has a type of UNKNOWN, the type will also be set to
624  * STRING; otherwise, the string value will be converted to the property's
625  * type.
626  *
627  * @param name The property name.
628  * @param val The new value for the property.
629  * @return true if the assignment succeeded, false otherwise.
630  */
631 inline bool fgSetString (const std::string & name, const std::string & val)
632 {
633     return fgSetString( name.c_str(), val.c_str() );
634 }
635
636
637 \f
638 ////////////////////////////////////////////////////////////////////////
639 // Convenience functions for setting property attributes.
640 ////////////////////////////////////////////////////////////////////////
641
642
643 /**
644  * Set the state of the archive attribute for a property.
645  *
646  * If the archive attribute is true, the property will be written
647  * when a flight is saved; if it is false, the property will be
648  * skipped.
649  *
650  * A warning message will be printed if the property does not exist.
651  *
652  * @param name The property name.
653  * @param state The state of the archive attribute (defaults to true).
654  */
655 extern void fgSetArchivable (const char * name, bool state = true);
656
657
658 /**
659  * Set the state of the read attribute for a property.
660  *
661  * If the read attribute is true, the property value will be readable;
662  * if it is false, the property value will always be the default value
663  * for its type.
664  *
665  * A warning message will be printed if the property does not exist.
666  *
667  * @param name The property name.
668  * @param state The state of the read attribute (defaults to true).
669  */
670 extern void fgSetReadable (const char * name, bool state = true);
671
672
673 /**
674  * Set the state of the write attribute for a property.
675  *
676  * If the write attribute is true, the property value may be modified
677  * (depending on how it is tied); if the write attribute is false, the
678  * property value may not be modified.
679  *
680  * A warning message will be printed if the property does not exist.
681  *
682  * @param name The property name.
683  * @param state The state of the write attribute (defaults to true).
684  */
685 extern void fgSetWritable (const char * name, bool state = true);
686
687
688 \f
689 ////////////////////////////////////////////////////////////////////////
690 // Convenience functions for tying properties, with logging.
691 ////////////////////////////////////////////////////////////////////////
692
693
694 /**
695  * Untie a property from an external data source.
696  *
697  * Classes should use this function to release control of any
698  * properties they are managing.
699  */
700 extern void fgUntie (const char * name);
701
702
703 /**
704  * Tie a property to a pair of simple functions.
705  *
706  * Every time the property value is queried, the getter (if any) will
707  * be invoked; every time the property value is modified, the setter
708  * (if any) will be invoked.  The getter can be 0 to make the property
709  * unreadable, and the setter can be 0 to make the property
710  * unmodifiable.
711  *
712  * @param name The property name to tie (full path).
713  * @param getter The getter function, or 0 if the value is unreadable.
714  * @param setter The setter function, or 0 if the value is unmodifiable.
715  * @param useDefault true if the setter should be invoked with any existing 
716  *        property value should be; false if the old value should be
717  *        discarded; defaults to true.
718  */
719 template <class V>
720 inline void
721 fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
722        bool useDefault = true)
723 {
724   if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
725                                  useDefault))
726     SG_LOG(SG_GENERAL, SG_WARN,
727            "Failed to tie property " << name << " to functions");
728 }
729
730
731 /**
732  * Tie a property to a pair of indexed functions.
733  *
734  * Every time the property value is queried, the getter (if any) will
735  * be invoked with the index provided; every time the property value
736  * is modified, the setter (if any) will be invoked with the index
737  * provided.  The getter can be 0 to make the property unreadable, and
738  * the setter can be 0 to make the property unmodifiable.
739  *
740  * @param name The property name to tie (full path).
741  * @param index The integer argument to pass to the getter and
742  *        setter functions.
743  * @param getter The getter function, or 0 if the value is unreadable.
744  * @param setter The setter function, or 0 if the value is unmodifiable.
745  * @param useDefault true if the setter should be invoked with any existing 
746  *        property value should be; false if the old value should be
747  *        discarded; defaults to true.
748  */
749 template <class V>
750 inline void
751 fgTie (const char * name, int index, V (*getter)(int),
752        void (*setter)(int, V) = 0, bool useDefault = true)
753 {
754   if (!globals->get_props()->tie(name,
755                                  SGRawValueFunctionsIndexed<V>(index,
756                                                                getter,
757                                                                setter),
758                                  useDefault))
759     SG_LOG(SG_GENERAL, SG_WARN,
760            "Failed to tie property " << name << " to indexed functions");
761 }
762
763
764 /**
765  * Tie a property to a pair of object methods.
766  *
767  * Every time the property value is queried, the getter (if any) will
768  * be invoked; every time the property value is modified, the setter
769  * (if any) will be invoked.  The getter can be 0 to make the property
770  * unreadable, and the setter can be 0 to make the property
771  * unmodifiable.
772  *
773  * @param name The property name to tie (full path).
774  * @param obj The object whose methods should be invoked.
775  * @param getter The object's getter method, or 0 if the value is
776  *        unreadable.
777  * @param setter The object's setter method, or 0 if the value is
778  *        unmodifiable.
779  * @param useDefault true if the setter should be invoked with any existing 
780  *        property value should be; false if the old value should be
781  *        discarded; defaults to true.
782  */
783 template <class T, class V>
784 inline void
785 fgTie (const char * name, T * obj, V (T::*getter)() const,
786        void (T::*setter)(V) = 0, bool useDefault = true)
787 {
788   if (!globals->get_props()->tie(name,
789                                  SGRawValueMethods<T,V>(*obj, getter, setter),
790                                  useDefault))
791     SG_LOG(SG_GENERAL, SG_WARN,
792            "Failed to tie property " << name << " to object methods");
793 }
794
795
796 /**
797  * Tie a property to a pair of indexed object methods.
798  *
799  * Every time the property value is queried, the getter (if any) will
800  * be invoked with the index provided; every time the property value
801  * is modified, the setter (if any) will be invoked with the index
802  * provided.  The getter can be 0 to make the property unreadable, and
803  * the setter can be 0 to make the property unmodifiable.
804  *
805  * @param name The property name to tie (full path).
806  * @param obj The object whose methods should be invoked.
807  * @param index The integer argument to pass to the getter and
808  *        setter methods.
809  * @param getter The getter method, or 0 if the value is unreadable.
810  * @param setter The setter method, or 0 if the value is unmodifiable.
811  * @param useDefault true if the setter should be invoked with any existing 
812  *        property value should be; false if the old value should be
813  *        discarded; defaults to true.
814  */
815 template <class T, class V>
816 inline void 
817 fgTie (const char * name, T * obj, int index,
818        V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
819        bool useDefault = true)
820 {
821   if (!globals->get_props()->tie(name,
822                                  SGRawValueMethodsIndexed<T,V>(*obj,
823                                                                index,
824                                                                getter,
825                                                                setter),
826                                  useDefault))
827     SG_LOG(SG_GENERAL, SG_WARN,
828            "Failed to tie property " << name << " to indexed object methods");
829 }
830
831
832 class FGMakeUpperCase : public SGPropertyChangeListener {
833 public:
834     void valueChanged(SGPropertyNode *node) {
835         if (node->getType() != simgear::props::STRING)
836             return;
837
838         char *s = const_cast<char *>(node->getStringValue());
839         for (; *s; s++)
840             *s = toupper(*s);
841     }
842 };
843
844
845 #endif // __FG_PROPS_HXX
846