1 // Copyright (C) 2010 James Turner - zakalawe@mac.com
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #ifndef SG_PROPERTY_OBJECT
19 #define SG_PROPERTY_OBJECT
21 #include <simgear/props/props.hxx>
26 class PropertyObjectBase
29 static void setDefaultRoot(SGPropertyNode* aRoot);
31 PropertyObjectBase(const PropertyObjectBase& aOther);
33 PropertyObjectBase(const char* aChild);
35 PropertyObjectBase(SGPropertyNode* aNode, const char* aChild = NULL);
37 SGPropertyNode* node(bool aCreate) const;
40 * Resolve the property node, or throw an exception if it could not
43 SGPropertyNode* getOrThrow() const;
45 mutable const char* _path;
48 * Important - if _path is NULL, this is the actual prop.
49 * If path is non-NULL, this is the parent which path should be resolved
50 * against (or NULL, if _path is absolute). Use node() instead of accessing
51 * this directly, and the above is handled automatically.
53 mutable SGPropertyNode* _prop;
57 class PropertyObject : PropertyObjectBase
61 * Create from path relative to the default root, and option default value
63 PropertyObject(const char* aChild) :
64 PropertyObjectBase(aChild)
68 * Create from a node, with optional relative path
70 PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
71 PropertyObjectBase(aNode, aChild)
77 PropertyObject(const PropertyObject<T>& aOther) :
78 PropertyObjectBase(aOther)
82 // create() form creates the property immediately
83 static PropertyObject<T> create(const char* aPath, T aValue)
85 PropertyObject<T> p(aPath);
90 static PropertyObject<T> create(SGPropertyNode* aNode, T aValue)
92 PropertyObject<T> p(aNode);
97 static PropertyObject<T> create(SGPropertyNode* aNode, const char* aChild, T aValue)
99 PropertyObject<T> p(aNode, aChild);
104 // conversion operators
107 return getOrThrow()->getValue<T>();
110 T operator=(const T& aValue)
112 SGPropertyNode* n = PropertyObjectBase::node(true);
117 n->setValue<T>(aValue);
121 SGPropertyNode* node() const
123 return PropertyObjectBase::node(false);
125 }; // of template PropertyObject
128 // specialization for const char* / std::string
131 class PropertyObject<std::string> : PropertyObjectBase
134 PropertyObject(const char* aChild) :
135 PropertyObjectBase(aChild)
140 PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
141 PropertyObjectBase(aNode, aChild)
147 PropertyObject(const PropertyObject<std::string>& aOther) :
148 PropertyObjectBase(aOther)
153 static PropertyObject<std::string> create(const char* aPath, const std::string& aValue)
155 PropertyObject<std::string> p(aPath);
160 static PropertyObject<std::string> create(SGPropertyNode* aNode, const std::string& aValue)
162 PropertyObject<std::string> p(aNode);
167 static PropertyObject<std::string> create(SGPropertyNode* aNode, const char* aChild, const std::string& aValue)
169 PropertyObject<std::string> p(aNode, aChild);
175 operator std::string () const
177 return getOrThrow()->getStringValue();
180 const char* operator=(const char* aValue)
182 SGPropertyNode* n = PropertyObjectBase::node(true);
187 n->setStringValue(aValue);
191 std::string operator=(const std::string& aValue)
193 SGPropertyNode* n = PropertyObjectBase::node(true);
198 n->setStringValue(aValue);
202 bool operator==(const char* value) const
204 std::string s(*this);
208 bool operator==(const std::string& value) const
210 std::string s(*this);
214 SGPropertyNode* node() const
216 return PropertyObjectBase::node(false);
221 } // of namespace simgear
224 typedef simgear::PropertyObject<double> SGPropObjDouble;
225 typedef simgear::PropertyObject<bool> SGPropObjBool;
226 typedef simgear::PropertyObject<std::string> SGPropObjString;
227 typedef simgear::PropertyObject<long> SGPropObjInt;