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 "HLADataTypeVisitor.hxx"
26 #include "HLAArrayDataElement.hxx"
27 #include "HLABasicDataElement.hxx"
28 #include "HLADataTypeVisitor.hxx"
29 #include "HLAEnumeratedDataElement.hxx"
30 #include "HLAFixedRecordDataElement.hxx"
31 #include "HLAVariantRecordDataElement.hxx"
35 HLADataElementFactoryVisitor::~HLADataElementFactoryVisitor()
40 HLADataElementFactoryVisitor::apply(const HLADataType& dataType)
42 SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Can not find a suitable data element for data type \""
43 << dataType.getName() << "\"");
47 HLADataElementFactoryVisitor::apply(const HLAInt8DataType& dataType)
49 _dataElement = new HLASCharDataElement(&dataType);
53 HLADataElementFactoryVisitor::apply(const HLAUInt8DataType& dataType)
55 _dataElement = new HLAUCharDataElement(&dataType);
59 HLADataElementFactoryVisitor::apply(const HLAInt16DataType& dataType)
61 _dataElement = new HLAShortDataElement(&dataType);
65 HLADataElementFactoryVisitor::apply(const HLAUInt16DataType& dataType)
67 _dataElement = new HLAUShortDataElement(&dataType);
71 HLADataElementFactoryVisitor::apply(const HLAInt32DataType& dataType)
73 _dataElement = new HLAIntDataElement(&dataType);
77 HLADataElementFactoryVisitor::apply(const HLAUInt32DataType& dataType)
79 _dataElement = new HLAUIntDataElement(&dataType);
83 HLADataElementFactoryVisitor::apply(const HLAInt64DataType& dataType)
85 _dataElement = new HLALongDataElement(&dataType);
89 HLADataElementFactoryVisitor::apply(const HLAUInt64DataType& dataType)
91 _dataElement = new HLAULongDataElement(&dataType);
95 HLADataElementFactoryVisitor::apply(const HLAFloat32DataType& dataType)
97 _dataElement = new HLAFloatDataElement(&dataType);
101 HLADataElementFactoryVisitor::apply(const HLAFloat64DataType& dataType)
103 _dataElement = new HLADoubleDataElement(&dataType);
106 class HLADataElementFactoryVisitor::ArrayDataElementFactory : public HLAArrayDataElement::DataElementFactory {
108 virtual HLADataElement* createElement(const HLAArrayDataElement& element, unsigned index)
110 const HLADataType* dataType = element.getElementDataType();
114 HLADataElementFactoryVisitor visitor;
115 dataType->accept(visitor);
116 return visitor.getDataElement();
121 HLADataElementFactoryVisitor::apply(const HLAFixedArrayDataType& dataType)
123 if (dataType.getIsString()) {
124 _dataElement = new HLAStringDataElement(&dataType);
126 SGSharedPtr<HLAArrayDataElement> arrayDataElement;
127 arrayDataElement = new HLAArrayDataElement(&dataType);
128 arrayDataElement->setDataElementFactory(new ArrayDataElementFactory);
129 arrayDataElement->setNumElements(dataType.getNumElements());
130 _dataElement = arrayDataElement;
135 HLADataElementFactoryVisitor::apply(const HLAVariableArrayDataType& dataType)
137 if (dataType.getIsString()) {
138 _dataElement = new HLAStringDataElement(&dataType);
140 SGSharedPtr<HLAArrayDataElement> arrayDataElement;
141 arrayDataElement = new HLAArrayDataElement(&dataType);
142 arrayDataElement->setDataElementFactory(new ArrayDataElementFactory);
143 _dataElement = arrayDataElement;
148 HLADataElementFactoryVisitor::apply(const HLAEnumeratedDataType& dataType)
150 _dataElement = new HLAEnumeratedDataElement(&dataType);
154 HLADataElementFactoryVisitor::apply(const HLAFixedRecordDataType& dataType)
156 SGSharedPtr<HLAFixedRecordDataElement> recordDataElement;
157 recordDataElement = new HLAFixedRecordDataElement(&dataType);
159 unsigned numFields = dataType.getNumFields();
160 for (unsigned i = 0; i < numFields; ++i) {
161 HLADataElementFactoryVisitor visitor;
162 dataType.getFieldDataType(i)->accept(visitor);
163 recordDataElement->setField(i, visitor._dataElement.get());
166 _dataElement = recordDataElement;
169 class HLADataElementFactoryVisitor::VariantRecordDataElementFactory : public HLAVariantRecordDataElement::DataElementFactory {
171 virtual HLADataElement* createElement(const HLAVariantRecordDataElement& element, unsigned index)
173 const HLAVariantRecordDataType* dataType = element.getDataType();
176 const HLADataType* alternativeDataType = element.getAlternativeDataType();
177 if (!alternativeDataType)
179 HLADataElementFactoryVisitor visitor;
180 alternativeDataType->accept(visitor);
181 return visitor.getDataElement();
186 HLADataElementFactoryVisitor::apply(const HLAVariantRecordDataType& dataType)
188 SGSharedPtr<HLAVariantRecordDataElement> variantRecordDataElement;
189 variantRecordDataElement = new HLAVariantRecordDataElement(&dataType);
190 variantRecordDataElement->setDataElementFactory(new VariantRecordDataElementFactory);
191 _dataElement = variantRecordDataElement;
194 } // namespace simgear