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 )
45 #include "simgear/props/props.hxx"
47 # include "simgear/math/SGMath.hxx"
50 #include "FGJSBBase.h"
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 #define ID_PROPERTYMANAGER "$Id: FGPropertyManager.h,v 1.17 2010/07/08 11:36:28 jberndt Exp $"
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 /** Class wrapper for property handling.
69 @author David Megginson, Tony Peden
72 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76 class FGPropertyManager : public SGPropertyNode, public FGJSBBase
79 static bool suppress_warning;
82 FGPropertyManager(void) {suppress_warning = false;}
84 virtual ~FGPropertyManager(void) {}
86 /** Property-ify a name
87 * replaces spaces with '-' and, optionally, makes name all lower case
88 * @param name string to change
89 * @param lowercase true to change all upper case chars to lower
90 * NOTE: this function changes its argument and thus relies
93 std::string mkPropertyName(std::string name, bool lowercase);
96 * Get a property node.
98 * @param path The path of the node, relative to root.
99 * @param create true to create the node if it doesn't exist.
100 * @return The node, or 0 if none exists and none was created.
103 GetNode (const std::string &path, bool create = false);
106 GetNode (const std::string &relpath, int index, bool create = false);
109 * Test whether a given node exists.
111 * @param path The path of the node, relative to root.
112 * @return true if the node exists, false otherwise.
114 bool HasNode (const std::string &path);
117 * Get the name of a node
119 std::string GetName( void );
122 * Get the name of a node without underscores, etc.
124 std::string GetPrintableName( void );
127 * Get the fully qualified name of a node
128 * This function is very slow, so is probably useful for debugging only.
130 std::string GetFullyQualifiedName(void);
133 * Get the qualified name of a node relative to given base path,
134 * otherwise the fully qualified name.
135 * This function is very slow, so is probably useful for debugging only.
137 * @param path The path to strip off, if found.
139 std::string GetRelativeName( const std::string &path = "/fdm/jsbsim/" );
142 * Get a bool value for a property.
144 * This method is convenient but inefficient. It should be used
145 * infrequently (i.e. for initializing, loading, saving, etc.),
146 * not in the main loop. If you need to get a value frequently,
147 * it is better to look up the node itself using GetNode and then
148 * use the node's getBoolValue() method, to avoid the lookup overhead.
150 * @param name The property name.
151 * @param defaultValue The default value to return if the property
153 * @return The property's value as a bool, or the default value provided.
155 bool GetBool (const std::string &name, bool defaultValue = false);
159 * Get an int value for a property.
161 * This method is convenient but inefficient. It should be used
162 * infrequently (i.e. for initializing, loading, saving, etc.),
163 * not in the main loop. If you need to get a value frequently,
164 * it is better to look up the node itself using GetNode and then
165 * use the node's getIntValue() method, to avoid the lookup overhead.
167 * @param name The property name.
168 * @param defaultValue The default value to return if the property
170 * @return The property's value as an int, or the default value provided.
172 int GetInt (const std::string &name, int defaultValue = 0);
176 * Get a long value for a property.
178 * This method is convenient but inefficient. It should be used
179 * infrequently (i.e. for initializing, loading, saving, etc.),
180 * not in the main loop. If you need to get a value frequently,
181 * it is better to look up the node itself using GetNode and then
182 * use the node's getLongValue() method, to avoid the lookup overhead.
184 * @param name The property name.
185 * @param defaultValue The default value to return if the property
187 * @return The property's value as a long, or the default value provided.
189 int GetLong (const std::string &name, long defaultValue = 0L);
193 * Get a float value for a property.
195 * This method is convenient but inefficient. It should be used
196 * infrequently (i.e. for initializing, loading, saving, etc.),
197 * not in the main loop. If you need to get a value frequently,
198 * it is better to look up the node itself using GetNode and then
199 * use the node's getFloatValue() method, to avoid the lookup overhead.
201 * @param name The property name.
202 * @param defaultValue The default value to return if the property
204 * @return The property's value as a float, or the default value provided.
206 float GetFloat (const std::string &name, float defaultValue = 0.0);
210 * Get a double value for a property.
212 * This method is convenient but inefficient. It should be used
213 * infrequently (i.e. for initializing, loading, saving, etc.),
214 * not in the main loop. If you need to get a value frequently,
215 * it is better to look up the node itself using GetNode and then
216 * use the node's getDoubleValue() method, to avoid the lookup overhead.
218 * @param name The property name.
219 * @param defaultValue The default value to return if the property
221 * @return The property's value as a double, or the default value provided.
223 double GetDouble (const std::string &name, double defaultValue = 0.0);
227 * Get a string value for a property.
229 * This method is convenient but inefficient. It should be used
230 * infrequently (i.e. for initializing, loading, saving, etc.),
231 * not in the main loop. If you need to get a value frequently,
232 * it is better to look up the node itself using GetNode and then
233 * use the node's getStringValue() method, to avoid the lookup overhead.
235 * @param name The property name.
236 * @param defaultValue The default value to return if the property
238 * @return The property's value as a string, or the default value provided.
240 std::string GetString (const std::string &name, std::string defaultValue = "");
244 * Set a bool value for a property.
246 * Assign a bool value to a property. If the property does not
247 * yet exist, it will be created and its type will be set to
248 * BOOL; if it has a type of UNKNOWN, the type will also be set to
249 * BOOL; otherwise, the bool value will be converted to the property's
252 * @param name The property name.
253 * @param val The new value for the property.
254 * @return true if the assignment succeeded, false otherwise.
256 bool SetBool (const std::string &name, bool val);
260 * Set an int value for a property.
262 * Assign an int value to a property. If the property does not
263 * yet exist, it will be created and its type will be set to
264 * INT; if it has a type of UNKNOWN, the type will also be set to
265 * INT; otherwise, the bool value will be converted to the property's
268 * @param name The property name.
269 * @param val The new value for the property.
270 * @return true if the assignment succeeded, false otherwise.
272 bool SetInt (const std::string &name, int val);
276 * Set a long value for a property.
278 * Assign a long value to a property. If the property does not
279 * yet exist, it will be created and its type will be set to
280 * LONG; if it has a type of UNKNOWN, the type will also be set to
281 * LONG; otherwise, the bool value will be converted to the property's
284 * @param name The property name.
285 * @param val The new value for the property.
286 * @return true if the assignment succeeded, false otherwise.
288 bool SetLong (const std::string &name, long val);
292 * Set a float value for a property.
294 * Assign a float value to a property. If the property does not
295 * yet exist, it will be created and its type will be set to
296 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
297 * FLOAT; otherwise, the bool value will be converted to the property's
300 * @param name The property name.
301 * @param val The new value for the property.
302 * @return true if the assignment succeeded, false otherwise.
304 bool SetFloat (const std::string &name, float val);
308 * Set a double value for a property.
310 * Assign a double value to a property. If the property does not
311 * yet exist, it will be created and its type will be set to
312 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
313 * DOUBLE; otherwise, the double value will be converted to the property's
316 * @param name The property name.
317 * @param val The new value for the property.
318 * @return true if the assignment succeeded, false otherwise.
320 bool SetDouble (const std::string &name, double val);
324 * Set a string value for a property.
326 * Assign a string value to a property. If the property does not
327 * yet exist, it will be created and its type will be set to
328 * STRING; if it has a type of UNKNOWN, the type will also be set to
329 * STRING; otherwise, the string value will be converted to the property's
332 * @param name The property name.
333 * @param val The new value for the property.
334 * @return true if the assignment succeeded, false otherwise.
336 bool SetString (const std::string &name, const std::string &val);
339 ////////////////////////////////////////////////////////////////////////
340 // Convenience functions for setting property attributes.
341 ////////////////////////////////////////////////////////////////////////
345 * Set the state of the archive attribute for a property.
347 * If the archive attribute is true, the property will be written
348 * when a flight is saved; if it is false, the property will be
351 * A warning message will be printed if the property does not exist.
353 * @param name The property name.
354 * @param state The state of the archive attribute (defaults to true).
356 void SetArchivable (const std::string &name, bool state = true);
360 * Set the state of the read attribute for a property.
362 * If the read attribute is true, the property value will be readable;
363 * if it is false, the property value will always be the default value
366 * A warning message will be printed if the property does not exist.
368 * @param name The property name.
369 * @param state The state of the read attribute (defaults to true).
371 void SetReadable (const std::string &name, bool state = true);
375 * Set the state of the write attribute for a property.
377 * If the write attribute is true, the property value may be modified
378 * (depending on how it is tied); if the write attribute is false, the
379 * property value may not be modified.
381 * A warning message will be printed if the property does not exist.
383 * @param name The property name.
384 * @param state The state of the write attribute (defaults to true).
386 void SetWritable (const std::string &name, bool state = true);
389 ////////////////////////////////////////////////////////////////////////
390 // Convenience functions for tying properties, with logging.
391 ////////////////////////////////////////////////////////////////////////
395 * Untie a property from an external data source.
397 * Classes should use this function to release control of any
398 * properties they are managing.
400 void Untie (const std::string &name);
403 // Templates cause ambiguity here
406 * Tie a property to an external bool variable.
408 * The property's value will automatically mirror the variable's
409 * value, and vice-versa, until the property is untied.
411 * @param name The property name to tie (full path).
412 * @param pointer A pointer to the variable.
413 * @param useDefault true if any existing property value should be
414 * copied to the variable; false if the variable should not
415 * be modified; defaults to true.
418 Tie (const std::string &name, bool *pointer, bool useDefault = true);
422 * Tie a property to an external int variable.
424 * The property's value will automatically mirror the variable's
425 * value, and vice-versa, until the property is untied.
427 * @param name The property name to tie (full path).
428 * @param pointer A pointer to the variable.
429 * @param useDefault true if any existing property value should be
430 * copied to the variable; false if the variable should not
431 * be modified; defaults to true.
434 Tie (const std::string &name, int *pointer, bool useDefault = true);
438 * Tie a property to an external long variable.
440 * The property's value will automatically mirror the variable's
441 * value, and vice-versa, until the property is untied.
443 * @param name The property name to tie (full path).
444 * @param pointer A pointer to the variable.
445 * @param useDefault true if any existing property value should be
446 * copied to the variable; false if the variable should not
447 * be modified; defaults to true.
450 Tie (const std::string &name, long *pointer, bool useDefault = true);
454 * Tie a property to an external float variable.
456 * The property's value will automatically mirror the variable's
457 * value, and vice-versa, until the property is untied.
459 * @param name The property name to tie (full path).
460 * @param pointer A pointer to the variable.
461 * @param useDefault true if any existing property value should be
462 * copied to the variable; false if the variable should not
463 * be modified; defaults to true.
466 Tie (const std::string &name, float *pointer, bool useDefault = true);
469 * Tie a property to an external double variable.
471 * The property's value will automatically mirror the variable's
472 * value, and vice-versa, until the property is untied.
474 * @param name The property name to tie (full path).
475 * @param pointer A pointer to the variable.
476 * @param useDefault true if any existing property value should be
477 * copied to the variable; false if the variable should not
478 * be modified; defaults to true.
481 Tie (const std::string &name, double *pointer, bool useDefault = true);
483 //============================================================================
485 // All of the following functions *must* be inlined, otherwise linker
486 // errors will result
488 //============================================================================
490 /* template <class V> void
491 Tie (const std::string &name, V (*getter)(), void (*setter)(V) = 0,
492 bool useDefault = true);
494 template <class V> void
495 Tie (const std::string &name, int index, V (*getter)(int),
496 void (*setter)(int, V) = 0, bool useDefault = true);
498 template <class T, class V> void
499 Tie (const std::string &name, T * obj, V (T::*getter)() const,
500 void (T::*setter)(V) = 0, bool useDefault = true);
502 template <class T, class V> void
503 Tie (const std::string &name, T * obj, int index,
504 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
505 bool useDefault = true); */
508 * Tie a property to a pair of simple functions.
510 * Every time the property value is queried, the getter (if any) will
511 * be invoked; every time the property value is modified, the setter
512 * (if any) will be invoked. The getter can be 0 to make the property
513 * unreadable, and the setter can be 0 to make the property
516 * @param name The property name to tie (full path).
517 * @param getter The getter function, or 0 if the value is unreadable.
518 * @param setter The setter function, or 0 if the value is unmodifiable.
519 * @param useDefault true if the setter should be invoked with any existing
520 * property value should be; false if the old value should be
521 * discarded; defaults to true.
524 template <class V> inline void
525 Tie (const std::string &name, V (*getter)(), void (*setter)(V) = 0, bool useDefault = true)
527 if (!tie(name.c_str(), SGRawValueFunctions<V>(getter, setter), useDefault))
528 std::cout << "Failed to tie property " << name << " to functions" << std::endl;
529 else if (debug_lvl & 0x20)
530 std::cout << name << std::endl;
535 * Tie a property to a pair of indexed functions.
537 * Every time the property value is queried, the getter (if any) will
538 * be invoked with the index provided; every time the property value
539 * is modified, the setter (if any) will be invoked with the index
540 * provided. The getter can be 0 to make the property unreadable, and
541 * the setter can be 0 to make the property unmodifiable.
543 * @param name The property name to tie (full path).
544 * @param index The integer argument to pass to the getter and
546 * @param getter The getter function, or 0 if the value is unreadable.
547 * @param setter The setter function, or 0 if the value is unmodifiable.
548 * @param useDefault true if the setter should be invoked with any existing
549 * property value should there be one; false if the old value should be
550 * discarded; defaults to true.
552 template <class V> inline void Tie (const std::string &name, int index, V (*getter)(int),
553 void (*setter)(int, V) = 0, bool useDefault = true)
555 if (!tie(name.c_str(), SGRawValueFunctionsIndexed<V>(index, getter, setter), useDefault))
556 std::cout << "Failed to tie property " << name << " to indexed functions" << std::endl;
557 else if (debug_lvl & 0x20)
558 std::cout << name << std::endl;
563 * Tie a property to a pair of object methods.
565 * Every time the property value is queried, the getter (if any) will
566 * be invoked; every time the property value is modified, the setter
567 * (if any) will be invoked. The getter can be 0 to make the property
568 * unreadable, and the setter can be 0 to make the property
571 * @param name The property name to tie (full path).
572 * @param obj The object whose methods should be invoked.
573 * @param getter The object's getter method, or 0 if the value is
575 * @param setter The object's setter method, or 0 if the value is
577 * @param useDefault true if the setter should be invoked with any existing
578 * property value should there be one; false if the old value should be
579 * discarded; defaults to true.
581 template <class T, class V> inline void
582 Tie (const std::string &name, T * obj, V (T::*getter)() const,
583 void (T::*setter)(V) = 0, bool useDefault = true)
585 if (!tie(name.c_str(), SGRawValueMethods<T,V>(*obj, getter, setter), useDefault))
586 std::cout << "Failed to tie property " << name << " to object methods" << std::endl;
587 else if (debug_lvl & 0x20)
588 std::cout << name << std::endl;
592 * Tie a property to a pair of indexed object methods.
594 * Every time the property value is queried, the getter (if any) will
595 * be invoked with the index provided; every time the property value
596 * is modified, the setter (if any) will be invoked with the index
597 * provided. The getter can be 0 to make the property unreadable, and
598 * the setter can be 0 to make the property unmodifiable.
600 * @param name The property name to tie (full path).
601 * @param obj The object whose methods should be invoked.
602 * @param index The integer argument to pass to the getter and
604 * @param getter The getter method, or 0 if the value is unreadable.
605 * @param setter The setter method, or 0 if the value is unmodifiable.
606 * @param useDefault true if the setter should be invoked with any existing
607 * property value should be; false if the old value should be
608 * discarded; defaults to true.
610 template <class T, class V> inline void
611 Tie (const std::string &name, T * obj, int index, V (T::*getter)(int) const,
612 void (T::*setter)(int, V) = 0, bool useDefault = true)
614 if (!tie(name.c_str(), SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter), useDefault))
615 std::cout << "Failed to tie property " << name << " to indexed object methods" << std::endl;
616 else if (debug_lvl & 0x20)
617 std::cout << name << std::endl;
621 #endif // FGPROPERTYMANAGER_H