]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAPropertyDataElement.hxx
hla: Provide a directly property based api for property data element.
[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     SGPropertyNode* getPropertyNode()
36     { return _propertyNode.get(); }
37
38     void setRootNode(SGPropertyNode* rootNode)
39     {
40         if (!rootNode)
41             _propertyNode.clear();
42         else
43             _propertyNode = rootNode->getNode(_relativePath, true);
44     }
45
46 private:
47     std::string _relativePath;
48     SGSharedPtr<SGPropertyNode> _propertyNode;
49 };
50
51 class HLAPropertyReferenceSet : public SGReferenced {
52 public:
53     void insert(const SGSharedPtr<HLAPropertyReference>& propertyReference)
54     {
55         _propertyReferenceSet.insert(propertyReference);
56         propertyReference->setRootNode(_rootNode.get());
57     }
58     void remove(const SGSharedPtr<HLAPropertyReference>& propertyReference)
59     {
60         PropertyReferenceSet::iterator i = _propertyReferenceSet.find(propertyReference);
61         if (i == _propertyReferenceSet.end())
62             return;
63         _propertyReferenceSet.erase(i);
64         propertyReference->setRootNode(0);
65     }
66
67     void setRootNode(SGPropertyNode* rootNode)
68     {
69         _rootNode = rootNode;
70         for (PropertyReferenceSet::iterator i = _propertyReferenceSet.begin();
71              i != _propertyReferenceSet.end(); ++i) {
72             (*i)->setRootNode(_rootNode.get());
73         }
74     }
75     SGPropertyNode* getRootNode()
76     { return _rootNode.get(); }
77
78 private:
79     SGSharedPtr<SGPropertyNode> _rootNode;
80
81     typedef std::set<SGSharedPtr<HLAPropertyReference> > PropertyReferenceSet;
82     PropertyReferenceSet _propertyReferenceSet;
83 };
84
85 class HLAPropertyDataElement : public HLADataElement {
86 public:
87     HLAPropertyDataElement(HLAPropertyReference* propertyReference);
88     HLAPropertyDataElement(const HLADataType* dataType, HLAPropertyReference* propertyReference);
89     HLAPropertyDataElement();
90     HLAPropertyDataElement(SGPropertyNode* propertyNode);
91     HLAPropertyDataElement(const HLADataType* dataType, SGPropertyNode* propertyNode);
92     virtual ~HLAPropertyDataElement();
93
94     virtual bool encode(HLAEncodeStream& stream) const;
95     virtual bool decode(HLADecodeStream& stream);
96
97     virtual const HLADataType* getDataType() const;
98     virtual bool setDataType(const HLADataType* dataType);
99
100     void setPropertyNode(SGPropertyNode* propertyNode);
101     SGPropertyNode* getPropertyNode();
102     const SGPropertyNode* getPropertyNode() const;
103     
104 private:
105     class DecodeVisitor;
106     class EncodeVisitor;
107
108     SGSharedPtr<const HLADataType> _dataType;
109     SGSharedPtr<HLAPropertyReference> _propertyReference;
110 };
111
112 } // namespace simgear
113
114 #endif