]> git.mxchange.org Git - simgear.git/blob - simgear/props/propertyObject.hxx
Terrain tiles are not light volumes
[simgear.git] / simgear / props / propertyObject.hxx
1 // Copyright (C) 2010  James Turner - zakalawe@mac.com
2 //
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.
7 //
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.
12 //
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.
16 //
17
18 #ifndef SG_PROPERTY_OBJECT
19 #define SG_PROPERTY_OBJECT
20
21 #include <simgear/props/props.hxx>
22
23 namespace simgear
24 {
25
26 class PropertyObjectBase
27 {
28 public:
29   static void setDefaultRoot(SGPropertyNode* aRoot);
30   
31   PropertyObjectBase();
32   
33   PropertyObjectBase(const PropertyObjectBase& aOther);
34     
35   PropertyObjectBase(const char* aChild);
36   
37   PropertyObjectBase(SGPropertyNode* aNode, const char* aChild = NULL);
38   
39   SGPropertyNode* node(bool aCreate) const;
40
41   /**
42    * Resolve the property node, or throw an exception if it could not
43    * be resolved.
44    */
45   SGPropertyNode* getOrThrow() const;
46 protected:
47   mutable const char* _path;
48
49   /**
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. 
54    */
55   mutable SGPropertyNode* _prop;
56 };
57
58 template <typename T>
59 class PropertyObject : PropertyObjectBase
60 {
61 public:
62   PropertyObject();
63   
64   /**
65    * Create from path relative to the default root, and option default value
66    */
67   PropertyObject(const char* aChild) :
68     PropertyObjectBase(aChild)
69   { }
70   
71   /**
72    * Create from a node, with optional relative path
73    */
74   PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
75     PropertyObjectBase(aNode, aChild)
76   {
77   
78   }
79   
80 // copy-constructor
81   PropertyObject(const PropertyObject<T>& aOther) :
82     PropertyObjectBase(aOther)
83   {
84   }
85
86 // create() form creates the property immediately
87   static PropertyObject<T> create(const char* aPath, T aValue)
88   {
89     PropertyObject<T> p(aPath);
90     p = aValue;
91     return p;
92   }
93   
94   static PropertyObject<T> create(SGPropertyNode* aNode, T aValue)
95   {
96     PropertyObject<T> p(aNode);
97     p = aValue;
98     return p;
99   }
100
101   static PropertyObject<T> create(SGPropertyNode* aNode, const char* aChild, T aValue)
102   {
103     PropertyObject<T> p(aNode, aChild);
104     p = aValue;
105     return p;
106   }
107   
108 // conversion operators
109   operator T () const
110   {
111     return getOrThrow()->getValue<T>();
112   }
113
114   T operator=(const T& aValue)
115   {
116     SGPropertyNode* n = PropertyObjectBase::node(true);
117     if (!n) {
118       return aValue;
119     }
120     
121     n->setValue<T>(aValue);
122     return aValue;
123   }
124
125   SGPropertyNode* node() const
126   {
127     return PropertyObjectBase::node(false);
128   }
129 }; // of template PropertyObject
130
131
132 // specialization for const char* / std::string
133
134 template <>
135 class PropertyObject<std::string> : PropertyObjectBase
136 {
137 public:
138   PropertyObject(const char* aChild) :
139     PropertyObjectBase(aChild)
140   { }
141   
142
143   
144   PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
145     PropertyObjectBase(aNode, aChild)
146   {
147   
148   }
149   
150 // copy-constructor
151   PropertyObject(const PropertyObject<std::string>& aOther) :
152     PropertyObjectBase(aOther)
153   {
154   }
155
156 // create form
157   static PropertyObject<std::string> create(const char* aPath, const std::string& aValue)
158   {
159     PropertyObject<std::string> p(aPath);
160     p = aValue;
161     return p;
162   }
163   
164   static PropertyObject<std::string> create(SGPropertyNode* aNode, const std::string& aValue)
165   {
166     PropertyObject<std::string> p(aNode);
167     p = aValue;
168     return p;
169   }
170
171   static PropertyObject<std::string> create(SGPropertyNode* aNode, const char* aChild, const std::string& aValue)
172   {
173     PropertyObject<std::string> p(aNode, aChild);
174     p = aValue;
175     return p;
176   }
177   
178   
179   operator std::string () const
180   {
181     return getOrThrow()->getStringValue();
182   }
183   
184   const char* operator=(const char* aValue)
185   {
186     SGPropertyNode* n = PropertyObjectBase::node(true);
187     if (!n) {
188       return aValue;
189     }
190     
191     n->setStringValue(aValue);
192     return aValue;
193   }
194   
195   std::string operator=(const std::string& aValue)
196   {
197     SGPropertyNode* n = PropertyObjectBase::node(true);
198     if (!n) {
199       return aValue;
200     }
201     
202     n->setStringValue(aValue);
203     return aValue;
204   }
205   
206   bool operator==(const char* value) const
207   {
208     std::string s(*this);
209     return (s == value);    
210   }
211
212   bool operator==(const std::string& value) const
213   {
214     std::string s(*this);
215     return (s == value);    
216   }
217
218   SGPropertyNode* node() const
219   {
220     return PropertyObjectBase::node(false);
221   }
222 private:
223 };
224
225 } // of namespace simgear
226
227 typedef simgear::PropertyObject<double> SGPropObjDouble;
228 typedef simgear::PropertyObject<bool> SGPropObjBool;
229 typedef simgear::PropertyObject<std::string> SGPropObjString;
230 typedef simgear::PropertyObject<long> SGPropObjInt;
231
232 #endif