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 "HLAArrayDataType.hxx"
20 #include "HLAArrayDataElement.hxx"
24 HLAArrayDataType::HLAArrayDataType(const std::string& name) :
29 HLAArrayDataType::~HLAArrayDataType()
34 HLAArrayDataType::accept(HLADataTypeVisitor& visitor) const
39 const HLAArrayDataType*
40 HLAArrayDataType::toArrayDataType() const
46 HLAArrayDataType::setElementDataType(const HLADataType* elementDataType)
48 // FIXME this only works if we do not reset the alignment to something smaller
49 if (getAlignment() < elementDataType->getAlignment())
50 setAlignment(elementDataType->getAlignment());
51 _elementDataType = elementDataType;
54 ///////////////////////////////////////////////////////////////////////////////////
56 HLAFixedArrayDataType::HLAFixedArrayDataType(const std::string& name) :
57 HLAArrayDataType(name)
61 HLAFixedArrayDataType::~HLAFixedArrayDataType()
66 HLAFixedArrayDataType::accept(HLADataTypeVisitor& visitor) const
72 HLAFixedArrayDataType::decode(HLADecodeStream& stream, HLAAbstractArrayDataElement& value) const
74 stream.alignOffsetForSize(getAlignment());
75 unsigned numElements = getNumElements();
76 if (!value.setNumElements(numElements))
78 for (unsigned i = 0; i < numElements; ++i)
79 if (!value.decodeElement(stream, i))
85 HLAFixedArrayDataType::encode(HLAEncodeStream& stream, const HLAAbstractArrayDataElement& value) const
87 stream.alignOffsetForSize(getAlignment());
88 unsigned numElementsType = getNumElements();
89 unsigned numElementsValue = value.getNumElements();
90 unsigned numElements = SGMisc<unsigned>::min(numElementsType, numElementsValue);
92 for (; i < numElements; ++i)
93 if (!value.encodeElement(stream, i))
95 for (; i < numElementsType; ++i) {
96 HLADataTypeEncodeVisitor visitor(stream);
97 getElementDataType()->accept(visitor);
102 /////////////////////////////////////////////////////////////////////////////////
104 HLAVariableArrayDataType::HLAVariableArrayDataType(const std::string& name) :
105 HLAArrayDataType(name)
107 setSizeDataType(new HLAUInt32BEDataType);
110 HLAVariableArrayDataType::~HLAVariableArrayDataType()
115 HLAVariableArrayDataType::accept(HLADataTypeVisitor& visitor) const
117 visitor.apply(*this);
121 HLAVariableArrayDataType::decode(HLADecodeStream& stream, HLAAbstractArrayDataElement& value) const
123 stream.alignOffsetForSize(getAlignment());
124 HLATemplateDecodeVisitor<unsigned> numElementsVisitor(stream);
125 getSizeDataType()->accept(numElementsVisitor);
126 unsigned numElements = numElementsVisitor.getValue();
127 if (!value.setNumElements(numElements))
129 for (unsigned i = 0; i < numElements; ++i)
130 if (!value.decodeElement(stream, i))
136 HLAVariableArrayDataType::encode(HLAEncodeStream& stream, const HLAAbstractArrayDataElement& value) const
138 stream.alignOffsetForSize(getAlignment());
139 unsigned numElements = value.getNumElements();
140 HLATemplateEncodeVisitor<unsigned> numElementsVisitor(stream, numElements);
141 getSizeDataType()->accept(numElementsVisitor);
142 for (unsigned i = 0; i < numElements; ++i)
143 if (!value.encodeElement(stream, i))
149 HLAVariableArrayDataType::setSizeDataType(const HLADataType* sizeDataType)
151 // FIXME this only works if we do not reset the alignment to something smaller
152 if (getAlignment() < sizeDataType->getAlignment())
153 setAlignment(sizeDataType->getAlignment());
154 _sizeDataType = sizeDataType;
155 // setAlignment(SGMisc<unsigned>::max(_sizeDataType->getAlignment(), _elementDataType->getAlignment());
158 } // namespace simgear