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 "HLAArrayDataElement.hxx"
20 #include <simgear/debug/logstream.hxx>
24 HLAAbstractArrayDataElement::HLAAbstractArrayDataElement(const HLAArrayDataType* dataType) :
29 HLAAbstractArrayDataElement::~HLAAbstractArrayDataElement()
34 HLAAbstractArrayDataElement::decode(HLADecodeStream& stream)
36 if (!_dataType.valid())
38 return _dataType->decode(stream, *this);
42 HLAAbstractArrayDataElement::encode(HLAEncodeStream& stream) const
44 if (!_dataType.valid())
46 return _dataType->encode(stream, *this);
49 const HLAArrayDataType*
50 HLAAbstractArrayDataElement::getDataType() const
52 return _dataType.get();
56 HLAAbstractArrayDataElement::setDataType(const HLADataType* dataType)
58 const HLAArrayDataType* arrayDataType = dynamic_cast<const HLAArrayDataType*>(dataType);
60 SG_LOG(SG_NETWORK, SG_WARN, "HLAArrayDataType: unable to set data type!");
63 _dataType = arrayDataType;
68 HLAAbstractArrayDataElement::getElementDataType() const
70 if (!_dataType.valid())
72 return _dataType->getElementDataType();
75 ////////////////////////////////////////////////////////////////////////
77 HLAArrayDataElement::DataElementFactory::~DataElementFactory()
81 HLAArrayDataElement::HLAArrayDataElement(const HLAArrayDataType* dataType) :
82 HLAAbstractArrayDataElement(dataType)
86 HLAArrayDataElement::~HLAArrayDataElement()
91 HLAArrayDataElement::setNumElements(unsigned size)
93 unsigned oldSize = _elementVector.size();
96 _elementVector.resize(size);
97 for (unsigned i = oldSize; i < size; ++i)
98 _elementVector[i] = newElement(i);
103 HLAArrayDataElement::decodeElement(HLADecodeStream& stream, unsigned i)
105 HLADataElement* dataElement = getElement(i);
108 return dataElement->decode(stream);
112 HLAArrayDataElement::getNumElements() const
114 return _elementVector.size();
118 HLAArrayDataElement::encodeElement(HLAEncodeStream& stream, unsigned i) const
120 const HLADataElement* dataElement = getElement(i);
123 return dataElement->encode(stream);
126 const HLADataElement*
127 HLAArrayDataElement::getElement(unsigned index) const
129 if (_elementVector.size() <= index)
131 return _elementVector[index].get();
135 HLAArrayDataElement::getElement(unsigned index)
137 if (_elementVector.size() <= index)
139 return _elementVector[index].get();
143 HLAArrayDataElement::getOrCreateElement(unsigned index)
145 if (_elementVector.size() <= index)
146 if (!setNumElements(index + 1))
148 return _elementVector[index].get();
152 HLAArrayDataElement::setElement(unsigned index, HLADataElement* value)
154 unsigned oldSize = _elementVector.size();
155 if (oldSize <= index) {
156 _elementVector.resize(index + 1);
157 for (unsigned j = oldSize; j < index; ++j)
158 _elementVector[j] = newElement(j);
160 _elementVector[index] = value;
164 HLAArrayDataElement::setDataElementFactory(HLAArrayDataElement::DataElementFactory* dataElementFactory)
166 _dataElementFactory = dataElementFactory;
169 HLAArrayDataElement::DataElementFactory*
170 HLAArrayDataElement::getDataElementFactory()
172 return _dataElementFactory.get();
176 HLAArrayDataElement::newElement(unsigned index)
178 if (!_dataElementFactory.valid())
180 return _dataElementFactory->createElement(*this, index);
183 ////////////////////////////////////////////////////////////////////////
185 HLAVariantArrayDataElement::HLAVariantArrayDataElement() :
186 HLAAbstractArrayDataElement(0)
190 HLAVariantArrayDataElement::~HLAVariantArrayDataElement()
195 HLAVariantArrayDataElement::setDataType(const HLADataType* dataType)
197 const HLAArrayDataType* arrayDataType = dataType->toArrayDataType();
198 if (!arrayDataType) {
199 SG_LOG(SG_NETWORK, SG_WARN, "HLAVariantArrayDataType: unable to set data type, dataType is not an array data type!");
202 const HLAVariantDataType* variantDataType = arrayDataType->getElementDataType()->toVariantDataType();
203 if (!variantDataType) {
204 SG_LOG(SG_NETWORK, SG_WARN, "HLAVariantArrayDataType: unable to set data type: arrayDataTypes element data type is no a variant data type!");
207 _dataType = arrayDataType;
212 HLAVariantArrayDataElement::setNumElements(unsigned size)
214 unsigned oldSize = _elementVector.size();
217 _elementVector.resize(size);
218 for (unsigned i = oldSize; i < size; ++i)
219 _elementVector[i] = newElement();
224 HLAVariantArrayDataElement::decodeElement(HLADecodeStream& stream, unsigned i)
226 HLAVariantDataElement* dataElement = getElement(i);
229 return dataElement->decode(stream);
233 HLAVariantArrayDataElement::getNumElements() const
235 return _elementVector.size();
239 HLAVariantArrayDataElement::encodeElement(HLAEncodeStream& stream, unsigned i) const
241 const HLADataElement* dataElement = getElement(i);
244 return dataElement->encode(stream);
247 const HLAVariantDataElement*
248 HLAVariantArrayDataElement::getElement(unsigned index) const
250 if (_elementVector.size() <= index)
252 return _elementVector[index].get();
255 HLAVariantDataElement*
256 HLAVariantArrayDataElement::getElement(unsigned index)
258 if (_elementVector.size() <= index)
260 return _elementVector[index].get();
263 HLAVariantDataElement*
264 HLAVariantArrayDataElement::getOrCreateElement(unsigned index)
266 if (_elementVector.size() <= index)
267 if (!setNumElements(index + 1))
269 return _elementVector[index].get();
273 HLAVariantArrayDataElement::setElement(unsigned index, HLAVariantDataElement* value)
275 unsigned oldSize = _elementVector.size();
276 if (oldSize <= index) {
277 _elementVector.resize(index + 1);
278 for (unsigned j = oldSize; j < index; ++j)
279 _elementVector[j] = newElement();
281 _elementVector[index] = value;
285 HLAVariantArrayDataElement::setAlternativeDataElementFactory(HLAVariantArrayDataElement::AlternativeDataElementFactory* alternativeDataElementFactory)
287 _alternativeDataElementFactory = alternativeDataElementFactory;
290 HLAVariantArrayDataElement::AlternativeDataElementFactory*
291 HLAVariantArrayDataElement::getAlternativeDataElementFactory()
293 return _alternativeDataElementFactory.get();
296 HLAVariantDataElement*
297 HLAVariantArrayDataElement::newElement()
299 const HLAArrayDataType* arrayDataType = getDataType();
302 const HLADataType* elementDataType = arrayDataType->getElementDataType();
303 if (!elementDataType)
305 const HLAVariantDataType* variantDataType = elementDataType->toVariantDataType();
306 if (!variantDataType)
308 HLAVariantDataElement* variantDataElement = new HLAVariantDataElement(variantDataType);
309 variantDataElement->setDataElementFactory(_alternativeDataElementFactory.get());
310 return variantDataElement;