]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_props.hxx
Fixed AIplane vertical speed.
[flightgear.git] / src / Main / fg_props.hxx
index a21c438c5c378269df425cf6fdeb03b95461b384..41772ff358345d94f66eeab7b8f9563e9f26d7ae 100644 (file)
@@ -6,34 +6,28 @@
 #ifndef __FG_PROPS_HXX
 #define __FG_PROPS_HXX 1
 
-#include <simgear/debug/logstream.hxx>
-#include <simgear/misc/props.hxx>
-#include "globals.hxx"
+#include <iosfwd>
+
+#include <simgear/structure/subsystem_mgr.hxx>
+#include <simgear/math/SGMath.hxx>
+
+#include <Main/globals.hxx>
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Property management.
 ////////////////////////////////////////////////////////////////////////
 
+class FGProperties : public SGSubsystem
+{
+public:
+    FGProperties ();
+    virtual ~FGProperties ();
 
-/**
- * Initialize the default FlightGear properties.
- *
- * These are mostly properties that haven't been claimed by a
- * specific module yet.  This function should be invoked once,
- * while the program is starting (and after the global property
- * tree has been created).
- */
-extern void fgInitProps ();    // fixme: how are they untied?
-
-
-/**
- * Update the default FlightGear properties.
- *
- * This function should be invoked once in each loop to update all
- * of the default properties.
- */
-extern void fgUpdateProps ();
+    void init ();
+    void bind ();
+    void unbind ();
+    void update (double dt);
+};
 
 
 /**
@@ -47,7 +41,7 @@ extern void fgUpdateProps ();
  *        just the ones flagged as archivable.
  * @return true if the flight was saved successfully.
  */
-extern bool fgSaveFlight (ostream &output, bool write_all = false);
+extern bool fgSaveFlight (std::ostream &output, bool write_all = false);
 
 
 /**
@@ -59,7 +53,22 @@ extern bool fgSaveFlight (ostream &output, bool write_all = false);
  * @param input The input stream to read the XML from.
  * @return true if the flight was restored successfully.
  */
-extern bool fgLoadFlight (istream &input);
+extern bool fgLoadFlight (std::istream &input);
+
+
+/**
+ * Load properties from a file.
+ *
+ * @param file The relative or absolute filename.
+ * @param props The property node to load the properties into.
+ * @param in_fg_root If true, look for the file relative to
+ *        $FG_ROOT; otherwise, look for the file relative to the
+ *        current working directory.
+ * @return true if the properties loaded successfully, false
+ *         otherwise.
+ */
+extern bool fgLoadProps (const char * path, SGPropertyNode * props,
+                         bool in_fg_root = true, int default_mode = 0);
 
 
 \f
@@ -67,7 +76,6 @@ extern bool fgLoadFlight (istream &input);
 // Convenience functions for getting property values.
 ////////////////////////////////////////////////////////////////////////
 
-
 /**
  * Get a property node.
  *
@@ -75,11 +83,7 @@ extern bool fgLoadFlight (istream &input);
  * @param create true to create the node if it doesn't exist.
  * @return The node, or 0 if none exists and none was created.
  */
-inline SGPropertyNode * 
-fgGetNode (const string &path, bool create = false)
-{
-  return globals->get_props()->getNode(path, create);
-}
+extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
 
 
 /**
@@ -96,11 +100,8 @@ fgGetNode (const string &path, bool create = false)
  * @param create true to create the node if it doesn't exist.
  * @return The node, or 0 if none exists and none was created.
  */
-inline SGPropertyNode * 
-fgGetNode (const string &path, int index, bool create = false)
-{
-  return globals->get_props()->getNode(path, index, create);
-}
+extern SGPropertyNode * fgGetNode (const char * path,
+                                  int index, bool create = false);
 
 
 /**
@@ -109,11 +110,31 @@ fgGetNode (const string &path, int index, bool create = false)
  * @param path The path of the node, relative to root.
  * @return true if the node exists, false otherwise.
  */
