]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLADataTypeVisitor.cxx
hla: Use HLADataElementIndices for HLAInteractionClass.
[simgear.git] / simgear / hla / HLADataTypeVisitor.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 "HLADataTypeVisitor.hxx"
25
26 #include "HLAArrayDataElement.hxx"
27 #include "HLABasicDataElement.hxx"
28 #include "HLADataTypeVisitor.hxx"
29 #include "HLAEnumeratedDataElement.hxx"
30 #include "HLAFixedRecordDataElement.hxx"
31 #include "HLAVariantRecordDataElement.hxx"
32
33 namespace simgear {
34
35 HLADataElementFactoryVisitor::~HLADataElementFactoryVisitor()
36 {
37 }
38
39 void
40 HLADataElementFactoryVisitor::apply(const HLADataType& dataType)
41 {
42     SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Can not find a suitable data element for data type \""
43            << dataType.getName() << "\"");
44 }
45
46 void
47 HLADataElementFactoryVisitor::apply(const HLAInt8DataType& dataType)
48 {
49     _dataElement = new HLASCharDataElement(&dataType);
50 }
51
52 void
53 HLADataElementFactoryVisitor::apply(const HLAUInt8DataType& dataType)
54 {
55     _dataElement = new HLAUCharDataElement(&dataType);
56 }
57
58 void
59 HLADataElementFactoryVisitor::apply(const HLAInt16DataType& dataType)
60 {
61     _dataElement = new HLAShortDataElement(&dataType);
62 }
63
64 void
65 HLADataElementFactoryVisitor::apply(const HLAUInt16DataType& dataType)
66 {
67     _dataElement = new HLAUShortDataElement(&dataType);
68 }
69
70 void
71 HLADataElementFactoryVisitor::apply(const HLAInt32DataType& dataType)
72 {
73     _dataElement = new HLAIntDataElement(&dataType);
74 }
75
76 void
77 HLADataElementFactoryVisitor::apply(const HLAUInt32DataType& dataType)
78 {
79     _dataElement = new HLAUIntDataElement(&dataType);
80 }
81
82 void
83 HLADataElementFactoryVisitor::apply(const HLAInt64DataType& dataType)
84 {
85     _dataElement = new HLALongDataElement(&dataType);
86 }
87
88 void
89 HLADataElementFactoryVisitor::apply(const HLAUInt64DataType& dataType)
90 {
91     _dataElement = new HLAULongDataElement(&dataType);
92 }
93
94 void
95 HLADataElementFactoryVisitor::apply(const HLAFloat32DataType& dataType)
96 {
97     _dataElement = new HLAFloatDataElement(&dataType);
98 }
99
100 void
101 HLADataElementFactoryVisitor::apply(const HLAFloat64DataType& dataType)
102 {
103     _dataElement = new HLADoubleDataElement(&dataType);
104 }
105
106 class HLADataElementFactoryVisitor::ArrayDataElementFactory : public HLAArrayDataElement::DataElementFactory {
107 public:
108     virtual HLADataElement* createElement(const HLAArrayDataElement& element, unsigned index)
109     {
110         const HLADataType* dataType = element.getElementDataType();
111         if (!dataType)
112             return 0;
113         
114         HLADataElementFactoryVisitor visitor;
115         dataType->accept(visitor);
116         return visitor.getDataElement();
117     }
118 };
119
120 void
121 HLADataElementFactoryVisitor::apply(const HLAFixedArrayDataType& dataType)
122 {
123     if (dataType.getIsString()) {
124         _dataElement = new HLAStringDataElement(&dataType);
125     } else {
126         SGSharedPtr<HLAArrayDataElement> arrayDataElement;
127         arrayDataElement = new HLAArrayDataElement(&dataType);
128         arrayDataElement->setDataElementFactory(new ArrayDataElementFactory);
129         arrayDataElement->setNumElements(dataType.getNumElements());
130         _dataElement = arrayDataElement;
131     }
132 }
133
134 void
135 HLADataElementFactoryVisitor::apply(const HLAVariableArrayDataType& dataType)
136 {
137     if (dataType.getIsString()) {
138         _dataElement = new HLAStringDataElement(&dataType);
139     } else {
140         SGSharedPtr<HLAArrayDataElement> arrayDataElement;
141         arrayDataElement = new HLAArrayDataElement(&dataType);
142         arrayDataElement->setDataElementFactory(new ArrayDataElementFactory);
143         _dataElement = arrayDataElement;
144     }
145 }
146
147 void
148 HLADataElementFactoryVisitor::apply(const HLAEnumeratedDataType& dataType)
149 {
150     _dataElement = new HLAEnumeratedDataElement(&dataType);
151 }
152
153 void
154 HLADataElementFactoryVisitor::apply(const HLAFixedRecordDataType& dataType)
155 {
156     SGSharedPtr<HLAFixedRecordDataElement> recordDataElement;
157     recordDataElement = new HLAFixedRecordDataElement(&dataType);
158
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());
164     }
165
166     _dataElement = recordDataElement;
167 }
168
169 class HLADataElementFactoryVisitor::VariantRecordDataElementFactory : public HLAVariantRecordDataElement::DataElementFactory {
170 public:
171     virtual HLADataElement* createElement(const HLAVariantRecordDataElement& element, unsigned index)
172     {
173         const HLAVariantRecordDataType* dataType = element.getDataType();
174         if (!dataType)
175             return 0;
176         const HLADataType* alternativeDataType = element.getAlternativeDataType();
177         if (!alternativeDataType)
178             return 0;
179         HLADataElementFactoryVisitor visitor;
180         alternativeDataType->accept(visitor);
181         return visitor.getDataElement();
182     }
183 };
184
185 void
186 HLADataElementFactoryVisitor::apply(const HLAVariantRecordDataType& dataType)
187 {
188     SGSharedPtr<HLAVariantRecordDataElement> variantRecordDataElement;
189     variantRecordDataElement = new HLAVariantRecordDataElement(&dataType);
190     variantRecordDataElement->setDataElementFactory(new VariantRecordDataElementFactory);
191     _dataElement = variantRecordDataElement;
192 }
193
194 } // namespace simgear