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 "HLAFixedRecordDataElement.hxx"
22 #include <simgear/debug/logstream.hxx>
23 #include "HLADataTypeVisitor.hxx"
27 HLAAbstractFixedRecordDataElement::HLAAbstractFixedRecordDataElement(const HLAFixedRecordDataType* dataType) :
32 HLAAbstractFixedRecordDataElement::~HLAAbstractFixedRecordDataElement()
37 HLAAbstractFixedRecordDataElement::decode(HLADecodeStream& stream)
39 if (!_dataType.valid())
41 return _dataType->decode(stream, *this);
45 HLAAbstractFixedRecordDataElement::encode(HLAEncodeStream& stream) const
47 if (!_dataType.valid())
49 return _dataType->encode(stream, *this);
52 const HLAFixedRecordDataType*
53 HLAAbstractFixedRecordDataElement::getDataType() const
55 return _dataType.get();
59 HLAAbstractFixedRecordDataElement::setDataType(const HLADataType* dataType)
61 const HLAFixedRecordDataType* fixedRecordDataType = dataType->toFixedRecordDataType();
62 if (!fixedRecordDataType) {
63 SG_LOG(SG_NETWORK, SG_WARN, "HLAFixedRecordDataType: unable to set data type!");
66 setDataType(fixedRecordDataType);
71 HLAAbstractFixedRecordDataElement::setDataType(const HLAFixedRecordDataType* dataType)
77 HLAAbstractFixedRecordDataElement::getNumFields() const
79 return _dataType->getNumFields();
83 HLAAbstractFixedRecordDataElement::getFieldNumber(const std::string& name) const
85 return _dataType->getFieldNumber(name);
89 HLAAbstractFixedRecordDataElement::getFieldDataType(unsigned i) const
91 return _dataType->getFieldDataType(i);
95 HLAAbstractFixedRecordDataElement::getFieldDataType(const std::string& name) const
97 return getFieldDataType(getFieldNumber(name));
101 HLAFixedRecordDataElement::HLAFixedRecordDataElement(const HLAFixedRecordDataType* dataType) :
102 HLAAbstractFixedRecordDataElement(dataType)
104 _fieldVector.resize(getNumFields());
107 HLAFixedRecordDataElement::~HLAFixedRecordDataElement()
112 HLAFixedRecordDataElement::decodeField(HLADecodeStream& stream, unsigned i)
114 HLADataElement* dataElement = getField(i);
116 return dataElement->decode(stream);
118 HLADataTypeDecodeVisitor visitor(stream);
119 getDataType()->getFieldDataType(i)->accept(visitor);
125 HLAFixedRecordDataElement::encodeField(HLAEncodeStream& stream, unsigned i) const
127 const HLADataElement* dataElement = getField(i);
129 return dataElement->encode(stream);
131 HLADataTypeEncodeVisitor visitor(stream);
132 getDataType()->getFieldDataType(i)->accept(visitor);
138 HLAFixedRecordDataElement::getField(const std::string& name)
140 return getField(getFieldNumber(name));
143 const HLADataElement*
144 HLAFixedRecordDataElement::getField(const std::string& name) const
146 return getField(getFieldNumber(name));
150 HLAFixedRecordDataElement::getField(unsigned i)
152 if (_fieldVector.size() <= i)
154 return _fieldVector[i].get();
157 const HLADataElement*
158 HLAFixedRecordDataElement::getField(unsigned i) const
160 if (_fieldVector.size() <= i)
162 return _fieldVector[i].get();
166 HLAFixedRecordDataElement::setField(unsigned index, HLADataElement* value)
168 if (getNumFields() <= index)
170 _fieldVector[index] = value;
174 HLAFixedRecordDataElement::setField(const std::string& name, HLADataElement* value)
176 setField(getFieldNumber(name), value);