]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAVariantRecordDataElement.cxx
hla: Use a different extrapolation method for HLALocation.
[simgear.git] / simgear / hla / HLAVariantRecordDataElement.cxx
1 // Copyright (C) 2009 - 2012  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 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21
22 #include <simgear/compiler.h>
23
24 #include "HLAVariantRecordDataElement.hxx"
25
26 #include <simgear/debug/logstream.hxx>
27
28 #include "HLADataElementVisitor.hxx"
29 #include "HLADataTypeVisitor.hxx"
30
31 namespace simgear {
32
33 HLAAbstractVariantRecordDataElement::HLAAbstractVariantRecordDataElement(const HLAVariantRecordDataType* dataType) :
34     _dataType(dataType)
35 {
36 }
37
38 HLAAbstractVariantRecordDataElement::~HLAAbstractVariantRecordDataElement()
39 {
40 }
41
42 void
43 HLAAbstractVariantRecordDataElement::accept(HLADataElementVisitor& visitor)
44 {
45     visitor.apply(*this);
46 }
47
48 void
49 HLAAbstractVariantRecordDataElement::accept(HLAConstDataElementVisitor& visitor) const
50 {
51     visitor.apply(*this);
52 }
53
54 bool
55 HLAAbstractVariantRecordDataElement::decode(HLADecodeStream& stream)
56 {
57     if (!_dataType.valid())
58         return false;
59     return _dataType->decode(stream, *this);
60 }
61
62 bool
63 HLAAbstractVariantRecordDataElement::encode(HLAEncodeStream& stream) const
64 {
65     if (!_dataType.valid())
66         return false;
67     return _dataType->encode(stream, *this);
68 }
69
70 const HLAVariantRecordDataType*
71 HLAAbstractVariantRecordDataElement::getDataType() const
72 {
73     return _dataType.get();
74 }
75
76 bool
77 HLAAbstractVariantRecordDataElement::setDataType(const HLADataType* dataType)
78 {
79     const HLAVariantRecordDataType* variantRecordDataType = dataType->toVariantRecordDataType();
80     if (!variantRecordDataType) {
81         SG_LOG(SG_NETWORK, SG_WARN, "HLAVariantRecordDataType: unable to set data type!");
82         return false;
83     }
84     setDataType(variantRecordDataType);
85     return true;
86 }
87
88 void
89 HLAAbstractVariantRecordDataElement::setDataType(const HLAVariantRecordDataType* dataType)
90 {
91     _dataType = dataType;
92 }
93
94 std::string
95 HLAAbstractVariantRecordDataElement::getAlternativeName() const
96 {
97     if (!_dataType.valid())
98         return std::string();
99     return _dataType->getAlternativeName(getAlternativeIndex());
100 }
101
102 const HLADataType*
103 HLAAbstractVariantRecordDataElement::getAlternativeDataType() const
104 {
105     if (!_dataType.valid())
106         return 0;
107     return _dataType->getAlternativeDataType(getAlternativeIndex());
108 }
109
110
111 HLAVariantRecordDataElement::DataElementFactory::~DataElementFactory()
112 {
113 }
114
115 HLAVariantRecordDataElement::HLAVariantRecordDataElement(const HLAVariantRecordDataType* dataType) :
116     HLAAbstractVariantRecordDataElement(dataType),
117     _alternativeIndex(~0u)
118 {
119 }
120
121 HLAVariantRecordDataElement::~HLAVariantRecordDataElement()
122 {
123     clearStamp();
124 }
125
126 bool
127 HLAVariantRecordDataElement::setDataElement(HLADataElementIndex::const_iterator begin, HLADataElementIndex::const_iterator end, HLADataElement* dataElement)
128 {
129     // Must have happened in the parent
130     if (begin == end)
131         return false;
132     unsigned index = *begin;
133     if (++begin != end) {
134         if (!setAlternativeIndex(index))
135             return false;
136         if (!_dataElement.valid() && getAlternativeDataType()) {
137             HLADataElementFactoryVisitor visitor;
138             getAlternativeDataType()->accept(visitor);
139             _dataElement = visitor.getDataElement();
140         }
141         if (!_dataElement.valid())
142             return false;
143         return _dataElement->setDataElement(begin, end, dataElement);
144     } else {
145         if (!setAlternativeIndex(index))
146             return false;
147         if (!dataElement->setDataType(getAlternativeDataType()))
148             return false;
149         _dataElement = dataElement;
150         return true;
151     }
152 }
153
154 HLADataElement*
155 HLAVariantRecordDataElement::getDataElement(HLADataElementIndex::const_iterator begin, HLADataElementIndex::const_iterator end)
156 {
157     if (begin == end)
158         return this;
159     if (getAlternativeIndex() != *begin)
160         return 0;
161     if (!_dataElement.valid())
162         return 0;
163     return _dataElement->getDataElement(++begin, end);
164 }
165
166 const HLADataElement*
167 HLAVariantRecordDataElement::getDataElement(HLADataElementIndex::const_iterator begin, HLADataElementIndex::const_iterator end) const
168 {
169     if (begin == end)
170         return this;
171     if (getAlternativeIndex() != *begin)
172         return 0;
173     if (!_dataElement.valid())
174         return 0;
175     return _dataElement->getDataElement(++begin, end);
176 }
177
178 bool
179 HLAVariantRecordDataElement::setAlternativeIndex(unsigned index)
180 {
181     if (_alternativeIndex == index)
182         return true;
183     SGSharedPtr<HLADataElement> dataElement = newElement(index);
184     if (!dataElement.valid())
185         return false;
186     _dataElement.swap(dataElement);
187     _alternativeIndex = index;
188     setDirty(true);
189     return true;
190 }
191
192 bool
193 HLAVariantRecordDataElement::decodeAlternative(HLADecodeStream& stream)
194 {
195     return _dataElement->decode(stream);
196 }
197
198 unsigned
199 HLAVariantRecordDataElement::getAlternativeIndex() const
200 {
201     return _alternativeIndex;
202 }
203
204 bool
205 HLAVariantRecordDataElement::encodeAlternative(HLAEncodeStream& stream) const
206 {
207     return _dataElement->encode(stream);
208 }
209
210 void
211 HLAVariantRecordDataElement::setDataElementFactory(HLAVariantRecordDataElement::DataElementFactory* dataElementFactory)
212 {
213     _dataElementFactory = dataElementFactory;
214 }
215
216 HLAVariantRecordDataElement::DataElementFactory*
217 HLAVariantRecordDataElement::getDataElementFactory()
218 {
219     return _dataElementFactory;
220 }
221
222 void
223 HLAVariantRecordDataElement::_setStamp(Stamp* stamp)
224 {
225     HLAAbstractVariantRecordDataElement::_setStamp(stamp);
226     if (!_dataElement.valid())
227         return;
228     _dataElement->attachStamp(*this);
229 }
230
231 HLADataElement*
232 HLAVariantRecordDataElement::newElement(unsigned index)
233 {
234     if (!_dataElementFactory.valid())
235         return 0;
236     HLADataElement* dataElement = _dataElementFactory->createElement(*this, index);
237     if (!dataElement)
238         return 0;
239     dataElement->attachStamp(*this);
240     return dataElement;
241 }
242
243 }