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 char* aChild);
34 PropertyObjectBase(SGPropertyNode* aNode, const char* aChild = NULL);
36 SGPropertyNode* node(bool aCreate) const;
39 SGPropertyNode* _base;
41 mutable SGPropertyNode* _prop;
45 class PropertyObject : PropertyObjectBase
49 * Create from path relative to the default root, and option default value
51 PropertyObject(const char* aChild) :
52 PropertyObjectBase(aChild)
56 * Create from a node, with optional relative path
58 PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
59 PropertyObjectBase(aNode, aChild)
64 // create() form creates the property immediately
65 static PropertyObject<T> create(const char* aPath, T aValue)
67 PropertyObject<T> p(aPath);
72 static PropertyObject<T> create(SGPropertyNode* aNode, T aValue)
74 PropertyObject<T> p(aNode);
79 static PropertyObject<T> create(SGPropertyNode* aNode, const char* aChild, T aValue)
81 PropertyObject<T> p(aNode, aChild);
86 // conversion operators
89 SGPropertyNode* n = node(false);
91 throw sg_exception("read of undefined property:", _path);
94 return n->getValue<T>();
97 T operator=(const T& aValue)
99 SGPropertyNode* n = node(true);
104 n->setValue<T>(aValue);
108 }; // of template PropertyObject
111 // specialization for const char* / std::string
114 class PropertyObject<std::string> : PropertyObjectBase
117 PropertyObject(const char* aChild) :
118 PropertyObjectBase(aChild)
123 PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
124 PropertyObjectBase(aNode, aChild)
131 operator std::string () const
133 SGPropertyNode* n = node(false);
135 throw sg_exception("read of undefined property:", _path);
138 return n->getStringValue();
141 const char* operator=(const char* aValue)
143 SGPropertyNode* n = node(true);
148 n->setStringValue(aValue);
152 std::string operator=(const std::string& aValue)
154 SGPropertyNode* n = node(true);
159 n->setStringValue(aValue);
166 } // of namespace simgear
168 typedef simgear::PropertyObject<double> SGPropObjDouble;
169 typedef simgear::PropertyObject<bool> SGPropObjBool;
170 typedef simgear::PropertyObject<std::string> SGPropObjString;
171 typedef simgear::PropertyObject<long> SGPropObjInt;