]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAEnumeratedDataType.cxx
hla: Fixes for the data element index implementation.
[simgear.git] / simgear / hla / HLAEnumeratedDataType.cxx
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 "HLAEnumeratedDataType.hxx"
25
26 #include <map>
27 #include <sstream>
28 #include <vector>
29 #include "HLAEnumeratedDataType.hxx"
30 #include "HLADataTypeVisitor.hxx"
31
32 namespace simgear {
33
34 template<typename DataType, typename T>
35 class HLAEnumeratedDataType::Map : public HLAEnumeratedDataType::AbstractMap {
36 public:
37     typedef typename std::pair<std::string,T> RepresentationPair;
38     typedef typename std::map<T, unsigned> RepresentationIndexMap;
39     typedef typename std::vector<RepresentationPair> IndexRepresentationMap;
40     typedef typename std::map<std::string, unsigned> StringIndexMap;
41
42     Map(const DataType* dataType) :
43         _dataType(dataType)
44     { }
45     virtual bool encode(HLAEncodeStream& stream, unsigned index) const
46     {
47         T value = _otherRepresentation;
48         if (index < _indexRepresentationMap.size())
49             value = _indexRepresentationMap[index].second;
50         if (!_dataType->encode(stream, value))
51             return false;
52         return true;
53     }
54     virtual bool decode(HLADecodeStream& stream, unsigned& index) const
55     {
56         T value;
57         if (!_dataType->decode(stream, value))
58             return false;
59         typename RepresentationIndexMap::const_iterator i;
60         i = _representationIndexMap.find(value);
61         if (i == _representationIndexMap.end())
62             index = _indexRepresentationMap.size();
63         else
64             index = i->second;
65         return true;
66     }
67     virtual const HLABasicDataType* getDataType() const
68     { return _dataType.get(); }
69
70     virtual bool add(const std::string& name, const std::string& number)
71     {
72         std::stringstream ss(number);
73         T value;
74         ss >> value;
75         if (ss.fail())
76             return false;
77         if (_representationIndexMap.find(value) != _representationIndexMap.end())
78             return false;
79         if (_stringIndexMap.find(name) != _stringIndexMap.end())
80             return false;
81         _stringIndexMap[name] = _indexRepresentationMap.size();
82         _representationIndexMap[value] = _indexRepresentationMap.size();
83         _indexRepresentationMap.push_back(RepresentationPair(name, value));
84         return true;
85     }
86     virtual std::string getString(unsigned index) const
87     {
88         if (_indexRepresentationMap.size() <= index)
89             return std::string();
90         return _indexRepresentationMap[index].first;
91     }
92     virtual unsigned getIndex(const std::string& name) const
93     {
94         typename StringIndexMap::const_iterator i = _stringIndexMap.find(name);
95         if (i == _stringIndexMap.end())
96             return ~unsigned(0);
97         return i->second;
98     }
99     virtual unsigned getNumEnumerators() const
100     { return _indexRepresentationMap.size(); }
101
102 private:
103     IndexRepresentationMap _indexRepresentationMap;
104     StringIndexMap _stringIndexMap;
105     RepresentationIndexMap _representationIndexMap;
106
107     T _otherRepresentation;
108     SGSharedPtr<const DataType> _dataType;
109 };
110
111 class HLAEnumeratedDataType::RepresentationVisitor : public HLADataTypeVisitor {
112 public:
113     virtual void apply(const HLAInt8DataType& dataType)
114     { _map = new Map<HLAInt8DataType, int8_t>(&dataType); }
115     virtual void apply(const HLAUInt8DataType& dataType)
116     { _map = new Map<HLAUInt8DataType, uint8_t>(&dataType); }
117     virtual void apply(const HLAInt16DataType& dataType)
118     { _map = new Map<HLAInt16DataType, int16_t>(&dataType); }
119     virtual void apply(const HLAUInt16DataType& dataType)
120     { _map = new Map<HLAUInt16DataType, uint16_t>(&dataType); }
121     virtual void apply(const HLAInt32DataType& dataType)
122     { _map = new Map<HLAInt32DataType, int32_t>(&dataType); }
123     virtual void apply(const HLAUInt32DataType& dataType)
124     { _map = new Map<HLAUInt32DataType, uint32_t>(&dataType); }
125     virtual void apply(const HLAInt64DataType& dataType)
126     { _map = new Map<HLAInt64DataType, int64_t>(&dataType); }
127     virtual void apply(const HLAUInt64DataType& dataType)
128     { _map = new Map<HLAUInt64DataType, uint64_t>(&dataType); }
129
130     SGSharedPtr<AbstractMap> _map;
131 };
132
133 HLAEnumeratedDataType::HLAEnumeratedDataType(const std::string& name) :
134     HLADataType(name)
135 {
136 }
137
138 HLAEnumeratedDataType::~HLAEnumeratedDataType()
139 {
140 }
141
142 void
143 HLAEnumeratedDataType::accept(HLADataTypeVisitor& visitor) const
144 {
145     visitor.apply(*this);
146 }
147
148 const HLAEnumeratedDataType*
149 HLAEnumeratedDataType::toEnumeratedDataType() const
150 {
151     return this;
152 }
153
154 bool
155 HLAEnumeratedDataType::decode(HLADecodeStream& stream, unsigned& index) const
156 {
157     if (!_map.valid())
158         return false;
159
160     if (!_map->decode(stream, index))
161         return false;
162     return true;
163 }
164
165 bool
166 HLAEnumeratedDataType::encode(HLAEncodeStream& stream, unsigned index) const
167 {
168     if (!_map.valid())
169         return false;
170
171     return _map->encode(stream, index);
172 }
173
174 void
175 HLAEnumeratedDataType::setRepresentation(HLABasicDataType* representation)
176 {
177     if (!representation)
178         return;
179
180     RepresentationVisitor representationVisitor;
181     representation->accept(representationVisitor);
182     _map.swap(representationVisitor._map);
183 }
184
185 void
186 HLAEnumeratedDataType::_recomputeAlignmentImplementation()
187 {
188     unsigned alignment = 1;
189     if (const HLADataType* dataType = getRepresentation())
190         alignment = std::max(alignment, dataType->getAlignment());
191     setAlignment(alignment);
192 }
193
194 } // namespace simgear
195