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.
18 #include "HLAEnumeratedDataType.hxx"
23 #include "HLAEnumeratedDataType.hxx"
24 #include "HLADataTypeVisitor.hxx"
28 template<typename DataType, typename T>
29 class HLAEnumeratedDataType::Map : public HLAEnumeratedDataType::AbstractMap {
31 typedef typename std::pair<std::string,T> RepresentationPair;
32 typedef typename std::map<T, unsigned> RepresentationIndexMap;
33 typedef typename std::vector<RepresentationPair> IndexRepresentationMap;
34 typedef typename std::map<std::string, unsigned> StringIndexMap;
36 Map(const DataType* dataType) :
39 virtual bool encode(HLAEncodeStream& stream, unsigned index) const
41 T value = _otherRepresentation;
42 if (index < _indexRepresentationMap.size())
43 value = _indexRepresentationMap[index].second;
44 if (!_dataType->encode(stream, value))
48 virtual bool decode(HLADecodeStream& stream, unsigned& index) const
51 if (!_dataType->decode(stream, value))
53 typename RepresentationIndexMap::const_iterator i;
54 i = _representationIndexMap.find(value);
55 if (i == _representationIndexMap.end())
56 index = _indexRepresentationMap.size();
61 virtual const HLABasicDataType* getDataType() const
62 { return _dataType.get(); }
64 virtual bool add(const std::string& name, const std::string& number)
66 std::stringstream ss(number);
71 if (_representationIndexMap.find(value) != _representationIndexMap.end())
73 if (_stringIndexMap.find(name) != _stringIndexMap.end())
75 _stringIndexMap[name] = _indexRepresentationMap.size();
76 _representationIndexMap[value] = _indexRepresentationMap.size();
77 _indexRepresentationMap.push_back(RepresentationPair(name, value));
80 virtual std::string getString(unsigned index) const
82 if (_indexRepresentationMap.size() <= index)
84 return _indexRepresentationMap[index].first;
86 virtual unsigned getIndex(const std::string& name) const
88 typename StringIndexMap::const_iterator i = _stringIndexMap.find(name);
89 if (i == _stringIndexMap.end())
93 virtual unsigned getNumEnumerators() const
94 { return _indexRepresentationMap.size(); }
97 IndexRepresentationMap _indexRepresentationMap;
98 StringIndexMap _stringIndexMap;
99 RepresentationIndexMap _representationIndexMap;
101 T _otherRepresentation;
102 SGSharedPtr<const DataType> _dataType;
105 class HLAEnumeratedDataType::RepresentationVisitor : public HLADataTypeVisitor {
107 virtual void apply(const HLAInt8DataType& dataType)
108 { _map = new Map<HLAInt8DataType, int8_t>(&dataType); }
109 virtual void apply(const HLAUInt8DataType& dataType)
110 { _map = new Map<HLAUInt8DataType, uint8_t>(&dataType); }
111 virtual void apply(const HLAInt16DataType& dataType)
112 { _map = new Map<HLAInt16DataType, int16_t>(&dataType); }
113 virtual void apply(const HLAUInt16DataType& dataType)
114 { _map = new Map<HLAUInt16DataType, uint16_t>(&dataType); }
115 virtual void apply(const HLAInt32DataType& dataType)
116 { _map = new Map<HLAInt32DataType, int32_t>(&dataType); }
117 virtual void apply(const HLAUInt32DataType& dataType)
118 { _map = new Map<HLAUInt32DataType, uint32_t>(&dataType); }
119 virtual void apply(const HLAInt64DataType& dataType)
120 { _map = new Map<HLAInt64DataType, int64_t>(&dataType); }
121 virtual void apply(const HLAUInt64DataType& dataType)
122 { _map = new Map<HLAUInt64DataType, uint64_t>(&dataType); }
124 SGSharedPtr<AbstractMap> _map;
127 HLAEnumeratedDataType::HLAEnumeratedDataType(const std::string& name) :
132 HLAEnumeratedDataType::~HLAEnumeratedDataType()
137 HLAEnumeratedDataType::accept(HLADataTypeVisitor& visitor) const
139 visitor.apply(*this);
142 const HLAEnumeratedDataType*
143 HLAEnumeratedDataType::toEnumeratedDataType() const
149 HLAEnumeratedDataType::decode(HLADecodeStream& stream, unsigned& index) const
154 if (!_map->decode(stream, index))
160 HLAEnumeratedDataType::encode(HLAEncodeStream& stream, unsigned index) const
165 return _map->encode(stream, index);
169 HLAEnumeratedDataType::setRepresentation(HLABasicDataType* representation)
174 RepresentationVisitor representationVisitor;
175 representation->accept(representationVisitor);
176 _map.swap(representationVisitor._map);
180 HLAEnumeratedDataType::_recomputeAlignmentImplementation()
182 unsigned alignment = 1;
183 if (const HLADataType* dataType = getRepresentation())
184 alignment = std::max(alignment, dataType->getAlignment());
185 setAlignment(alignment);
188 } // namespace simgear