]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAEnumeratedDataType.cxx
hla: Move callbacks into the rti federate implementation.
[simgear.git] / simgear / hla / HLAEnumeratedDataType.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 "HLAEnumeratedDataType.hxx"
19
20 #include <map>
21 #include <sstream>
22 #include <vector>
23 #include "HLAEnumeratedDataType.hxx"
24 #include "HLADataTypeVisitor.hxx"
25
26 namespace simgear {
27
28 template<typename DataType, typename T>
29 class HLAEnumeratedDataType::Map : public HLAEnumeratedDataType::AbstractMap {
30 public:
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;
35
36     Map(const DataType* dataType) :
37         _dataType(dataType)
38     { }
39     virtual bool encode(HLAEncodeStream& stream, unsigned index) const
40     {
41         T value = _otherRepresentation;
42         if (index < _indexRepresentationMap.size())
43             value = _indexRepresentationMap[index].second;
44         if (!_dataType->encode(stream, value))
45             return false;
46         return true;
47     }
48     virtual bool decode(HLADecodeStream& stream, unsigned& index) const
49     {
50         T value;
51         if (!_dataType->decode(stream, value))
52             return false;
53         typename RepresentationIndexMap::const_iterator i;
54         i = _representationIndexMap.find(value);
55         if (i == _representationIndexMap.end())
56             index = _indexRepresentationMap.size();
57         else
58             index = i->second;
59         return true;
60     }
61     virtual const HLABasicDataType* getDataType() const
62     { return _dataType.get(); }
63
64     virtual bool add(const std::string& name, const std::string& number)
65     {
66         std::stringstream ss(number);
67         T value;
68         ss >> value;
69         if (ss.fail())
70             return false;
71         if (_representationIndexMap.find(value) != _representationIndexMap.end())
72             return false;
73         if (_stringIndexMap.find(name) != _stringIndexMap.end())
74             return false;
75         _stringIndexMap[name] = _indexRepresentationMap.size();
76         _representationIndexMap[value] = _indexRepresentationMap.size();
77         _indexRepresentationMap.push_back(RepresentationPair(name, value));
78         return true;
79     }
80     virtual std::string getString(unsigned index) const
81     {
82         if (_indexRepresentationMap.size() <= index)
83             return std::string();
84         return _indexRepresentationMap[index].first;
85     }
86     virtual unsigned getIndex(const std::string& name) const
87     {
88         typename StringIndexMap::const_iterator i = _stringIndexMap.find(name);
89         if (i == _stringIndexMap.end())
90             return ~unsigned(0);
91         return i->second;
92     }
93     virtual unsigned getNumEnumerators() const
94     { return _indexRepresentationMap.size(); }
95
96 private:
97     IndexRepresentationMap _indexRepresentationMap;
98     StringIndexMap _stringIndexMap;
99     RepresentationIndexMap _representationIndexMap;
100
101     T _otherRepresentation;
102     SGSharedPtr<const DataType> _dataType;
103 };
104
105 class HLAEnumeratedDataType::RepresentationVisitor : public HLADataTypeVisitor {
106 public:
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); }
123
124     SGSharedPtr<AbstractMap> _map;
125 };
126
127 HLAEnumeratedDataType::HLAEnumeratedDataType(const std::string& name) :
128     HLADataType(name)
129 {
130 }
131
132 HLAEnumeratedDataType::~HLAEnumeratedDataType()
133 {
134 }
135
136 void
137 HLAEnumeratedDataType::accept(HLADataTypeVisitor& visitor) const
138 {
139     visitor.apply(*this);
140 }
141
142 const HLAEnumeratedDataType*
143 HLAEnumeratedDataType::toEnumeratedDataType() const
144 {
145     return this;
146 }
147
148 bool
149 HLAEnumeratedDataType::decode(HLADecodeStream& stream, unsigned& index) const
150 {
151     if (!_map.valid())
152         return false;
153
154     if (!_map->decode(stream, index))
155         return false;
156     return true;
157 }
158
159 bool
160 HLAEnumeratedDataType::encode(HLAEncodeStream& stream, unsigned index) const
161 {
162     if (!_map.valid())
163         return false;
164
165     return _map->encode(stream, index);
166 }
167
168 void
169 HLAEnumeratedDataType::setRepresentation(HLABasicDataType* representation)
170 {
171     if (!representation)
172         return;
173
174     RepresentationVisitor representationVisitor;
175     representation->accept(representationVisitor);
176     _map.swap(representationVisitor._map);
177 }
178
179 } // namespace simgear
180