1 // Copyright (C) 2009 - 2011 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 "HLAArrayDataType.hxx"
20 #include "HLAArrayDataElement.hxx"
24 HLAArrayDataType::HLAArrayDataType(const std::string& name) :
31 HLAArrayDataType::~HLAArrayDataType()
36 HLAArrayDataType::accept(HLADataTypeVisitor& visitor) const
41 const HLAArrayDataType*
42 HLAArrayDataType::toArrayDataType() const
48 HLAArrayDataType::setElementDataType(const HLADataType* elementDataType)
50 // FIXME this only works if we do not reset the alignment to something smaller
51 if (getAlignment() < elementDataType->getAlignment())
52 setAlignment(elementDataType->getAlignment());
53 _elementDataType = elementDataType;
57 HLAArrayDataType::setIsOpaque(bool isOpaque)
63 HLAArrayDataType::setIsString(bool isString)
68 ///////////////////////////////////////////////////////////////////////////////////
70 HLAFixedArrayDataType::HLAFixedArrayDataType(const std::string& name) :
71 HLAArrayDataType(name)
75 HLAFixedArrayDataType::~HLAFixedArrayDataType()
80 HLAFixedArrayDataType::accept(HLADataTypeVisitor& visitor) const
86 HLAFixedArrayDataType::decode(HLADecodeStream& stream, HLAAbstractArrayDataElement& value) const
88 stream.alignOffsetForSize(getAlignment());
89 unsigned numElements = getNumElements();
90 if (!value.setNumElements(numElements))
92 for (unsigned i = 0; i < numElements; ++i)
93 if (!value.decodeElement(stream, i))
99 HLAFixedArrayDataType::encode(HLAEncodeStream& stream, const HLAAbstractArrayDataElement& value) const
101 stream.alignOffsetForSize(getAlignment());
102 unsigned numElementsType = getNumElements();
103 unsigned numElementsValue = value.getNumElements();
104 unsigned numElements = SGMisc<unsigned>::min(numElementsType, numElementsValue);
106 for (; i < numElements; ++i)
107 if (!value.encodeElement(stream, i))
109 for (; i < numElementsType; ++i) {
110 HLADataTypeEncodeVisitor visitor(stream);
111 getElementDataType()->accept(visitor);
116 /////////////////////////////////////////////////////////////////////////////////
118 HLAVariableArrayDataType::HLAVariableArrayDataType(const std::string& name) :
119 HLAArrayDataType(name)
121 setSizeDataType(new HLAUInt32BEDataType);
124 HLAVariableArrayDataType::~HLAVariableArrayDataType()
129 HLAVariableArrayDataType::accept(HLADataTypeVisitor& visitor) const
131 visitor.apply(*this);
135 HLAVariableArrayDataType::decode(HLADecodeStream& stream, HLAAbstractArrayDataElement& value) const
137 stream.alignOffsetForSize(getAlignment());
138 HLATemplateDecodeVisitor<unsigned> numElementsVisitor(stream);
139 getSizeDataType()->accept(numElementsVisitor);
140 unsigned numElements = numElementsVisitor.getValue();
141 if (!value.setNumElements(numElements))
143 for (unsigned i = 0; i < numElements; ++i)
144 if (!value.decodeElement(stream, i))
150 HLAVariableArrayDataType::encode(HLAEncodeStream& stream, const HLAAbstractArrayDataElement& value) const
152 stream.alignOffsetForSize(getAlignment());
153 unsigned numElements = value.getNumElements();
154 HLATemplateEncodeVisitor<unsigned> numElementsVisitor(stream, numElements);
155 getSizeDataType()->accept(numElementsVisitor);
156 for (unsigned i = 0; i < numElements; ++i)
157 if (!value.encodeElement(stream, i))
163 HLAVariableArrayDataType::setSizeDataType(const HLADataType* sizeDataType)
165 // FIXME this only works if we do not reset the alignment to something smaller
166 if (getAlignment() < sizeDataType->getAlignment())
167 setAlignment(sizeDataType->getAlignment());
168 _sizeDataType = sizeDataType;
169 // setAlignment(SGMisc<unsigned>::max(_sizeDataType->getAlignment(), _elementDataType->getAlignment());
172 } // namespace simgear