]> git.mxchange.org Git - simgear.git/blobdiff - simgear/props/propertyObject.hxx
Melchior FRANZ: fix SGPropertyNode::LAST_USED_ATTRIBUTE
[simgear.git] / simgear / props / propertyObject.hxx
index 7aeb0337285217166133f849807425db6b7f7708..5cc1588ba0514dd8f264b419ace108b6472e6e2c 100644 (file)
@@ -19,7 +19,6 @@
 #define SG_PROPERTY_OBJECT
 
 #include <simgear/props/props.hxx>
-#include <simgear/structure/exception.hxx>
 
 namespace simgear
 {
@@ -37,9 +36,20 @@ public:
   
   SGPropertyNode* node(bool aCreate) const;
 
+  /**
+   * Resolve the property node, or throw an exception if it could not
+   * be resolved.
+   */
+  SGPropertyNode* getOrThrow() const;
 protected:
-  SGPropertyNode* _base;
-  const char* _path;
+  mutable const char* _path;
+
+  /**
+   * Important - if _path is NULL, this is the actual prop.
+   * If path is non-NULL, this is the parent which path should be resolved
+   * against (or NULL, if _path is absolute). Use node() instead of accessing
+   * this directly, and the above is handled automatically. 
+   */
   mutable SGPropertyNode* _prop;
 };
 
@@ -94,12 +104,7 @@ public:
 // conversion operators
   operator T () const
   {
-    SGPropertyNode* n = node();
-    if (!n) {
-      throw sg_exception("read of undefined property:", _path);
-    }
-    
-    return n->getValue<T>();
+    return getOrThrow()->getValue<T>();
   }
 
   T operator=(const T& aValue)
@@ -138,16 +143,38 @@ public:
   
   }
   
+// copy-constructor
+  PropertyObject(const PropertyObject<std::string>& aOther) :
+    PropertyObjectBase(aOther)
+  {
+  }
+
+// create form
+  static PropertyObject<std::string> create(const char* aPath, const std::string& aValue)
+  {
+    PropertyObject<std::string> p(aPath);
+    p = aValue;
+    return p;
+  }
+  
+  static PropertyObject<std::string> create(SGPropertyNode* aNode, const std::string& aValue)
+  {
+    PropertyObject<std::string> p(aNode);
+    p = aValue;
+    return p;
+  }
 
+  static PropertyObject<std::string> create(SGPropertyNode* aNode, const char* aChild, const std::string& aValue)
+  {
+    PropertyObject<std::string> p(aNode, aChild);
+    p = aValue;
+    return p;
+  }
+  
   
   operator std::string () const
   {
-    SGPropertyNode* n = node();
-    if (!n) {
-      throw sg_exception("read of undefined property:", _path);
-    }
-    
-    return n->getStringValue();
+    return getOrThrow()->getStringValue();
   }
   
   const char* operator=(const char* aValue)
@@ -193,9 +220,11 @@ private:
 
 } // of namespace simgear
 
+/*
 typedef simgear::PropertyObject<double> SGPropObjDouble;
 typedef simgear::PropertyObject<bool> SGPropObjBool;
 typedef simgear::PropertyObject<std::string> SGPropObjString;
 typedef simgear::PropertyObject<long> SGPropObjInt;
+*/
 
 #endif