]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLADataType.cxx
Some basic C++/Nasal binding helpers
[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, size_t 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             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
111                    << "Expected array subscript at the end of the path!");
112             return;
113         }
114         if (_path[_offset] != '[') {
115             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
116                    << "Expected array subscript at the end of the path!");
117             return;
118         }
119         ++_offset;
120         if (_path.size() <= _offset) {
121             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
122                    << "Expected closing array subscript at the end of the path!");
123             return;
124         }
125         unsigned index = 0;
126         bool closed = false;
127         while (_offset <= _path.size()) {
128             if (_path[_offset] == ']') {
129                 ++_offset;
130                 closed = true;
131                 break;
132             }
133             unsigned v = _path[_offset] - '0';
134             // Error, no number
135             if (10 <= v) {
136                 SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
137                        << "Array subscript is not a number!");
138                 return;
139             }
140             index *= 10;
141             index += v;
142             ++_offset;
143         }
144         if (!closed) {
145             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
146                    << "Expected closing array subscript at the end of the path!");
147             return;
148         }
149         if (!dataType.getElementDataType()) {
150             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
151                    << "Undefined array element data type!");
152             return;
153         }
154         _index.push_back(index);
155         _success = dataType.getElementDataType()->getDataElementIndex(_index, _path, _offset);
156     }
157
158     virtual void apply(const HLAFixedRecordDataType& dataType)
159     {
160         if (_path.size() <= _offset) {
161             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
162                    << "Expected field name at the end of the path!");
163             return;
164         }
165         if (_path[_offset] == '.')
166             ++_offset;
167         if (_path.size() <= _offset) {
168             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
169                    << "Expected field name at the end of the path!");
170             return;
171         }
172         size_t len = _path.find_first_of("[.", _offset) - _offset;
173         unsigned index = 0;
174         while (index < dataType.getNumFields()) {
175             if (_path.compare(_offset, len, dataType.getFieldName(index)) == 0)
176                 break;
177             ++index;
178         }
179         if (dataType.getNumFields() <= index) {
180             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
181                    << "Field \"" << _path.substr(_offset, len) << "\" not found in fixed record data type \""
182                    << dataType.getName() << "\"!");
183             return;
184         }
185         if (!dataType.getFieldDataType(index)) {
186             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
187                    << "Undefined field data type in variant record data type \""
188                    << dataType.getName() << "\" at field \"" << dataType.getFieldName(index) << "\"!");
189             return;
190         }
191         _index.push_back(index);
192         _success = dataType.getFieldDataType(index)->getDataElementIndex(_index, _path, _offset + len);
193     }
194
195     virtual void apply(const HLAVariantRecordDataType& dataType)
196     {
197         if (_path.size() <= _offset) {
198             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
199                    << "Expected alternative name at the end of the path!");
200             return;
201         }
202         if (_path[_offset] == '.')
203             ++_offset;
204         if (_path.size() <= _offset) {
205             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
206                    << "Expected alternative name at the end of the path!");
207             return;
208         }
209         size_t len = _path.find_first_of("[.", _offset) - _offset;
210         unsigned index = 0;
211         while (index < dataType.getNumAlternatives()) {
212             if (_path.compare(_offset, len, dataType.getAlternativeName(index)) == 0)
213                 break;
214             ++index;
215         }
216         if (dataType.getNumAlternatives() <= index) {
217             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
218                    << "Alternative \"" << _path.substr(_offset, len) << "\" not found in variant record data type \""
219                    << dataType.getName() << "\"!");
220             return;
221         }
222         if (!dataType.getAlternativeDataType(index)) {
223             SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
224                    << "Undefined alternative data type in variant record data type \""
225                    << dataType.getName() << "\" at alternative \"" << dataType.getAlternativeName(index) << "\"!");
226             return;
227         }
228         _index.push_back(index);
229         _success = dataType.getAlternativeDataType(index)->getDataElementIndex(_index, _path, _offset + len);
230     }
231
232     HLADataElementIndex& _index;
233     const std::string& _path;
234     size_t _offset;
235     bool _success;
236 };
237
238 bool
239 HLADataType::getDataElementIndex(HLADataElementIndex& index, const std::string& path, size_t offset) const
240 {
241     _DataElementIndexVisitor visitor(index, path, offset);
242     accept(visitor);
243     return visitor._success;
244 }
245
246 void
247 HLADataType::setAlignment(unsigned alignment)
248 {
249     /// FIXME: more checks
250     if (alignment == 0)
251         _alignment = 1;
252     else
253         _alignment = alignment;
254 }
255
256 void
257 HLADataType::_recomputeAlignmentImplementation()
258 {
259 }
260
261 }