X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fprops%2FpropertyObject.hxx;h=5cc1588ba0514dd8f264b419ace108b6472e6e2c;hb=708ae35068499af33329f9db91f55441f4956acb;hp=0067cb5e3eba1dae60ff7aef708d894a37d85e15;hpb=6da8ef83af22c4a7d65826b5d1e91f98381a12ca;p=simgear.git diff --git a/simgear/props/propertyObject.hxx b/simgear/props/propertyObject.hxx index 0067cb5e..5cc1588b 100644 --- a/simgear/props/propertyObject.hxx +++ b/simgear/props/propertyObject.hxx @@ -19,7 +19,6 @@ #define SG_PROPERTY_OBJECT #include -#include namespace simgear { @@ -29,15 +28,28 @@ class PropertyObjectBase public: static void setDefaultRoot(SGPropertyNode* aRoot); + PropertyObjectBase(const PropertyObjectBase& aOther); + PropertyObjectBase(const char* aChild); PropertyObjectBase(SGPropertyNode* aNode, const char* aChild = NULL); 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; }; @@ -61,6 +73,12 @@ public: } +// copy-constructor + PropertyObject(const PropertyObject& aOther) : + PropertyObjectBase(aOther) + { + } + // create() form creates the property immediately static PropertyObject create(const char* aPath, T aValue) { @@ -86,17 +104,12 @@ public: // conversion operators operator T () const { - SGPropertyNode* n = node(false); - if (!n) { - throw sg_exception("read of undefined property:", _path); - } - - return n->getValue(); + return getOrThrow()->getValue(); } T operator=(const T& aValue) { - SGPropertyNode* n = node(true); + SGPropertyNode* n = PropertyObjectBase::node(true); if (!n) { return aValue; } @@ -105,6 +118,10 @@ public: return aValue; } + SGPropertyNode* node() const + { + return PropertyObjectBase::node(false); + } }; // of template PropertyObject @@ -126,21 +143,43 @@ public: } +// copy-constructor + PropertyObject(const PropertyObject& aOther) : + PropertyObjectBase(aOther) + { + } + +// create form + static PropertyObject create(const char* aPath, const std::string& aValue) + { + PropertyObject p(aPath); + p = aValue; + return p; + } + + static PropertyObject create(SGPropertyNode* aNode, const std::string& aValue) + { + PropertyObject p(aNode); + p = aValue; + return p; + } + static PropertyObject create(SGPropertyNode* aNode, const char* aChild, const std::string& aValue) + { + PropertyObject p(aNode, aChild); + p = aValue; + return p; + } + operator std::string () const { - SGPropertyNode* n = node(false); - if (!n) { - throw sg_exception("read of undefined property:", _path); - } - - return n->getStringValue(); + return getOrThrow()->getStringValue(); } const char* operator=(const char* aValue) { - SGPropertyNode* n = node(true); + SGPropertyNode* n = PropertyObjectBase::node(true); if (!n) { return aValue; } @@ -151,7 +190,7 @@ public: std::string operator=(const std::string& aValue) { - SGPropertyNode* n = node(true); + SGPropertyNode* n = PropertyObjectBase::node(true); if (!n) { return aValue; } @@ -160,14 +199,32 @@ public: return aValue; } + bool operator==(const char* value) const + { + std::string s(*this); + return (s == value); + } + + bool operator==(const std::string& value) const + { + std::string s(*this); + return (s == value); + } + + SGPropertyNode* node() const + { + return PropertyObjectBase::node(false); + } private: }; } // of namespace simgear +/* typedef simgear::PropertyObject SGPropObjDouble; typedef simgear::PropertyObject SGPropObjBool; typedef simgear::PropertyObject SGPropObjString; typedef simgear::PropertyObject SGPropObjInt; +*/ #endif