-// props.hxx -- class to manage global FlightGear properties.
+// props.hxx - interface definition for a property list.
+// Started Fall 2000 by David Megginson, david@megginson.com
+// This code is released into the Public Domain.
//
-// Copyright (C) 2000 David Megginson - david@megginson.com
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License as
-// published by the Free Software Foundation; either version 2 of the
-// License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// See props.html for documentation [replace with URL when available].
//
// $Id$
-
#ifndef __PROPS_HXX
#define __PROPS_HXX
-#include <string>
-#include <map>
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <simgear/compiler.h>
+
+#include <stdio.h>
+
+#include STL_STRING
+#include <vector>
+#include STL_IOSTREAM
+
+SG_USING_STD(string);
+SG_USING_STD(vector);
+#if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
+SG_USING_STD(istream);
+SG_USING_STD(ostream);
+#endif
+
+#ifdef ALIAS
+#pragma warn A sloppy coder has defined ALIAS as a macro!
+#undef ALIAS
+#endif
+
+#ifdef UNKNOWN
+#pragma warn A sloppy coder has defined UNKNOWN as a macro!
+#undef UNKNOWN
+#endif
+
+#ifdef BOOL
+#pragma warn A sloppy coder has defined BOOL as a macro!
+#undef BOOL
+#endif
+
+#ifdef INT
+#pragma warn A sloppy coder has defined INT as a macro!
+#undef INT
+#endif
+
+#ifdef LONG
+#pragma warn A sloppy coder has defined LONG as a macro!
+#undef LONG
+#endif
+
+#ifdef FLOAT
+#pragma warn A sloppy coder has defined FLOAT as a macro!
+#undef FLOAT
+#endif
-using std::string;
-using std::map;
+#ifdef DOUBLE
+#pragma warn A sloppy coder has defined DOUBLE as a macro!
+#undef DOUBLE
+#endif
+
+#ifdef STRING
+#pragma warn A sloppy coder has defined STRING as a macro!
+#undef STRING
+#endif
\f
////////////////////////////////////////////////////////////////////////
-// Values.
+// A raw value.
+//
+// This is the mechanism that information-providing routines can
+// use to link their own values to the property manager. Any
+// SGValue can be tied to a raw value and then untied again.
////////////////////////////////////////////////////////////////////////
+
/**
- * Abstract representation of a FlightGear value.
+ * Abstract base class for a raw value.
+ *
+ * Unlike values, raw values are not persistent -- the raw value can
+ * change frequently, but the changes are not visible to the application.
*
- * This value is designed to be fairly robust -- it can exist without
- * a specified value, it can be any of several types, and it can
- * be tied to an external variable without disrupting any existing
- * pointers or references to the value. Some basic type conversions
- * are also handled automatically.
+ * The SGValue class always keeps a *copy* of a raw value, not the
+ * original one passed to it; if you override a derived class but do
+ * not replace the clone() method, strange things will happen.
*
- * Values also have attributes that control whether they can be read
- * from, written to, or archived (i.e. saved to disk).
+ * All raw values must implement getValue, setValue, and clone for the
+ * appropriate type.
*/
-class FGValue
+template <class T>
+class SGRawValue
{
public:
+ static const T DefaultValue; // Default for this kind of raw value.
- // External getters
- typedef bool (*bool_getter)();
- typedef int (*int_getter)();
- typedef float (*float_getter)();
- typedef double (*double_getter)();
- typedef const string &(*string_getter)();
+ SGRawValue () {}
+ virtual ~SGRawValue () {}
+ virtual T getValue () const = 0;
+ virtual bool setValue (T value) = 0;
+ virtual SGRawValue * clone () const = 0;
+};
- // External setters
- typedef void (*bool_setter)(bool);
- typedef void (*int_setter)(int);
- typedef void (*float_setter)(float);
- typedef void (*double_setter)(double);
- typedef void (*string_setter)(const string &);
- enum Type {
- UNKNOWN, // no value assigned yet
- BOOL, // boolean
- INT, // integer
- FLOAT, // floating point
- DOUBLE, // double precision
- STRING // text
- };
+/**
+ * A value managed internally.
+ *
+ * Instances of this class are created automatically, by default,
+ * by the SGValue class; ordinarily the application should not
+ * need to touch it.
+ */
+template <class T>
+class SGRawValueInternal : public SGRawValue<T>
+{
+public:
+ SGRawValueInternal () {}
+ SGRawValueInternal (T value) : _value(value) {}
+ virtual ~SGRawValueInternal () {}
+ virtual T getValue () const { return _value; }
+ virtual bool setValue (T value) { _value = value; return true; }
+ virtual SGRawValue<T> * clone () const {
+ return new SGRawValueInternal<T>(_value);
+ }
+private:
+ T _value;
+};
- FGValue ();
- virtual ~FGValue ();
-
- // Meta information.
- virtual Type getType () const { return _type; }
- virtual bool isTied () const { return _tied; }
-
- // Accessors.
- virtual bool getBoolValue () const;
- virtual int getIntValue () const;
- virtual float getFloatValue () const;
- virtual double getDoubleValue () const;
- virtual const string &getStringValue () const;
-
- // Setters.
- virtual bool setBoolValue (bool value);
- virtual bool setIntValue (int value);
- virtual bool setFloatValue (float value);
- virtual bool setDoubleValue (double value);
- virtual bool setStringValue (const string &value);
-
- // Tie to external variables.
- virtual bool tieBool (bool_getter getter,
- bool_setter setter = 0,
- bool useDefault = true);
- virtual bool tieInt (int_getter getter,
- int_setter setter = 0,
- bool useDefault = true);
- virtual bool tieFloat (float_getter getter,
- float_setter setter = 0,
- bool useDefault = true);
- virtual bool tieDouble (double_getter getter,
- double_setter setter = 0,
- bool useDefault = true);
- virtual bool tieString (string_getter getter,
- string_setter setter = 0,
- bool useDefault = true);
-
- // Untie from external variables.
- virtual bool untieBool ();
- virtual bool untieInt ();
- virtual bool untieFloat ();
- virtual bool untieDouble ();
- virtual bool untieString ();
-protected:
+/**
+ * A value managed through a direct pointer.
+ *
+ * This is the most efficient way to tie an external value, but also
+ * the most dangerous, because there is no way for the supplier to
+ * perform bounds checking and derived calculations except by polling
+ * the variable to see if it has changed.
+ */
+template <class T>
+class SGRawValuePointer : public SGRawValue<T>
+{
+public:
+ SGRawValuePointer (T * ptr) : _ptr(ptr) {}
+ virtual ~SGRawValuePointer () {}
+ virtual T getValue () const { return *_ptr; }
+ virtual bool setValue (T value) { *_ptr = value; return true; }
+ virtual SGRawValue<T> * clone () const {
+ return new SGRawValuePointer<T>(_ptr);
+ }
+private:
+ T * _ptr;
+};
+
- bool getRawBool () const;
- int getRawInt () const;
- float getRawFloat () const;
- double getRawDouble () const;
- const string &getRawString () const;
+/**
+ * A value managed through static functions.
+ *
+ * A read-only value will not have a setter; a write-only value will
+ * not have a getter.
+ */
+template <class T>
+class SGRawValueFunctions : public SGRawValue<T>
+{
+public:
+ typedef T (*getter_t)();
+ typedef void (*setter_t)(T);
+ SGRawValueFunctions (getter_t getter = 0, setter_t setter = 0)
+ : _getter(getter), _setter(setter) {}
+ virtual ~SGRawValueFunctions () {}
+ virtual T getValue () const {
+ if (_getter) return (*_getter)();
+ else return SGRawValue<T>::DefaultValue;
+ }
+ virtual bool setValue (T value) {
+ if (_setter) { (*_setter)(value); return true; }
+ else return false;
+ }
+ virtual SGRawValue<T> * clone () const {
+ return new SGRawValueFunctions<T>(_getter,_setter);
+ }
+private:
+ getter_t _getter;
+ setter_t _setter;
+};
- bool setRawBool (bool value);
- bool setRawInt (int value);
- bool setRawFloat (float value);
- bool setRawDouble (double value);
- bool setRawString (const string & value);
+/**
+ * An indexed value managed through static functions.
+ *
+ * A read-only value will not have a setter; a write-only value will
+ * not have a getter.
+ */
+template <class T>
+class SGRawValueFunctionsIndexed : public SGRawValue<T>
+{
+public:
+ typedef T (*getter_t)(int);
+ typedef void (*setter_t)(int,T);
+ SGRawValueFunctionsIndexed (int index, getter_t getter = 0, setter_t setter = 0)
+ : _index(index), _getter(getter), _setter(setter) {}
+ virtual ~SGRawValueFunctionsIndexed () {}
+ virtual T getValue () const {
+ if (_getter) return (*_getter)(_index);
+ else return SGRawValue<T>::DefaultValue;
+ }
+ virtual bool setValue (T value) {
+ if (_setter) { (*_setter)(_index, value); return true; }
+ else return false;
+ }
+ virtual SGRawValue<T> * clone () const {
+ return new SGRawValueFunctionsIndexed<T>(_index, _getter, _setter);
+ }
private:
+ int _index;
+ getter_t _getter;
+ setter_t _setter;
+};
- Type _type;
- bool _tied;
- // The value is one of the following...
- union {
+/**
+ * A value managed through an object and access methods.
+ *
+ * A read-only value will not have a setter; a write-only value will
+ * not have a getter.
+ */
+template <class C, class T>
+class SGRawValueMethods : public SGRawValue<T>
+{
+public:
+ typedef T (C::*getter_t)() const;
+ typedef void (C::*setter_t)(T);
+ SGRawValueMethods (C &obj, getter_t getter = 0, setter_t setter = 0)
+ : _obj(obj), _getter(getter), _setter(setter) {}
+ virtual ~SGRawValueMethods () {}
+ virtual T getValue () const {
+ if (_getter) { return (_obj.*_getter)(); }
+ else { return SGRawValue<T>::DefaultValue; }
+ }
+ virtual bool setValue (T value) {
+ if (_setter) { (_obj.*_setter)(value); return true; }
+ else return false;
+ }
+ virtual SGRawValue<T> * clone () const {
+ return new SGRawValueMethods<C,T>(_obj, _getter, _setter);
+ }
+private:
+ C &_obj;
+ getter_t _getter;
+ setter_t _setter;
+};
+
+
+/**
+ * An indexed value managed through an object and access methods.
+ *
+ * A read-only value will not have a setter; a write-only value will
+ * not have a getter.
+ */
+template <class C, class T>
+class SGRawValueMethodsIndexed : public SGRawValue<T>
+{
+public:
+ typedef T (C::*getter_t)(int) const;
+ typedef void (C::*setter_t)(int, T);
+ SGRawValueMethodsIndexed (C &obj, int index,
+ getter_t getter = 0, setter_t setter = 0)
+ : _obj(obj), _index(index), _getter(getter), _setter(setter) {}
+ virtual ~SGRawValueMethodsIndexed () {}
+ virtual T getValue () const {
+ if (_getter) { return (_obj.*_getter)(_index); }
+ else { return SGRawValue<T>::DefaultValue; }
+ }
+ virtual bool setValue (T value) {
+ if (_setter) { (_obj.*_setter)(_index, value); return true; }
+ else return false;
+ }
+ virtual SGRawValue<T> * clone () const {
+ return new SGRawValueMethodsIndexed<C,T>(_obj, _index, _getter, _setter);
+ }
+private:
+ C &_obj;
+ int _index;
+ getter_t _getter;
+ setter_t _setter;
+};
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// A cooked value.
+//
+// This is the value that property-list clients see. It is a
+// persistent layer over the possibly-changing raw value; once a
+// client gets an SGValue from the property manager, the pointer
+// will be good for the life of the property manager itself, no
+// matter how often the pointer is tied or untied.
+////////////////////////////////////////////////////////////////////////
+
+class SGValue
+{
+public:
+ enum Type {
+ BOOL,
+ INT,
+ LONG,
+ FLOAT,
+ DOUBLE,
+ STRING,
+ UNKNOWN
+ };
+ SGValue ();
+ SGValue (const SGValue &value);
+ ~SGValue ();
+
+ Type getType () const;
+
+ SGValue * getAlias ();
+ const SGValue * getAlias () const;
+ bool alias (SGValue * alias);
+ bool unalias ();
+ bool isAlias () const { return _type == ALIAS; }
+
+ bool getBoolValue () const;
+ int getIntValue () const;
+ long getLongValue () const;
+ float getFloatValue () const;
+ double getDoubleValue () const;
+ string getStringValue () const;
+
+ bool setBoolValue (bool value);
+ bool setIntValue (int value);
+ bool setLongValue (long value);
+ bool setFloatValue (float value);
+ bool setDoubleValue (double value);
+ bool setStringValue (string value);
+ bool setUnknownValue (string value);
+
+ bool isTied () const { return _tied; }
+
+ bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<string> &rawValue, bool useDefault = true);
+
+ bool untie ();
- bool bool_val;
- int int_val;
- float float_val;
- double double_val;
- string * string_val;
-
- struct {
- bool_setter setter;
- bool_getter getter;
- } bool_func;
-
- struct {
- int_setter setter;
- int_getter getter;
- } int_func;
-
- struct {
- void * obj;
- float_setter setter;
- float_getter getter;
- } float_func;
-
- struct {
- void * obj;
- double_setter setter;
- double_getter getter;
- } double_func;
-
- struct {
- string_setter setter;
- string_getter getter;
- } string_func;
+private:
+
+ enum {
+ ALIAS = -1
+ };
+
+ void clear_value ();
+ int _type;
+ bool _tied;
+
+ // The right kind of pointer...
+ union {
+ SGValue * alias;
+ SGRawValue<bool> * bool_val;
+ SGRawValue<int> * int_val;
+ SGRawValue<long> * long_val;
+ SGRawValue<float> * float_val;
+ SGRawValue<double> * double_val;
+ SGRawValue<string> * string_val;
} _value;
};
\f
////////////////////////////////////////////////////////////////////////
-// Top-level manager.
+// A node in a property tree.
////////////////////////////////////////////////////////////////////////
-
-/**
- * A list of FlightGear properties.
- *
- * This list associates names (conventional written as paths,
- * i.e. "/foo/bar/hack") with FGValue classes. Once an FGValue
- * object is associated with the name, the association is
- * permanent -- it is safe to keep a pointer or reference.
- * however, that the type of a value may change if it is tied
- * to a variable.
- *
- * When iterating through the list, the value type is
- *
- * pair<string,FGValue>
- *
- * To get the name from a const_iterator, use
- *
- * it->first
- *
- * and to get the value from a const_iterator, use
- *
- * it->second
- */
-class FGPropertyList
+class SGPropertyNode
{
+
public:
- typedef map<string, FGValue> value_map;
-
- typedef FGValue::bool_getter bool_getter;
- typedef FGValue::int_getter int_getter;
- typedef FGValue::float_getter float_getter;
- typedef FGValue::double_getter double_getter;
- typedef FGValue::string_getter string_getter;
-
- typedef FGValue::bool_setter bool_setter;
- typedef FGValue::int_setter int_setter;
- typedef FGValue::float_setter float_setter;
- typedef FGValue::double_setter double_setter;
- typedef FGValue::string_setter string_setter;
-
- typedef value_map::value_type value_type;
- typedef value_map::size_type size_type;
- typedef value_map::const_iterator const_iterator;
-
- FGPropertyList ();
- virtual ~FGPropertyList ();
-
- virtual size_type size () const { return _props.size(); }
-
- virtual const_iterator begin () const { return _props.begin(); }
- virtual const_iterator end () const { return _props.end(); }
-
- virtual FGValue * getValue (const string &name, bool create = false);
- virtual const FGValue * getValue (const string &name) const;
-
- virtual bool getBoolValue (const string &name) const;
- virtual int getIntValue (const string &name) const;
- virtual float getFloatValue (const string &name) const;
- virtual double getDoubleValue (const string &name) const;
- virtual const string &getStringValue (const string &name) const;
-
- virtual bool setBoolValue (const string &name, bool value);
- virtual bool setIntValue (const string &name, int value);
- virtual bool setFloatValue (const string &name, float value);
- virtual bool setDoubleValue (const string &name, double value);
- virtual bool setStringValue (const string &name, const string &value);
-
- virtual bool tieBool (const string &name,
- bool_getter getter,
- bool_setter setter = 0,
- bool useDefault = true);
- virtual bool tieInt (const string &name,
- int_getter getter,
- int_setter setter = 0,
- bool useDefault = true);
- virtual bool tieFloat (const string &name,
- float_getter getter,
- float_setter setter = 0,
- bool useDefault = true);
- virtual bool tieDouble (const string &name,
- double_getter getter,
- double_setter setter = 0,
- bool useDefault = true);
- virtual bool tieString (const string &name,
- string_getter getter,
- string_setter setter = 0,
- bool useDefault = true);
-
- virtual bool untieBool (const string &name);
- virtual bool untieInt (const string &name);
- virtual bool untieFloat (const string &name);
- virtual bool untieDouble (const string &name);
- virtual bool untieString (const string &name);
-
- virtual void dumpToLog () const;
+
+ SGPropertyNode ();
+ virtual ~SGPropertyNode ();
+
+ // Basic properties.
+ bool hasValue () const { return (_value != 0); }
+ SGValue * getValue () { return _value; }
+ SGValue * getValue (bool create);
+ const SGValue * getValue () const { return _value; }
+ const string &getName () const { return _name; }
+ const int getIndex () const { return _index; }
+ SGPropertyNode * getParent () { return _parent; }
+ const SGPropertyNode * getParent () const { return _parent; }
+
+ // Alias support.
+ bool alias (SGPropertyNode * target);
+ bool alias (const string &path);
+ bool unalias ();
+ bool isAlias () const;
+ SGPropertyNode * getAliasTarget ();
+ const SGPropertyNode * getAliasTarget () const;
+
+ // Children.
+ const int nChildren () const { return _children.size(); }
+ SGPropertyNode * getChild (int position);
+ const SGPropertyNode * getChild (int position) const;
+ SGPropertyNode * getChild (const string &name, int index = 0,
+ bool create = false);
+ const SGPropertyNode * getChild (const string &name, int index = 0) const;
+
+ vector<SGPropertyNode *> getChildren (const string &name);
+ vector<const SGPropertyNode *> getChildren (const string &name) const;
+
+ // Path information.
+ string getPath (bool simplify = false) const;
+
+ // Relative or absolute paths.
+ SGPropertyNode * getRootNode ();
+ const SGPropertyNode * getRootNode () const;
+ SGPropertyNode * getNode (const string &relative_path, bool create = false);
+ const SGPropertyNode * getNode (const string &relative_path) const;
+
+ // Value-related stuff.
+ SGValue::Type getType () const;
+
+ bool getBoolValue () const;
+ int getIntValue () const;
+ long getLongValue () const;
+ float getFloatValue () const;
+ double getDoubleValue () const;
+ string getStringValue () const;
+
+ bool setBoolValue (bool value);
+ bool setIntValue (int value);
+ bool setLongValue (long value);
+ bool setFloatValue (float value);
+ bool setDoubleValue (double value);
+ bool setStringValue (string value);
+ bool setUnknownValue (string value);
+
+ bool isTied () const;
+
+ bool tie (const SGRawValue<bool> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<int> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<long> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<float> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<double> &rawValue, bool useDefault = true);
+ bool tie (const SGRawValue<string> &rawValue, bool useDefault = true);
+
+ bool untie ();
+
+ // Values from paths.
+ bool hasValue (const string &relative_path) const;
+ SGValue * getValue (const string &relative_path, bool create = false);
+ const SGValue * getValue (const string &relative_path) const;
+
+ SGValue::Type getType (const string &relative_path) const;
+
+ bool getBoolValue (const string &relative_path,
+ bool defaultValue = false) const;
+ int getIntValue (const string &relative_path,
+ int defaultValue = 0) const;
+ long getLongValue (const string &relative_path,
+ long defaultValue = 0L) const;
+ float getFloatValue (const string &relative_path,
+ float defaultValue = 0.0) const;
+ double getDoubleValue (const string &relative_path,
+ double defaultValue = 0.0L) const;
+ string getStringValue (const string &relative_path,
+ string defaultValue = "") const;
+
+ bool setBoolValue (const string &relative_path, bool value);
+ bool setIntValue (const string &relative_path, int value);
+ bool setLongValue (const string &relative_path, long value);
+ bool setFloatValue (const string &relative_path, float value);
+ bool setDoubleValue (const string &relative_path, double value);
+ bool setStringValue (const string &relative_path, string value);
+ bool setUnknownValue (const string &relative_path, string value);
+
+ bool isTied (const string &relative_path) const;
+
+ bool tie (const string &relative_path, const SGRawValue<bool> &rawValue,
+ bool useDefault = true);
+ bool tie (const string &relative_path, const SGRawValue<int> &rawValue,
+ bool useDefault = true);
+ bool tie (const string &relative_path, const SGRawValue<long> &rawValue,
+ bool useDefault = true);
+ bool tie (const string &relative_path, const SGRawValue<float> &rawValue,
+ bool useDefault = true);
+ bool tie (const string &relative_path, const SGRawValue<double> &rawValue,
+ bool useDefault = true);
+ bool tie (const string &relative_path, const SGRawValue<string> &rawValue,
+ bool useDefault = true);
+
+ bool untie (const string &relative_path);
+
+protected:
+
+ SGPropertyNode (const string &name, int index, SGPropertyNode * parent);
private:
- value_map _props;
+
+ SGPropertyNode (const SGPropertyNode &node) {}
+
+ SGValue * _value;
+ string _name;
+ int _index;
+ SGPropertyNode * _parent;
+ vector<SGPropertyNode *> _children;
+ mutable SGPropertyNode * _target;
+
};
\f
////////////////////////////////////////////////////////////////////////
-// Global property manager.
+// I/O functions.
////////////////////////////////////////////////////////////////////////
-extern FGPropertyList current_properties;
+bool readProperties (istream &input, SGPropertyNode * start_node,
+ const string &base = "");
+bool readProperties (const string &file, SGPropertyNode * start_node);
+bool writeProperties (ostream &output, const SGPropertyNode * start_node);
+bool writeProperties (const string &file, const SGPropertyNode * start_node);
+bool copyProperties (const SGPropertyNode *in, SGPropertyNode *out);
+
+#endif // __PROPS_HXX
-#endif __PROPS_HXX
+// end of props.hxx