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 "HLAVariantRecordDataType.hxx"
20 #include "HLADataTypeVisitor.hxx"
21 #include "HLAVariantRecordDataElement.hxx"
25 HLAVariantRecordDataType::HLAVariantRecordDataType(const std::string& name) :
30 HLAVariantRecordDataType::~HLAVariantRecordDataType()
35 HLAVariantRecordDataType::accept(HLADataTypeVisitor& visitor) const
40 const HLAVariantRecordDataType*
41 HLAVariantRecordDataType::toVariantRecordDataType() const
47 HLAVariantRecordDataType::releaseDataTypeReferences()
49 for (AlternativeList::iterator i = _alternativeList.begin(); i != _alternativeList.end(); ++i)
54 HLAVariantRecordDataType::decode(HLADecodeStream& stream, HLAAbstractVariantRecordDataElement& value) const
56 if (!stream.alignOffsetForSize(getAlignment()))
58 if (!_enumeratedDataType.valid())
61 if (!_enumeratedDataType->decode(stream, index))
63 if (!value.setAlternativeIndex(index))
65 if (!value.decodeAlternative(stream))
71 HLAVariantRecordDataType::encode(HLAEncodeStream& stream, const HLAAbstractVariantRecordDataElement& value) const
73 if (!stream.alignOffsetForSize(getAlignment()))
75 if (!_enumeratedDataType.valid())
77 unsigned index = value.getAlternativeIndex();
78 if (!_enumeratedDataType->encode(stream, index))
80 if (!value.encodeAlternative(stream))
86 HLAVariantRecordDataType::setEnumeratedDataType(HLAEnumeratedDataType* dataType)
88 _enumeratedDataType = dataType;
92 HLAVariantRecordDataType::addAlternative(const std::string& name, const std::string& enumerator,
93 const HLADataType* dataType, const std::string& semantics)
95 if (!_enumeratedDataType.valid())
97 unsigned index = _enumeratedDataType->getIndex(enumerator);
98 if (_enumeratedDataType->getNumEnumerators() <= index)
100 _alternativeList.resize(_enumeratedDataType->getNumEnumerators());
101 _alternativeList[index]._name = name;
102 _alternativeList[index]._dataType = dataType;
103 _alternativeList[index]._semantics = semantics;
108 HLAVariantRecordDataType::_recomputeAlignmentImplementation()
110 unsigned alignment = 1;
111 if (const HLADataType* dataType = getEnumeratedDataType())
112 alignment = std::max(alignment, dataType->getAlignment());
113 for (unsigned i = 0; i < getNumAlternatives(); ++i) {
114 if (const HLADataType* dataType = getAlternativeDataType(i))
115 alignment = std::max(alignment, dataType->getAlignment());
117 setAlignment(alignment);
120 } // namespace simgear