-inline bool
-fgHasNode (const string &path)
-{
-  return (fgGetNode(path, false) != 0);
-}
+extern bool fgHasNode (const char * path);
+
+
+/**
+ * Add a listener to a node.
+ *
+ * @param listener The listener to add to the node.
+ * @param path The path of the node, relative to root.
+ * @param index The index for the last member of the path (overrides
+ * any given in the string).
+ */
+extern void fgAddChangeListener (SGPropertyChangeListener * listener,
+                                const char * path);
+
+
+/**
+ * Add a listener to a node.
+ *
+ * @param listener The listener to add to the node.
+ * @param path The path of the node, relative to root.
+ * @param index The index for the last member of the path (overrides
+ * any given in the string).
+ */
+extern void fgAddChangeListener (SGPropertyChangeListener * listener,
+                                const char * path, int index);
 
 
 /**
@@ -130,10 +151,7 @@ fgHasNode (const string &path)
  *        does not exist.
  * @return The property's value as a bool, or the default value provided.
  */
-inline bool fgGetBool (const string &name, bool defaultValue = false)
-{
-  return globals->get_props()->getBoolValue(name, defaultValue);
-}
+extern bool fgGetBool (const char * name, bool defaultValue = false);
 
 
 /**
@@ -150,10 +168,7 @@ inline bool fgGetBool (const string &name, bool defaultValue = false)
  *        does not exist.
  * @return The property's value as an int, or the default value provided.
  */
-inline int fgGetInt (const string &name, int defaultValue = 0)
-{
-  return globals->get_props()->getIntValue(name, defaultValue);
-}
+extern int fgGetInt (const char * name, int defaultValue = 0);
 
 
 /**
@@ -170,10 +185,7 @@ inline int fgGetInt (const string &name, int defaultValue = 0)
  *        does not exist.
  * @return The property's value as a long, or the default value provided.
  */
-inline int fgGetLong (const string &name, long defaultValue = 0L)
-{
-  return globals->get_props()->getLongValue(name, defaultValue);
-}
+extern int fgGetLong (const char * name, long defaultValue = 0L);
 
 
 /**
@@ -190,10 +202,7 @@ inline int fgGetLong (const string &name, long defaultValue = 0L)
  *        does not exist.
  * @return The property's value as a float, or the default value provided.
  */
-inline float fgGetFloat (const string &name, float defaultValue = 0.0)
-{
-  return globals->get_props()->getFloatValue(name, defaultValue);
-}
+extern float fgGetFloat (const char * name, float defaultValue = 0.0);
 
 
 /**
@@ -210,10 +219,7 @@ inline float fgGetFloat (const string &name, float defaultValue = 0.0)
  *        does not exist.
  * @return The property's value as a double, or the default value provided.
  */
-inline double fgGetDouble (const string &name, double defaultValue = 0.0)
-{
-  return globals->get_props()->getDoubleValue(name, defaultValue);
-}
+extern double fgGetDouble (const char * name, double defaultValue = 0.0);
 
 
 /**
@@ -230,10 +236,8 @@ inline double fgGetDouble (const string &name, double defaultValue = 0.0)
  *        does not exist.
  * @return The property's value as a string, or the default value provided.
  */
-inline string fgGetString (const string &name, string defaultValue = "")
-{
-  return globals->get_props()->getStringValue(name, defaultValue);
-}
+extern const char * fgGetString (const char * name,
+                                const char * defaultValue = "");
 
 
 /**
@@ -249,10 +253,7 @@ inline string fgGetString (const string &name, string defaultValue = "")
  * @param val The new value for the property.
  * @return true if the assignment succeeded, false otherwise.
  */
-inline bool fgSetBool (const string &name, bool val)
-{
-  return globals->get_props()->setBoolValue(name, val);
-}
+extern bool fgSetBool (const char * name, bool val);
 
 
 /**
@@ -268,10 +269,7 @@ inline bool fgSetBool (const string &name, bool val)
  * @param val The new value for the property.
  * @return true if the assignment succeeded, false otherwise.
  */
-inline bool fgSetInt (const string &name, int val)
-{
-  return globals->get_props()->setIntValue(name, val);
-}
+extern bool fgSetInt (const char * name, int val);
 
 
 /**
@@ -287,10 +285,7 @@ inline bool fgSetInt (const string &name, int val)
  * @param val The new value for the property.
  * @return true if the assignment succeeded, false otherwise.
  */
-inline bool fgSetLong (const string &name, long val)
-{
-  return globals->get_props()->setLongValue(name, val);
-}
+extern bool fgSetLong (const char * name, long val);
 
 
 /**
@@ -306,10 +301,7 @@ inline bool fgSetLong (const string &name, long val)
  * @param val The new value for the property.
  * @return true if the assignment succeeded, false otherwise.
  */
