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 #ifndef HLAArrayDataElement_hxx
19 #define HLAArrayDataElement_hxx
23 #include <simgear/math/SGMath.hxx>
24 #include "HLAArrayDataType.hxx"
25 #include "HLADataElement.hxx"
26 #include "HLAVariantRecordDataElement.hxx"
27 #include "HLADataTypeVisitor.hxx"
31 class HLAAbstractArrayDataElement : public HLADataElement {
33 HLAAbstractArrayDataElement(const HLAArrayDataType* dataType);
34 virtual ~HLAAbstractArrayDataElement();
36 virtual void accept(HLADataElementVisitor& visitor);
37 virtual void accept(HLAConstDataElementVisitor& visitor) const;
39 virtual bool decode(HLADecodeStream& stream);
40 virtual bool encode(HLAEncodeStream& stream) const;
42 virtual const HLAArrayDataType* getDataType() const;
43 virtual bool setDataType(const HLADataType* dataType);
45 const HLADataType* getElementDataType() const;
47 virtual bool setNumElements(unsigned count) = 0;
48 virtual bool decodeElement(HLADecodeStream& stream, unsigned i) = 0;
50 virtual unsigned getNumElements() const = 0;
51 virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const = 0;
54 SGSharedPtr<const HLAArrayDataType> _dataType;
57 class HLAArrayDataElement : public HLAAbstractArrayDataElement {
59 HLAArrayDataElement(const HLAArrayDataType* dataType = 0);
60 virtual ~HLAArrayDataElement();
62 virtual bool setDataElement(HLADataElementIndex::const_iterator begin, HLADataElementIndex::const_iterator end, HLADataElement* dataElement);
63 virtual HLADataElement* getDataElement(HLADataElementIndex::const_iterator begin, HLADataElementIndex::const_iterator end);
64 virtual const HLADataElement* getDataElement(HLADataElementIndex::const_iterator begin, HLADataElementIndex::const_iterator end) const;
66 virtual bool setDataType(const HLADataType* dataType);
68 virtual bool setNumElements(unsigned size);
69 virtual bool decodeElement(HLADecodeStream& stream, unsigned i);
70 virtual unsigned getNumElements() const;
71 virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const;
73 const HLADataElement* getElement(unsigned index) const;
74 HLADataElement* getElement(unsigned index);
75 HLADataElement* getOrCreateElement(unsigned index);
76 void setElement(unsigned i, HLADataElement* value);
78 class DataElementFactory : public SGReferenced {
80 virtual ~DataElementFactory();
81 virtual HLADataElement* createElement(const HLAArrayDataElement&, unsigned) = 0;
84 void setDataElementFactory(DataElementFactory* dataElementFactory);
85 DataElementFactory* getDataElementFactory();
88 virtual void _setStamp(Stamp* stamp);
91 HLADataElement* newElement(unsigned index);
93 typedef std::vector<SGSharedPtr<HLADataElement> > ElementVector;
94 ElementVector _elementVector;
96 SGSharedPtr<DataElementFactory> _dataElementFactory;
99 // Holds an array of variants.
100 // Factors out common code for that use case.
101 class HLAVariantArrayDataElement : public HLAAbstractArrayDataElement {
103 HLAVariantArrayDataElement();
104 virtual ~HLAVariantArrayDataElement();
106 // Overwrite this from the abstract class, need some more checks here
107 virtual bool setDataType(const HLADataType* dataType);
109 virtual bool setNumElements(unsigned size);
110 virtual bool decodeElement(HLADecodeStream& stream, unsigned i);
111 virtual unsigned getNumElements() const;
112 virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const;
114 const HLAVariantRecordDataElement* getElement(unsigned index) const;
115 HLAVariantRecordDataElement* getElement(unsigned index);
116 HLAVariantRecordDataElement* getOrCreateElement(unsigned index);
117 void setElement(unsigned index, HLAVariantRecordDataElement* value);
119 typedef HLAVariantRecordDataElement::DataElementFactory AlternativeDataElementFactory;
121 void setAlternativeDataElementFactory(AlternativeDataElementFactory* alternativeDataElementFactory);
122 AlternativeDataElementFactory* getAlternativeDataElementFactory();
125 virtual void _setStamp(Stamp* stamp);
128 HLAVariantRecordDataElement* newElement();
130 typedef std::vector<SGSharedPtr<HLAVariantRecordDataElement> > ElementVector;
131 ElementVector _elementVector;
133 SGSharedPtr<AlternativeDataElementFactory> _alternativeDataElementFactory;
136 class HLAStringDataElement : public HLAAbstractArrayDataElement {
138 HLAStringDataElement(const HLAArrayDataType* dataType = 0) :
139 HLAAbstractArrayDataElement(dataType)
141 HLAStringDataElement(const HLAArrayDataType* dataType, const std::string& value) :
142 HLAAbstractArrayDataElement(dataType),
145 const std::string& getValue() const
147 void setValue(const std::string& value)
148 { _value = value; setDirty(true); }
150 virtual bool setNumElements(unsigned count)
152 _value.resize(count);
155 virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
157 HLATemplateDecodeVisitor<std::string::value_type> visitor(stream);
158 getElementDataType()->accept(visitor);
159 _value[i] = visitor.getValue();
163 virtual unsigned getNumElements() const
165 return _value.size();
167 virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
169 HLATemplateEncodeVisitor<std::string::value_type> visitor(stream, _value[i]);
170 getElementDataType()->accept(visitor);
178 class HLAStringData {
181 _value(new HLAStringDataElement(0))
183 HLAStringData(const std::string& value) :
184 _value(new HLAStringDataElement(0))
185 { _value->setValue(value); }
187 operator const std::string&() const
188 { return _value->getValue(); }
189 HLAStringData& operator=(const std::string& value)
190 { _value->setValue(value); return *this; }
192 const std::string& getValue() const
193 { return _value->getValue(); }
194 void setValue(const std::string& value)
195 { _value->setValue(value); }
197 const HLAStringDataElement* getDataElement() const
198 { return _value.get(); }
199 HLAStringDataElement* getDataElement()
200 { return _value.get(); }
202 const HLAArrayDataType* getDataType() const
203 { return _value->getDataType(); }
204 void setDataType(const HLAArrayDataType* dataType)
205 { _value->setDataType(dataType); }
208 SGSharedPtr<HLAStringDataElement> _value;
212 class HLAVec2DataElement : public HLAAbstractArrayDataElement {
214 HLAVec2DataElement(const HLAArrayDataType* dataType = 0) :
215 HLAAbstractArrayDataElement(dataType),
216 _value(SGVec2<T>::zeros())
218 HLAVec2DataElement(const HLAArrayDataType* dataType, const SGVec2<T>& value) :
219 HLAAbstractArrayDataElement(dataType),
222 const SGVec2<T>& getValue() const
224 void setValue(const SGVec2<T>& value)
225 { _value = value; setDirty(true); }
227 virtual bool setNumElements(unsigned count)
229 for (unsigned i = count; i < 2; ++i)
233 virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
236 HLATemplateDecodeVisitor<typename SGVec2<T>::value_type> visitor(stream);
237 getElementDataType()->accept(visitor);
238 _value[i] = visitor.getValue();
240 HLADataTypeDecodeVisitor visitor(stream);
241 getElementDataType()->accept(visitor);
246 virtual unsigned getNumElements() const
250 virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
253 HLATemplateEncodeVisitor<typename SGVec2<T>::value_type> visitor(stream, _value[i]);
254 getElementDataType()->accept(visitor);
256 HLADataTypeEncodeVisitor visitor(stream);
257 getElementDataType()->accept(visitor);
270 _value(new HLAVec2DataElement<T>(0))
272 HLAVec2Data(const SGVec2<T>& value) :
273 _value(new HLAVec2DataElement<T>(0, value))
276 operator const SGVec2<T>&() const
277 { return _value->getValue(); }
278 HLAVec2Data& operator=(const SGVec2<T>& value)
279 { _value->setValue(value); return *this; }
281 const SGVec2<T>& getValue() const
282 { return _value->getValue(); }
283 void setValue(const SGVec2<T>& value)
284 { _value->setValue(value); }
286 const HLAVec2DataElement<T>* getDataElement() const
287 { return _value.get(); }
288 HLAVec2DataElement<T>* getDataElement()
289 { return _value.get(); }
291 const HLAArrayDataType* getDataType() const
292 { return _value->getDataType(); }
293 void setDataType(const HLAArrayDataType* dataType)
294 { _value->setDataType(dataType); }
297 SGSharedPtr<HLAVec2DataElement<T> > _value;
300 typedef HLAVec2Data<float> HLAVec2fData;
301 typedef HLAVec2Data<double> HLAVec2dData;
304 class HLAVec3DataElement : public HLAAbstractArrayDataElement {
306 HLAVec3DataElement(const HLAArrayDataType* dataType = 0) :
307 HLAAbstractArrayDataElement(dataType),
308 _value(SGVec3<T>::zeros())
310 HLAVec3DataElement(const HLAArrayDataType* dataType, const SGVec3<T>& value) :
311 HLAAbstractArrayDataElement(dataType),
314 const SGVec3<T>& getValue() const
316 void setValue(const SGVec3<T>& value)
317 { _value = value; setDirty(true); }
319 virtual bool setNumElements(unsigned count)
321 for (unsigned i = count; i < 3; ++i)
325 virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
328 HLATemplateDecodeVisitor<typename SGVec3<T>::value_type> visitor(stream);
329 getElementDataType()->accept(visitor);
330 _value[i] = visitor.getValue();
332 HLADataTypeDecodeVisitor visitor(stream);
333 getElementDataType()->accept(visitor);
338 virtual unsigned getNumElements() const
342 virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
345 HLATemplateEncodeVisitor<typename SGVec3<T>::value_type> visitor(stream, _value[i]);
346 getElementDataType()->accept(visitor);
348 HLADataTypeEncodeVisitor visitor(stream);
349 getElementDataType()->accept(visitor);
362 _value(new HLAVec3DataElement<T>(0))
364 HLAVec3Data(const SGVec3<T>& value) :
365 _value(new HLAVec3DataElement<T>(0, value))
368 operator const SGVec3<T>&() const
369 { return _value->getValue(); }
370 HLAVec3Data& operator=(const SGVec3<T>& value)
371 { _value->setValue(value); return *this; }
373 const SGVec3<T>& getValue() const
374 { return _value->getValue(); }
375 void setValue(const SGVec3<T>& value)
376 { _value->setValue(value); }
378 const HLAVec3DataElement<T>* getDataElement() const
379 { return _value.get(); }
380 HLAVec3DataElement<T>* getDataElement()
381 { return _value.get(); }
383 const HLAArrayDataType* getDataType() const
384 { return _value->getDataType(); }
385 void setDataType(const HLAArrayDataType* dataType)
386 { _value->setDataType(dataType); }
389 SGSharedPtr<HLAVec3DataElement<T> > _value;
392 typedef HLAVec3Data<float> HLAVec3fData;
393 typedef HLAVec3Data<double> HLAVec3dData;
396 class HLAVec4DataElement : public HLAAbstractArrayDataElement {
398 HLAVec4DataElement(const HLAArrayDataType* dataType = 0) :
399 HLAAbstractArrayDataElement(dataType),
400 _value(SGVec4<T>::zeros())
402 HLAVec4DataElement(const HLAArrayDataType* dataType, const SGVec4<T>& value) :
403 HLAAbstractArrayDataElement(dataType),
406 const SGVec4<T>& getValue() const
408 void setValue(const SGVec4<T>& value)
409 { _value = value; setDirty(true); }
411 virtual bool setNumElements(unsigned count)
413 for (unsigned i = count; i < 4; ++i)
417 virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
420 HLATemplateDecodeVisitor<typename SGVec4<T>::value_type> visitor(stream);
421 getElementDataType()->accept(visitor);
422 _value[i] = visitor.getValue();
424 HLADataTypeDecodeVisitor visitor(stream);
425 getElementDataType()->accept(visitor);
430 virtual unsigned getNumElements() const
434 virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
437 HLATemplateEncodeVisitor<typename SGVec4<T>::value_type> visitor(stream, _value[i]);
438 getElementDataType()->accept(visitor);
440 HLADataTypeEncodeVisitor visitor(stream);
441 getElementDataType()->accept(visitor);
454 _value(new HLAVec4DataElement<T>(0))
456 HLAVec4Data(const SGVec4<T>& value) :
457 _value(new HLAVec4DataElement<T>(0, value))
460 operator const SGVec4<T>&() const
461 { return _value->getValue(); }
462 HLAVec4Data& operator=(const SGVec4<T>& value)
463 { _value->setValue(value); return *this; }
465 const SGVec4<T>& getValue() const
466 { return _value->getValue(); }
467 void setValue(const SGVec4<T>& value)
468 { _value->setValue(value); }
470 const HLAVec4DataElement<T>* getDataElement() const
471 { return _value.get(); }
472 HLAVec4DataElement<T>* getDataElement()
473 { return _value.get(); }
475 const HLAArrayDataType* getDataType() const
476 { return _value->getDataType(); }
477 void setDataType(const HLAArrayDataType* dataType)
478 { _value->setDataType(dataType); }
481 SGSharedPtr<HLAVec4DataElement<T> > _value;
484 typedef HLAVec4Data<float> HLAVec4fData;
485 typedef HLAVec4Data<double> HLAVec4dData;
488 class HLAQuatDataElement : public HLAAbstractArrayDataElement {
490 HLAQuatDataElement(const HLAArrayDataType* dataType = 0) :
491 HLAAbstractArrayDataElement(dataType),
492 _value(SGQuat<T>::zeros())
494 HLAQuatDataElement(const HLAArrayDataType* dataType, const SGQuat<T>& value) :
495 HLAAbstractArrayDataElement(dataType),
498 const SGQuat<T>& getValue() const
500 void setValue(const SGQuat<T>& value)
501 { _value = value; setDirty(true); }
503 virtual bool setNumElements(unsigned count)
505 for (unsigned i = count; i < 4; ++i)
509 virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
512 HLATemplateDecodeVisitor<typename SGQuat<T>::value_type> visitor(stream);
513 getElementDataType()->accept(visitor);
514 _value[i] = visitor.getValue();
516 HLADataTypeDecodeVisitor visitor(stream);
517 getElementDataType()->accept(visitor);
522 virtual unsigned getNumElements() const
526 virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
529 HLATemplateEncodeVisitor<typename SGQuat<T>::value_type> visitor(stream, _value[i]);
530 getElementDataType()->accept(visitor);
532 HLADataTypeEncodeVisitor visitor(stream);
533 getElementDataType()->accept(visitor);