1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Header: FGPropertyManager.h
5 Based on work originally by David Megginson
8 ------------- Copyright (C) 2002 -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
31 #ifndef FGPROPERTYMANAGER_H
32 #define FGPROPERTYMANAGER_H
34 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 #include <simgear/misc/props.hxx>
41 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 #define ID_PROPERTYMANAGER "$Id$"
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61 /** Class wrapper for property handling.
62 @author David Megginson, Tony Peden
63 @see <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jsbsim/JSBSim/FGPropertyManager.h?rev=HEAD&content-type=text/vnd.viewcvs-markup">
65 @see <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jsbsim/JSBSim/FGPropertyManager.cpp?rev=HEAD&content-type=text/vnd.viewcvs-markup">
69 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73 class FGPropertyManager : public SGPropertyNode {
76 FGPropertyManager(void) {}
78 ~FGPropertyManager(void) {}
80 /** Property-ify a name
81 * replaces spaces with '-' and, optionally, makes name all lower case
82 * @param name string to change
83 * @param lowercase true to change all upper case chars to lower
84 * NOTE: this function changes its argument and thus relies
87 string mkPropertyName(string name, bool lowercase);
90 * Get a property node.
92 * @param path The path of the node, relative to root.
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.
97 GetNode (const string &path, bool create = false);
100 GetNode (const string &relpath, int index, bool create = false);
103 * Test whether a given node exists.
105 * @param path The path of the node, relative to root.
106 * @return true if the node exists, false otherwise.
108 bool HasNode (const string &path);
111 * Get the name of a node
113 string GetName( void );
116 * Get the fully qualified name of a node
117 * This function is very slow, so is probably useful for debugging only.
119 string GetFullyQualifiedName(void);
122 * Get a bool value for a property.
124 * This method is convenient but inefficient. It should be used
125 * infrequently (i.e. for initializing, loading, saving, etc.),
126 * not in the main loop. If you need to get a value frequently,
127 * it is better to look up the node itself using GetNode and then
128 * use the node's getBoolValue() method, to avoid the lookup overhead.
130 * @param name The property name.
131 * @param defaultValue The default value to return if the property
133 * @return The property's value as a bool, or the default value provided.
135 bool GetBool (const string &name, bool defaultValue = false);
139 * Get an int value for a property.
141 * This method is convenient but inefficient. It should be used
142 * infrequently (i.e. for initializing, loading, saving, etc.),
143 * not in the main loop. If you need to get a value frequently,
144 * it is better to look up the node itself using GetNode and then
145 * use the node's getIntValue() method, to avoid the lookup overhead.
147 * @param name The property name.
148 * @param defaultValue The default value to return if the property
150 * @return The property's value as an int, or the default value provided.
152 int GetInt (const string &name, int defaultValue = 0);
156 * Get a long value for a property.
158 * This method is convenient but inefficient. It should be used
159 * infrequently (i.e. for initializing, loading, saving, etc.),
160 * not in the main loop. If you need to get a value frequently,
161 * it is better to look up the node itself using GetNode and then
162 * use the node's getLongValue() method, to avoid the lookup overhead.
164 * @param name The property name.
165 * @param defaultValue The default value to return if the property
167 * @return The property's value as a long, or the default value provided.
169 int GetLong (const string &name, long defaultValue = 0L);
173 * Get a float value for a property.
175 * This method is convenient but inefficient. It should be used
176 * infrequently (i.e. for initializing, loading, saving, etc.),
177 * not in the main loop. If you need to get a value frequently,
178 * it is better to look up the node itself using GetNode and then
179 * use the node's getFloatValue() method, to avoid the lookup overhead.
181 * @param name The property name.
182 * @param defaultValue The default value to return if the property
184 * @return The property's value as a float, or the default value provided.
186 float GetFloat (const string &name, float defaultValue = 0.0);
190 * Get a double value for a property.
192 * This method is convenient but inefficient. It should be used
193 * infrequently (i.e. for initializing, loading, saving, etc.),
194 * not in the main loop. If you need to get a value frequently,
195 * it is better to look up the node itself using GetNode and then
196 * use the node's getDoubleValue() method, to avoid the lookup overhead.
198 * @param name The property name.
199 * @param defaultValue The default value to return if the property
201 * @return The property's value as a double, or the default value provided.
203 double GetDouble (const string &name, double defaultValue = 0.0);
207 * Get a string value for a property.
209 * This method is convenient but inefficient. It should be used
210 * infrequently (i.e. for initializing, loading, saving, etc.),
211 * not in the main loop. If you need to get a value frequently,
212 * it is better to look up the node itself using GetNode and then
213 * use the node's getStringValue() method, to avoid the lookup overhead.
215 * @param name The property name.
216 * @param defaultValue The default value to return if the property
218 * @return The property's value as a string, or the default value provided.
220 string GetString (const string &name, string defaultValue = "");
224 * Set a bool value for a property.
226 * Assign a bool value to a property. If the property does not
227 * yet exist, it will be created and its type will be set to
228 * BOOL; if it has a type of UNKNOWN, the type will also be set to
229 * BOOL; otherwise, the bool value will be converted to the property's
232 * @param name The property name.
233 * @param val The new value for the property.
234 * @return true if the assignment succeeded, false otherwise.
236 bool SetBool (const string &name, bool val);
240 * Set an int value for a property.
242 * Assign an int value to a property. If the property does not
243 * yet exist, it will be created and its type will be set to
244 * INT; if it has a type of UNKNOWN, the type will also be set to
245 * INT; otherwise, the bool value will be converted to the property's
248 * @param name The property name.
249 * @param val The new value for the property.
250 * @return true if the assignment succeeded, false otherwise.
252 bool SetInt (const string &name, int val);
256 * Set a long value for a property.
258 * Assign a long value to a property. If the property does not
259 * yet exist, it will be created and its type will be set to
260 * LONG; if it has a type of UNKNOWN, the type will also be set to
261 * LONG; otherwise, the bool value will be converted to the property's
264 * @param name The property name.
265 * @param val The new value for the property.
266 * @return true if the assignment succeeded, false otherwise.
268 bool SetLong (const string &name, long val);
272 * Set a float value for a property.
274 * Assign a float value to a property. If the property does not
275 * yet exist, it will be created and its type will be set to
276 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
277 * FLOAT; otherwise, the bool value will be converted to the property's
280 * @param name The property name.
281 * @param val The new value for the property.
282 * @return true if the assignment succeeded, false otherwise.
284 bool SetFloat (const string &name, float val);
288 * Set a double value for a property.
290 * Assign a double value to a property. If the property does not
291 * yet exist, it will be created and its type will be set to
292 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
293 * DOUBLE; otherwise, the double value will be converted to the property's
296 * @param name The property name.
297 * @param val The new value for the property.
298 * @return true if the assignment succeeded, false otherwise.
300 bool SetDouble (const string &name, double val);
304 * Set a string value for a property.
306 * Assign a string value to a property. If the property does not
307 * yet exist, it will be created and its type will be set to
308 * STRING; if it has a type of UNKNOWN, the type will also be set to
309 * STRING; otherwise, the string value will be converted to the property's
312 * @param name The property name.
313 * @param val The new value for the property.
314 * @return true if the assignment succeeded, false otherwise.
316 bool SetString (const string &name, const string &val);
319 ////////////////////////////////////////////////////////////////////////
320 // Convenience functions for setting property attributes.
321 ////////////////////////////////////////////////////////////////////////
325 * Set the state of the archive attribute for a property.
327 * If the archive attribute is true, the property will be written
328 * when a flight is saved; if it is false, the property will be
331 * A warning message will be printed if the property does not exist.
333 * @param name The property name.
334 * @param state The state of the archive attribute (defaults to true).
336 void SetArchivable (const string &name, bool state = true);
340 * Set the state of the read attribute for a property.
342 * If the read attribute is true, the property value will be readable;
343 * if it is false, the property value will always be the default value
346 * A warning message will be printed if the property does not exist.
348 * @param name The property name.
349 * @param state The state of the read attribute (defaults to true).
351 void SetReadable (const string &name, bool state = true);
355 * Set the state of the write attribute for a property.
357 * If the write attribute is true, the property value may be modified
358 * (depending on how it is tied); if the write attribute is false, the
359 * property value may not be modified.
361 * A warning message will be printed if the property does not exist.
363 * @param name The property name.
364 * @param state The state of the write attribute (defaults to true).
366 void SetWritable (const string &name, bool state = true);
369 ////////////////////////////////////////////////////////////////////////
370 // Convenience functions for tying properties, with logging.
371 ////////////////////////////////////////////////////////////////////////
375 * Untie a property from an external data source.
377 * Classes should use this function to release control of any
378 * properties they are managing.
380 void Untie (const string &name);
383 // Templates cause ambiguity here
386 * Tie a property to an external bool variable.
388 * The property's value will automatically mirror the variable's
389 * value, and vice-versa, until the property is untied.
391 * @param name The property name to tie (full path).
392 * @param pointer A pointer to the variable.
393 * @param useDefault true if any existing property value should be
394 * copied to the variable; false if the variable should not
395 * be modified; defaults to true.
398 Tie (const string &name, bool *pointer, bool useDefault = true);
402 * Tie a property to an external int variable.
404 * The property's value will automatically mirror the variable's
405 * value, and vice-versa, until the property is untied.
407 * @param name The property name to tie (full path).
408 * @param pointer A pointer to the variable.
409 * @param useDefault true if any existing property value should be
410 * copied to the variable; false if the variable should not
411 * be modified; defaults to true.
414 Tie (const string &name, int *pointer, bool useDefault = true);
418 * Tie a property to an external long variable.
420 * The property's value will automatically mirror the variable's
421 * value, and vice-versa, until the property is untied.
423 * @param name The property name to tie (full path).
424 * @param pointer A pointer to the variable.
425 * @param useDefault true if any existing property value should be
426 * copied to the variable; false if the variable should not
427 * be modified; defaults to true.
430 Tie (const string &name, long *pointer, bool useDefault = true);
434 * Tie a property to an external float variable.
436 * The property's value will automatically mirror the variable's
437 * value, and vice-versa, until the property is untied.
439 * @param name The property name to tie (full path).
440 * @param pointer A pointer to the variable.
441 * @param useDefault true if any existing property value should be
442 * copied to the variable; false if the variable should not
443 * be modified; defaults to true.
446 Tie (const string &name, float *pointer, bool useDefault = true);
449 * Tie a property to an external double variable.
451 * The property's value will automatically mirror the variable's
452 * value, and vice-versa, until the property is untied.
454 * @param name The property name to tie (full path).
455 * @param pointer A pointer to the variable.
456 * @param useDefault true if any existing property value should be
457 * copied to the variable; false if the variable should not
458 * be modified; defaults to true.
461 Tie (const string &name, double *pointer, bool useDefault = true);
463 //============================================================================
465 // All of the following functions *must* be inlined, otherwise linker
466 // errors will result
468 //============================================================================
470 /* template <class V> void
471 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0,
472 bool useDefault = true);
474 template <class V> void
475 Tie (const string &name, int index, V (*getter)(int),
476 void (*setter)(int, V) = 0, bool useDefault = true);
478 template <class T, class V> void
479 Tie (const string &name, T * obj, V (T::*getter)() const,
480 void (T::*setter)(V) = 0, bool useDefault = true);
482 template <class T, class V> void
483 Tie (const string &name, T * obj, int index,
484 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
485 bool useDefault = true); */
488 * Tie a property to a pair of simple functions.
490 * Every time the property value is queried, the getter (if any) will
491 * be invoked; every time the property value is modified, the setter
492 * (if any) will be invoked. The getter can be 0 to make the property
493 * unreadable, and the setter can be 0 to make the property
496 * @param name The property name to tie (full path).
497 * @param getter The getter function, or 0 if the value is unreadable.
498 * @param setter The setter function, or 0 if the value is unmodifiable.
499 * @param useDefault true if the setter should be invoked with any existing
500 * property value should be; false if the old value should be
501 * discarded; defaults to true.
504 template <class V> inline void
505 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0,
506 bool useDefault = true)
508 if (!tie(name.c_str(), SGRawValueFunctions<V>(getter, setter),
511 "Failed to tie property " << name << " to functions" << endl;
516 * Tie a property to a pair of indexed functions.
518 * Every time the property value is queried, the getter (if any) will
519 * be invoked with the index provided; every time the property value
520 * is modified, the setter (if any) will be invoked with the index
521 * provided. The getter can be 0 to make the property unreadable, and
522 * the setter can be 0 to make the property unmodifiable.
524 * @param name The property name to tie (full path).
525 * @param index The integer argument to pass to the getter and
527 * @param getter The getter function, or 0 if the value is unreadable.
528 * @param setter The setter function, or 0 if the value is unmodifiable.
529 * @param useDefault true if the setter should be invoked with any existing
530 * property value should be; false if the old value should be
531 * discarded; defaults to true.
533 template <class V> inline void Tie (const string &name,
534 int index, V (*getter)(int),
535 void (*setter)(int, V) = 0, bool useDefault = true)
537 if (!tie(name.c_str(),
538 SGRawValueFunctionsIndexed<V>(index,
543 "Failed to tie property " << name << " to indexed functions" << endl;
548 * Tie a property to a pair of object methods.
550 * Every time the property value is queried, the getter (if any) will
551 * be invoked; every time the property value is modified, the setter
552 * (if any) will be invoked. The getter can be 0 to make the property
553 * unreadable, and the setter can be 0 to make the property
556 * @param name The property name to tie (full path).
557 * @param obj The object whose methods should be invoked.
558 * @param getter The object's getter method, or 0 if the value is
560 * @param setter The object's setter method, or 0 if the value is
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.
566 template <class T, class V> inline void
567 Tie (const string &name, T * obj, V (T::*getter)() const,
568 void (T::*setter)(V) = 0, bool useDefault = true)
570 if (!tie(name.c_str(),
571 SGRawValueMethods<T,V>(*obj, getter, setter),
574 "Failed to tie property " << name << " to object methods" << endl;
578 * Tie a property to a pair of indexed object methods.
580 * Every time the property value is queried, the getter (if any) will
581 * be invoked with the index provided; every time the property value
582 * is modified, the setter (if any) will be invoked with the index
583 * provided. The getter can be 0 to make the property unreadable, and
584 * the setter can be 0 to make the property unmodifiable.
586 * @param name The property name to tie (full path).
587 * @param obj The object whose methods should be invoked.
588 * @param index The integer argument to pass to the getter and
590 * @param getter The getter method, or 0 if the value is unreadable.
591 * @param setter The setter method, or 0 if the value is unmodifiable.
592 * @param useDefault true if the setter should be invoked with any existing
593 * property value should be; false if the old value should be
594 * discarded; defaults to true.
596 template <class T, class V> inline void
597 Tie (const string &name, T * obj, int index,
598 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
599 bool useDefault = true)
601 if (!tie(name.c_str(),
602 SGRawValueMethodsIndexed<T,V>(*obj,
608 "Failed to tie property " << name << " to indexed object methods" << endl;
612 #endif // FGPROPERTYMANAGER_H