-inline bool fgSetFloat (const string &name, float val)
-{
-  return globals->get_props()->setFloatValue(name, val);
-}
+extern bool fgSetFloat (const char * name, float val);
 
 
 /**
@@ -325,10 +317,7 @@ inline bool fgSetFloat (const string &name, float val)
  * @param val The new value for the property.
  * @return true if the assignment succeeded, false otherwise.
  */
-inline bool fgSetDouble (const string &name, double val)
-{
-  return globals->get_props()->setDoubleValue(name, val);
-}
+extern bool fgSetDouble (const char * name, double val);
 
 
 /**
@@ -344,10 +333,7 @@ inline bool fgSetDouble (const string &name, double val)
  * @param val The new value for the property.
  * @return true if the assignment succeeded, false otherwise.
  */
-inline bool fgSetString (const string &name, const string &val)
-{
-  return globals->get_props()->setStringValue(name, val);
-}
+extern bool fgSetString (const char * name, const char * val);
 
 
 \f
@@ -368,17 +354,7 @@ inline bool fgSetString (const string &name, const string &val)
  * @param name The property name.
  * @param state The state of the archive attribute (defaults to true).
  */
-inline void
-fgSetArchivable (const string &name, bool state = true)
-{
-  SGPropertyNode * node = globals->get_props()->getNode(name);
-  if (node == 0)
-    SG_LOG(SG_GENERAL, SG_ALERT,
-          "Attempt to set archive flag for non-existant property "
-          << name);
-  else
-    node->setAttribute(SGPropertyNode::ARCHIVE, state);
-}
+extern void fgSetArchivable (const char * name, bool state = true);
 
 
 /**
@@ -393,17 +369,7 @@ fgSetArchivable (const string &name, bool state = true)
  * @param name The property name.
  * @param state The state of the read attribute (defaults to true).
  */
-inline void
-fgSetReadable (const string &name, bool state = true)
-{
-  SGPropertyNode * node = globals->get_props()->getNode(name);
-  if (node == 0)
-    SG_LOG(SG_GENERAL, SG_ALERT,
-          "Attempt to set read flag for non-existant property "
-          << name);
-  else
-    node->setAttribute(SGPropertyNode::READ, state);
-}
+extern void fgSetReadable (const char * name, bool state = true);
 
 
 /**
@@ -418,17 +384,7 @@ fgSetReadable (const string &name, bool state = true)
  * @param name The property name.
  * @param state The state of the write attribute (defaults to true).
  */
-inline void
-fgSetWritable (const string &name, bool state = true)
-{
-  SGPropertyNode * node = globals->get_props()->getNode(name);
-  if (node == 0)
-    SG_LOG(SG_GENERAL, SG_ALERT,
-          "Attempt to set write flag for non-existant property "
-          << name);
-  else
-    node->setAttribute(SGPropertyNode::WRITE, state);
-}
+extern void fgSetWritable (const char * name, bool state = true);
 
 
 \f
@@ -443,146 +399,7 @@ fgSetWritable (const string &name, bool state = true)
  * Classes should use this function to release control of any
  * properties they are managing.
  */
