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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 class FGPropertyManager : public SGPropertyNode {
78 FGPropertyManager(void) {}
80 ~FGPropertyManager(void) {}
82 /** Property-ify a name
83 * replaces spaces with '-' and, optionally, makes name all lower case
84 * @param name string to change
85 * @param lowercase true to change all upper case chars to lower
86 * NOTE: this function changes its argument and thus relies
89 string mkPropertyName(string name, bool lowercase);
92 * Get a property node.
94 * @param path The path of the node, relative to root.
95 * @param create true to create the node if it doesn't exist.
96 * @return The node, or 0 if none exists and none was created.
99 GetNode (const string &path, bool create = false);
102 GetNode (const string &relpath, int index, bool create = false);
105 * Test whether a given node exists.
107 * @param path The path of the node, relative to root.
108 * @return true if the node exists, false otherwise.
110 bool HasNode (const string &path);
113 * Get the name of a node
115 string GetName( void );
118 * Get the fully qualified name of a node
119 * This function is very slow, so is probably useful for debugging only.
121 string GetFullyQualifiedName(void);
124 * Get a bool value for a property.
126 * This method is convenient but inefficient. It should be used
127 * infrequently (i.e. for initializing, loading, saving, etc.),
128 * not in the main loop. If you need to get a value frequently,
129 * it is better to look up the node itself using GetNode and then
130 * use the node's getBoolValue() method, to avoid the lookup overhead.
132 * @param name The property name.
133 * @param defaultValue The default value to return if the property
135 * @return The property's value as a bool, or the default value provided.
137 bool GetBool (const string &name, bool defaultValue = false);
141 * Get an int value for a property.
143 * This method is convenient but inefficient. It should be used
144 * infrequently (i.e. for initializing, loading, saving, etc.),
145 * not in the main loop. If you need to get a value frequently,
146 * it is better to look up the node itself using GetNode and then
147 * use the node's getIntValue() method, to avoid the lookup overhead.
149 * @param name The property name.
150 * @param defaultValue The default value to return if the property
152 * @return The property's value as an int, or the default value provided.
154 int GetInt (const string &name, int defaultValue = 0);
158 * Get a long value for a property.
160 * This method is convenient but inefficient. It should be used
161 * infrequently (i.e. for initializing, loading, saving, etc.),
162 * not in the main loop. If you need to get a value frequently,
163 * it is better to look up the node itself using GetNode and then
164 * use the node's getLongValue() method, to avoid the lookup overhead.
166 * @param name The property name.
167 * @param defaultValue The default value to return if the property
169 * @return The property's value as a long, or the default value provided.
171 int GetLong (const string &name, long defaultValue = 0L);
175 * Get a float value for a property.
177 * This method is convenient but inefficient. It should be used
178 * infrequently (i.e. for initializing, loading, saving, etc.),
179 * not in the main loop. If you need to get a value frequently,
180 * it is better to look up the node itself using GetNode and then
181 * use the node's getFloatValue() method, to avoid the lookup overhead.
183 * @param name The property name.
184 * @param defaultValue The default value to return if the property
186 * @return The property's value as a float, or the default value provided.
188 float GetFloat (const string &name, float defaultValue = 0.0);
192 * Get a double value for a property.
194 * This method is convenient but inefficient. It should be used
195 * infrequently (i.e. for initializing, loading, saving, etc.),
196 * not in the main loop. If you need to get a value frequently,
197 * it is better to look up the node itself using GetNode and then
198 * use the node's getDoubleValue() method, to avoid the lookup overhead.
200 * @param name The property name.
201 * @param defaultValue The default value to return if the property
203 * @return The property's value as a double, or the default value provided.
205 double GetDouble (const string &name, double defaultValue = 0.0);
209 * Get a string value for a property.
211 * This method is convenient but inefficient. It should be used
212 * infrequently (i.e. for initializing, loading, saving, etc.),
213 * not in the main loop. If you need to get a value frequently,
214 * it is better to look up the node itself using GetNode and then
215 * use the node's getStringValue() method, to avoid the lookup overhead.
217 * @param name The property name.
218 * @param defaultValue The default value to return if the property
220 * @return The property's value as a string, or the default value provided.
222 string GetString (const string &name, string defaultValue = "");
226 * Set a bool value for a property.
228 * Assign a bool value to a property. If the property does not
229 * yet exist, it will be created and its type will be set to
230 * BOOL; if it has a type of UNKNOWN, the type will also be set to
231 * BOOL; otherwise, the bool value will be converted to the property's
234 * @param name The property name.
235 * @param val The new value for the property.
236 * @return true if the assignment succeeded, false otherwise.
238 bool SetBool (const string &name, bool val);
242 * Set an int value for a property.
244 * Assign an int value to a property. If the property does not
245 * yet exist, it will be created and its type will be set to
246 * INT; if it has a type of UNKNOWN, the type will also be set to
247 * INT; otherwise, the bool value will be converted to the property's
250 * @param name The property name.
251 * @param val The new value for the property.
252 * @return true if the assignment succeeded, false otherwise.
254 bool SetInt (const string &name, int val);
258 * Set a long value for a property.
260 * Assign a long value to a property. If the property does not
261 * yet exist, it will be created and its type will be set to
262 * LONG; if it has a type of UNKNOWN, the type will also be set to
263 * LONG; otherwise, the bool value will be converted to the property's
266 * @param name The property name.
267 * @param val The new value for the property.
268 * @return true if the assignment succeeded, false otherwise.
270 bool SetLong (const string &name, long val);
274 * Set a float value for a property.
276 * Assign a float value to a property. If the property does not
277 * yet exist, it will be created and its type will be set to
278 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
279 * FLOAT; otherwise, the bool value will be converted to the property's
282 * @param name The property name.
283 * @param val The new value for the property.
284 * @return true if the assignment succeeded, false otherwise.
286 bool SetFloat (const string &name, float val);
290 * Set a double value for a property.
292 * Assign a double value to a property. If the property does not
293 * yet exist, it will be created and its type will be set to
294 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
295 * DOUBLE; otherwise, the double value will be converted to the property's
298 * @param name The property name.
299 * @param val The new value for the property.
300 * @return true if the assignment succeeded, false otherwise.
302 bool SetDouble (const string &name, double val);
306 * Set a string value for a property.
308 * Assign a string value to a property. If the property does not
309 * yet exist, it will be created and its type will be set to
310 * STRING; if it has a type of UNKNOWN, the type will also be set to
311 * STRING; otherwise, the string value will be converted to the property's
314 * @param name The property name.
315 * @param val The new value for the property.
316 * @return true if the assignment succeeded, false otherwise.
318 bool SetString (const string &name, const string &val);
321 ////////////////////////////////////////////////////////////////////////
322 // Convenience functions for setting property attributes.
323 ////////////////////////////////////////////////////////////////////////
327 * Set the state of the archive attribute for a property.
329 * If the archive attribute is true, the property will be written
330 * when a flight is saved; if it is false, the property will be
333 * A warning message will be printed if the property does not exist.
335 * @param name The property name.
336 * @param state The state of the archive attribute (defaults to true).
338 void SetArchivable (const string &name, bool state = true);
342 * Set the state of the read attribute for a property.
344 * If the read attribute is true, the property value will be readable;
345 * if it is false, the property value will always be the default value
348 * A warning message will be printed if the property does not exist.
350 * @param name The property name.
351 * @param state The state of the read attribute (defaults to true).
353 void SetReadable (const string &name, bool state = true);
357 * Set the state of the write attribute for a property.
359 * If the write attribute is true, the property value may be modified
360 * (depending on how it is tied); if the write attribute is false, the
361 * property value may not be modified.
363 * A warning message will be printed if the property does not exist.
365 * @param name The property name.
366 * @param state The state of the write attribute (defaults to true).
368 void SetWritable (const string &name, bool state = true);
371 ////////////////////////////////////////////////////////////////////////
372 // Convenience functions for tying properties, with logging.
373 ////////////////////////////////////////////////////////////////////////
377 * Untie a property from an external data source.
379 * Classes should use this function to release control of any
380 * properties they are managing.
382 void Untie (const string &name);
385 // Templates cause ambiguity here
388 * Tie a property to an external bool variable.
390 * The property's value will automatically mirror the variable's
391 * value, and vice-versa, until the property is untied.
393 * @param name The property name to tie (full path).
394 * @param pointer A pointer to the variable.
395 * @param useDefault true if any existing property value should be
396 * copied to the variable; false if the variable should not
397 * be modified; defaults to true.
400 Tie (const string &name, bool *pointer, bool useDefault = true);
404 * Tie a property to an external int variable.
406 * The property's value will automatically mirror the variable's
407 * value, and vice-versa, until the property is untied.
409 * @param name The property name to tie (full path).
410 * @param pointer A pointer to the variable.
411 * @param useDefault true if any existing property value should be
412 * copied to the variable; false if the variable should not
413 * be modified; defaults to true.
416 Tie (const string &name, int *pointer, bool useDefault = true);
420 * Tie a property to an external long variable.
422 * The property's value will automatically mirror the variable's
423 * value, and vice-versa, until the property is untied.
425 * @param name The property name to tie (full path).
426 * @param pointer A pointer to the variable.
427 * @param useDefault true if any existing property value should be
428 * copied to the variable; false if the variable should not
429 * be modified; defaults to true.
432 Tie (const string &name, long *pointer, bool useDefault = true);
436 * Tie a property to an external float variable.
438 * The property's value will automatically mirror the variable's
439 * value, and vice-versa, until the property is untied.
441 * @param name The property name to tie (full path).
442 * @param pointer A pointer to the variable.
443 * @param useDefault true if any existing property value should be
444 * copied to the variable; false if the variable should not
445 * be modified; defaults to true.
448 Tie (const string &name, float *pointer, bool useDefault = true);
451 * Tie a property to an external double variable.
453 * The property's value will automatically mirror the variable's
454 * value, and vice-versa, until the property is untied.
456 * @param name The property name to tie (full path).
457 * @param pointer A pointer to the variable.
458 * @param useDefault true if any existing property value should be
459 * copied to the variable; false if the variable should not
460 * be modified; defaults to true.
463 Tie (const string &name, double *pointer, bool useDefault = true);
465 //============================================================================
467 // All of the following functions *must* be inlined, otherwise linker
468 // errors will result
470 //============================================================================
472 /* template <class V> void
473 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0,
474 bool useDefault = true);
476 template <class V> void
477 Tie (const string &name, int index, V (*getter)(int),
478 void (*setter)(int, V) = 0, bool useDefault = true);
480 template <class T, class V> void
481 Tie (const string &name, T * obj, V (T::*getter)() const,
482 void (T::*setter)(V) = 0, bool useDefault = true);
484 template <class T, class V> void
485 Tie (const string &name, T * obj, int index,
486 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
487 bool useDefault = true); */
490 * Tie a property to a pair of simple functions.
492 * Every time the property value is queried, the getter (if any) will
493 * be invoked; every time the property value is modified, the setter
494 * (if any) will be invoked. The getter can be 0 to make the property
495 * unreadable, and the setter can be 0 to make the property
498 * @param name The property name to tie (full path).
499 * @param getter The getter function, or 0 if the value is unreadable.
500 * @param setter The setter function, or 0 if the value is unmodifiable.
501 * @param useDefault true if the setter should be invoked with any existing
502 * property value should be; false if the old value should be
503 * discarded; defaults to true.
506 template <class V> inline void
507 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0,
508 bool useDefault = true)
510 if (!tie(name.c_str(), SGRawValueFunctions<V>(getter, setter),
513 "Failed to tie property " << name << " to functions" << endl;
518 * Tie a property to a pair of indexed functions.
520 * Every time the property value is queried, the getter (if any) will
521 * be invoked with the index provided; every time the property value
522 * is modified, the setter (if any) will be invoked with the index
523 * provided. The getter can be 0 to make the property unreadable, and
524 * the setter can be 0 to make the property unmodifiable.
526 * @param name The property name to tie (full path).
527 * @param index The integer argument to pass to the getter and
529 * @param getter The getter function, or 0 if the value is unreadable.
530 * @param setter The setter function, or 0 if the value is unmodifiable.
531 * @param useDefault true if the setter should be invoked with any existing
532 * property value should be; false if the old value should be
533 * discarded; defaults to true.
535 template <class V> inline void Tie (const string &name,
536 int index, V (*getter)(int),
537 void (*setter)(int, V) = 0, bool useDefault = true)
539 if (!tie(name.c_str(),
540 SGRawValueFunctionsIndexed<V>(index,
545 "Failed to tie property " << name << " to indexed functions" << endl;
550 * Tie a property to a pair of object methods.
552 * Every time the property value is queried, the getter (if any) will
553 * be invoked; every time the property value is modified, the setter
554 * (if any) will be invoked. The getter can be 0 to make the property
555 * unreadable, and the setter can be 0 to make the property
558 * @param name The property name to tie (full path).
559 * @param obj The object whose methods should be invoked.
560 * @param getter The object's getter method, or 0 if the value is
562 * @param setter The object's setter method, or 0 if the value is
564 * @param useDefault true if the setter should be invoked with any existing
565 * property value should be; false if the old value should be
566 * discarded; defaults to true.
568 template <class T, class V> inline void
569 Tie (const string &name, T * obj, V (T::*getter)() const,
570 void (T::*setter)(V) = 0, bool useDefault = true)
572 if (!tie(name.c_str(),
573 SGRawValueMethods<T,V>(*obj, getter, setter),
576 "Failed to tie property " << name << " to object methods" << endl;
580 * Tie a property to a pair of indexed object methods.
582 * Every time the property value is queried, the getter (if any) will
583 * be invoked with the index provided; every time the property value
584 * is modified, the setter (if any) will be invoked with the index
585 * provided. The getter can be 0 to make the property unreadable, and
586 * the setter can be 0 to make the property unmodifiable.
588 * @param name The property name to tie (full path).
589 * @param obj The object whose methods should be invoked.
590 * @param index The integer argument to pass to the getter and
592 * @param getter The getter method, or 0 if the value is unreadable.
593 * @param setter The setter method, or 0 if the value is unmodifiable.
594 * @param useDefault true if the setter should be invoked with any existing
595 * property value should be; false if the old value should be
596 * discarded; defaults to true.
598 template <class T, class V> inline void
599 Tie (const string &name, T * obj, int index,
600 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
601 bool useDefault = true)
603 if (!tie(name.c_str(),
604 SGRawValueMethodsIndexed<T,V>(*obj,
610 "Failed to tie property " << name << " to indexed object methods" << endl;
614 #endif // FGPROPERTYMANAGER_H