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 "HLAVariantRecordDataType.hxx"
26 #include "HLADataTypeVisitor.hxx"
27 #include "HLAVariantRecordDataElement.hxx"
31 HLAVariantRecordDataType::HLAVariantRecordDataType(const std::string& name) :
36 HLAVariantRecordDataType::~HLAVariantRecordDataType()
41 HLAVariantRecordDataType::accept(HLADataTypeVisitor& visitor) const
46 const HLAVariantRecordDataType*
47 HLAVariantRecordDataType::toVariantRecordDataType() const
53 HLAVariantRecordDataType::releaseDataTypeReferences()
55 for (AlternativeList::iterator i = _alternativeList.begin(); i != _alternativeList.end(); ++i)
60 HLAVariantRecordDataType::decode(HLADecodeStream& stream, HLAAbstractVariantRecordDataElement& value) const
62 if (!stream.alignOffsetForSize(getAlignment()))
64 if (!_enumeratedDataType.valid())
67 if (!_enumeratedDataType->decode(stream, index))
69 if (!value.setAlternativeIndex(index))
71 if (!value.decodeAlternative(stream))
77 HLAVariantRecordDataType::encode(HLAEncodeStream& stream, const HLAAbstractVariantRecordDataElement& value) const
79 if (!stream.alignOffsetForSize(getAlignment()))
81 if (!_enumeratedDataType.valid())
83 unsigned index = value.getAlternativeIndex();
84 if (!_enumeratedDataType->encode(stream, index))
86 if (!value.encodeAlternative(stream))
92 HLAVariantRecordDataType::setEnumeratedDataType(HLAEnumeratedDataType* dataType)
94 _enumeratedDataType = dataType;
98 HLAVariantRecordDataType::addAlternative(const std::string& name, const std::string& enumerator,
99 const HLADataType* dataType, const std::string& semantics)
101 if (!_enumeratedDataType.valid())
103 unsigned index = _enumeratedDataType->getIndex(enumerator);
104 if (_enumeratedDataType->getNumEnumerators() <= index)
106 _alternativeList.resize(_enumeratedDataType->getNumEnumerators());
107 _alternativeList[index]._name = name;
108 _alternativeList[index]._dataType = dataType;
109 _alternativeList[index]._semantics = semantics;
114 HLAVariantRecordDataType::_recomputeAlignmentImplementation()
116 unsigned alignment = 1;
117 if (const HLADataType* dataType = getEnumeratedDataType())
118 alignment = std::max(alignment, dataType->getAlignment());
119 for (unsigned i = 0; i < getNumAlternatives(); ++i) {
120 if (const HLADataType* dataType = getAlternativeDataType(i))
121 alignment = std::max(alignment, dataType->getAlignment());
123 setAlignment(alignment);
126 } // namespace simgear