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.
19 # include <simgear_config.h>
22 #include <simgear/compiler.h>
24 #include "HLAVariantRecordDataElement.hxx"
26 #include <simgear/debug/logstream.hxx>
28 #include "HLADataElementVisitor.hxx"
29 #include "HLADataTypeVisitor.hxx"
33 HLAAbstractVariantRecordDataElement::HLAAbstractVariantRecordDataElement(const HLAVariantRecordDataType* dataType) :
38 HLAAbstractVariantRecordDataElement::~HLAAbstractVariantRecordDataElement()
43 HLAAbstractVariantRecordDataElement::accept(HLADataElementVisitor& visitor)
49 HLAAbstractVariantRecordDataElement::accept(HLAConstDataElementVisitor& visitor) const
55 HLAAbstractVariantRecordDataElement::decode(HLADecodeStream& stream)
57 if (!_dataType.valid())
59 return _dataType->decode(stream, *this);
63 HLAAbstractVariantRecordDataElement::encode(HLAEncodeStream& stream) const
65 if (!_dataType.valid())
67 return _dataType->encode(stream, *this);
70 const HLAVariantRecordDataType*
71 HLAAbstractVariantRecordDataElement::getDataType() const
73 return _dataType.get();
77 HLAAbstractVariantRecordDataElement::setDataType(const HLADataType* dataType)
79 const HLAVariantRecordDataType* variantRecordDataType = dataType->toVariantRecordDataType();
80 if (!variantRecordDataType) {
81 SG_LOG(SG_NETWORK, SG_WARN, "HLAVariantRecordDataType: unable to set data type!");
84 setDataType(variantRecordDataType);
89 HLAAbstractVariantRecordDataElement::setDataType(const HLAVariantRecordDataType* dataType)
95 HLAAbstractVariantRecordDataElement::getAlternativeName() const
97 if (!_dataType.valid())
99 return _dataType->getAlternativeName(getAlternativeIndex());
103 HLAAbstractVariantRecordDataElement::getAlternativeDataType() const
105 if (!_dataType.valid())
107 return _dataType->getAlternativeDataType(getAlternativeIndex());
111 HLAVariantRecordDataElement::DataElementFactory::~DataElementFactory()
115 HLAVariantRecordDataElement::HLAVariantRecordDataElement(const HLAVariantRecordDataType* dataType) :
116 HLAAbstractVariantRecordDataElement(dataType),
117 _alternativeIndex(~0u)
121 HLAVariantRecordDataElement::~HLAVariantRecordDataElement()
127 HLAVariantRecordDataElement::setDataElement(HLADataElementIndex::const_iterator begin, HLADataElementIndex::const_iterator end, HLADataElement* dataElement)
129 // Must have happened in the parent
132 unsigned index = *begin;
133 if (++begin != end) {
134 if (!setAlternativeIndex(index))
136 if (!_dataElement.valid() && getAlternativeDataType()) {
137 HLADataElementFactoryVisitor visitor;
138 getAlternativeDataType()->accept(visitor);
139 _dataElement = visitor.getDataElement();
141 if (!_dataElement.valid())
143 return _dataElement->setDataElement(begin, end, dataElement);
145 if (!setAlternativeIndex(index))
147 if (!dataElement->setDataType(getAlternativeDataType()))
149 _dataElement = dataElement;
155 HLAVariantRecordDataElement::getDataElement(HLADataElementIndex::const_iterator begin, HLADataElementIndex::const_iterator end)
159 if (getAlternativeIndex() != *begin)
161 if (!_dataElement.valid())
163 return _dataElement->getDataElement(++begin, end);
166 const HLADataElement*
167 HLAVariantRecordDataElement::getDataElement(HLADataElementIndex::const_iterator begin, HLADataElementIndex::const_iterator end) const
171 if (getAlternativeIndex() != *begin)
173 if (!_dataElement.valid())
175 return _dataElement->getDataElement(++begin, end);
179 HLAVariantRecordDataElement::setAlternativeIndex(unsigned index)
181 if (_alternativeIndex == index)
183 SGSharedPtr<HLADataElement> dataElement = newElement(index);
184 if (!dataElement.valid())
186 _dataElement.swap(dataElement);
187 _alternativeIndex = index;
193 HLAVariantRecordDataElement::decodeAlternative(HLADecodeStream& stream)
195 return _dataElement->decode(stream);
199 HLAVariantRecordDataElement::getAlternativeIndex() const
201 return _alternativeIndex;
205 HLAVariantRecordDataElement::encodeAlternative(HLAEncodeStream& stream) const
207 return _dataElement->encode(stream);
211 HLAVariantRecordDataElement::setDataElementFactory(HLAVariantRecordDataElement::DataElementFactory* dataElementFactory)
213 _dataElementFactory = dataElementFactory;
216 HLAVariantRecordDataElement::DataElementFactory*
217 HLAVariantRecordDataElement::getDataElementFactory()
219 return _dataElementFactory;
223 HLAVariantRecordDataElement::_setStamp(Stamp* stamp)
225 HLAAbstractVariantRecordDataElement::_setStamp(stamp);
226 if (!_dataElement.valid())
228 _dataElement->attachStamp(*this);
232 HLAVariantRecordDataElement::newElement(unsigned index)
234 if (!_dataElementFactory.valid())
236 HLADataElement* dataElement = _dataElementFactory->createElement(*this, index);
239 dataElement->attachStamp(*this);