3 * Interface definition for a property list.
4 * Started Fall 2000 by David Megginson, david@megginson.com
5 * This code is released into the Public Domain.
7 * See props.html for documentation [replace with URL when available].
15 #include <simgear/compiler.h>
16 #include <simgear/debug/logstream.hxx>
28 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
29 SG_USING_STD(istream);
30 SG_USING_STD(ostream);
34 #pragma warn A sloppy coder has defined NONE as a macro!
39 #pragma warn A sloppy coder has defined ALIAS as a macro!
44 #pragma warn A sloppy coder has defined UNSPECIFIED as a macro!
49 #pragma warn A sloppy coder has defined BOOL as a macro!
54 #pragma warn A sloppy coder has defined INT as a macro!
59 #pragma warn A sloppy coder has defined LONG as a macro!
64 #pragma warn A sloppy coder has defined FLOAT as a macro!
69 #pragma warn A sloppy coder has defined DOUBLE as a macro!
74 #pragma warn A sloppy coder has defined STRING as a macro!
80 ////////////////////////////////////////////////////////////////////////
83 // This is the mechanism that information-providing routines can
84 // use to link their own values to the property manager. Any
85 // SGValue can be tied to a raw value and then untied again.
86 ////////////////////////////////////////////////////////////////////////
90 * Abstract base class for a raw value.
92 * The property manager is implemented in three layers. The {@link
93 * SGPropertyNode} is the highest and most abstract layer,
94 * representing * an LValue/RValue pair: it * records the position
95 * of the property in the property tree and * contains facilities
96 * for navigation to other nodes. Each node * may contain an {@link
97 * SGValue}, which is guaranteed persistent: the * {@link SGValue}
98 * will not change during a session, even if the * property is bound
99 * and unbound multiple times. The SGValue is the * abstraction of
100 * an RValue: it allows for conversion among all of the different
101 * types, and can be bound to external pointers, functions, methods,
102 * or other data sources. Every SGValue contains an SGRawValue of
103 * a specific type. The SGRawValue (this class) may change frequently
104 * during a session as a value is retyped or bound and unbound to
105 * various data source, but the abstract SGValue layer insulates
106 * the application from those changes. The raw value contains no
107 * facilities for data binding or for type conversion: it is simply
108 * the abstraction of a primitive data type (or a compound data
109 * type, in the case of a string).
111 * The SGValue class always keeps a *copy* of a raw value, not the
112 * original one passed to it; if you override a derived class but do
113 * not replace the {@link #clone} method, strange things will happen.
115 * All raw values must implement {@link #getValue}, {@link #setValue},
116 * and {@link #clone} for the appropriate type.
119 * @see SGPropertyNode */
126 * The default underlying value for this type.
128 * Every raw value has a default; the default is false for a
129 * boolean, 0 for the various numeric values, and "" for a string.
130 * If additional types of raw values are added in the future, they
131 * may need different kinds of default values (such as epoch for a
132 * date type). The default value is used when creating new values.
134 static const T DefaultValue; // Default for this kind of raw value.
140 * Use the default value for this type.
148 virtual ~SGRawValue () {}
152 * Return the underlying value.
154 * @return The actual value for the property.
157 virtual T getValue () const = 0;
161 * Assign a new underlying value.
163 * If the new value cannot be set (because this is a read-only
164 * raw value, or because the new value is not acceptable for
165 * some reason) this method returns false and leaves the original
168 * @param value The actual value for the property.
169 * @return true if the value was set successfully, false otherwise.
172 virtual bool setValue (T value) = 0;
176 * Create a new deep copy of this raw value.
178 * The copy will contain its own version of the underlying value
181 * @return A deep copy of the current object.
183 virtual SGRawValue * clone () const = 0;
188 * An unbound raw value, stored internally.
190 * Instances of this class are created automatically, by default,
191 * by the SGValue class; ordinarily the application should not
195 class SGRawValueInternal : public SGRawValue<T>
200 * Default constructor.
202 * Initialize with the default value for this type.
204 SGRawValueInternal () {}
207 * Explicit value constructor.
209 * Initialize with the underlying value provided.
211 * @param value The initial value for this property.
213 SGRawValueInternal (T value) : _value(value) {}
218 virtual ~SGRawValueInternal () {}
221 * Get the underlying value.
223 virtual T getValue () const { return _value; }
226 * Set the underlying value.
228 virtual bool setValue (T value) { _value = value; return true; }
231 * Create a deep copy of this raw value.
233 virtual SGRawValue<T> * clone () const {
234 return new SGRawValueInternal<T>(_value);
243 * A raw value bound to a pointer.
245 * This is the most efficient way to tie an external value, but also
246 * the most dangerous, because there is no way for the supplier to
247 * perform bounds checking and derived calculations except by polling
248 * the variable to see if it has changed. There is no default
249 * constructor, because this class would be meaningless without a
253 class SGRawValuePointer : public SGRawValue<T>
258 * Explicit pointer constructor.
260 * Create a new raw value bound to the value of the variable
261 * referenced by the pointer.
263 * @param ptr The pointer to the variable to which this raw value
266 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
271 virtual ~SGRawValuePointer () {}
274 * Get the underlying value.
276 * This method will dereference the pointer and return the
279 virtual T getValue () const { return *_ptr; }
282 * Set the underlying value.
284 * This method will dereference the pointer and change the
287 virtual bool setValue (T value) { *_ptr = value; return true; }
290 * Create a copy of this raw value.
292 * The copy will use the same external pointer as the original.
294 virtual SGRawValue<T> * clone () const {
295 return new SGRawValuePointer<T>(_ptr);
304 * A value managed through static functions.
306 * A read-only value will not have a setter; a write-only value will
310 class SGRawValueFunctions : public SGRawValue<T>
315 * The template type of a static getter function.
317 typedef T (*getter_t)();
320 * The template type of a static setter function.
322 typedef void (*setter_t)(T);
325 * Explicit constructor.
327 * Create a new raw value bound to the getter and setter supplied.
329 * @param getter A static function for getting a value, or 0
330 * to read-disable the value.
331 * @param setter A static function for setting a value, or 0
332 * to write-disable the value.
334 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
335 : _getter(getter), _setter(setter) {}
340 virtual ~SGRawValueFunctions () {}
343 * Get the underlying value.
345 * This method will invoke the getter function to get a value.
346 * If no getter function was supplied, this method will always
347 * return the default value for the type.
349 virtual T getValue () const {
350 if (_getter) return (*_getter)();
351 else return SGRawValue<T>::DefaultValue;
355 * Set the underlying value.
357 * This method will invoke the setter function to change the
358 * underlying value. If no setter function was supplied, this
359 * method will return false.
361 virtual bool setValue (T value) {
362 if (_setter) { (*_setter)(value); return true; }
367 * Create a copy of this raw value, bound to the same functions.
369 virtual SGRawValue<T> * clone () const {
370 return new SGRawValueFunctions<T>(_getter,_setter);
380 * An indexed value bound to static functions.
382 * A read-only value will not have a setter; a write-only value will
383 * not have a getter. An indexed value is useful for binding one
384 * of a list of possible values (such as multiple engines for a
385 * plane). The index is hard-coded at creation time.
388 class SGRawValueFunctionsIndexed : public SGRawValue<T>
391 typedef T (*getter_t)(int);
392 typedef void (*setter_t)(int,T);
393 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
394 : _index(index), _getter(getter), _setter(setter) {}
395 virtual ~SGRawValueFunctionsIndexed () {}
396 virtual T getValue () const {
397 if (_getter) return (*_getter)(_index);
398 else return SGRawValue<T>::DefaultValue;
400 virtual bool setValue (T value) {
401 if (_setter) { (*_setter)(_index, value); return true; }
404 virtual SGRawValue<T> * clone () const {
405 return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
415 * A value managed through an object and access methods.
417 * A read-only value will not have a setter; a write-only value will
420 template <class C, class T>
421 class SGRawValueMethods : public SGRawValue<T>
424 typedef T (C::*getter_t)() const;
425 typedef void (C::*setter_t)(T);
426 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
427 : _obj(obj), _getter(getter), _setter(setter) {}
428 virtual ~SGRawValueMethods () {}
429 virtual T getValue () const {
430 if (_getter) { return (_obj.*_getter)(); }
431 else { return SGRawValue<T>::DefaultValue; }
433 virtual bool setValue (T value) {
434 if (_setter) { (_obj.*_setter)(value); return true; }
437 virtual SGRawValue<T> * clone () const {
438 return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
448 * An indexed value managed through an object and access methods.
450 * A read-only value will not have a setter; a write-only value will
453 template <class C, class T>
454 class SGRawValueMethodsIndexed : public SGRawValue<T>
457 typedef T (C::*getter_t)(int) const;
458 typedef void (C::*setter_t)(int, T);
459 SGRawValueMethodsIndexed (C &obj, int index,
460 getter_t getter = 0, setter_t setter = 0)
461 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
462 virtual ~SGRawValueMethodsIndexed () {}
463 virtual T getValue () const {
464 if (_getter) { return (_obj.*_getter)(_index); }
465 else { return SGRawValue<T>::DefaultValue; }
467 virtual bool setValue (T value) {
468 if (_setter) { (_obj.*_setter)(_index, value); return true; }
471 virtual SGRawValue<T> * clone () const {
472 return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
484 * A node in a property tree.
493 * Property value types.
509 * Access mode attributes.
511 * <p>The ARCHIVE attribute is strictly advisory, and controls
512 * whether the property should normally be saved and restored.</p>
524 * Default constructor.
532 SGPropertyNode (const SGPropertyNode &node);
538 virtual ~SGPropertyNode ();
547 * Test whether this node contains a primitive leaf value.
549 bool hasValue () const { return (_type != NONE); }
553 * Get the node's simple (XML) name.
555 const string &getName () const { return _name; }
559 * Get the node's integer index.
561 const int getIndex () const { return _index; }
565 * Get a non-const pointer to the node's parent.
567 SGPropertyNode * getParent () { return _parent; }
571 * Get a const pointer to the node's parent.
573 const SGPropertyNode * getParent () const { return _parent; }
582 * Get the number of child nodes.
584 const int nChildren () const { return _children.size(); }
588 * Get a child node by position (*NOT* index).
590 SGPropertyNode * getChild (int position);
594 * Get a const child node by position (*NOT* index).
596 const SGPropertyNode * getChild (int position) const;
600 * Get a child node by name and index.
602 SGPropertyNode * getChild (const string &name, int index = 0,
603 bool create = false);
607 * Get a const child node by name and index.
609 const SGPropertyNode * getChild (const string &name, int index = 0) const;
613 * Get a vector of all children with the specified name.
615 vector<SGPropertyNode *> getChildren (const string &name);
619 * Get a vector all all children (const) with the specified name.
621 vector<const SGPropertyNode *> getChildren (const string &name) const;
630 * Alias this node's leaf value to another's.
632 bool alias (SGPropertyNode * target);
636 * Alias this node's leaf value to another's by relative path.
638 bool alias (const string &path);
642 * Remove any alias for this node.
648 * Test whether the node's leaf value is aliased to another's.
650 bool isAlias () const { return (_type == ALIAS); }
654 * Get a non-const pointer to the current alias target, if any.
656 SGPropertyNode * getAliasTarget ();
660 * Get a const pointer to the current alias target, if any.
662 const SGPropertyNode * getAliasTarget () const;
671 * Get the path to this node from the root.
673 string getPath (bool simplify = false) const;
677 * Get a pointer to the root node.
679 SGPropertyNode * getRootNode ();
683 * Get a const pointer to the root node.
685 const SGPropertyNode * getRootNode () const;
689 * Get a pointer to another node by relative path.
691 SGPropertyNode * getNode (const string &relative_path, bool create = false);
695 * Get a pointer to another node by relative path.
697 * This method leaves the index off the last member of the path,
698 * so that the user can specify it separately (and save some
699 * string building). For example, getNode("/bar[1]/foo", 3) is
700 * exactly equivalent to getNode("bar[1]/foo[3]"). The index
701 * provided overrides any given in the path itself for the last
704 SGPropertyNode * getNode (const string &relative_path, int index,
705 bool create = false);
709 * Get a const pointer to another node by relative path.
711 const SGPropertyNode * getNode (const string &relative_path) const;
715 * Get a const pointer to another node by relative path.
717 * This method leaves the index off the last member of the path,
718 * so that the user can specify it separate.
720 const SGPropertyNode * getNode (const string &relative_path,
729 * Check a single mode attribute for the property node.
731 bool getAttribute (Attribute attr) const { return ((_attr & attr) != 0); }
735 * Set a single mode attribute for the property node.
737 void setAttribute (Attribute attr, bool state) {
738 (state ? _attr |= attr : _attr &= ~attr);
743 * Get all of the mode attributes for the property node.
745 int getAttributes () const { return _attr; }
749 * Set all of the mode attributes for the property node.
751 void setAttributes (int attr) { _attr = attr; }
755 // Leaf Value (primitive).
760 * Get the type of leaf value, if any, for this node.
762 Type getType () const;
766 * Get a bool value for this node.
768 bool getBoolValue () const;
772 * Get an int value for this node.
774 int getIntValue () const;
778 * Get a long int value for this node.
780 long getLongValue () const;
784 * Get a float value for this node.
786 float getFloatValue () const;
790 * Get a double value for this node.
792 double getDoubleValue () const;
796 * Get a string value for this node.
798 string getStringValue () const;
803 * Set a bool value for this node.
805 bool setBoolValue (bool value);
809 * Set an int value for this node.
811 bool setIntValue (int value);
815 * Set a long int value for this node.
817 bool setLongValue (long value);
821 * Set a float value for this node.
823 bool setFloatValue (float value);
827 * Set a double value for this node.
829 bool setDoubleValue (double value);
833 * Set a string value for this node.
835 bool setStringValue (string value);
839 * Set a value of unspecified type for this node.
841 bool setUnspecifiedValue (string value);
850 * Test whether this node is bound to an external data source.
852 bool isTied () const { return _tied; }
856 * Bind this node to an external bool source.
858 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
862 * Bind this node to an external int source.
864 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
868 * Bind this node to an external long int source.
870 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
874 * Bind this node to an external float source.
876 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
880 * Bind this node to an external double source.
882 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
886 * Bind this node to an external string source.
888 bool tie (const SGRawValue<string> &rawValue, bool useDefault = true);
892 * Unbind this node from any external data source.
898 // Convenience methods using paths.
899 // TODO: add attribute methods
904 * Get another node's type.
906 Type getType (const string &relative_path) const;
910 * Test whether another node has a leaf value.
912 bool hasValue (const string &relative_path) const;
916 * Get another node's value as a bool.
918 bool getBoolValue (const string &relative_path,
919 bool defaultValue = false) const;
923 * Get another node's value as an int.
925 int getIntValue (const string &relative_path,
926 int defaultValue = 0) const;
930 * Get another node's value as a long int.
932 long getLongValue (const string &relative_path,
933 long defaultValue = 0L) const;
937 * Get another node's value as a float.
939 float getFloatValue (const string &relative_path,
940 float defaultValue = 0.0) const;
944 * Get another node's value as a double.
946 double getDoubleValue (const string &relative_path,
947 double defaultValue = 0.0L) const;
951 * Get another node's value as a string.
953 string getStringValue (const string &relative_path,
954 string defaultValue = "") const;
958 * Set another node's value as a bool.
960 bool setBoolValue (const string &relative_path, bool value);
964 * Set another node's value as an int.
966 bool setIntValue (const string &relative_path, int value);
970 * Set another node's value as a long int.
972 bool setLongValue (const string &relative_path, long value);
976 * Set another node's value as a float.
978 bool setFloatValue (const string &relative_path, float value);
982 * Set another node's value as a double.
984 bool setDoubleValue (const string &relative_path, double value);
988 * Set another node's value as a string.
990 bool setStringValue (const string &relative_path, string value);
994 * Set another node's value with no specified type.
996 bool setUnspecifiedValue (const string &relative_path, string value);
1000 * Test whether another node is bound to an external data source.
1002 bool isTied (const string &relative_path) const;
1006 * Bind another node to an external bool source.
1008 bool tie (const string &relative_path, const SGRawValue<bool> &rawValue,
1009 bool useDefault = true);
1013 * Bind another node to an external int source.
1015 bool tie (const string &relative_path, const SGRawValue<int> &rawValue,
1016 bool useDefault = true);
1020 * Bind another node to an external long int source.
1022 bool tie (const string &relative_path, const SGRawValue<long> &rawValue,
1023 bool useDefault = true);
1027 * Bind another node to an external float source.
1029 bool tie (const string &relative_path, const SGRawValue<float> &rawValue,
1030 bool useDefault = true);
1034 * Bind another node to an external double source.
1036 bool tie (const string &relative_path, const SGRawValue<double> &rawValue,
1037 bool useDefault = true);
1041 * Bind another node to an external string source.
1043 bool tie (const string &relative_path, const SGRawValue<string> &rawValue,
1044 bool useDefault = true);
1048 * Unbind another node from any external data source.
1050 bool untie (const string &relative_path);
1057 * Protected constructor for making new nodes on demand.
1059 SGPropertyNode (const string &name, int index, SGPropertyNode * parent);
1064 // Get the raw value
1065 bool get_bool () const;
1066 int get_int () const;
1067 long get_long () const;
1068 float get_float () const;
1069 double get_double () const;
1070 const string get_string () const;
1072 // Set the raw value
1073 bool set_bool (bool value);
1074 bool set_int (int value);
1075 bool set_long (long value);
1076 bool set_float (float value);
1077 bool set_double (double value);
1078 bool set_string (const string &value);
1082 * Clear any existing value and set the type to NONE.
1084 void clear_value ();
1088 * Get the value as a string.
1090 string make_string () const;
1094 * Trace a read access.
1096 void trace_read () const;
1100 * Trace a write access.
1102 void trace_write () const;
1106 SGPropertyNode * _parent;
1107 vector<SGPropertyNode *> _children;
1108 typedef map<const string,SGPropertyNode *> cache_map;
1109 cache_map * _path_cache;
1114 // The right kind of pointer...
1116 SGPropertyNode * alias;
1117 SGRawValue<bool> * bool_val;
1118 SGRawValue<int> * int_val;
1119 SGRawValue<long> * long_val;
1120 SGRawValue<float> * float_val;
1121 SGRawValue<double> * double_val;
1122 SGRawValue<string> * string_val;
1131 string * string_val;
1137 #endif // __PROPS_HXX