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 "HLAFixedRecordDataElement.hxx"
28 #include <simgear/debug/logstream.hxx>
30 #include "HLADataElementVisitor.hxx"
31 #include "HLADataTypeVisitor.hxx"
35 HLAAbstractFixedRecordDataElement::HLAAbstractFixedRecordDataElement(const HLAFixedRecordDataType* dataType) :
40 HLAAbstractFixedRecordDataElement::~HLAAbstractFixedRecordDataElement()
45 HLAAbstractFixedRecordDataElement::accept(HLADataElementVisitor& visitor)
51 HLAAbstractFixedRecordDataElement::accept(HLAConstDataElementVisitor& visitor) const
57 HLAAbstractFixedRecordDataElement::decode(HLADecodeStream& stream)
59 if (!_dataType.valid())
61 return _dataType->decode(stream, *this);
65 HLAAbstractFixedRecordDataElement::encode(HLAEncodeStream& stream) const
67 if (!_dataType.valid())
69 return _dataType->encode(stream, *this);
72 const HLAFixedRecordDataType*
73 HLAAbstractFixedRecordDataElement::getDataType() const
75 return _dataType.get();
79 HLAAbstractFixedRecordDataElement::setDataType(const HLADataType* dataType)
81 const HLAFixedRecordDataType* fixedRecordDataType = dataType->toFixedRecordDataType();
82 if (!fixedRecordDataType) {
83 SG_LOG(SG_NETWORK, SG_WARN, "HLAFixedRecordDataType: unable to set data type!");
86 setDataType(fixedRecordDataType);
91 HLAAbstractFixedRecordDataElement::setDataType(const HLAFixedRecordDataType* dataType)
97 HLAAbstractFixedRecordDataElement::getNumFields() const
99 if (!_dataType.valid())
101 return _dataType->getNumFields();
105 HLAAbstractFixedRecordDataElement::getFieldNumber(const std::string& name) const
107 if (!_dataType.valid())
109 return _dataType->getFieldNumber(name);
113 HLAAbstractFixedRecordDataElement::getFieldDataType(unsigned i) const
115 if (!_dataType.valid())
117 return _dataType->getFieldDataType(i);
121 HLAAbstractFixedRecordDataElement::getFieldDataType(const std::string& name) const
123 if (!_dataType.valid())
125 return getFieldDataType(getFieldNumber(name));
129 HLAFixedRecordDataElement::HLAFixedRecordDataElement(const HLAFixedRecordDataType* dataType) :
130 HLAAbstractFixedRecordDataElement(dataType)
132 _fieldVector.resize(getNumFields());
135 HLAFixedRecordDataElement::~HLAFixedRecordDataElement()
141 HLAFixedRecordDataElement::setDataType(const HLADataType* dataType)
143 if (!HLAAbstractFixedRecordDataElement::setDataType(dataType))
145 _fieldVector.resize(getNumFields());
150 HLAFixedRecordDataElement::decodeField(HLADecodeStream& stream, unsigned i)
152 HLADataElement* dataElement = getField(i);
154 return dataElement->decode(stream);
156 HLADataTypeDecodeVisitor visitor(stream);
157 getDataType()->getFieldDataType(i)->accept(visitor);
163 HLAFixedRecordDataElement::encodeField(HLAEncodeStream& stream, unsigned i) const
165 const HLADataElement* dataElement = getField(i);
167 return dataElement->encode(stream);
169 HLADataTypeEncodeVisitor visitor(stream);
170 getDataType()->getFieldDataType(i)->accept(visitor);
176 HLAFixedRecordDataElement::getField(const std::string& name)
178 return getField(getFieldNumber(name));
181 const HLADataElement*
182 HLAFixedRecordDataElement::getField(const std::string& name) const
184 return getField(getFieldNumber(name));
188 HLAFixedRecordDataElement::getField(unsigned i)
190 if (_fieldVector.size() <= i)
192 return _fieldVector[i].get();
195 const HLADataElement*
196 HLAFixedRecordDataElement::getField(unsigned i) const
198 if (_fieldVector.size() <= i)
200 return _fieldVector[i].get();
204 HLAFixedRecordDataElement::setField(unsigned index, HLADataElement* value)
206 if (getNumFields() <= index)
208 if (_fieldVector[index].valid())
209 _fieldVector[index]->clearStamp();
210 _fieldVector[index] = value;
212 value->attachStamp(*this);
217 HLAFixedRecordDataElement::setField(const std::string& name, HLADataElement* value)
219 setField(getFieldNumber(name), value);
223 HLAFixedRecordDataElement::_setStamp(Stamp* stamp)
225 HLAAbstractFixedRecordDataElement::_setStamp(stamp);
226 for (FieldVector::iterator i = _fieldVector.begin(); i != _fieldVector.end(); ++i) {
229 (*i)->attachStamp(*this);