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>
22 #include <simgear/structure/exception.hxx>
27 class PropertyObjectBase
30 static void setDefaultRoot(SGPropertyNode* aRoot);
32 PropertyObjectBase(const PropertyObjectBase& aOther);
34 PropertyObjectBase(const char* aChild);
36 PropertyObjectBase(SGPropertyNode* aNode, const char* aChild = NULL);
38 SGPropertyNode* node(bool aCreate) const;
41 SGPropertyNode* _base;
43 mutable SGPropertyNode* _prop;
47 class PropertyObject : PropertyObjectBase
51 * Create from path relative to the default root, and option default value
53 PropertyObject(const char* aChild) :
54 PropertyObjectBase(aChild)
58 * Create from a node, with optional relative path
60 PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
61 PropertyObjectBase(aNode, aChild)
67 PropertyObject(const PropertyObject<T>& aOther) :
68 PropertyObjectBase(aOther)
72 // create() form creates the property immediately
73 static PropertyObject<T> create(const char* aPath, T aValue)
75 PropertyObject<T> p(aPath);
80 static PropertyObject<T> create(SGPropertyNode* aNode, T aValue)
82 PropertyObject<T> p(aNode);
87 static PropertyObject<T> create(SGPropertyNode* aNode, const char* aChild, T aValue)
89 PropertyObject<T> p(aNode, aChild);
94 // conversion operators
97 SGPropertyNode* n = node();
99 throw sg_exception("read of undefined property:", _path);
102 return n->getValue<T>();
105 T operator=(const T& aValue)
107 SGPropertyNode* n = PropertyObjectBase::node(true);
112 n->setValue<T>(aValue);
116 SGPropertyNode* node() const
118 return PropertyObjectBase::node(false);
120 }; // of template PropertyObject
123 // specialization for const char* / std::string
126 class PropertyObject<std::string> : PropertyObjectBase
129 PropertyObject(const char* aChild) :
130 PropertyObjectBase(aChild)
135 PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
136 PropertyObjectBase(aNode, aChild)
143 operator std::string () const
145 SGPropertyNode* n = node();
147 throw sg_exception("read of undefined property:", _path);
150 return n->getStringValue();
153 const char* operator=(const char* aValue)
155 SGPropertyNode* n = PropertyObjectBase::node(true);
160 n->setStringValue(aValue);
164 std::string operator=(const std::string& aValue)
166 SGPropertyNode* n = PropertyObjectBase::node(true);
171 n->setStringValue(aValue);
175 bool operator==(const char* value) const
177 std::string s(*this);
181 bool operator==(const std::string& value) const
183 std::string s(*this);
187 SGPropertyNode* node() const
189 return PropertyObjectBase::node(false);
194 } // of namespace simgear
196 typedef simgear::PropertyObject<double> SGPropObjDouble;
197 typedef simgear::PropertyObject<bool> SGPropObjBool;
198 typedef simgear::PropertyObject<std::string> SGPropObjString;
199 typedef simgear::PropertyObject<long> SGPropObjInt;