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$"
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 a bool value for a property.
135 * This method is convenient but inefficient. It should be used
136 * infrequently (i.e. for initializing, loading, saving, etc.),
137 * not in the main loop. If you need to get a value frequently,
138 * it is better to look up the node itself using GetNode and then
139 * use the node's getBoolValue() method, to avoid the lookup overhead.
141 * @param name The property name.
142 * @param defaultValue The default value to return if the property
144 * @return The property's value as a bool, or the default value provided.
146 bool GetBool (const std::string &name, bool defaultValue = false);
150 * Get an int value for a property.
152 * This method is convenient but inefficient. It should be used
153 * infrequently (i.e. for initializing, loading, saving, etc.),
154 * not in the main loop. If you need to get a value frequently,
155 * it is better to look up the node itself using GetNode and then
156 * use the node's getIntValue() method, to avoid the lookup overhead.
158 * @param name The property name.
159 * @param defaultValue The default value to return if the property
161 * @return The property's value as an int, or the default value provided.
163 int GetInt (const std::string &name, int defaultValue = 0);
167 * Get a long value for a property.
169 * This method is convenient but inefficient. It should be used
170 * infrequently (i.e. for initializing, loading, saving, etc.),
171 * not in the main loop. If you need to get a value frequently,
172 * it is better to look up the node itself using GetNode and then
173 * use the node's getLongValue() method, to avoid the lookup overhead.
175 * @param name The property name.
176 * @param defaultValue The default value to return if the property
178 * @return The property's value as a long, or the default value provided.
180 int GetLong (const std::string &name, long defaultValue = 0L);
184 * Get a float value for a property.
186 * This method is convenient but inefficient. It should be used
187 * infrequently (i.e. for initializing, loading, saving, etc.),
188 * not in the main loop. If you need to get a value frequently,
189 * it is better to look up the node itself using GetNode and then
190 * use the node's getFloatValue() method, to avoid the lookup overhead.
192 * @param name The property name.
193 * @param defaultValue The default value to return if the property
195 * @return The property's value as a float, or the default value provided.
197 float GetFloat (const std::string &name, float defaultValue = 0.0);
201 * Get a double value for a property.
203 * This method is convenient but inefficient. It should be used
204 * infrequently (i.e. for initializing, loading, saving, etc.),
205 * not in the main loop. If you need to get a value frequently,
206 * it is better to look up the node itself using GetNode and then
207 * use the node's getDoubleValue() method, to avoid the lookup overhead.
209 * @param name The property name.
210 * @param defaultValue The default value to return if the property
212 * @return The property's value as a double, or the default value provided.
214 double GetDouble (const std::string &name, double defaultValue = 0.0);
218 * Get a string value for a property.
220 * This method is convenient but inefficient. It should be used
221 * infrequently (i.e. for initializing, loading, saving, etc.),
222 * not in the main loop. If you need to get a value frequently,
223 * it is better to look up the node itself using GetNode and then
224 * use the node's getStringValue() method, to avoid the lookup overhead.
226 * @param name The property name.
227 * @param defaultValue The default value to return if the property
229 * @return The property's value as a string, or the default value provided.
231 std::string GetString (const std::string &name, std::string defaultValue = "");
235 * Set a bool value for a property.
237 * Assign a bool value to a property. If the property does not
238 * yet exist, it will be created and its type will be set to
239 * BOOL; if it has a type of UNKNOWN, the type will also be set to
240 * BOOL; otherwise, the bool value will be converted to the property's
243 * @param name The property name.
244 * @param val The new value for the property.
245 * @return true if the assignment succeeded, false otherwise.
247 bool SetBool (const std::string &name, bool val);
251 * Set an int value for a property.
253 * Assign an int value to a property. If the property does not
254 * yet exist, it will be created and its type will be set to
255 * INT; if it has a type of UNKNOWN, the type will also be set to
256 * INT; otherwise, the bool value will be converted to the property's
259 * @param name The property name.
260 * @param val The new value for the property.
261 * @return true if the assignment succeeded, false otherwise.
263 bool SetInt (const std::string &name, int val);
267 * Set a long value for a property.
269 * Assign a long value to a property. If the property does not
270 * yet exist, it will be created and its type will be set to
271 * LONG; if it has a type of UNKNOWN, the type will also be set to
272 * LONG; otherwise, the bool value will be converted to the property's
275 * @param name The property name.
276 * @param val The new value for the property.
277 * @return true if the assignment succeeded, false otherwise.
279 bool SetLong (const std::string &name, long val);
283 * Set a float value for a property.
285 * Assign a float value to a property. If the property does not
286 * yet exist, it will be created and its type will be set to
287 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
288 * FLOAT; otherwise, the bool value will be converted to the property's
291 * @param name The property name.
292 * @param val The new value for the property.
293 * @return true if the assignment succeeded, false otherwise.
295 bool SetFloat (const std::string &name, float val);
299 * Set a double value for a property.
301 * Assign a double value to a property. If the property does not
302 * yet exist, it will be created and its type will be set to
303 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
304 * DOUBLE; otherwise, the double value will be converted to the property's
307 * @param name The property name.
308 * @param val The new value for the property.
309 * @return true if the assignment succeeded, false otherwise.
311 bool SetDouble (const std::string &name, double val);
315 * Set a string value for a property.
317 * Assign a string value to a property. If the property does not
318 * yet exist, it will be created and its type will be set to
319 * STRING; if it has a type of UNKNOWN, the type will also be set to
320 * STRING; otherwise, the string value will be converted to the property's
323 * @param name The property name.
324 * @param val The new value for the property.
325 * @return true if the assignment succeeded, false otherwise.
327 bool SetString (const std::string &name, const std::string &val);
330 ////////////////////////////////////////////////////////////////////////
331 // Convenience functions for setting property attributes.
332 ////////////////////////////////////////////////////////////////////////
336 * Set the state of the archive attribute for a property.
338 * If the archive attribute is true, the property will be written
339 * when a flight is saved; if it is false, the property will be
342 * A warning message will be printed if the property does not exist.
344 * @param name The property name.
345 * @param state The state of the archive attribute (defaults to true).
347 void SetArchivable (const std::string &name, bool state = true);
351 * Set the state of the read attribute for a property.
353 * If the read attribute is true, the property value will be readable;
354 * if it is false, the property value will always be the default value
357 * A warning message will be printed if the property does not exist.
359 * @param name The property name.
360 * @param state The state of the read attribute (defaults to true).
362 void SetReadable (const std::string &name, bool state = true);
366 * Set the state of the write attribute for a property.
368 * If the write attribute is true, the property value may be modified
369 * (depending on how it is tied); if the write attribute is false, the
370 * property value may not be modified.
372 * A warning message will be printed if the property does not exist.
374 * @param name The property name.
375 * @param state The state of the write attribute (defaults to true).
377 void SetWritable (const std::string &name, bool state = true);
380 ////////////////////////////////////////////////////////////////////////
381 // Convenience functions for tying properties, with logging.
382 ////////////////////////////////////////////////////////////////////////
386 * Untie a property from an external data source.
388 * Classes should use this function to release control of any
389 * properties they are managing.
391 void Untie (const std::string &name);
394 // Templates cause ambiguity here
397 * Tie a property to an external bool variable.
399 * The property's value will automatically mirror the variable's
400 * value, and vice-versa, until the property is untied.
402 * @param name The property name to tie (full path).
403 * @param pointer A pointer to the variable.
404 * @param useDefault true if any existing property value should be
405 * copied to the variable; false if the variable should not
406 * be modified; defaults to true.
409 Tie (const std::string &name, bool *pointer, bool useDefault = true);
413 * Tie a property to an external int variable.
415 * The property's value will automatically mirror the variable's
416 * value, and vice-versa, until the property is untied.
418 * @param name The property name to tie (full path).
419 * @param pointer A pointer to the variable.
420 * @param useDefault true if any existing property value should be
421 * copied to the variable; false if the variable should not
422 * be modified; defaults to true.
425 Tie (const std::string &name, int *pointer, bool useDefault = true);
429 * Tie a property to an external long variable.
431 * The property's value will automatically mirror the variable's
432 * value, and vice-versa, until the property is untied.
434 * @param name The property name to tie (full path).
435 * @param pointer A pointer to the variable.
436 * @param useDefault true if any existing property value should be
437 * copied to the variable; false if the variable should not
438 * be modified; defaults to true.
441 Tie (const std::string &name, long *pointer, bool useDefault = true);
445 * Tie a property to an external float variable.
447 * The property's value will automatically mirror the variable's
448 * value, and vice-versa, until the property is untied.
450 * @param name The property name to tie (full path).
451 * @param pointer A pointer to the variable.
452 * @param useDefault true if any existing property value should be
453 * copied to the variable; false if the variable should not
454 * be modified; defaults to true.
457 Tie (const std::string &name, float *pointer, bool useDefault = true);
460 * Tie a property to an external double variable.
462 * The property's value will automatically mirror the variable's
463 * value, and vice-versa, until the property is untied.
465 * @param name The property name to tie (full path).
466 * @param pointer A pointer to the variable.
467 * @param useDefault true if any existing property value should be
468 * copied to the variable; false if the variable should not
469 * be modified; defaults to true.
472 Tie (const std::string &name, double *pointer, bool useDefault = true);
474 //============================================================================
476 // All of the following functions *must* be inlined, otherwise linker
477 // errors will result
479 //============================================================================
481 /* template <class V> void
482 Tie (const std::string &name, V (*getter)(), void (*setter)(V) = 0,
483 bool useDefault = true);
485 template <class V> void
486 Tie (const std::string &name, int index, V (*getter)(int),
487 void (*setter)(int, V) = 0, bool useDefault = true);
489 template <class T, class V> void
490 Tie (const std::string &name, T * obj, V (T::*getter)() const,
491 void (T::*setter)(V) = 0, bool useDefault = true);
493 template <class T, class V> void
494 Tie (const std::string &name, T * obj, int index,
495 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
496 bool useDefault = true); */
499 * Tie a property to a pair of simple functions.
501 * Every time the property value is queried, the getter (if any) will
502 * be invoked; every time the property value is modified, the setter
503 * (if any) will be invoked. The getter can be 0 to make the property
504 * unreadable, and the setter can be 0 to make the property
507 * @param name The property name to tie (full path).
508 * @param getter The getter function, or 0 if the value is unreadable.
509 * @param setter The setter function, or 0 if the value is unmodifiable.
510 * @param useDefault true if the setter should be invoked with any existing
511 * property value should be; false if the old value should be
512 * discarded; defaults to true.
515 template <class V> inline void
516 Tie (const std::string &name, V (*getter)(), void (*setter)(V) = 0, bool useDefault = true)
518 if (!tie(name.c_str(), SGRawValueFunctions<V>(getter, setter), useDefault))
519 std::cout << "Failed to tie property " << name << " to functions" << std::endl;
520 else if (debug_lvl & 0x20)
521 std::cout << name << std::endl;
526 * Tie a property to a pair of indexed functions.
528 * Every time the property value is queried, the getter (if any) will
529 * be invoked with the index provided; every time the property value
530 * is modified, the setter (if any) will be invoked with the index
531 * provided. The getter can be 0 to make the property unreadable, and
532 * the setter can be 0 to make the property unmodifiable.
534 * @param name The property name to tie (full path).
535 * @param index The integer argument to pass to the getter and
537 * @param getter The getter function, or 0 if the value is unreadable.
538 * @param setter The setter function, or 0 if the value is unmodifiable.
539 * @param useDefault true if the setter should be invoked with any existing
540 * property value should there be one; false if the old value should be
541 * discarded; defaults to true.
543 template <class V> inline void Tie (const std::string &name, int index, V (*getter)(int),
544 void (*setter)(int, V) = 0, bool useDefault = true)
546 if (!tie(name.c_str(), SGRawValueFunctionsIndexed<V>(index, getter, setter), useDefault))
547 std::cout << "Failed to tie property " << name << " to indexed functions" << std::endl;
548 else if (debug_lvl & 0x20)
549 std::cout << name << std::endl;
554 * Tie a property to a pair of object methods.
556 * Every time the property value is queried, the getter (if any) will
557 * be invoked; every time the property value is modified, the setter
558 * (if any) will be invoked. The getter can be 0 to make the property
559 * unreadable, and the setter can be 0 to make the property
562 * @param name The property name to tie (full path).
563 * @param obj The object whose methods should be invoked.
564 * @param getter The object's getter method, or 0 if the value is
566 * @param setter The object's setter method, or 0 if the value is
568 * @param useDefault true if the setter should be invoked with any existing
569 * property value should there be one; false if the old value should be
570 * discarded; defaults to true.
572 template <class T, class V> inline void
573 Tie (const std::string &name, T * obj, V (T::*getter)() const,
574 void (T::*setter)(V) = 0, bool useDefault = true)
576 if (!tie(name.c_str(), SGRawValueMethods<T,V>(*obj, getter, setter), useDefault))
577 std::cout << "Failed to tie property " << name << " to object methods" << std::endl;
578 else if (debug_lvl & 0x20)
579 std::cout << name << std::endl;
583 * Tie a property to a pair of indexed object methods.
585 * Every time the property value is queried, the getter (if any) will
586 * be invoked with the index provided; every time the property value
587 * is modified, the setter (if any) will be invoked with the index
588 * provided. The getter can be 0 to make the property unreadable, and
589 * the setter can be 0 to make the property unmodifiable.
591 * @param name The property name to tie (full path).
592 * @param obj The object whose methods should be invoked.
593 * @param index The integer argument to pass to the getter and
595 * @param getter The getter method, or 0 if the value is unreadable.
596 * @param setter The setter method, or 0 if the value is unmodifiable.
597 * @param useDefault true if the setter should be invoked with any existing
598 * property value should be; false if the old value should be
599 * discarded; defaults to true.
601 template <class T, class V> inline void
602 Tie (const std::string &name, T * obj, int index, V (T::*getter)(int) const,
603 void (T::*setter)(int, V) = 0, bool useDefault = true)
605 if (!tie(name.c_str(), SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter), useDefault))
606 std::cout << "Failed to tie property " << name << " to indexed object methods" << std::endl;
607 else if (debug_lvl & 0x20)
608 std::cout << name << std::endl;
612 #endif // FGPROPERTYMANAGER_H