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);
33 PropertyObjectBase(const PropertyObjectBase& aOther);
35 PropertyObjectBase(const char* aChild);
37 PropertyObjectBase(SGPropertyNode* aNode, const char* aChild = NULL);
39 SGPropertyNode* node(bool aCreate) const;
42 * Resolve the property node, or throw an exception if it could not
45 SGPropertyNode* getOrThrow() const;
47 mutable const char* _path;
50 * Important - if _path is NULL, this is the actual prop.
51 * If path is non-NULL, this is the parent which path should be resolved
52 * against (or NULL, if _path is absolute). Use node() instead of accessing
53 * this directly, and the above is handled automatically.
55 mutable SGPropertyNode* _prop;
59 class PropertyObject : PropertyObjectBase
66 * Create from path relative to the default root, and option default value
68 explicit PropertyObject(const char* aChild) :
69 PropertyObjectBase(aChild)
73 * Create from a node, with optional relative path
75 explicit PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
76 PropertyObjectBase(aNode, aChild)
82 PropertyObject(const PropertyObject<T>& aOther) :
83 PropertyObjectBase(aOther)
87 // create() form creates the property immediately
88 static PropertyObject<T> create(const char* aPath, T aValue)
90 PropertyObject<T> p(aPath);
95 static PropertyObject<T> create(SGPropertyNode* aNode, T aValue)
97 PropertyObject<T> p(aNode);
102 static PropertyObject<T> create(SGPropertyNode* aNode, const char* aChild, T aValue)
104 PropertyObject<T> p(aNode, aChild);
109 // conversion operators
112 return getOrThrow()->template getValue<T>();
115 T operator=(const T& aValue)
117 SGPropertyNode* n = PropertyObjectBase::node(true);
121 n->setValue<T>(aValue);
125 #define SG_DEF_ASSIGN_OP(op)\
126 T operator op##=(const T rhs)\
128 SGPropertyNode* n = getOrThrow();\
129 T new_val = n->getValue<T>() op rhs;\
130 n->setValue<T>(new_val);\
145 #undef SG_DEF_ASSIGN_OP
147 SGPropertyNode* node() const
149 return PropertyObjectBase::node(false);
151 }; // of template PropertyObject
154 // specialization for const char* / std::string
157 class PropertyObject<std::string> : PropertyObjectBase
160 explicit PropertyObject(const char* aChild) :
161 PropertyObjectBase(aChild)
166 explicit PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
167 PropertyObjectBase(aNode, aChild)
173 PropertyObject(const PropertyObject<std::string>& aOther) :
174 PropertyObjectBase(aOther)
179 static PropertyObject<std::string> create(const char* aPath, const std::string& aValue)
181 PropertyObject<std::string> p(aPath);
186 static PropertyObject<std::string> create(SGPropertyNode* aNode, const std::string& aValue)
188 PropertyObject<std::string> p(aNode);
193 static PropertyObject<std::string> create(SGPropertyNode* aNode, const char* aChild, const std::string& aValue)
195 PropertyObject<std::string> p(aNode, aChild);
201 operator std::string () const
203 return getOrThrow()->getStringValue();
206 const char* operator=(const char* aValue)
208 SGPropertyNode* n = PropertyObjectBase::node(true);
213 n->setStringValue(aValue);
217 std::string operator=(const std::string& aValue)
219 SGPropertyNode* n = PropertyObjectBase::node(true);
224 n->setStringValue(aValue);
228 bool operator==(const char* value) const
230 std::string s(*this);
234 bool operator==(const std::string& value) const
236 std::string s(*this);
240 SGPropertyNode* node() const
242 return PropertyObjectBase::node(false);
247 } // of namespace simgear
249 typedef simgear::PropertyObject<double> SGPropObjDouble;
250 typedef simgear::PropertyObject<bool> SGPropObjBool;
251 typedef simgear::PropertyObject<std::string> SGPropObjString;
252 typedef simgear::PropertyObject<long> SGPropObjInt;