1 // Copyright (C) 2009 - 2012 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>
22 #include "HLADataElementVisitor.hxx"
26 HLAAbstractArrayDataElement::HLAAbstractArrayDataElement(const HLAArrayDataType* dataType) :
31 HLAAbstractArrayDataElement::~HLAAbstractArrayDataElement()
36 HLAAbstractArrayDataElement::accept(HLADataElementVisitor& visitor)
42 HLAAbstractArrayDataElement::accept(HLAConstDataElementVisitor& visitor) const
48 HLAAbstractArrayDataElement::decode(HLADecodeStream& stream)
50 if (!_dataType.valid())
52 return _dataType->decode(stream, *this);
56 HLAAbstractArrayDataElement::encode(HLAEncodeStream& stream) const
58 if (!_dataType.valid())
60 return _dataType->encode(stream, *this);
63 const HLAArrayDataType*
64 HLAAbstractArrayDataElement::getDataType() const
66 return _dataType.get();
70 HLAAbstractArrayDataElement::setDataType(const HLADataType* dataType)
72 const HLAArrayDataType* arrayDataType = dynamic_cast<const HLAArrayDataType*>(dataType);
74 SG_LOG(SG_NETWORK, SG_WARN, "HLAArrayDataType: unable to set data type!");
77 _dataType = arrayDataType;
82 HLAAbstractArrayDataElement::getElementDataType() const
84 if (!_dataType.valid())
86 return _dataType->getElementDataType();
89 ////////////////////////////////////////////////////////////////////////
91 HLAArrayDataElement::DataElementFactory::~DataElementFactory()
95 HLAArrayDataElement::HLAArrayDataElement(const HLAArrayDataType* dataType) :
96 HLAAbstractArrayDataElement(dataType)
100 HLAArrayDataElement::~HLAArrayDataElement()
106 HLAArrayDataElement::setNumElements(unsigned size)
108 unsigned oldSize = _elementVector.size();
111 _elementVector.resize(size);
112 for (unsigned i = oldSize; i < size; ++i)
113 _elementVector[i] = newElement(i);
119 HLAArrayDataElement::decodeElement(HLADecodeStream& stream, unsigned i)
121 HLADataElement* dataElement = getElement(i);
124 return dataElement->decode(stream);
128 HLAArrayDataElement::getNumElements() const
130 return _elementVector.size();
134 HLAArrayDataElement::encodeElement(HLAEncodeStream& stream, unsigned i) const
136 const HLADataElement* dataElement = getElement(i);
139 return dataElement->encode(stream);
142 const HLADataElement*
143 HLAArrayDataElement::getElement(unsigned index) const
145 if (_elementVector.size() <= index)
147 return _elementVector[index].get();
151 HLAArrayDataElement::getElement(unsigned index)
153 if (_elementVector.size() <= index)
155 return _elementVector[index].get();
159 HLAArrayDataElement::getOrCreateElement(unsigned index)
161 if (_elementVector.size() <= index)
162 if (!setNumElements(index + 1))
164 return _elementVector[index].get();
168 HLAArrayDataElement::setElement(unsigned index, HLADataElement* value)
170 unsigned oldSize = _elementVector.size();
171 if (oldSize <= index) {
172 _elementVector.resize(index + 1);
173 for (unsigned j = oldSize; j < index; ++j)
174 _elementVector[j] = newElement(j);
176 if (_elementVector[index].valid())
177 _elementVector[index]->clearStamp();
178 _elementVector[index] = value;
180 value->attachStamp(*this);
185 HLAArrayDataElement::setDataElementFactory(HLAArrayDataElement::DataElementFactory* dataElementFactory)
187 _dataElementFactory = dataElementFactory;
190 HLAArrayDataElement::DataElementFactory*
191 HLAArrayDataElement::getDataElementFactory()
193 return _dataElementFactory.get();
197 HLAArrayDataElement::_setStamp(Stamp* stamp)
199 HLAAbstractArrayDataElement::_setStamp(stamp);
200 for (ElementVector::iterator i = _elementVector.begin(); i != _elementVector.end(); ++i) {
203 (*i)->attachStamp(*this);
208 HLAArrayDataElement::newElement(unsigned index)
210 if (!_dataElementFactory.valid())
212 HLADataElement* dataElement = _dataElementFactory->createElement(*this, index);
215 dataElement->attachStamp(*this);
219 ////////////////////////////////////////////////////////////////////////
221 HLAVariantArrayDataElement::HLAVariantArrayDataElement() :
222 HLAAbstractArrayDataElement(0)
226 HLAVariantArrayDataElement::~HLAVariantArrayDataElement()
232 HLAVariantArrayDataElement::setDataType(const HLADataType* dataType)
234 const HLAArrayDataType* arrayDataType = dataType->toArrayDataType();
235 if (!arrayDataType) {
236 SG_LOG(SG_NETWORK, SG_WARN, "HLAVariantArrayDataType: unable to set data type, dataType is not an array data type!");
239 const HLAVariantRecordDataType* variantRecordDataType = arrayDataType->getElementDataType()->toVariantRecordDataType();
240 if (!variantRecordDataType) {
241 SG_LOG(SG_NETWORK, SG_WARN, "HLAVariantArrayDataType: unable to set data type: arrayDataTypes element data type is no a variant data type!");
244 _dataType = arrayDataType;
249 HLAVariantArrayDataElement::setNumElements(unsigned size)
251 unsigned oldSize = _elementVector.size();
254 _elementVector.resize(size);
255 for (unsigned i = oldSize; i < size; ++i)
256 _elementVector[i] = newElement();
262 HLAVariantArrayDataElement::decodeElement(HLADecodeStream& stream, unsigned i)
264 HLAVariantRecordDataElement* dataElement = getElement(i);
267 return dataElement->decode(stream);
271 HLAVariantArrayDataElement::getNumElements() const
273 return _elementVector.size();
277 HLAVariantArrayDataElement::encodeElement(HLAEncodeStream& stream, unsigned i) const
279 const HLADataElement* dataElement = getElement(i);
282 return dataElement->encode(stream);
285 const HLAVariantRecordDataElement*
286 HLAVariantArrayDataElement::getElement(unsigned index) const
288 if (_elementVector.size() <= index)
290 return _elementVector[index].get();
293 HLAVariantRecordDataElement*
294 HLAVariantArrayDataElement::getElement(unsigned index)
296 if (_elementVector.size() <= index)
298 return _elementVector[index].get();
301 HLAVariantRecordDataElement*
302 HLAVariantArrayDataElement::getOrCreateElement(unsigned index)
304 if (_elementVector.size() <= index)
305 if (!setNumElements(index + 1))
307 return _elementVector[index].get();
311 HLAVariantArrayDataElement::setElement(unsigned index, HLAVariantRecordDataElement* value)
313 unsigned oldSize = _elementVector.size();
314 if (oldSize <= index) {
315 _elementVector.resize(index + 1);
316 for (unsigned j = oldSize; j < index; ++j)
317 _elementVector[j] = newElement();
319 if (_elementVector[index].valid())
320 _elementVector[index]->clearStamp();
321 _elementVector[index] = value;
323 value->attachStamp(*this);
328 HLAVariantArrayDataElement::setAlternativeDataElementFactory(HLAVariantArrayDataElement::AlternativeDataElementFactory* alternativeDataElementFactory)
330 _alternativeDataElementFactory = alternativeDataElementFactory;
333 HLAVariantArrayDataElement::AlternativeDataElementFactory*
334 HLAVariantArrayDataElement::getAlternativeDataElementFactory()
336 return _alternativeDataElementFactory.get();
340 HLAVariantArrayDataElement::_setStamp(Stamp* stamp)
342 HLAAbstractArrayDataElement::_setStamp(stamp);
343 for (ElementVector::iterator i = _elementVector.begin(); i != _elementVector.end(); ++i) {
346 (*i)->attachStamp(*this);
350 HLAVariantRecordDataElement*
351 HLAVariantArrayDataElement::newElement()
353 const HLAArrayDataType* arrayDataType = getDataType();
356 const HLADataType* elementDataType = arrayDataType->getElementDataType();
357 if (!elementDataType)
359 const HLAVariantRecordDataType* variantRecordDataType = elementDataType->toVariantRecordDataType();
360 if (!variantRecordDataType)
362 HLAVariantRecordDataElement* variantRecordDataElement = new HLAVariantRecordDataElement(variantRecordDataType);
363 variantRecordDataElement->setDataElementFactory(_alternativeDataElementFactory.get());
364 variantRecordDataElement->attachStamp(*this);
365 return variantRecordDataElement;