]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLADataType.cxx
hla: Fixes for the data element index implementation.
[simgear.git] / simgear / hla / HLADataType.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 "HLADataType.hxx"
25
26 #include "HLADataElement.hxx"
27 #include "HLADataTypeVisitor.hxx"
28
29 namespace simgear {
30
31 HLADataType::HLADataType(const std::string& name, unsigned alignment) :
32     _name(name),
33     _alignment(1)
34 {
35     setAlignment(alignment);
36 }
37
38 HLADataType::~HLADataType()
39 {
40 }
41
42 void
43 HLADataType::accept(HLADataTypeVisitor& visitor) const
44 {
45     visitor.apply(*this);
46 }
47
48 const HLABasicDataType*
49 HLADataType::toBasicDataType() const
50 {
51     return 0;
52 }
53
54 const HLAArrayDataType*
55 HLADataType::toArrayDataType() const
56 {
57     return 0;
58 }
59
60 const HLAEnumeratedDataType*
61 HLADataType::toEnumeratedDataType() const
62 {
63     return 0;
64 }
65
66 const HLAFixedRecordDataType*
67 HLADataType::toFixedRecordDataType() const
68 {
69     return 0;
70 }
71
72 const HLAVariantRecordDataType*
73 HLADataType::toVariantRecordDataType() const
74 {
75     return 0;
76 }
77
78 bool
79 HLADataType::recomputeAlignment()
80 {
81     unsigned alignment = getAlignment();
82     _recomputeAlignmentImplementation();
83     return alignment != getAlignment();
84 }
85
86 void
87 HLADataType::releaseDataTypeReferences()
88 {
89 }
90
91 class HLADataType::_DataElementIndexVisitor : public HLADataTypeVisitor {
92 public:
93     _DataElementIndexVisitor(HLADataElementIndex& index, const std::string& path, std::string::size_type offset) :
94         _index(index),
95         _path(path),
96         _offset(offset),
97         _success(false)
98     { }
99     virtual ~_DataElementIndexVisitor()
100     { }
101
102     virtual void apply(const HLADataType& dataType)
103     {
104         if (_path.size() == _offset)
105             _success = true;
106     }
107     virtual void apply(const HLAArrayDataType& dataType)
108     {
109         if (_path.size() == _offset) {
110             _success = true;
111             return;
112         }
113         if (_path.size() < _offset) {
114             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
115                    << "Expected array subscript at the end of the path!");
116             return;
117         }
118         if (_path[_offset] != '[') {
119             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
120                    << "Expected array subscript at the end of the path!");
121             return;
122         }
123         ++_offset;
124         if (_path.size() <= _offset) {
125             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
126                    << "Expected closing array subscript at the end of the path!");
127             return;
128         }
129         unsigned index = 0;
130         bool closed = false;
131         while (_offset <= _path.size()) {
132             if (_path[_offset] == ']') {
133                 ++_offset;
134                 closed = true;
135                 break;
136             }
137             unsigned v = _path[_offset] - '0';
138             // Error, no number
139             if (10 <= v) {
140                 SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
141                        << "Array subscript is not a number!");
142                 return;
143             }
144             index *= 10;
145             index += v;
146             ++_offset;
147         }
148         if (!closed) {
149             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
150                    << "Expected closing array subscript at the end of the path!");
151             return;
152         }
153         if (!dataType.getElementDataType()) {
154             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
155                    << "Undefined array element data type!");
156             return;
157         }
158         _index.push_back(index);
159         _success = dataType.getElementDataType()->getDataElementIndex(_index, _path, _offset);
160     }
161
162     virtual void apply(const HLAFixedRecordDataType& dataType)
163     {
164         if (_path.size() == _offset) {
165             _success = true;
166             return;
167         }
168         if (_path.size() < _offset) {
169             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
170                    << "Expected field name at the end of the path!");
171             return;
172         }
173         if (_path[_offset] == '.')
174             ++_offset;
175         if (_path.size() <= _offset) {
176             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
177                    << "Expected field name at the end of the path!");
178             return;
179         }
180         std::string::size_type len = std::min(_path.find_first_of("[.", _offset), _path.size()) - _offset;
181         unsigned index = 0;
182         while (index < dataType.getNumFields()) {
183             if (_path.compare(_offset, len, dataType.getFieldName(index)) == 0)
184                 break;
185             ++index;
186         }
187         if (dataType.getNumFields() <= index) {
188             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
189                    << "Field \"" << _path.substr(_offset, len) << "\" not found in fixed record data type \""
190                    << dataType.getName() << "\"!");
191             return;
192         }
193         if (!dataType.getFieldDataType(index)) {
194             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
195                    << "Undefined field data type in variant record data type \""
196                    << dataType.getName() << "\" at field \"" << dataType.getFieldName(index) << "\"!");
197             return;
198         }
199         _index.push_back(index);
200         _success = dataType.getFieldDataType(index)->getDataElementIndex(_index, _path, _offset + len);
201     }
202
203     virtual void apply(const HLAVariantRecordDataType& dataType)
204     {
205         if (_path.size() == _offset) {
206             _success = true;
207             return;
208         }
209         if (_path.size() < _offset) {
210             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
211                    << "Expected alternative name at the end of the path!");
212             return;
213         }
214         if (_path[_offset] == '.')
215             ++_offset;
216         if (_path.size() <= _offset) {
217             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
218                    << "Expected alternative name at the end of the path!");
219             return;
220         }
221         std::string::size_type len = std::min(_path.find_first_of("[.", _offset), _path.size()) - _offset;
222         unsigned index = 0;
223         while (index < dataType.getNumAlternatives()) {
224             if (_path.compare(_offset, len, dataType.getAlternativeName(index)) == 0)
225                 break;
226             ++index;
227         }
228         if (dataType.getNumAlternatives() <= index) {
229             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
230                    << "Alternative \"" << _path.substr(_offset, len) << "\" not found in variant record data type \""
231                    << dataType.getName() << "\"!");
232             return;
233         }
234         if (!dataType.getAlternativeDataType(index)) {
235             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
236                    << "Undefined alternative data type in variant record data type \""
237                    << dataType.getName() << "\" at alternative \"" << dataType.getAlternativeName(index) << "\"!");
238             return;
239         }
240         _index.push_back(index);
241         _success = dataType.getAlternativeDataType(index)->getDataElementIndex(_index, _path, _offset + len);
242     }
243
244     HLADataElementIndex& _index;
245     const std::string& _path;
246     std::string::size_type _offset;
247     bool _success;
248 };
249
250 bool
251 HLADataType::getDataElementIndex(HLADataElementIndex& index, const std::string& path, std::string::size_type offset) const
252 {
253     _DataElementIndexVisitor visitor(index, path, offset);
254     accept(visitor);
255     return visitor._success;
256 }
257
258 void
259 HLADataType::setAlignment(unsigned alignment)
260 {
261     /// FIXME: more checks
262     if (alignment == 0)
263         _alignment = 1;
264     else
265         _alignment = alignment;
266 }
267
268 void
269 HLADataType::_recomputeAlignmentImplementation()
270 {
271 }
272
273 }