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());
361 ////////////////////////////////////////////////////////////////////////
362 // Convenience functions for setting property attributes.
363 ////////////////////////////////////////////////////////////////////////
367 * Set the state of the archive attribute for a property.
369 * If the archive attribute is true, the property will be written
370 * when a flight is saved; if it is false, the property will be
373 * A warning message will be printed if the property does not exist.
375 * @param name The property name.
376 * @param state The state of the archive attribute (defaults to true).
379 SetArchivable (const string &name, bool state = true)
381 SGPropertyNode * node = getNode(name.c_str());
384 "Attempt to set archive flag for non-existant property "
387 node->setAttribute(SGPropertyNode::ARCHIVE, state);
392 * Set the state of the read attribute for a property.
394 * If the read attribute is true, the property value will be readable;
395 * if it is false, the property value will always be the default value
398 * A warning message will be printed if the property does not exist.
400 * @param name The property name.
401 * @param state The state of the read attribute (defaults to true).
404 SetReadable (const string &name, bool state = true)
406 SGPropertyNode * node = getNode(name.c_str());
409 "Attempt to set read flag for non-existant property "
412 node->setAttribute(SGPropertyNode::READ, state);
417 * Set the state of the write attribute for a property.
419 * If the write attribute is true, the property value may be modified
420 * (depending on how it is tied); if the write attribute is false, the
421 * property value may not be modified.
423 * A warning message will be printed if the property does not exist.
425 * @param name The property name.
426 * @param state The state of the write attribute (defaults to true).
429 SetWritable (const string &name, bool state = true)
431 SGPropertyNode * node = getNode(name.c_str());
434 "Attempt to set write flag for non-existant property "
437 node->setAttribute(SGPropertyNode::WRITE, state);
442 ////////////////////////////////////////////////////////////////////////
443 // Convenience functions for tying properties, with logging.
444 ////////////////////////////////////////////////////////////////////////
448 * Untie a property from an external data source.
450 * Classes should use this function to release control of any
451 * properties they are managing.
454 Untie (const string &name)
456 if (!untie(name.c_str()))
457 cout << "Failed to untie property " << name;
461 // Templates cause ambiguity here
464 * Tie a property to an external bool variable.
466 * The property's value will automatically mirror the variable's
467 * value, and vice-versa, until the property is untied.
469 * @param name The property name to tie (full path).
470 * @param pointer A pointer to the variable.
471 * @param useDefault true if any existing property value should be
472 * copied to the variable; false if the variable should not
473 * be modified; defaults to true.
476 Tie (const string &name, bool *pointer, bool useDefault = true)
478 if (!tie(name.c_str(), SGRawValuePointer<bool>(pointer),
481 "Failed to tie property " << name << " to a pointer";
486 * Tie a property to an external int variable.
488 * The property's value will automatically mirror the variable's
489 * value, and vice-versa, until the property is untied.
491 * @param name The property name to tie (full path).
492 * @param pointer A pointer to the variable.
493 * @param useDefault true if any existing property value should be
494 * copied to the variable; false if the variable should not
495 * be modified; defaults to true.
498 Tie (const string &name, int *pointer, bool useDefault = true)
500 if (!tie(name.c_str(), SGRawValuePointer<int>(pointer),
503 "Failed to tie property " << name << " to a pointer";
508 * Tie a property to an external long variable.
510 * The property's value will automatically mirror the variable's
511 * value, and vice-versa, until the property is untied.
513 * @param name The property name to tie (full path).
514 * @param pointer A pointer to the variable.
515 * @param useDefault true if any existing property value should be
516 * copied to the variable; false if the variable should not
517 * be modified; defaults to true.
520 Tie (const string &name, long *pointer, bool useDefault = true)
522 if (!tie(name.c_str(), SGRawValuePointer<long>(pointer),
525 "Failed to tie property " << name << " to a pointer";
530 * Tie a property to an external float variable.
532 * The property's value will automatically mirror the variable's
533 * value, and vice-versa, until the property is untied.
535 * @param name The property name to tie (full path).
536 * @param pointer A pointer to the variable.
537 * @param useDefault true if any existing property value should be
538 * copied to the variable; false if the variable should not
539 * be modified; defaults to true.
542 Tie (const string &name, float *pointer, bool useDefault = true)
544 if (!tie(name.c_str(), SGRawValuePointer<float>(pointer),
547 "Failed to tie property " << name << " to a pointer";
552 * Tie a property to an external double variable.
554 * The property's value will automatically mirror the variable's
555 * value, and vice-versa, until the property is untied.
557 * @param name The property name to tie (full path).
558 * @param pointer A pointer to the variable.
559 * @param useDefault true if any existing property value should be
560 * copied to the variable; false if the variable should not
561 * be modified; defaults to true.
564 Tie (const string &name, double *pointer, bool useDefault = true)
566 if (!tie(name.c_str(), SGRawValuePointer<double>(pointer),
569 "Failed to tie property " << name << " to a pointer";
572 /* template <class V> void
573 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0,
574 bool useDefault = true);
576 template <class V> void
577 Tie (const string &name, int index, V (*getter)(int),
578 void (*setter)(int, V) = 0, bool useDefault = true);
580 template <class T, class V> void
581 Tie (const string &name, T * obj, V (T::*getter)() const,
582 void (T::*setter)(V) = 0, bool useDefault = true);
584 template <class T, class V> void
585 Tie (const string &name, T * obj, int index,
586 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
587 bool useDefault = true); */
590 * Tie a property to a pair of simple functions.
592 * Every time the property value is queried, the getter (if any) will
593 * be invoked; every time the property value is modified, the setter
594 * (if any) will be invoked. The getter can be 0 to make the property
595 * unreadable, and the setter can be 0 to make the property
598 * @param name The property name to tie (full path).
599 * @param getter The getter function, or 0 if the value is unreadable.
600 * @param setter The setter function, or 0 if the value is unmodifiable.
601 * @param useDefault true if the setter should be invoked with any existing
602 * property value should be; false if the old value should be
603 * discarded; defaults to true.
607 Tie (const string &name, V (*getter)(), void (*setter)(V) = 0,
608 bool useDefault = true)
610 if (!tie(name.c_str(), SGRawValueFunctions<V>(getter, setter),
613 "Failed to tie property " << name << " to functions";
618 * Tie a property to a pair of indexed functions.
620 * Every time the property value is queried, the getter (if any) will
621 * be invoked with the index provided; every time the property value
622 * is modified, the setter (if any) will be invoked with the index
623 * provided. The getter can be 0 to make the property unreadable, and
624 * the setter can be 0 to make the property unmodifiable.
626 * @param name The property name to tie (full path).
627 * @param index The integer argument to pass to the getter and
629 * @param getter The getter function, or 0 if the value is unreadable.
630 * @param setter The setter function, or 0 if the value is unmodifiable.
631 * @param useDefault true if the setter should be invoked with any existing
632 * property value should be; false if the old value should be
633 * discarded; defaults to true.
637 Tie (const string &name, int index, V (*getter)(int),
638 void (*setter)(int, V) = 0, bool useDefault = true)
640 if (!tie(name.c_str(),
641 SGRawValueFunctionsIndexed<V>(index,
646 "Failed to tie property " << name << " to indexed functions";
651 * Tie a property to a pair of object methods.
653 * Every time the property value is queried, the getter (if any) will
654 * be invoked; every time the property value is modified, the setter
655 * (if any) will be invoked. The getter can be 0 to make the property
656 * unreadable, and the setter can be 0 to make the property
659 * @param name The property name to tie (full path).
660 * @param obj The object whose methods should be invoked.
661 * @param getter The object's getter method, or 0 if the value is
663 * @param setter The object's setter method, or 0 if the value is
665 * @param useDefault true if the setter should be invoked with any existing
666 * property value should be; false if the old value should be
667 * discarded; defaults to true.
669 template <class T, class V>
671 Tie (const string &name, T * obj, V (T::*getter)() const,
672 void (T::*setter)(V) = 0, bool useDefault = true)
674 if (!tie(name.c_str(),
675 SGRawValueMethods<T,V>(*obj, getter, setter),
678 "Failed to tie property " << name << " to object methods";
683 * Tie a property to a pair of indexed object methods.
685 * Every time the property value is queried, the getter (if any) will
686 * be invoked with the index provided; every time the property value
687 * is modified, the setter (if any) will be invoked with the index
688 * provided. The getter can be 0 to make the property unreadable, and
689 * the setter can be 0 to make the property unmodifiable.
691 * @param name The property name to tie (full path).
692 * @param obj The object whose methods should be invoked.
693 * @param index The integer argument to pass to the getter and
695 * @param getter The getter method, or 0 if the value is unreadable.
696 * @param setter The setter method, or 0 if the value is unmodifiable.
697 * @param useDefault true if the setter should be invoked with any existing
698 * property value should be; false if the old value should be
699 * discarded; defaults to true.
701 template <class T, class V>
703 Tie (const string &name, T * obj, int index,
704 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
705 bool useDefault = true)
707 if (!tie(name.c_str(),
708 SGRawValueMethodsIndexed<T,V>(*obj,
714 "Failed to tie property " << name << " to indexed object methods";
720 #endif // FGPROPERTYMANAGER_H