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.
18 #include "HLADataTypeVisitor.hxx"
20 #include "HLAArrayDataElement.hxx"
21 #include "HLABasicDataElement.hxx"
22 #include "HLADataTypeVisitor.hxx"
23 #include "HLAEnumeratedDataElement.hxx"
24 #include "HLAFixedRecordDataElement.hxx"
25 #include "HLAVariantRecordDataElement.hxx"
29 HLADataElementFactoryVisitor::~HLADataElementFactoryVisitor()
34 HLADataElementFactoryVisitor::apply(const HLADataType& dataType)
36 SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Can not find a suitable data element for data type \""
37 << dataType.getName() << "\"");
41 HLADataElementFactoryVisitor::apply(const HLAInt8DataType& dataType)
43 _dataElement = new HLASCharDataElement(&dataType);
47 HLADataElementFactoryVisitor::apply(const HLAUInt8DataType& dataType)
49 _dataElement = new HLAUCharDataElement(&dataType);
53 HLADataElementFactoryVisitor::apply(const HLAInt16DataType& dataType)
55 _dataElement = new HLAShortDataElement(&dataType);
59 HLADataElementFactoryVisitor::apply(const HLAUInt16DataType& dataType)
61 _dataElement = new HLAUShortDataElement(&dataType);
65 HLADataElementFactoryVisitor::apply(const HLAInt32DataType& dataType)
67 _dataElement = new HLAIntDataElement(&dataType);
71 HLADataElementFactoryVisitor::apply(const HLAUInt32DataType& dataType)
73 _dataElement = new HLAUIntDataElement(&dataType);
77 HLADataElementFactoryVisitor::apply(const HLAInt64DataType& dataType)
79 _dataElement = new HLALongDataElement(&dataType);
83 HLADataElementFactoryVisitor::apply(const HLAUInt64DataType& dataType)
85 _dataElement = new HLAULongDataElement(&dataType);
89 HLADataElementFactoryVisitor::apply(const HLAFloat32DataType& dataType)
91 _dataElement = new HLAFloatDataElement(&dataType);
95 HLADataElementFactoryVisitor::apply(const HLAFloat64DataType& dataType)
97 _dataElement = new HLADoubleDataElement(&dataType);
100 class HLADataElementFactoryVisitor::ArrayDataElementFactory : public HLAArrayDataElement::DataElementFactory {
102 virtual HLADataElement* createElement(const HLAArrayDataElement& element, unsigned index)
104 const HLADataType* dataType = element.getElementDataType();
108 HLADataElementFactoryVisitor visitor;
109 dataType->accept(visitor);
110 return visitor.getDataElement();
115 HLADataElementFactoryVisitor::apply(const HLAFixedArrayDataType& dataType)
117 if (dataType.getIsString()) {
118 _dataElement = new HLAStringDataElement(&dataType);
120 SGSharedPtr<HLAArrayDataElement> arrayDataElement;
121 arrayDataElement = new HLAArrayDataElement(&dataType);
122 arrayDataElement->setDataElementFactory(new ArrayDataElementFactory);
123 arrayDataElement->setNumElements(dataType.getNumElements());
124 _dataElement = arrayDataElement;
129 HLADataElementFactoryVisitor::apply(const HLAVariableArrayDataType& dataType)
131 if (dataType.getIsString()) {
132 _dataElement = new HLAStringDataElement(&dataType);
134 SGSharedPtr<HLAArrayDataElement> arrayDataElement;
135 arrayDataElement = new HLAArrayDataElement(&dataType);
136 arrayDataElement->setDataElementFactory(new ArrayDataElementFactory);
137 _dataElement = arrayDataElement;
142 HLADataElementFactoryVisitor::apply(const HLAEnumeratedDataType& dataType)
144 _dataElement = new HLAEnumeratedDataElement(&dataType);
148 HLADataElementFactoryVisitor::apply(const HLAFixedRecordDataType& dataType)
150 SGSharedPtr<HLAFixedRecordDataElement> recordDataElement;
151 recordDataElement = new HLAFixedRecordDataElement(&dataType);
153 unsigned numFields = dataType.getNumFields();
154 for (unsigned i = 0; i < numFields; ++i) {
155 HLADataElementFactoryVisitor visitor;
156 dataType.getFieldDataType(i)->accept(visitor);
157 recordDataElement->setField(i, visitor._dataElement.get());
160 _dataElement = recordDataElement;
163 class HLADataElementFactoryVisitor::VariantRecordDataElementFactory : public HLAVariantRecordDataElement::DataElementFactory {
165 virtual HLADataElement* createElement(const HLAVariantRecordDataElement& element, unsigned index)
167 const HLAVariantRecordDataType* dataType = element.getDataType();
170 const HLADataType* alternativeDataType = element.getAlternativeDataType();
171 if (!alternativeDataType)
173 HLADataElementFactoryVisitor visitor;
174 alternativeDataType->accept(visitor);
175 return visitor.getDataElement();
180 HLADataElementFactoryVisitor::apply(const HLAVariantRecordDataType& dataType)
182 SGSharedPtr<HLAVariantRecordDataElement> variantRecordDataElement;
183 variantRecordDataElement = new HLAVariantRecordDataElement(&dataType);
184 variantRecordDataElement->setDataElementFactory(new VariantRecordDataElementFactory);
185 _dataElement = variantRecordDataElement;
188 } // namespace simgear