1 // Copyright (C) 2009 - 2012  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 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21 
22 #include <simgear/compiler.h>
23 
24 #include "HLAEnumeratedDataElement.hxx"
25 
26 #include <simgear/debug/logstream.hxx>
27 
28 #include "HLADataElementVisitor.hxx"
29 
30 namespace simgear {
31 
HLAAbstractEnumeratedDataElement(const HLAEnumeratedDataType * dataType)32 HLAAbstractEnumeratedDataElement::HLAAbstractEnumeratedDataElement(const HLAEnumeratedDataType* dataType) :
33     _dataType(dataType)
34 {
35 }
36 
~HLAAbstractEnumeratedDataElement()37 HLAAbstractEnumeratedDataElement::~HLAAbstractEnumeratedDataElement()
38 {
39 }
40 
41 void
accept(HLADataElementVisitor & visitor)42 HLAAbstractEnumeratedDataElement::accept(HLADataElementVisitor& visitor)
43 {
44     visitor.apply(*this);
45 }
46 
47 void
accept(HLAConstDataElementVisitor & visitor) const48 HLAAbstractEnumeratedDataElement::accept(HLAConstDataElementVisitor& visitor) const
49 {
50     visitor.apply(*this);
51 }
52 
53 bool
decode(HLADecodeStream & stream)54 HLAAbstractEnumeratedDataElement::decode(HLADecodeStream& stream)
55 {
56     if (!_dataType.valid())
57         return false;
58     unsigned index;
59     if (!_dataType->decode(stream, index))
60         return false;
61     setEnumeratorIndex(index);
62     return true;
63 }
64 
65 bool
encode(HLAEncodeStream & stream) const66 HLAAbstractEnumeratedDataElement::encode(HLAEncodeStream& stream) const
67 {
68     if (!_dataType.valid())
69         return false;
70     return _dataType->encode(stream, getEnumeratorIndex());
71 }
72 
73 const HLAEnumeratedDataType*
getDataType() const74 HLAAbstractEnumeratedDataElement::getDataType() const
75 {
76     return _dataType.get();
77 }
78 
79 bool
setDataType(const HLADataType * dataType)80 HLAAbstractEnumeratedDataElement::setDataType(const HLADataType* dataType)
81 {
82     const HLAEnumeratedDataType* enumeratedDataType = dataType->toEnumeratedDataType();
83     if (!enumeratedDataType) {
84         SG_LOG(SG_NETWORK, SG_WARN, "HLAEnumeratedDataType: unable to set data type!");
85         return false;
86     }
87     setDataType(enumeratedDataType);
88     return true;
89 }
90 
91 void
setDataType(const HLAEnumeratedDataType * dataType)92 HLAAbstractEnumeratedDataElement::setDataType(const HLAEnumeratedDataType* dataType)
93 {
94     _dataType = dataType;
95 }
96 
97 const HLABasicDataType*
getRepresentationDataType() const98 HLAAbstractEnumeratedDataElement::getRepresentationDataType() const
99 {
100     if (!_dataType.valid())
101         return 0;
102     return _dataType->getRepresentation();
103 }
104 
105 std::string
getStringRepresentation() const106 HLAAbstractEnumeratedDataElement::getStringRepresentation() const
107 {
108     if (!_dataType.valid())
109         return std::string();
110     return _dataType->getString(getEnumeratorIndex());
111 }
112 
113 bool
setStringRepresentation(const std::string & name)114 HLAAbstractEnumeratedDataElement::setStringRepresentation(const std::string& name)
115 {
116     if (!_dataType.valid())
117         return false;
118     unsigned index = _dataType->getIndex(name);
119     if (_dataType->getNumEnumerators() <= index)
120         return false;
121     setEnumeratorIndex(index);
122     return true;
123 }
124 
125 
HLAEnumeratedDataElement(const HLAEnumeratedDataType * dataType)126 HLAEnumeratedDataElement::HLAEnumeratedDataElement(const HLAEnumeratedDataType* dataType) :
127     HLAAbstractEnumeratedDataElement(dataType),
128     _enumeratorIndex(~unsigned(0))
129 {
130 }
131 
~HLAEnumeratedDataElement()132 HLAEnumeratedDataElement::~HLAEnumeratedDataElement()
133 {
134 }
135 
136 unsigned
getEnumeratorIndex() const137 HLAEnumeratedDataElement::getEnumeratorIndex() const
138 {
139     return _enumeratorIndex;
140 }
141 
142 void
setEnumeratorIndex(unsigned index)143 HLAEnumeratedDataElement::setEnumeratorIndex(unsigned index)
144 {
145     _enumeratorIndex = index;
146 }
147 
148 }
149