]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAEnumeratedDataElement.cxx
hla: add missing file fir the last commit.
[simgear.git] / simgear / hla / HLAEnumeratedDataElement.cxx
1 // Copyright (C) 2009 - 2010  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
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.
7 //
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.
12 //
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.
16 //
17
18 #include "HLAEnumeratedDataElement.hxx"
19
20 #include <simgear/debug/logstream.hxx>
21
22 namespace simgear {
23
24 HLAAbstractEnumeratedDataElement::HLAAbstractEnumeratedDataElement(const HLAEnumeratedDataType* dataType) :
25     _dataType(dataType)
26 {
27 }
28
29 HLAAbstractEnumeratedDataElement::~HLAAbstractEnumeratedDataElement()
30 {
31 }
32
33 bool
34 HLAAbstractEnumeratedDataElement::decode(HLADecodeStream& stream)
35 {
36     if (!_dataType.valid())
37         return false;
38     unsigned index;
39     if (!_dataType->decode(stream, index))
40         return false;
41     setEnumeratorIndex(index);
42     return true;
43 }
44
45 bool
46 HLAAbstractEnumeratedDataElement::encode(HLAEncodeStream& stream) const
47 {
48     if (!_dataType.valid())
49         return false;
50     return _dataType->encode(stream, getEnumeratorIndex());
51 }
52
53 const HLAEnumeratedDataType*
54 HLAAbstractEnumeratedDataElement::getDataType() const
55 {
56     return _dataType.get();
57 }
58
59 bool
60 HLAAbstractEnumeratedDataElement::setDataType(const HLADataType* dataType)
61 {
62     const HLAEnumeratedDataType* enumeratedDataType = dataType->toEnumeratedDataType();
63     if (!enumeratedDataType) {
64         SG_LOG(SG_NETWORK, SG_WARN, "HLAEnumeratedDataType: unable to set data type!");
65         return false;
66     }
67     setDataType(enumeratedDataType);
68     return true;
69 }
70
71 void
72 HLAAbstractEnumeratedDataElement::setDataType(const HLAEnumeratedDataType* dataType)
73 {
74     _dataType = dataType;
75 }
76
77 const HLABasicDataType*
78 HLAAbstractEnumeratedDataElement::getRepresentationDataType() const
79 {
80     if (!_dataType.valid())
81         return 0;
82     return _dataType->getRepresentation();
83 }
84
85 std::string
86 HLAAbstractEnumeratedDataElement::getStringRepresentation() const
87 {
88     if (!_dataType.valid())
89         return std::string();
90     return _dataType->getString(getEnumeratorIndex());
91 }
92
93 bool
94 HLAAbstractEnumeratedDataElement::setStringRepresentation(const std::string& name)
95 {
96     if (!_dataType.valid())
97         return false;
98     unsigned index = _dataType->getIndex(name);
99     if (_dataType->getNumEnumerators() <= index)
100         return false;
101     setEnumeratorIndex(index);
102     return true;
103 }
104
105
106 HLAEnumeratedDataElement::HLAEnumeratedDataElement(const HLAEnumeratedDataType* dataType) :
107     HLAAbstractEnumeratedDataElement(dataType),
108     _enumeratorIndex(~unsigned(0))
109 {
110 }
111
112 HLAEnumeratedDataElement::~HLAEnumeratedDataElement()
113 {
114 }
115
116 unsigned
117 HLAEnumeratedDataElement::getEnumeratorIndex() const
118 {
119     return _enumeratorIndex;
120 }
121
122 void
123 HLAEnumeratedDataElement::setEnumeratorIndex(unsigned index)
124 {
125     _enumeratorIndex = index;
126 }
127
128 }