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>
25 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
26 SG_USING_STD(istream);
27 SG_USING_STD(ostream);
31 #pragma warn A sloppy coder has defined ALIAS as a macro!
36 #pragma warn A sloppy coder has defined UNKNOWN as a macro!
41 #pragma warn A sloppy coder has defined BOOL as a macro!
46 #pragma warn A sloppy coder has defined INT as a macro!
51 #pragma warn A sloppy coder has defined LONG as a macro!
56 #pragma warn A sloppy coder has defined FLOAT as a macro!
61 #pragma warn A sloppy coder has defined DOUBLE as a macro!
66 #pragma warn A sloppy coder has defined STRING as a macro!
72 ////////////////////////////////////////////////////////////////////////
75 // This is the mechanism that information-providing routines can
76 // use to link their own values to the property manager. Any
77 // SGValue can be tied to a raw value and then untied again.
78 ////////////////////////////////////////////////////////////////////////
82 * Abstract base class for a raw value.
84 * The property manager is implemented in three layers. The {@link
85 * SGPropertyNode} is the highest and most abstract layer,
86 * representing * an LValue/RValue pair: it * records the position
87 * of the property in the property tree and * contains facilities
88 * for navigation to other nodes. Each node * may contain an {@link
89 * SGValue}, which is guaranteed persistent: the * {@link SGValue}
90 * will not change during a session, even if the * property is bound
91 * and unbound multiple times. The SGValue is the * abstraction of
92 * an RValue: it allows for conversion among all of the different
93 * types, and can be bound to external pointers, functions, methods,
94 * or other data sources. Every SGValue contains an SGRawValue of
95 * a specific type. The SGRawValue (this class) may change frequently
96 * during a session as a value is retyped or bound and unbound to
97 * various data source, but the abstract SGValue layer insulates
98 * the application from those changes. The raw value contains no
99 * facilities for data binding or for type conversion: it is simply
100 * the abstraction of a primitive data type (or a compound data
101 * type, in the case of a string).
103 * The SGValue class always keeps a *copy* of a raw value, not the
104 * original one passed to it; if you override a derived class but do
105 * not replace the {@link #clone} method, strange things will happen.
107 * All raw values must implement {@link #getValue}, {@link #setValue},
108 * and {@link #clone} for the appropriate type.
111 * @see SGPropertyNode */
118 * The default underlying value for this type.
120 * Every raw value has a default; the default is false for a
121 * boolean, 0 for the various numeric values, and "" for a string.
122 * If additional types of raw values are added in the future, they
123 * may need different kinds of default values (such as epoch for a
124 * date type). The default value is used when creating new values.
126 static const T DefaultValue; // Default for this kind of raw value.
132 * Use the default value for this type.
140 virtual ~SGRawValue () {}
144 * Return the underlying value.
146 * @return The actual value for the property.
149 virtual T getValue () const = 0;
153 * Assign a new underlying value.
155 * If the new value cannot be set (because this is a read-only
156 * raw value, or because the new value is not acceptable for
157 * some reason) this method returns false and leaves the original
160 * @param value The actual value for the property.
161 * @return true if the value was set successfully, false otherwise.
164 virtual bool setValue (T value) = 0;
168 * Create a new deep copy of this raw value.
170 * The copy will contain its own version of the underlying value
173 * @return A deep copy of the current object.
175 virtual SGRawValue * clone () const = 0;
180 * An unbound raw value, stored internally.
182 * Instances of this class are created automatically, by default,
183 * by the SGValue class; ordinarily the application should not
187 class SGRawValueInternal : public SGRawValue<T>
192 * Default constructor.
194 * Initialize with the default value for this type.
196 SGRawValueInternal () {}
199 * Explicit value constructor.
201 * Initialize with the underlying value provided.
203 * @param value The initial value for this property.
205 SGRawValueInternal (T value) : _value(value) {}
210 virtual ~SGRawValueInternal () {}
213 * Get the underlying value.
215 virtual T getValue () const { return _value; }
218 * Set the underlying value.
220 virtual bool setValue (T value) { _value = value; return true; }
223 * Create a deep copy of this raw value.
225 virtual SGRawValue<T> * clone () const {
226 return new SGRawValueInternal<T>(_value);
235 * A raw value bound to a pointer.
237 * This is the most efficient way to tie an external value, but also
238 * the most dangerous, because there is no way for the supplier to
239 * perform bounds checking and derived calculations except by polling
240 * the variable to see if it has changed. There is no default
241 * constructor, because this class would be meaningless without a
245 class SGRawValuePointer : public SGRawValue<T>
250 * Explicit pointer constructor.
252 * Create a new raw value bound to the value of the variable
253 * referenced by the pointer.
255 * @param ptr The pointer to the variable to which this raw value
258 SGRawValuePointer (T * ptr) : _ptr(ptr) {}
263 virtual ~SGRawValuePointer () {}
266 * Get the underlying value.
268 * This method will dereference the pointer and return the
271 virtual T getValue () const { return *_ptr; }
274 * Set the underlying value.
276 * This method will dereference the pointer and change the
279 virtual bool setValue (T value) { *_ptr = value; return true; }
282 * Create a copy of this raw value.
284 * The copy will use the same external pointer as the original.
286 virtual SGRawValue<T> * clone () const {
287 return new SGRawValuePointer<T>(_ptr);
296 * A value managed through static functions.
298 * A read-only value will not have a setter; a write-only value will
302 class SGRawValueFunctions : public SGRawValue<T>
307 * The template type of a static getter function.
309 typedef T (*getter_t)();
312 * The template type of a static setter function.
314 typedef void (*setter_t)(T);
317 * Explicit constructor.
319 * Create a new raw value bound to the getter and setter supplied.
321 * @param getter A static function for getting a value, or 0
322 * to read-disable the value.
323 * @param setter A static function for setting a value, or 0
324 * to write-disable the value.
326 SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
327 : _getter(getter), _setter(setter) {}
332 virtual ~SGRawValueFunctions () {}
335 * Get the underlying value.
337 * This method will invoke the getter function to get a value.
338 * If no getter function was supplied, this method will always
339 * return the default value for the type.
341 virtual T getValue () const {
342 if (_getter) return (*_getter)();
343 else return SGRawValue<T>::DefaultValue;
347 * Set the underlying value.
349 * This method will invoke the setter function to change the
350 * underlying value. If no setter function was supplied, this
351 * method will return false.
353 virtual bool setValue (T value) {
354 if (_setter) { (*_setter)(value); return true; }
359 * Create a copy of this raw value, bound to the same functions.
361 virtual SGRawValue<T> * clone () const {
362 return new SGRawValueFunctions<T>(_getter,_setter);
372 * An indexed value bound to static functions.
374 * A read-only value will not have a setter; a write-only value will
375 * not have a getter. An indexed value is useful for binding one
376 * of a list of possible values (such as multiple engines for a
377 * plane). The index is hard-coded at creation time.
380 class SGRawValueFunctionsIndexed : public SGRawValue<T>
383 typedef T (*getter_t)(int);
384 typedef void (*setter_t)(int,T);
385 SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
386 : _index(index), _getter(getter), _setter(setter) {}
387 virtual ~SGRawValueFunctionsIndexed () {}
388 virtual T getValue () const {
389 if (_getter) return (*_getter)(_index);
390 else return SGRawValue<T>::DefaultValue;
392 virtual bool setValue (T value) {
393 if (_setter) { (*_setter)(_index, value); return true; }
396 virtual SGRawValue<T> * clone () const {
397 return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
407 * A value managed through an object and access methods.
409 * A read-only value will not have a setter; a write-only value will
412 template <class C, class T>
413 class SGRawValueMethods : public SGRawValue<T>
416 typedef T (C::*getter_t)() const;
417 typedef void (C::*setter_t)(T);
418 SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
419 : _obj(obj), _getter(getter), _setter(setter) {}
420 virtual ~SGRawValueMethods () {}
421 virtual T getValue () const {
422 if (_getter) { return (_obj.*_getter)(); }
423 else { return SGRawValue<T>::DefaultValue; }
425 virtual bool setValue (T value) {
426 if (_setter) { (_obj.*_setter)(value); return true; }
429 virtual SGRawValue<T> * clone () const {
430 return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
440 * An indexed value managed through an object and access methods.
442 * A read-only value will not have a setter; a write-only value will
445 template <class C, class T>
446 class SGRawValueMethodsIndexed : public SGRawValue<T>
449 typedef T (C::*getter_t)(int) const;
450 typedef void (C::*setter_t)(int, T);
451 SGRawValueMethodsIndexed (C &obj, int index,
452 getter_t getter = 0, setter_t setter = 0)
453 : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
454 virtual ~SGRawValueMethodsIndexed () {}
455 virtual T getValue () const {
456 if (_getter) { return (_obj.*_getter)(_index); }
457 else { return SGRawValue<T>::DefaultValue; }
459 virtual bool setValue (T value) {
460 if (_setter) { (_obj.*_setter)(_index, value); return true; }
463 virtual SGRawValue<T> * clone () const {
464 return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
478 * This is the value that property-list clients see. It is a
479 * persistent layer over the possibly-changing raw value; once a
480 * client gets an SGValue from the property manager, the pointer
481 * will be good for the life of the property manager itself, no
482 * matter how often the pointer is tied or untied.
497 SGValue (const SGValue &value);
500 Type getType () const;
502 SGValue * getAlias ();
503 const SGValue * getAlias () const;
504 bool alias (SGValue * alias);
506 bool isAlias () const { return _type == ALIAS; }
508 bool getBoolValue () const;
509 int getIntValue () const;
510 long getLongValue () const;
511 float getFloatValue () const;
512 double getDoubleValue () const;
513 string getStringValue () const;
515 bool setBoolValue (bool value);
516 bool setIntValue (int value);
517 bool setLongValue (long value);
518 bool setFloatValue (float value);
519 bool setDoubleValue (double value);
520 bool setStringValue (string value);
521 bool setUnknownValue (string value);
523 bool isTied () const { return _tied; }
525 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
526 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
527 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
528 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
529 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
530 bool tie (const SGRawValue<string> &rawValue, bool useDefault = true);
545 // The right kind of pointer...
548 SGRawValue<bool> * bool_val;
549 SGRawValue<int> * int_val;
550 SGRawValue<long> * long_val;
551 SGRawValue<float> * float_val;
552 SGRawValue<double> * double_val;
553 SGRawValue<string> * string_val;
561 * A node in a property tree.
569 * Property value types.
571 * Right now, this duplicates SGValue::Type, but SGValue will be
585 virtual ~SGPropertyNode ();
588 bool hasValue () const { return (_value != 0); }
589 SGValue * getValue () { return _value; }
590 SGValue * getValue (bool create);
591 const SGValue * getValue () const { return _value; }
592 const string &getName () const { return _name; }
593 const int getIndex () const { return _index; }
594 SGPropertyNode * getParent () { return _parent; }
595 const SGPropertyNode * getParent () const { return _parent; }
598 bool alias (SGPropertyNode * target);
599 bool alias (const string &path);
601 bool isAlias () const;
602 SGPropertyNode * getAliasTarget ();
603 const SGPropertyNode * getAliasTarget () const;
606 const int nChildren () const { return _children.size(); }
607 SGPropertyNode * getChild (int position);
608 const SGPropertyNode * getChild (int position) const;
609 SGPropertyNode * getChild (const string &name, int index = 0,
610 bool create = false);
611 const SGPropertyNode * getChild (const string &name, int index = 0) const;
613 vector<SGPropertyNode *> getChildren (const string &name);
614 vector<const SGPropertyNode *> getChildren (const string &name) const;
617 string getPath (bool simplify = false) const;
619 // Relative or absolute paths.
620 SGPropertyNode * getRootNode ();
621 const SGPropertyNode * getRootNode () const;
622 SGPropertyNode * getNode (const string &relative_path, bool create = false);
623 const SGPropertyNode * getNode (const string &relative_path) const;
625 // Value-related stuff.
626 Type getType () const;
628 bool getBoolValue () const;
629 int getIntValue () const;
630 long getLongValue () const;
631 float getFloatValue () const;
632 double getDoubleValue () const;
633 string getStringValue () const;
635 bool setBoolValue (bool value);
636 bool setIntValue (int value);
637 bool setLongValue (long value);
638 bool setFloatValue (float value);
639 bool setDoubleValue (double value);
640 bool setStringValue (string value);
641 bool setUnknownValue (string value);
643 bool isTied () const;
645 bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
646 bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
647 bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
648 bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
649 bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
650 bool tie (const SGRawValue<string> &rawValue, bool useDefault = true);
654 // Values from paths.
655 Type getType (const string &relative_path) const;
657 bool getBoolValue (const string &relative_path,
658 bool defaultValue = false) const;
659 int getIntValue (const string &relative_path,
660 int defaultValue = 0) const;
661 long getLongValue (const string &relative_path,
662 long defaultValue = 0L) const;
663 float getFloatValue (const string &relative_path,
664 float defaultValue = 0.0) const;
665 double getDoubleValue (const string &relative_path,
666 double defaultValue = 0.0L) const;
667 string getStringValue (const string &relative_path,
668 string defaultValue = "") const;
670 bool setBoolValue (const string &relative_path, bool value);
671 bool setIntValue (const string &relative_path, int value);
672 bool setLongValue (const string &relative_path, long value);
673 bool setFloatValue (const string &relative_path, float value);
674 bool setDoubleValue (const string &relative_path, double value);
675 bool setStringValue (const string &relative_path, string value);
676 bool setUnknownValue (const string &relative_path, string value);
678 bool isTied (const string &relative_path) const;
680 bool tie (const string &relative_path, const SGRawValue<bool> &rawValue,
681 bool useDefault = true);
682 bool tie (const string &relative_path, const SGRawValue<int> &rawValue,
683 bool useDefault = true);
684 bool tie (const string &relative_path, const SGRawValue<long> &rawValue,
685 bool useDefault = true);
686 bool tie (const string &relative_path, const SGRawValue<float> &rawValue,
687 bool useDefault = true);
688 bool tie (const string &relative_path, const SGRawValue<double> &rawValue,
689 bool useDefault = true);
690 bool tie (const string &relative_path, const SGRawValue<string> &rawValue,
691 bool useDefault = true);
693 bool untie (const string &relative_path);
697 SGPropertyNode (const string &name, int index, SGPropertyNode * parent);
701 bool hasValue (const string &relative_path) const;
702 SGValue * getValue (const string &relative_path, bool create = false);
703 const SGValue * getValue (const string &relative_path) const;
705 SGPropertyNode (const SGPropertyNode &node) {}
710 SGPropertyNode * _parent;
711 vector<SGPropertyNode *> _children;
712 mutable SGPropertyNode * _target;
718 ////////////////////////////////////////////////////////////////////////
720 ////////////////////////////////////////////////////////////////////////
722 bool readProperties (istream &input, SGPropertyNode * start_node,
723 const string &base = "");
724 bool readProperties (const string &file, SGPropertyNode * start_node);
725 bool writeProperties (ostream &output, const SGPropertyNode * start_node);
726 bool writeProperties (const string &file, const SGPropertyNode * start_node);
727 bool copyProperties (const SGPropertyNode *in, SGPropertyNode *out);
730 #endif // __PROPS_HXX