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 "HLAVariantDataElement.hxx"
20 #include <simgear/debug/logstream.hxx>
24 HLAAbstractVariantDataElement::HLAAbstractVariantDataElement(const HLAVariantDataType* dataType) :
29 HLAAbstractVariantDataElement::~HLAAbstractVariantDataElement()
34 HLAAbstractVariantDataElement::decode(HLADecodeStream& stream)
36 if (!_dataType.valid())
38 return _dataType->decode(stream, *this);
42 HLAAbstractVariantDataElement::encode(HLAEncodeStream& stream) const
44 if (!_dataType.valid())
46 return _dataType->encode(stream, *this);
49 const HLAVariantDataType*
50 HLAAbstractVariantDataElement::getDataType() const
52 return _dataType.get();
56 HLAAbstractVariantDataElement::setDataType(const HLADataType* dataType)
58 const HLAVariantDataType* variantDataType = dataType->toVariantDataType();
59 if (!variantDataType) {
60 SG_LOG(SG_NETWORK, SG_WARN, "HLAVariantDataType: unable to set data type!");
63 setDataType(variantDataType);
68 HLAAbstractVariantDataElement::setDataType(const HLAVariantDataType* dataType)
74 HLAAbstractVariantDataElement::getAlternativeName() const
76 if (!_dataType.valid())
78 return _dataType->getAlternativeName(getAlternativeIndex());
82 HLAAbstractVariantDataElement::getAlternativeDataType() const
84 if (!_dataType.valid())
86 return _dataType->getAlternativeDataType(getAlternativeIndex());
90 HLAVariantDataElement::DataElementFactory::~DataElementFactory()
94 HLAVariantDataElement::HLAVariantDataElement(const HLAVariantDataType* dataType) :
95 HLAAbstractVariantDataElement(dataType),
96 _alternativeIndex(~0u)
100 HLAVariantDataElement::~HLAVariantDataElement()
105 HLAVariantDataElement::setAlternativeIndex(unsigned index)
107 if (_alternativeIndex == index)
109 SGSharedPtr<HLADataElement> dataElement = newElement(index);
110 if (!dataElement.valid())
112 _dataElement.swap(dataElement);
113 _alternativeIndex = index;
118 HLAVariantDataElement::decodeAlternative(HLADecodeStream& stream)
120 return _dataElement->decode(stream);
124 HLAVariantDataElement::getAlternativeIndex() const
126 return _alternativeIndex;
130 HLAVariantDataElement::encodeAlternative(HLAEncodeStream& stream) const
132 return _dataElement->encode(stream);
136 HLAVariantDataElement::setDataElementFactory(HLAVariantDataElement::DataElementFactory* dataElementFactory)
138 _dataElementFactory = dataElementFactory;
141 HLAVariantDataElement::DataElementFactory*
142 HLAVariantDataElement::getDataElementFactory()
144 return _dataElementFactory;
148 HLAVariantDataElement::newElement(unsigned index)
150 if (!_dataElementFactory.valid())
152 return _dataElementFactory->createElement(*this, index);