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, size_t 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) {
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!");
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!");
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!");
127 while (_offset <= _path.size()) {
128 if (_path[_offset] == ']') {
133 unsigned v = _path[_offset] - '0';
136 SG_LOG(SG_NETWORK, SG_ALERT, "HLADataType: faild to parse data element index \"" << _path << "\":\n"
137 << "Array subscript is not a number!");
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!");
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!");
154 _index.push_back(index);
155 _success = dataType.getElementDataType()->getDataElementIndex(_index, _path, _offset);
158 virtual void apply(const HLAFixedRecordDataType& dataType)
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!");
165 if (_path[_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!");
172 size_t len = _path.find_first_of("[.", _offset) - _offset;
174 while (index < dataType.getNumFields()) {
175 if (_path.compare(_offset, len, dataType.getFieldName(index)) == 0)
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() << "\"!");
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) << "\"!");
191 _index.push_back(index);
192 _success = dataType.getFieldDataType(index)->getDataElementIndex(_index, _path, _offset + len);
195 virtual void apply(const HLAVariantRecordDataType& dataType)
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!");
202 if (_path[_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!");
209 size_t len = _path.find_first_of("[.", _offset) - _offset;
211 while (index < dataType.getNumAlternatives()) {
212 if (_path.compare(_offset, len, dataType.getAlternativeName(index)) == 0)
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() << "\"!");
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) << "\"!");
228 _index.push_back(index);
229 _success = dataType.getAlternativeDataType(index)->getDataElementIndex(_index, _path, _offset + len);
232 HLADataElementIndex& _index;
233 const std::string& _path;
239 HLADataType::getDataElementIndex(HLADataElementIndex& index, const std::string& path, size_t offset) const
241 _DataElementIndexVisitor visitor(index, path, offset);
243 return visitor._success;
247 HLADataType::setAlignment(unsigned alignment)
249 /// FIXME: more checks
253 _alignment = alignment;
257 HLADataType::_recomputeAlignmentImplementation()