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 "HLADataType.hxx"
26 #include "HLADataElement.hxx"
27 #include "HLADataTypeVisitor.hxx"
31 HLADataType::HLADataType(const std::string& name, unsigned alignment) :
35 setAlignment(alignment);
38 HLADataType::~HLADataType()
43 HLADataType::accept(HLADataTypeVisitor& visitor) const
48 const HLABasicDataType*
49 HLADataType::toBasicDataType() const
54 const HLAArrayDataType*
55 HLADataType::toArrayDataType() const
60 const HLAEnumeratedDataType*
61 HLADataType::toEnumeratedDataType() const
66 const HLAFixedRecordDataType*
67 HLADataType::toFixedRecordDataType() const
72 const HLAVariantRecordDataType*
73 HLADataType::toVariantRecordDataType() const
79 HLADataType::recomputeAlignment()
81 unsigned alignment = getAlignment();
82 _recomputeAlignmentImplementation();
83 return alignment != getAlignment();
87 HLADataType::releaseDataTypeReferences()
91 class HLADataType::_DataElementIndexVisitor : public HLADataTypeVisitor {
93 _DataElementIndexVisitor(HLADataElementIndex& index, const std::string& path, std::string::size_type offset) :
99 virtual ~_DataElementIndexVisitor()
102 virtual void apply(const HLADataType& dataType)
104 if (_path.size() == _offset)
107 virtual void apply(const HLAArrayDataType& dataType)
109 if (_path.size() == _offset) {
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!");
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!");
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!");
131 while (_offset <= _path.size()) {
132 if (_path[_offset] == ']') {
137 unsigned v = _path[_offset] - '0';
140 SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
141 << "Array subscript is not a number!");
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!");
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!");
158 _index.push_back(index);
159 _success = dataType.getElementDataType()->getDataElementIndex(_index, _path, _offset);
162 virtual void apply(const HLAFixedRecordDataType& dataType)
164 if (_path.size() == _offset) {
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!");
173 if (_path[_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!");
180 std::string::size_type len = std::min(_path.find_first_of("[.", _offset), _path.size()) - _offset;
182 while (index < dataType.getNumFields()) {
183 if (_path.compare(_offset, len, dataType.getFieldName(index)) == 0)
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() << "\"!");
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) << "\"!");
199 _index.push_back(index);
200 _success = dataType.getFieldDataType(index)->getDataElementIndex(_index, _path, _offset + len);
203 virtual void apply(const HLAVariantRecordDataType& dataType)
205 if (_path.size() == _offset) {
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!");
214 if (_path[_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!");
221 std::string::size_type len = std::min(_path.find_first_of("[.", _offset), _path.size()) - _offset;
223 while (index < dataType.getNumAlternatives()) {
224 if (_path.compare(_offset, len, dataType.getAlternativeName(index)) == 0)
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() << "\"!");
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) << "\"!");
240 _index.push_back(index);
241 _success = dataType.getAlternativeDataType(index)->getDataElementIndex(_index, _path, _offset + len);
244 HLADataElementIndex& _index;
245 const std::string& _path;
246 std::string::size_type _offset;
251 HLADataType::getDataElementIndex(HLADataElementIndex& index, const std::string& path, std::string::size_type offset) const
253 _DataElementIndexVisitor visitor(index, path, offset);
255 return visitor._success;
259 HLADataType::setAlignment(unsigned alignment)
261 /// FIXME: more checks
265 _alignment = alignment;
269 HLADataType::_recomputeAlignmentImplementation()