]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAPropertyDataElement.hxx
Merge remote branch 'origin/releases/2.2.0' into next
[simgear.git] / simgear / hla / HLAPropertyDataElement.hxx
1 // Copyright (C) 2009 - 2010  Mathias Froehlich - Mathias.Froehlich@web.de
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 HLAPropertyDataElement_hxx
19 #define HLAPropertyDataElement_hxx
20
21 #include <set>
22 #include <simgear/props/props.hxx>
23 #include "HLADataElement.hxx"
24
25 namespace simgear {
26
27 class HLAPropertyReference : public SGReferenced {
28 public:
29     HLAPropertyReference()
30     { }
31     HLAPropertyReference(const std::string& relativePath) :
32         _relativePath(relativePath)
33     { }
34
35     void setIntValue(int value)
36     {
37         if (!_propertyNode.valid())
38             return;
39         _propertyNode->setIntValue(value);
40     }
41     int getIntValue() const
42     {
43         if (!_propertyNode.valid())
44             return 0;
45         return _propertyNode->getIntValue();
46     }
47
48     void setLongValue(long value)
49     {
50         if (!_propertyNode.valid())
51             return;
52         _propertyNode->setLongValue(value);
53     }
54     long getLongValue() const
55     {
56         if (!_propertyNode.valid())
57             return 0;
58         return _propertyNode->getLongValue();
59     }
60
61     void setFloatValue(float value)
62     {
63         if (!_propertyNode.valid())
64             return;
65         _propertyNode->setFloatValue(value);
66     }
67     float getFloatValue() const
68     {
69         if (!_propertyNode.valid())
70             return 0;
71         return _propertyNode->getFloatValue();
72     }
73
74     void setDoubleValue(double value)
75     {
76         if (!_propertyNode.valid())
77             return;
78         _propertyNode->setDoubleValue(value);
79     }
80     double getDoubleValue() const
81     {
82         if (!_propertyNode.valid())
83             return 0;
84         return _propertyNode->getDoubleValue();
85     }
86
87     void setStringValue(const std::string& value)
88     {
89         if (!_propertyNode.valid())
90             return;
91         _propertyNode->setStringValue(value);
92     }
93     std::string getStringValue() const
94     {
95         if (!_propertyNode.valid())
96             return std::string();
97         return _propertyNode->getStringValue();
98     }
99
100     SGPropertyNode* getPropertyNode()
101     { return _propertyNode.get(); }
102
103     void setRootNode(SGPropertyNode* rootNode)
104     {
105         if (!rootNode)
106             _propertyNode.clear();
107         else
108             _propertyNode = rootNode->getNode(_relativePath, true);
109     }
110
111 private:
112     std::string _relativePath;
113     SGSharedPtr<SGPropertyNode> _propertyNode;
114 };
115
116 class HLAPropertyReferenceSet : public SGReferenced {
117 public:
118     void insert(const SGSharedPtr<HLAPropertyReference>& propertyReference)
119     {
120         _propertyReferenceSet.insert(propertyReference);
121         propertyReference->setRootNode(_rootNode.get());
122     }
123     void remove(const SGSharedPtr<HLAPropertyReference>& propertyReference)
124     {
125         PropertyReferenceSet::iterator i = _propertyReferenceSet.find(propertyReference);
126         if (i == _propertyReferenceSet.end())
127             return;
128         _propertyReferenceSet.erase(i);
129         propertyReference->setRootNode(0);
130     }
131
132     void setRootNode(SGPropertyNode* rootNode)
133     {
134         _rootNode = rootNode;
135         for (PropertyReferenceSet::iterator i = _propertyReferenceSet.begin();
136              i != _propertyReferenceSet.end(); ++i) {
137             (*i)->setRootNode(_rootNode.get());
138         }
139     }
140     SGPropertyNode* getRootNode()
141     { return _rootNode.get(); }
142
143 private:
144     SGSharedPtr<SGPropertyNode> _rootNode;
145
146     typedef std::set<SGSharedPtr<HLAPropertyReference> > PropertyReferenceSet;
147     PropertyReferenceSet _propertyReferenceSet;
148 };
149
150 class HLAPropertyDataElement : public HLADataElement {
151 public:
152     HLAPropertyDataElement(HLAPropertyReference* propertyReference);
153     HLAPropertyDataElement(const simgear::HLADataType* dataType, HLAPropertyReference* propertyReference);
154     ~HLAPropertyDataElement();
155
156     virtual bool encode(HLAEncodeStream& stream) const;
157     virtual bool decode(HLADecodeStream& stream);
158
159     virtual const HLADataType* getDataType() const;
160     virtual bool setDataType(const HLADataType* dataType);
161
162 private:
163     class DecodeVisitor;
164     class EncodeVisitor;
165
166     SGSharedPtr<const HLADataType> _dataType;
167     SGSharedPtr<HLAPropertyReference> _propertyReference;
168 };
169
170 } // namespace simgear
171
172 #endif