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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38 #include <simgear/misc/props.hxx>
40 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 #define ID_PROPERTYMANAGER "$Id$"
46 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
60 /** Class wrapper for property handling.
61 @author David Megginson, Tony Peden
62 @see <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jsbsim/JSBSim/FGPropertyManager.h?rev=HEAD&content-type=text/vnd.viewcvs-markup">
64 @see <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jsbsim/JSBSim/FGPropertyManager.cpp?rev=HEAD&content-type=text/vnd.viewcvs-markup">
68 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
72 class FGPropertyManager:public SGPropertyNode {
75 FGPropertyManager(void) {
79 ~FGPropertyManager(void) {
83 * Get a property node.
85 * @param path The path of the node, relative to root.
86 * @param create true to create the node if it doesn't exist.
87 * @return The node, or 0 if none exists and none was created.
89 inline FGPropertyManager*
90 GetNode (const string &path, bool create = false)
92 SGPropertyNode* node=this->getNode(path.c_str(), create);
94 cout << "FGPropertyManager::GetNode() No node found for "
96 return (FGPropertyManager*)node;
99 inline FGPropertyManager*
100 GetNode (const string &relpath, int index, bool create = false)
102 return (FGPropertyManager*)getNode(relpath.c_str(),index,create);
107 * Test whether a given node exists.
109 * @param path The path of the node, relative to root.
110 * @return true if the node exists, false otherwise.
113 HasNode (const string &path)
115 return (GetNode(path, false) != 0);
119 * Get the name of a node
123 return string( getName() );
127 * Get a bool value for a property.
129 * This method is convenient but inefficient. It should be used
130 * infrequently (i.e. for initializing, loading, saving, etc.),
131 * not in the main loop. If you need to get a value frequently,
132 * it is better to look up the node itself using GetNode and then
133 * use the node's getBoolValue() method, to avoid the lookup overhead.
135 * @param name The property name.
136 * @param defaultValue The default value to return if the property
138 * @return The property's value as a bool, or the default value provided.
140 inline bool GetBool (const string &name, bool defaultValue = false)
142 return getBoolValue(name.c_str(), defaultValue);
147 * Get an int value for a property.
149 * This method is convenient but inefficient. It should be used
150 * infrequently (i.e. for initializing, loading, saving, etc.),
151 * not in the main loop. If you need to get a value frequently,
152 * it is better to look up the node itself using GetNode and then
153 * use the node's getIntValue() method, to avoid the lookup overhead.
155 * @param name The property name.
156 * @param defaultValue The default value to return if the property
158 * @return The property's value as an int, or the default value provided.
160 inline int GetInt (const string &name, int defaultValue = 0)
162 return getIntValue(name.c_str(), defaultValue);
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 inline int GetLong (const string &name, long defaultValue = 0L)
182 return getLongValue(name.c_str(), defaultValue);
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 inline float GetFloat (const string &name, float defaultValue = 0.0)
202 return getFloatValue(name.c_str(), defaultValue);
207 * Get a double 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 getDoubleValue() 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 double, or the default value provided.
220 inline double GetDouble (const string &name, double defaultValue = 0.0)
222 return getDoubleValue(name.c_str(), defaultValue);
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 inline string GetString (const string &name, string defaultValue = "")
242 return string(getStringValue(name.c_str(), defaultValue.c_str()));
247 * Set a bool value for a property.
249 * Assign a bool value to a property. If the property does not
250 * yet exist, it will be created and its type will be set to
251 * BOOL; if it has a type of UNKNOWN, the type will also be set to
252 * BOOL; otherwise, the bool value will be converted to the property's
255 * @param name The property name.
256 * @param val The new value for the property.
257 * @return true if the assignment succeeded, false otherwise.
259 inline bool SetBool (const string &name, bool val)
261 return setBoolValue(name.c_str(), val);
266 * Set an int value for a property.
268 * Assign an int value to a property. If the property does not
269 * yet exist, it will be created and its type will be set to
270 * INT; if it has a type of UNKNOWN, the type will also be set to
271 * INT; otherwise, the bool value will be converted to the property's
274 * @param name The property name.
275 * @param val The new value for the property.
276 * @return true if the assignment succeeded, false otherwise.
278 inline bool SetInt (const string &name, int val)
280 return setIntValue(name.c_str(), val);
285 * Set a long value for a property.
287 * Assign a long value to a property. If the property does not
288 * yet exist, it will be created and its type will be set to
289 * LONG; if it has a type of UNKNOWN, the type will also be set to
290 * LONG; otherwise, the bool value will be converted to the property's
293 * @param name The property name.
294 * @param val The new value for the property.
295 * @return true if the assignment succeeded, false otherwise.
297 inline bool SetLong (const string &name, long val)
299 return setLongValue(name.c_str(), val);
304 * Set a float value for a property.
306 * Assign a float value to a property. If the property does not
307 * yet exist, it will be created and its type will be set to
308 * FLOAT; if it has a type of UNKNOWN, the type will also be set to
309 * FLOAT; otherwise, the bool 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 inline bool SetFloat (const string &name, float val)
318 return setFloatValue(name.c_str(), val);
323 * Set a double value for a property.
325 * Assign a double value to a property. If the property does not
326 * yet exist, it will be created and its type will be set to
327 * DOUBLE; if it has a type of UNKNOWN, the type will also be set to
328 * DOUBLE; otherwise, the double value will be converted to the property's
331 * @param name The property name.
332 * @param val The new value for the property.
333 * @return true if the assignment succeeded, false otherwise.
335 inline bool SetDouble (const string &name, double val)
337 return setDoubleValue(name.c_str(), val);
342 * Set a string value for a property.
344 * Assign a string value to a property. If the property does not
345 * yet exist, it will be created and its type will be set to
346 * STRING; if it has a type of UNKNOWN, the type will also be set to
347 * STRING; otherwise, the string value will be converted to the property's
350 * @param name The property name.
351 * @param val The new value for the property.
352 * @return true if the assignment succeeded, false otherwise.
354 inline bool SetString (const string &name, const string &val)
356 return setStringValue(name.c_str(), val.c_str());
360 ////////////////////////////////////////////////////////////////////////
361 // Convenience functions for setting property attributes.
362 ////////////////////////////////////////////////////////////////////////
366 * Set the state of the archive attribute for a property.
368 * If the archive attribute is true, the property will be written
369 * when a flight is saved; if it is false, the property will be
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 archive attribute (defaults to true).
378 SetArchivable (const string &name, bool state = true)
380 SGPropertyNode * node = getNode(name.c_str());
383 "Attempt to set archive flag for non-existant property "
386 node->setAttribute(SGPropertyNode::ARCHIVE, state);
391 * Set the state of the read attribute for a property.
393 * If the read attribute is true, the property value will be readable;
394 * if it is false, the property value will always be the default value
397 * A warning message will be printed if the property does not exist.
399 * @param name The property name.
400 * @param state The state of the read attribute (defaults to true).
403 SetReadable (const string &name, bool state = true)
405 SGPropertyNode * node = getNode(name.c_str());
408 "Attempt to set read flag for non-existant property "
411 node->setAttribute(SGPropertyNode::READ, state);
416 * Set the state of the write attribute for a property.
418 * If the write attribute is true, the property value may be modified
419 * (depending on how it is tied); if the write attribute is false, the
420 * property value may not be modified.
422 * A warning message will be printed if the property does not exist.
424 * @param name The property name.
425 * @param state The state of the write attribute (defaults to true).
428 SetWritable (const string &name, bool state = true)
430 SGPropertyNode * node = getNode(name.c_str());
433 "Attempt to set write flag for non-existant property "
436 node->setAttribute(SGPropertyNode::WRITE, state);
440 ////////////////////////////////////////////////////////////////////////
441 // Convenience functions for tying properties, with logging.
442 ////////////////////////////////////////////////////////////////////////
446 * Untie a property from an external data source.
448 * Classes should use this function to release control of any
449 * properties they are managing.
452 Untie (const string &name)
454 if (!untie(name.c_str()))
455 cout << "Failed to untie property " << name << endl;
459 // Templates cause ambiguity here
462 * Tie a property to an external bool variable.
464 * The property's value will automatically mirror the variable's
465 * value, and vice-versa, until the property is untied.
467 * @param name The property name to tie (full path).
468 * @param pointer A pointer to the variable.
469 * @param useDefault true if any existing property value should be
470 * copied to the variable; false if the variable should not
471 * be modified; defaults to true.
474 Tie (const string &name, bool *pointer, bool useDefault = true)
476 if (!tie(name.c_str(), SGRawValuePointer<bool>(pointer),
479 "Failed to tie property " << name << " to a pointer" << endl;
484 * Tie a property to an external int variable.
486 * The property's value will automatically mirror the variable's
487 * value, and vice-versa, until the property is untied.
489 * @param name The property name to tie (full path).
490 * @param pointer A pointer to the variable.
491 * @param useDefault true if any existing property value should be
492 * copied to the variable; false if the variable should not
493 * be modified; defaults to true.
496 Tie (const string &name, int *pointer, bool useDefault = true)
498 if (!tie(name.c_str(), SGRawValuePointer<int>(pointer),
501 "Failed to tie property " << name << " to a pointer" << endl;
506 * Tie a property to an external long variable.
508 * The property's value will automatically mirror the variable's
509 * value, and vice-versa, until the property is untied.
511 * @param name The property name to tie (full path).
512 * @param pointer A pointer to the variable.
513 * @param useDefault true if any existing property value should be
514 * copied to the variable; false if the variable should not
515 * be modified; defaults to true.
518 Tie (const string &name, long *pointer, bool useDefault = true)
520 if (!tie(name.c_str(), SGRawValuePointer<long>(pointer),
523 "Failed to tie property " << name << " to a pointer" << endl;
528 * Tie a property to an external float variable.
530 * The property's value will automatically mirror the variable's
531 * value, and vice-versa, until the property is untied.
533 * @param name The property name to tie (full path).
534 * @param pointer A pointer to the variable.
535 * @param useDefault true if any existing property value should be
536 * copied to the variable; false if the variable should not
537 * be modified; defaults to true.
540 Tie (const string &name, float *pointer, bool useDefault = true)
542 if (!tie(name.c_str(), SGRawValuePointer<float>(pointer),
545 "Failed to tie property " << name << " to a pointer" << endl;
550 * Tie a property to an external double variable.
552 * The property's value will automatically mirror the variable's
553 * value, and vice-versa, until the property is untied.
555 * @param name The property name to tie (full path).
556 * @param pointer A pointer to the variable.
557 * @param useDefault true if any existing property value should be
558 * copied to the variable; false if the variable should not
559 * be modified; defaults to true.
562 Tie (const string &name, double *pointer, bool useDefault = true)
564 if (!tie(name.c_str(), SGRawValuePointer<double>(pointer),
567 "Failed to tie property " << name << " to a pointer" << endl;
570 /* template <class V> void
571 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0,
572 bool useDefault = true);
574 template <class V> void
575 Tie (const string &name, int index, V (*getter)(int),
576 void (*setter)(int, V) = 0, bool useDefault = true);
578 template <class T, class V> void
579 Tie (const string &name, T * obj, V (T::*getter)() const,
580 void (T::*setter)(V) = 0, bool useDefault = true);
582 template <class T, class V> void
583 Tie (const string &name, T * obj, int index,
584 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
585 bool useDefault = true); */
588 * Tie a property to a pair of simple functions.
590 * Every time the property value is queried, the getter (if any) will
591 * be invoked; every time the property value is modified, the setter
592 * (if any) will be invoked. The getter can be 0 to make the property
593 * unreadable, and the setter can be 0 to make the property
596 * @param name The property name to tie (full path).
597 * @param getter The getter function, or 0 if the value is unreadable.
598 * @param setter The setter function, or 0 if the value is unmodifiable.
599 * @param useDefault true if the setter should be invoked with any existing
600 * property value should be; false if the old value should be
601 * discarded; defaults to true.
605 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0,
606 bool useDefault = true)
608 if (!tie(name.c_str(), SGRawValueFunctions<V>(getter, setter),
611 "Failed to tie property " << name << " to functions" << endl;
616 * Tie a property to a pair of indexed functions.
618 * Every time the property value is queried, the getter (if any) will
619 * be invoked with the index provided; every time the property value
620 * is modified, the setter (if any) will be invoked with the index
621 * provided. The getter can be 0 to make the property unreadable, and
622 * the setter can be 0 to make the property unmodifiable.
624 * @param name The property name to tie (full path).
625 * @param index The integer argument to pass to the getter and
627 * @param getter The getter function, or 0 if the value is unreadable.
628 * @param setter The setter function, or 0 if the value is unmodifiable.
629 * @param useDefault true if the setter should be invoked with any existing
630 * property value should be; false if the old value should be
631 * discarded; defaults to true.
635 Tie (const string &name, int index, V (*getter)(int),
636 void (*setter)(int, V) = 0, bool useDefault = true)
638 if (!tie(name.c_str(),
639 SGRawValueFunctionsIndexed<V>(index,
644 "Failed to tie property " << name << " to indexed functions" << endl;
649 * Tie a property to a pair of object methods.
651 * Every time the property value is queried, the getter (if any) will
652 * be invoked; every time the property value is modified, the setter
653 * (if any) will be invoked. The getter can be 0 to make the property
654 * unreadable, and the setter can be 0 to make the property
657 * @param name The property name to tie (full path).
658 * @param obj The object whose methods should be invoked.
659 * @param getter The object's getter method, or 0 if the value is
661 * @param setter The object's setter method, or 0 if the value is
663 * @param useDefault true if the setter should be invoked with any existing
664 * property value should be; false if the old value should be
665 * discarded; defaults to true.
667 template <class T, class V>
669 Tie (const string &name, T * obj, V (T::*getter)() const,
670 void (T::*setter)(V) = 0, bool useDefault = true)
672 if (!tie(name.c_str(),
673 SGRawValueMethods<T,V>(*obj, getter, setter),
676 "Failed to tie property " << name << " to object methods" << endl;
681 * Tie a property to a pair of indexed object methods.
683 * Every time the property value is queried, the getter (if any) will
684 * be invoked with the index provided; every time the property value
685 * is modified, the setter (if any) will be invoked with the index
686 * provided. The getter can be 0 to make the property unreadable, and
687 * the setter can be 0 to make the property unmodifiable.
689 * @param name The property name to tie (full path).
690 * @param obj The object whose methods should be invoked.
691 * @param index The integer argument to pass to the getter and
693 * @param getter The getter method, or 0 if the value is unreadable.
694 * @param setter The setter method, or 0 if the value is unmodifiable.
695 * @param useDefault true if the setter should be invoked with any existing
696 * property value should be; false if the old value should be
697 * discarded; defaults to true.
699 template <class T, class V>
701 Tie (const string &name, T * obj, int index,
702 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
703 bool useDefault = true)
705 if (!tie(name.c_str(),
706 SGRawValueMethodsIndexed<T,V>(*obj,
712 "Failed to tie property " << name << " to indexed object methods" << endl;
717 #endif // FGPROPERTYMANAGER_H