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 "HLAEnumeratedDataElement.hxx"
20 #include <simgear/debug/logstream.hxx>
24 HLAAbstractEnumeratedDataElement::HLAAbstractEnumeratedDataElement(const HLAEnumeratedDataType* dataType) :
29 HLAAbstractEnumeratedDataElement::~HLAAbstractEnumeratedDataElement()
34 HLAAbstractEnumeratedDataElement::decode(HLADecodeStream& stream)
36 if (!_dataType.valid())
39 if (!_dataType->decode(stream, index))
41 setEnumeratorIndex(index);
46 HLAAbstractEnumeratedDataElement::encode(HLAEncodeStream& stream) const
48 if (!_dataType.valid())
50 return _dataType->encode(stream, getEnumeratorIndex());
53 const HLAEnumeratedDataType*
54 HLAAbstractEnumeratedDataElement::getDataType() const
56 return _dataType.get();
60 HLAAbstractEnumeratedDataElement::setDataType(const HLADataType* dataType)
62 const HLAEnumeratedDataType* enumeratedDataType = dataType->toEnumeratedDataType();
63 if (!enumeratedDataType) {
64 SG_LOG(SG_NETWORK, SG_WARN, "HLAEnumeratedDataType: unable to set data type!");
67 setDataType(enumeratedDataType);
72 HLAAbstractEnumeratedDataElement::setDataType(const HLAEnumeratedDataType* dataType)
77 const HLABasicDataType*
78 HLAAbstractEnumeratedDataElement::getRepresentationDataType() const
80 if (!_dataType.valid())
82 return _dataType->getRepresentation();
86 HLAAbstractEnumeratedDataElement::getStringRepresentation() const
88 if (!_dataType.valid())
90 return _dataType->getString(getEnumeratorIndex());
94 HLAAbstractEnumeratedDataElement::setStringRepresentation(const std::string& name)
96 if (!_dataType.valid())
98 unsigned index = _dataType->getIndex(name);
99 if (_dataType->getNumEnumerators() <= index)
101 setEnumeratorIndex(index);
106 HLAEnumeratedDataElement::HLAEnumeratedDataElement(const HLAEnumeratedDataType* dataType) :
107 HLAAbstractEnumeratedDataElement(dataType),
108 _enumeratorIndex(~unsigned(0))
112 HLAEnumeratedDataElement::~HLAEnumeratedDataElement()
117 HLAEnumeratedDataElement::getEnumeratorIndex() const
119 return _enumeratorIndex;
123 HLAEnumeratedDataElement::setEnumeratorIndex(unsigned index)
125 _enumeratorIndex = index;