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 "HLAEnumeratedDataType.hxx"
29 #include "HLAEnumeratedDataType.hxx"
30 #include "HLADataTypeVisitor.hxx"
34 template<typename DataType, typename T>
35 class HLAEnumeratedDataType::Map : public HLAEnumeratedDataType::AbstractMap {
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;
42 Map(const DataType* dataType) :
45 virtual bool encode(HLAEncodeStream& stream, unsigned index) const
47 T value = _otherRepresentation;
48 if (index < _indexRepresentationMap.size())
49 value = _indexRepresentationMap[index].second;
50 if (!_dataType->encode(stream, value))
54 virtual bool decode(HLADecodeStream& stream, unsigned& index) const
57 if (!_dataType->decode(stream, value))
59 typename RepresentationIndexMap::const_iterator i;
60 i = _representationIndexMap.find(value);
61 if (i == _representationIndexMap.end())
62 index = _indexRepresentationMap.size();
67 virtual const HLABasicDataType* getDataType() const
68 { return _dataType.get(); }
70 virtual bool add(const std::string& name, const std::string& number)
72 std::stringstream ss(number);
77 if (_representationIndexMap.find(value) != _representationIndexMap.end())
79 if (_stringIndexMap.find(name) != _stringIndexMap.end())
81 _stringIndexMap[name] = _indexRepresentationMap.size();
82 _representationIndexMap[value] = _indexRepresentationMap.size();
83 _indexRepresentationMap.push_back(RepresentationPair(name, value));
86 virtual std::string getString(unsigned index) const
88 if (_indexRepresentationMap.size() <= index)
90 return _indexRepresentationMap[index].first;
92 virtual unsigned getIndex(const std::string& name) const
94 typename StringIndexMap::const_iterator i = _stringIndexMap.find(name);
95 if (i == _stringIndexMap.end())
99 virtual unsigned getNumEnumerators() const
100 { return _indexRepresentationMap.size(); }
103 IndexRepresentationMap _indexRepresentationMap;
104 StringIndexMap _stringIndexMap;
105 RepresentationIndexMap _representationIndexMap;
107 T _otherRepresentation;
108 SGSharedPtr<const DataType> _dataType;
111 class HLAEnumeratedDataType::RepresentationVisitor : public HLADataTypeVisitor {
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); }
130 SGSharedPtr<AbstractMap> _map;
133 HLAEnumeratedDataType::HLAEnumeratedDataType(const std::string& name) :
138 HLAEnumeratedDataType::~HLAEnumeratedDataType()
143 HLAEnumeratedDataType::accept(HLADataTypeVisitor& visitor) const
145 visitor.apply(*this);
148 const HLAEnumeratedDataType*
149 HLAEnumeratedDataType::toEnumeratedDataType() const
155 HLAEnumeratedDataType::decode(HLADecodeStream& stream, unsigned& index) const
160 if (!_map->decode(stream, index))
166 HLAEnumeratedDataType::encode(HLAEncodeStream& stream, unsigned index) const
171 return _map->encode(stream, index);
175 HLAEnumeratedDataType::setRepresentation(HLABasicDataType* representation)
180 RepresentationVisitor representationVisitor;
181 representation->accept(representationVisitor);
182 _map.swap(representationVisitor._map);
186 HLAEnumeratedDataType::_recomputeAlignmentImplementation()
188 unsigned alignment = 1;
189 if (const HLADataType* dataType = getRepresentation())
190 alignment = std::max(alignment, dataType->getAlignment());
191 setAlignment(alignment);
194 } // namespace simgear