1 // Copyright (C) 2009 - 2010 Mathias Froehlich - Mathias.Froehlich@web.de
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.
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.
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.
18 #include "HLAPropertyDataElement.hxx"
20 #include "HLADataTypeVisitor.hxx"
24 class HLAPropertyDataElement::DecodeVisitor : public HLADataTypeDecodeVisitor {
26 DecodeVisitor(HLADecodeStream& stream, HLAPropertyReference& propertyReference) :
27 HLADataTypeDecodeVisitor(stream),
28 _propertyReference(propertyReference)
31 virtual void apply(const HLAInt8DataType& dataType)
34 dataType.decode(_stream, value);
35 _propertyReference.setIntValue(value);
37 virtual void apply(const HLAUInt8DataType& dataType)
40 dataType.decode(_stream, value);
41 _propertyReference.setIntValue(value);
43 virtual void apply(const HLAInt16DataType& dataType)
46 dataType.decode(_stream, value);
47 _propertyReference.setIntValue(value);
49 virtual void apply(const HLAUInt16DataType& dataType)
52 dataType.decode(_stream, value);
53 _propertyReference.setIntValue(value);
55 virtual void apply(const HLAInt32DataType& dataType)
58 dataType.decode(_stream, value);
59 _propertyReference.setIntValue(value);
61 virtual void apply(const HLAUInt32DataType& dataType)
64 dataType.decode(_stream, value);
65 _propertyReference.setIntValue(value);
67 virtual void apply(const HLAInt64DataType& dataType)
70 dataType.decode(_stream, value);
71 _propertyReference.setLongValue(value);
73 virtual void apply(const HLAUInt64DataType& dataType)
76 dataType.decode(_stream, value);
77 _propertyReference.setLongValue(value);
79 virtual void apply(const HLAFloat32DataType& dataType)
82 dataType.decode(_stream, value);
83 _propertyReference.setFloatValue(value);
85 virtual void apply(const HLAFloat64DataType& dataType)
88 dataType.decode(_stream, value);
89 _propertyReference.setDoubleValue(value);
92 virtual void apply(const HLAFixedArrayDataType& dataType)
94 unsigned numElements = dataType.getNumElements();
96 value.reserve(numElements);
97 for (unsigned i = 0; i < numElements; ++i) {
98 HLATemplateDecodeVisitor<char> visitor(_stream);
99 dataType.getElementDataType()->accept(visitor);
100 value.push_back(visitor.getValue());
102 _propertyReference.setStringValue(value);
104 virtual void apply(const HLAVariableArrayDataType& dataType)
106 HLATemplateDecodeVisitor<unsigned> numElementsVisitor(_stream);
107 dataType.getSizeDataType()->accept(numElementsVisitor);
108 unsigned numElements = numElementsVisitor.getValue();
110 value.reserve(numElements);
111 for (unsigned i = 0; i < numElements; ++i) {
112 HLATemplateDecodeVisitor<char> visitor(_stream);
113 dataType.getElementDataType()->accept(visitor);
114 value.push_back(visitor.getValue());
116 _propertyReference.setStringValue(value);
120 HLAPropertyReference& _propertyReference;
123 class HLAPropertyDataElement::EncodeVisitor : public HLADataTypeEncodeVisitor {
125 EncodeVisitor(HLAEncodeStream& stream, const HLAPropertyReference& propertyReference) :
126 HLADataTypeEncodeVisitor(stream),
127 _propertyReference(propertyReference)
130 virtual void apply(const HLAInt8DataType& dataType)
132 dataType.encode(_stream, _propertyReference.getIntValue());
134 virtual void apply(const HLAUInt8DataType& dataType)
136 dataType.encode(_stream, _propertyReference.getIntValue());
138 virtual void apply(const HLAInt16DataType& dataType)
140 dataType.encode(_stream, _propertyReference.getIntValue());
142 virtual void apply(const HLAUInt16DataType& dataType)
144 dataType.encode(_stream, _propertyReference.getIntValue());
146 virtual void apply(const HLAInt32DataType& dataType)
148 dataType.encode(_stream, _propertyReference.getIntValue());
150 virtual void apply(const HLAUInt32DataType& dataType)
152 dataType.encode(_stream, _propertyReference.getIntValue());
154 virtual void apply(const HLAInt64DataType& dataType)
156 dataType.encode(_stream, _propertyReference.getLongValue());
158 virtual void apply(const HLAUInt64DataType& dataType)
160 dataType.encode(_stream, _propertyReference.getLongValue());
162 virtual void apply(const HLAFloat32DataType& dataType)
164 dataType.encode(_stream, _propertyReference.getFloatValue());
166 virtual void apply(const HLAFloat64DataType& dataType)
168 dataType.encode(_stream, _propertyReference.getDoubleValue());
171 virtual void apply(const HLAFixedArrayDataType& dataType)
173 unsigned numElements = dataType.getNumElements();
174 std::string value = _propertyReference.getStringValue();
175 for (unsigned i = 0; i < numElements; ++i) {
176 if (i < value.size()) {
177 HLATemplateEncodeVisitor<char> visitor(_stream, value[i]);
178 dataType.getElementDataType()->accept(visitor);
180 HLADataTypeEncodeVisitor visitor(_stream);
181 dataType.getElementDataType()->accept(visitor);
186 virtual void apply(const HLAVariableArrayDataType& dataType)
188 std::string value = _propertyReference.getStringValue();
189 HLATemplateEncodeVisitor<std::string::size_type> numElementsVisitor(_stream, value.size());
190 dataType.getSizeDataType()->accept(numElementsVisitor);
191 for (unsigned i = 0; i < value.size(); ++i) {
192 HLATemplateEncodeVisitor<char> visitor(_stream, value[i]);
193 dataType.getElementDataType()->accept(visitor);
198 const HLAPropertyReference& _propertyReference;
201 HLAPropertyDataElement::HLAPropertyDataElement(HLAPropertyReference* propertyReference) :
202 _propertyReference(propertyReference)
206 HLAPropertyDataElement::HLAPropertyDataElement(const simgear::HLADataType* dataType, HLAPropertyReference* propertyReference) :
208 _propertyReference(propertyReference)
212 HLAPropertyDataElement::~HLAPropertyDataElement()
217 HLAPropertyDataElement::encode(HLAEncodeStream& stream) const
219 if (!_dataType.valid())
221 if (_propertyReference.valid()) {
222 EncodeVisitor visitor(stream, *_propertyReference);
223 _dataType->accept(visitor);
225 HLADataTypeEncodeVisitor visitor(stream);
226 _dataType->accept(visitor);
232 HLAPropertyDataElement::decode(HLADecodeStream& stream)
234 if (!_dataType.valid())
236 if (_propertyReference.valid()) {
237 DecodeVisitor visitor(stream, *_propertyReference);
238 _dataType->accept(visitor);
240 HLADataTypeDecodeVisitor visitor(stream);
241 _dataType->accept(visitor);
247 HLAPropertyDataElement::getDataType() const
249 return _dataType.get();
253 HLAPropertyDataElement::setDataType(const HLADataType* dataType)
255 if (dataType->toBasicDataType()) {
256 _dataType = dataType;
259 const HLAArrayDataType* arrayDataType = dataType->toArrayDataType();
260 if (arrayDataType && arrayDataType->getElementDataType() &&
261 arrayDataType->getElementDataType()->toBasicDataType()) {
262 _dataType = dataType;
269 } // namespace simgear