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 Lesser 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 Lesser General Public License for more
20 You should have received a copy of the GNU Lesser 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 Lesser 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38 // This is needed by MSVC9 when included in FlightGear because of
39 // the new Vec4d class in props.hxx
40 #if defined( HAVE_CONFIG_H )
46 #include "simgear/props/props.hxx"
48 # include "simgear/math/SGMath.hxx"
51 #include "FGJSBBase.h"
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 #define ID_PROPERTYMANAGER "$Id$"
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71 /** Class wrapper for property handling.
72 @author David Megginson, Tony Peden
75 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
79 class FGPropertyManager : public SGPropertyNode, public FGJSBBase
82 static bool suppress_warning;
85 FGPropertyManager(void) {suppress_warning = false;}
87 virtual ~FGPropertyManager(void) {}
89 /** Property-ify a name
90 * replaces spaces with '-' and, optionally, makes name all lower case
91 * @param name string to change
92 * @param lowercase true to change all upper case chars to lower
93 * NOTE: this function changes its argument and thus relies
96 string mkPropertyName(string name, bool lowercase);
99 * Get a property node.
101 * @param path The path of the node, relative to root.
102 * @param create true to create the node if it doesn't exist.
103 * @return The node, or 0 if none exists and none was created.
106 GetNode (const string &path, bool create = false);
109 GetNode (const string &relpath, int index, bool create = false);
112 * Test whether a given node exists.
114 * @param path The path of the node, relative to root.
115 * @return true if the node exists, false otherwise.
117 bool HasNode (const string &path);
120 * Get the name of a node
122 string GetName( void );
125 * Get the name of a node without underscores, etc.
127 string GetPrintableName( void );
130 * Get the fully qualified name of a node
131 * This function is very slow, so is probably useful for debugging only.
133 string GetFullyQualifiedName(void);
136 * Get a bool value for a property.
138 * This method is convenient but inefficient. It should be used
139 * infrequently (i.e. for initializing, loading, saving, etc.),
140 * not in the main loop. If you need to get a value frequently,
141 * it is better to look up the node itself using GetNode and then
142 * use the node's getBoolValue() method, to avoid the lookup overhead.
144 * @param name The property name.
145 * @param defaultValue The default value to return if the property
147 * @return The property's value as a bool, or the default value provided.
149 bool GetBool (const string &name, bool defaultValue = false);
153 * Get an int value for a property.
155 * This method is convenient but inefficient. It should be used
156 * infrequently (i.e. for initializing, loading, saving, etc.),
157 * not in the main loop. If you need to get a value frequently,
158 * it is better to look up the node itself using GetNode and then
159 * use the node's getIntValue() method, to avoid the lookup overhead.
161 * @param name The property name.
162 * @param defaultValue The default value to return if the property
164 * @return The property's value as an int, or the default value provided.
166 int GetInt (const string &name, int defaultValue = 0);
170 * Get a long value for a property.
172 * This method is convenient but inefficient. It should be used
173 * infrequently (i.e. for initializing, loading, saving, etc.),
174 * not in the main loop. If you need to get a value frequently,
175 * it is better to look up the node itself using GetNode and then
176 * use the node's getLongValue() method, to avoid the lookup overhead.
178 * @param name The property name.
179 * @param defaultValue The default value to return if the property
181 * @return The property's value as a long, or the default value provided.
183 int GetLong (const string &name, long defaultValue = 0L);
187 * Get a float value for a property.
189 * This method is convenient but inefficient. It should be used
190 * infrequently (i.e. for initializing, loading, saving, etc.),
191 * not in the main loop. If you need to get a value frequently,
192 * it is better to look up the node itself using GetNode and then
193 * use the node's getFloatValue() method, to avoid the lookup overhead.
195 * @param name The property name.
196 * @param defaultValue The default value to return if the property
198 * @return The property's value as a float, or the default value provided.
200 float GetFloat (const string &name, float defaultValue = 0.0);
204 * Get a double value for a property.
206 * This method is convenient but inefficient. It should be used
207 * infrequently (i.e. for initializing, loading, saving, etc.),
208 * not in the main loop. If you need to get a value frequently,
209 * it is better to look up the node itself using GetNode and then
210 * use the node's getDoubleValue() method, to avoid the lookup overhead.
212 * @param name The property name.
213 * @param defaultValue The default value to return if the property
215 * @return The property's value as a double, or the default value provided.
217 double GetDouble (const string &name, double defaultValue = 0.0);
221 * Get a string value for a property.
223 * This method is convenient but inefficient. It should be used
224 * infrequently (i.e. for initializing, loading, saving, etc.),
225 * not in the main loop. If you need to get a value frequently,
226 * it is better to look up the node itself using GetNode and then
227 * use the node's getStringValue() method, to avoid the lookup overhead.
229 * @param name The property name.
230 * @param defaultValue The default value to return if the property
232 * @return The property's value as a string, or the default value provided.
234 string GetString (const string &name, string defaultValue = "");
238 * Set a bool value for a property.
240 * Assign a bool value to a property. If the property does not
241 * yet exist, it will be created and its type will be set to
242 * BOOL; if it has a type of UNKNOWN, the type will also be set to
243 * BOOL; otherwise, the bool value will be converted to the property's
246 * @param name The property name.
247 * @param val The new value for the property.
248 * @return true if the assignment succeeded, false otherwise.
250 bool SetBool (const string &name, bool val);
254 * Set an int value for a property.
256 * Assign an int value to a property. If the property does not
257 * yet exist, it will be created and its type will be set to
258 * INT; if it has a type of UNKNOWN, the type will also be set to
259 * INT; otherwise, the bool value will be converted to the property's
262 * @param name The property name.
263 * @param val The new value for the property.
264 * @return true if the assignment succeeded, false otherwise.
266 bool SetInt (const string &name, int val);
270 * Set a long value for a property.
272 * Assign a long value to a property. If the property does not
273 * yet exist, it will be created and its type will be set to
274 * LONG; if it has a type of UNKNOWN, the type will also be set to
275 * LONG; otherwise, the bool value will be converted to the property's
278 * @param name The property name.
279 * @param val The new value for the property.
280 * @return true if the assignment succeeded, false otherwise.
282 bool SetLong (const string &name, long val);
286 * Set a float value for a property.
288 * Assign a float value to a property. If the property does not
289 * yet exist, it will be created and its type will be set to
290 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
291 * FLOAT; otherwise, the bool value will be converted to the property's
294 * @param name The property name.
295 * @param val The new value for the property.
296 * @return true if the assignment succeeded, false otherwise.
298 bool SetFloat (const string &name, float val);
302 * Set a double value for a property.
304 * Assign a double value to a property. If the property does not
305 * yet exist, it will be created and its type will be set to
306 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
307 * DOUBLE; otherwise, the double value will be converted to the property's
310 * @param name The property name.
311 * @param val The new value for the property.
312 * @return true if the assignment succeeded, false otherwise.
314 bool SetDouble (const string &name, double val);
318 * Set a string value for a property.
320 * Assign a string value to a property. If the property does not
321 * yet exist, it will be created and its type will be set to
322 * STRING; if it has a type of UNKNOWN, the type will also be set to
323 * STRING; otherwise, the string value will be converted to the property's
326 * @param name The property name.
327 * @param val The new value for the property.
328 * @return true if the assignment succeeded, false otherwise.
330 bool SetString (const string &name, const string &val);
333 ////////////////////////////////////////////////////////////////////////
334 // Convenience functions for setting property attributes.
335 ////////////////////////////////////////////////////////////////////////
339 * Set the state of the archive attribute for a property.
341 * If the archive attribute is true, the property will be written
342 * when a flight is saved; if it is false, the property will be
345 * A warning message will be printed if the property does not exist.
347 * @param name The property name.
348 * @param state The state of the archive attribute (defaults to true).
350 void SetArchivable (const string &name, bool state = true);
354 * Set the state of the read attribute for a property.
356 * If the read attribute is true, the property value will be readable;
357 * if it is false, the property value will always be the default value
360 * A warning message will be printed if the property does not exist.
362 * @param name The property name.
363 * @param state The state of the read attribute (defaults to true).
365 void SetReadable (const string &name, bool state = true);
369 * Set the state of the write attribute for a property.
371 * If the write attribute is true, the property value may be modified
372 * (depending on how it is tied); if the write attribute is false, the
373 * property value may not be modified.
375 * A warning message will be printed if the property does not exist.
377 * @param name The property name.
378 * @param state The state of the write attribute (defaults to true).
380 void SetWritable (const string &name, bool state = true);
383 ////////////////////////////////////////////////////////////////////////
384 // Convenience functions for tying properties, with logging.
385 ////////////////////////////////////////////////////////////////////////
389 * Untie a property from an external data source.
391 * Classes should use this function to release control of any
392 * properties they are managing.
394 void Untie (const string &name);
397 // Templates cause ambiguity here
400 * Tie a property to an external bool variable.
402 * The property's value will automatically mirror the variable's
403 * value, and vice-versa, until the property is untied.
405 * @param name The property name to tie (full path).
406 * @param pointer A pointer to the variable.
407 * @param useDefault true if any existing property value should be
408 * copied to the variable; false if the variable should not
409 * be modified; defaults to true.
412 Tie (const string &name, bool *pointer, bool useDefault = true);
416 * Tie a property to an external int variable.
418 * The property's value will automatically mirror the variable's
419 * value, and vice-versa, until the property is untied.
421 * @param name The property name to tie (full path).
422 * @param pointer A pointer to the variable.
423 * @param useDefault true if any existing property value should be
424 * copied to the variable; false if the variable should not
425 * be modified; defaults to true.
428 Tie (const string &name, int *pointer, bool useDefault = true);
432 * Tie a property to an external long variable.
434 * The property's value will automatically mirror the variable's
435 * value, and vice-versa, until the property is untied.
437 * @param name The property name to tie (full path).
438 * @param pointer A pointer to the variable.
439 * @param useDefault true if any existing property value should be
440 * copied to the variable; false if the variable should not
441 * be modified; defaults to true.
444 Tie (const string &name, long *pointer, bool useDefault = true);
448 * Tie a property to an external float variable.
450 * The property's value will automatically mirror the variable's
451 * value, and vice-versa, until the property is untied.
453 * @param name The property name to tie (full path).
454 * @param pointer A pointer to the variable.
455 * @param useDefault true if any existing property value should be
456 * copied to the variable; false if the variable should not
457 * be modified; defaults to true.
460 Tie (const string &name, float *pointer, bool useDefault = true);
463 * Tie a property to an external double variable.
465 * The property's value will automatically mirror the variable's
466 * value, and vice-versa, until the property is untied.
468 * @param name The property name to tie (full path).
469 * @param pointer A pointer to the variable.
470 * @param useDefault true if any existing property value should be
471 * copied to the variable; false if the variable should not
472 * be modified; defaults to true.
475 Tie (const string &name, double *pointer, bool useDefault = true);
477 //============================================================================
479 // All of the following functions *must* be inlined, otherwise linker
480 // errors will result
482 //============================================================================
484 /* template <class V> void
485 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0,
486 bool useDefault = true);
488 template <class V> void
489 Tie (const string &name, int index, V (*getter)(int),
490 void (*setter)(int, V) = 0, bool useDefault = true);
492 template <class T, class V> void
493 Tie (const string &name, T * obj, V (T::*getter)() const,
494 void (T::*setter)(V) = 0, bool useDefault = true);
496 template <class T, class V> void
497 Tie (const string &name, T * obj, int index,
498 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
499 bool useDefault = true); */
502 * Tie a property to a pair of simple functions.
504 * Every time the property value is queried, the getter (if any) will
505 * be invoked; every time the property value is modified, the setter
506 * (if any) will be invoked. The getter can be 0 to make the property
507 * unreadable, and the setter can be 0 to make the property
510 * @param name The property name to tie (full path).
511 * @param getter The getter function, or 0 if the value is unreadable.
512 * @param setter The setter function, or 0 if the value is unmodifiable.
513 * @param useDefault true if the setter should be invoked with any existing
514 * property value should be; false if the old value should be
515 * discarded; defaults to true.
518 template <class V> inline void
519 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0, bool useDefault = true)
521 if (!tie(name.c_str(), SGRawValueFunctions<V>(getter, setter), useDefault))
522 cout << "Failed to tie property " << name << " to functions" << endl;
523 else if (debug_lvl & 0x20)
524 cout << name << endl;
529 * Tie a property to a pair of indexed functions.
531 * Every time the property value is queried, the getter (if any) will
532 * be invoked with the index provided; every time the property value
533 * is modified, the setter (if any) will be invoked with the index
534 * provided. The getter can be 0 to make the property unreadable, and
535 * the setter can be 0 to make the property unmodifiable.
537 * @param name The property name to tie (full path).
538 * @param index The integer argument to pass to the getter and
540 * @param getter The getter function, or 0 if the value is unreadable.
541 * @param setter The setter function, or 0 if the value is unmodifiable.
542 * @param useDefault true if the setter should be invoked with any existing
543 * property value should there be one; false if the old value should be
544 * discarded; defaults to true.
546 template <class V> inline void Tie (const string &name, int index, V (*getter)(int),
547 void (*setter)(int, V) = 0, bool useDefault = true)
549 if (!tie(name.c_str(), SGRawValueFunctionsIndexed<V>(index, getter, setter), useDefault))
550 cout << "Failed to tie property " << name << " to indexed functions" << endl;
551 else if (debug_lvl & 0x20)
552 cout << name << endl;
557 * Tie a property to a pair of object methods.
559 * Every time the property value is queried, the getter (if any) will
560 * be invoked; every time the property value is modified, the setter
561 * (if any) will be invoked. The getter can be 0 to make the property
562 * unreadable, and the setter can be 0 to make the property
565 * @param name The property name to tie (full path).
566 * @param obj The object whose methods should be invoked.
567 * @param getter The object's getter method, or 0 if the value is
569 * @param setter The object's setter method, or 0 if the value is
571 * @param useDefault true if the setter should be invoked with any existing
572 * property value should there be one; false if the old value should be
573 * discarded; defaults to true.
575 template <class T, class V> inline void
576 Tie (const string &name, T * obj, V (T::*getter)() const,
577 void (T::*setter)(V) = 0, bool useDefault = true)
579 if (!tie(name.c_str(), SGRawValueMethods<T,V>(*obj, getter, setter), useDefault))
580 cout << "Failed to tie property " << name << " to object methods" << endl;
581 else if (debug_lvl & 0x20)
582 cout << name << endl;
586 * Tie a property to a pair of indexed object methods.
588 * Every time the property value is queried, the getter (if any) will
589 * be invoked with the index provided; every time the property value
590 * is modified, the setter (if any) will be invoked with the index
591 * provided. The getter can be 0 to make the property unreadable, and
592 * the setter can be 0 to make the property unmodifiable.
594 * @param name The property name to tie (full path).
595 * @param obj The object whose methods should be invoked.
596 * @param index The integer argument to pass to the getter and
598 * @param getter The getter method, or 0 if the value is unreadable.
599 * @param setter The setter method, or 0 if the value is unmodifiable.
600 * @param useDefault true if the setter should be invoked with any existing
601 * property value should be; false if the old value should be
602 * discarded; defaults to true.
604 template <class T, class V> inline void
605 Tie (const string &name, T * obj, int index, V (T::*getter)(int) const,
606 void (T::*setter)(int, V) = 0, bool useDefault = true)
608 if (!tie(name.c_str(), SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter), useDefault))
609 cout << "Failed to tie property " << name << " to indexed object methods" << endl;
610 else if (debug_lvl & 0x20)
611 cout << name << endl;
615 #endif // FGPROPERTYMANAGER_H