]> git.mxchange.org Git - simgear.git/blob - simgear/props/propertyObject.hxx
Initial propertyObject work.
[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 #include <simgear/structure/exception.hxx>
23
24 namespace simgear
25 {
26
27 class PropertyObjectBase
28 {
29 public:
30   static void setDefaultRoot(SGPropertyNode* aRoot);
31   
32   PropertyObjectBase(const char* aChild);
33   
34   PropertyObjectBase(SGPropertyNode* aNode, const char* aChild = NULL);
35   
36   SGPropertyNode* node(bool aCreate) const;
37
38 protected:
39   SGPropertyNode* _base;
40   const char* _path;
41   mutable SGPropertyNode* _prop;
42 };
43
44 template <typename T>
45 class PropertyObject : PropertyObjectBase
46 {
47 public:
48   /**
49    * Create from path relative to the default root, and option default value
50    */
51   PropertyObject(const char* aChild) :
52     PropertyObjectBase(aChild)
53   { }
54   
55   /**
56    * Create from a node, with optional relative path
57    */
58   PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
59     PropertyObjectBase(aNode, aChild)
60   {
61   
62   }
63   
64 // create() form creates the property immediately
65   static PropertyObject<T> create(const char* aPath, T aValue)
66   {
67     PropertyObject<T> p(aPath);
68     p = aValue;
69     return p;
70   }
71   
72   static PropertyObject<T> create(SGPropertyNode* aNode, T aValue)
73   {
74     PropertyObject<T> p(aNode);
75     p = aValue;
76     return p;
77   }
78
79   static PropertyObject<T> create(SGPropertyNode* aNode, const char* aChild, T aValue)
80   {
81     PropertyObject<T> p(aNode, aChild);
82     p = aValue;
83     return p;
84   }
85   
86 // conversion operators
87   operator T () const
88   {
89     SGPropertyNode* n = node(false);
90     if (!n) {
91       throw sg_exception("read of undefined property:", _path);
92     }
93     
94     return n->getValue<T>();
95   }
96
97   T operator=(const T& aValue)
98   {
99     SGPropertyNode* n = node(true);
100     if (!n) {
101       return aValue;
102     }
103     
104     n->setValue<T>(aValue);
105     return aValue;
106   }
107
108 }; // of template PropertyObject
109
110
111 // specialization for const char* / std::string
112
113 template <>
114 class PropertyObject<std::string> : PropertyObjectBase
115 {
116 public:
117   PropertyObject(const char* aChild) :
118     PropertyObjectBase(aChild)
119   { }
120   
121
122   
123   PropertyObject(SGPropertyNode* aNode, const char* aChild = NULL) :
124     PropertyObjectBase(aNode, aChild)
125   {
126   
127   }
128   
129
130   
131   operator std::string () const
132   {
133     SGPropertyNode* n = node(false);
134     if (!n) {
135       throw sg_exception("read of undefined property:", _path);
136     }
137     
138     return n->getStringValue();
139   }
140   
141   const char* operator=(const char* aValue)
142   {
143     SGPropertyNode* n = node(true);
144     if (!n) {
145       return aValue;
146     }
147     
148     n->setStringValue(aValue);
149     return aValue;
150   }
151   
152   std::string operator=(const std::string& aValue)
153   {
154     SGPropertyNode* n = node(true);
155     if (!n) {
156       return aValue;
157     }
158     
159     n->setStringValue(aValue);
160     return aValue;
161   }
162   
163 private:
164 };
165
166 } // of namespace simgear
167
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;
172
173 #endif