-inline void
-fgUntie (const string &name)
-{
-  if (!globals->get_props()->untie(name))
-    SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
-}
-
-
-                               // Templates cause ambiguity here
-
-/**
- * Tie a property to an external bool variable.
- *
- * The property's value will automatically mirror the variable's
- * value, and vice-versa, until the property is untied.
- *
- * @param name The property name to tie (full path).
- * @param pointer A pointer to the variable.
- * @param useDefault true if any existing property value should be
- *        copied to the variable; false if the variable should not
- *        be modified; defaults to true.
- */
-inline void
-fgTie (const string &name, bool *pointer, bool useDefault = true)
-{
-  if (!globals->get_props()->tie(name, SGRawValuePointer<bool>(pointer),
-                                useDefault))
-    SG_LOG(SG_GENERAL, SG_WARN,
-          "Failed to tie property " << name << " to a pointer");
-}
-
-
-/**
- * Tie a property to an external int variable.
- *
- * The property's value will automatically mirror the variable's
- * value, and vice-versa, until the property is untied.
- *
- * @param name The property name to tie (full path).
- * @param pointer A pointer to the variable.
- * @param useDefault true if any existing property value should be
- *        copied to the variable; false if the variable should not
- *        be modified; defaults to true.
- */
-inline void
-fgTie (const string &name, int *pointer, bool useDefault = true)
-{
-  if (!globals->get_props()->tie(name, SGRawValuePointer<int>(pointer),
-                                useDefault))
-    SG_LOG(SG_GENERAL, SG_WARN,
-          "Failed to tie property " << name << " to a pointer");
-}
-
-
-/**
- * Tie a property to an external long variable.
- *
- * The property's value will automatically mirror the variable's
- * value, and vice-versa, until the property is untied.
- *
- * @param name The property name to tie (full path).
- * @param pointer A pointer to the variable.
- * @param useDefault true if any existing property value should be
- *        copied to the variable; false if the variable should not
- *        be modified; defaults to true.
- */
-inline void
-fgTie (const string &name, long *pointer, bool useDefault = true)
-{
-  if (!globals->get_props()->tie(name, SGRawValuePointer<long>(pointer),
-                                useDefault))
-    SG_LOG(SG_GENERAL, SG_WARN,
-          "Failed to tie property " << name << " to a pointer");
-}
-
-
-/**
- * Tie a property to an external float variable.
- *
- * The property's value will automatically mirror the variable's
- * value, and vice-versa, until the property is untied.
- *
- * @param name The property name to tie (full path).
- * @param pointer A pointer to the variable.
- * @param useDefault true if any existing property value should be
- *        copied to the variable; false if the variable should not
- *        be modified; defaults to true.
- */
-inline void
-fgTie (const string &name, float *pointer, bool useDefault = true)
-{
-  if (!globals->get_props()->tie(name, SGRawValuePointer<float>(pointer),
-                                useDefault))
-    SG_LOG(SG_GENERAL, SG_WARN,
-          "Failed to tie property " << name << " to a pointer");
-}
-
-
-/**
- * Tie a property to an external double variable.
- *
- * The property's value will automatically mirror the variable's
- * value, and vice-versa, until the property is untied.
- *
- * @param name The property name to tie (full path).
- * @param pointer A pointer to the variable.
- * @param useDefault true if any existing property value should be
- *        copied to the variable; false if the variable should not
- *        be modified; defaults to true.
- */
-inline void
-fgTie (const string &name, double *pointer, bool useDefault = true)
-{
-  if (!globals->get_props()->tie(name, SGRawValuePointer<double>(pointer),
-                                useDefault))
-    SG_LOG(SG_GENERAL, SG_WARN,
-          "Failed to tie property " << name << " to a pointer");
-}
-
-
-/**
- * Tie a property to an external string variable.
- *
- * The property's value will automatically mirror the variable's
- * value, and vice-versa, until the property is untied.
- *
- * @param name The property name to tie (full path).
- * @param pointer A pointer to the variable.
- * @param useDefault true if any existing property value should be
- *        copied to the variable; false if the variable should not
- *        be modified; defaults to true.
- */
-inline void
-fgTie (const string &name, string *pointer, bool useDefault = true)
-{
-  if (!globals->get_props()->tie(name, SGRawValuePointer<string>(pointer),
-                                useDefault))
-    SG_LOG(SG_GENERAL, SG_WARN,
-          "Failed to tie property " << name << " to a pointer");
-}
+extern void fgUntie (const char * name);
 
 
 /**
@@ -603,7 +420,7 @@ fgTie (const string &name, string *pointer, bool useDefault = true)
  */
 template <class V>
 inline void
-fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0,
+fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
        bool useDefault = true)
 {
   if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
@@ -633,7 +450,7 @@ fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0,
  */
 template <class V>
 inline void
-fgTie (const string &name, int index, V (*getter)(int),
+fgTie (const char * name, int index, V (*getter)(int),
        void (*setter)(int, V) = 0, bool useDefault = true)
 {
   if (!globals->get_props()->tie(name,
@@ -667,7 +484,7 @@ fgTie (const string &name, int index, V (*getter)(int),
  */
 template <class T, class V>
 inline void
-fgTie (const string &name, T * obj, V (T::*getter)() const,
+fgTie (const char * name, T * obj, V (T::*getter)() const,
        void (T::*setter)(V) = 0, bool useDefault = true)
 {
   if (!globals->get_props()->tie(name,
@@ -699,7 +516,7 @@ fgTie (const string &name, T * obj, V (T::*getter)() const,
  */
 template <class T, class V>
 inline void 
-fgTie (const string &name, T * obj, int index,
+fgTie (const char * name, T * obj, int index,
        V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
        bool useDefault = true)
 {
@@ -714,162 +531,18 @@ fgTie (const string &name, T * obj, int index,
 }
 
 
-\f
-////////////////////////////////////////////////////////////////////////
-// Conditions.
-////////////////////////////////////////////////////////////////////////
-
-
-/**
- * An encoded condition.
- *
- * This class encodes a single condition of some sort, possibly
- * connected with properties.
- *
- * This class should migrate to somewhere more general.
- */
-class FGCondition
-{
+class FGMakeUpperCase : public SGPropertyChangeListener {
 public:
-  FGCondition ();
-  virtual ~FGCondition ();
-  virtual bool test () const = 0;
+    void valueChanged(SGPropertyNode *node) {
+        if (node->getType() != simgear::props::STRING)
+            return;
+
+        char *s = const_cast<char *>(node->getStringValue());
+        for (; *s; s++)
+            *s = toupper(*s);
+    }
 };
 
 
-/**
- * Condition for a single property.
- *
- * This condition is true only if the property returns a boolean
- * true value.
- */
-class FGPropertyCondition : public FGCondition
-{
-public:
-  FGPropertyCondition (const string &propname);
-  virtual ~FGPropertyCondition ();
-  virtual bool test () const { return _node->getBoolValue(); }
-private:
-  const SGPropertyNode * _node;
-};
-
-
-/**
- * Condition for a 'not' operator.
- *
- * This condition is true only if the child condition is false.
- */
-class FGNotCondition : public FGCondition
-{
-public:
-                               // transfer pointer ownership
-  FGNotCondition (FGCondition * condition);
-  virtual ~FGNotCondition ();
-  virtual bool test () const;
-private:
-  FGCondition * _condition;
-};
-
-
-/**
- * Condition for an 'and' group.
- *
- * This condition is true only if all of the conditions
- * in the group are true.
- */
-class FGAndCondition : public FGCondition
-{
-public:
-  FGAndCondition ();
-  virtual ~FGAndCondition ();
-  virtual bool test () const;
-                               // transfer pointer ownership
-  virtual void addCondition (FGCondition * condition);
-private:
-  vector<FGCondition *> _conditions;
-};
-
-
-/**
- * Condition for an 'or' group.
- *
- * This condition is true if at least one of the conditions in the
- * group is true.
- */
-class FGOrCondition : public FGCondition
-{
-public:
-  FGOrCondition ();
-  virtual ~FGOrCondition ();
-  virtual bool test () const;
-                               // transfer pointer ownership
-  virtual void addCondition (FGCondition * condition);
-private:
-  vector<FGCondition *> _conditions;
-};
-
-
-/**
- * Abstract base class for property comparison conditions.
- */
-class FGComparisonCondition : public FGCondition
-{
-public:
-  enum Type {
-    LESS_THAN,
-    GREATER_THAN,
-    EQUALS
-  };
-  FGComparisonCondition (Type type, bool reverse = false);
-  virtual ~FGComparisonCondition ();
-  virtual bool test () const;
-  virtual void setLeftProperty (const string &propname);
-  virtual void setRightProperty (const string &propname);
-                               // will make a local copy
-  virtual void setRightValue (const SGPropertyNode * value);
-private:
-  Type _type;
-  bool _reverse;
-  const SGPropertyNode * _left_property;
-  const SGPropertyNode * _right_property;
-  const SGPropertyNode * _right_value;
-};
-
-
-/**
- * Base class for a conditional components.
- *
- * This class manages the conditions and tests; the component should
- * invoke the test() method whenever it needs to decide whether to
- * active itself, draw itself, and so on.
- */
-class FGConditional
-{
-public:
-  FGConditional ();
-  virtual ~FGConditional ();
-                               // transfer pointer ownership
-  virtual void setCondition (FGCondition * condition);
-  virtual const FGCondition * getCondition () const { return _condition; }
-  virtual bool test () const;
-private:
-  FGCondition * _condition;
-};
-
-
-/**
- * Global function to make a condition out of properties.
- *
- * The top-level is always an implicit 'and' group, whatever the
- * node's name (it should usually be "condition").
- *
- * @param node The top-level condition node (usually named "condition").
- * @return A pointer to a newly-allocated condition; it is the
- *         responsibility of the caller to delete the condition when
- *         it is no longer needed.
- */
-FGCondition * fgReadCondition (const SGPropertyNode * node);
-
-
 #endif // __FG_PROPS_HXX