]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAArrayDataElement.hxx
First working version of DOM like Canvas event handling
[simgear.git] / simgear / hla / HLAArrayDataElement.hxx
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 #ifndef HLAArrayDataElement_hxx
19 #define HLAArrayDataElement_hxx
20
21 #include <string>
22 #include <vector>
23 #include <simgear/math/SGMath.hxx>
24 #include "HLAArrayDataType.hxx"
25 #include "HLADataElement.hxx"
26 #include "HLAVariantRecordDataElement.hxx"
27 #include "HLADataTypeVisitor.hxx"
28
29 namespace simgear {
30
31 class HLAAbstractArrayDataElement : public HLADataElement {
32 public:
33     HLAAbstractArrayDataElement(const HLAArrayDataType* dataType);
34     virtual ~HLAAbstractArrayDataElement();
35
36     virtual void accept(HLADataElementVisitor& visitor);
37     virtual void accept(HLAConstDataElementVisitor& visitor) const;
38
39     virtual bool decode(HLADecodeStream& stream);
40     virtual bool encode(HLAEncodeStream& stream) const;
41
42     virtual const HLAArrayDataType* getDataType() const;
43     virtual bool setDataType(const HLADataType* dataType);
44
45     const HLADataType* getElementDataType() const;
46
47     virtual bool setNumElements(unsigned count) = 0;
48     virtual bool decodeElement(HLADecodeStream& stream, unsigned i) = 0;
49
50     virtual unsigned getNumElements() const = 0;
51     virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const = 0;
52
53 protected:
54     SGSharedPtr<const HLAArrayDataType> _dataType;
55 };
56
57 class HLAArrayDataElement : public HLAAbstractArrayDataElement {
58 public:
59     HLAArrayDataElement(const HLAArrayDataType* dataType = 0);
60     virtual ~HLAArrayDataElement();
61
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;
65
66     virtual bool setDataType(const HLADataType* dataType);
67
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;
72
73     const HLADataElement* getElement(unsigned index) const;
74     HLADataElement* getElement(unsigned index);
75     HLADataElement* getOrCreateElement(unsigned index);
76     void setElement(unsigned i, HLADataElement* value);
77
78     class DataElementFactory : public SGReferenced {
79     public:
80         virtual ~DataElementFactory();
81         virtual HLADataElement* createElement(const HLAArrayDataElement&, unsigned) = 0;
82     };
83
84     void setDataElementFactory(DataElementFactory* dataElementFactory);
85     DataElementFactory* getDataElementFactory();
86
87 protected:
88     virtual void _setStamp(Stamp* stamp);
89
90 private:
91     HLADataElement* newElement(unsigned index);
92
93     typedef std::vector<SGSharedPtr<HLADataElement> > ElementVector;
94     ElementVector _elementVector;
95
96     SGSharedPtr<DataElementFactory> _dataElementFactory;
97 };
98
99 // Holds an array of variants.
100 // Factors out common code for that use case.
101 class HLAVariantArrayDataElement : public HLAAbstractArrayDataElement {
102 public:
103     HLAVariantArrayDataElement();
104     virtual ~HLAVariantArrayDataElement();
105
106     // Overwrite this from the abstract class, need some more checks here
107     virtual bool setDataType(const HLADataType* dataType);
108
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;
113
114     const HLAVariantRecordDataElement* getElement(unsigned index) const;
115     HLAVariantRecordDataElement* getElement(unsigned index);
116     HLAVariantRecordDataElement* getOrCreateElement(unsigned index);
117     void setElement(unsigned index, HLAVariantRecordDataElement* value);
118
119     typedef HLAVariantRecordDataElement::DataElementFactory AlternativeDataElementFactory;
120
121     void setAlternativeDataElementFactory(AlternativeDataElementFactory* alternativeDataElementFactory);
122     AlternativeDataElementFactory* getAlternativeDataElementFactory();
123
124 protected:
125     virtual void _setStamp(Stamp* stamp);
126
127 private:
128     HLAVariantRecordDataElement* newElement();
129
130     typedef std::vector<SGSharedPtr<HLAVariantRecordDataElement> > ElementVector;
131     ElementVector _elementVector;
132
133     SGSharedPtr<AlternativeDataElementFactory> _alternativeDataElementFactory;
134 };
135
136 class HLAStringDataElement : public HLAAbstractArrayDataElement {
137 public:
138     HLAStringDataElement(const HLAArrayDataType* dataType = 0) :
139         HLAAbstractArrayDataElement(dataType)
140     {}
141     HLAStringDataElement(const HLAArrayDataType* dataType, const std::string& value) :
142         HLAAbstractArrayDataElement(dataType),
143         _value(value)
144     {}
145     const std::string& getValue() const
146     { return _value; }
147     void setValue(const std::string& value)
148     { _value = value; setDirty(true); }
149
150     virtual bool setNumElements(unsigned count)
151     {
152         _value.resize(count);
153         return true;
154     }
155     virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
156     {
157         HLATemplateDecodeVisitor<std::string::value_type> visitor(stream);
158         getElementDataType()->accept(visitor);
159         _value[i] = visitor.getValue();
160         return true;
161     }
162
163     virtual unsigned getNumElements() const
164     {
165         return _value.size();
166     }
167     virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
168     {
169         HLATemplateEncodeVisitor<std::string::value_type> visitor(stream, _value[i]);
170         getElementDataType()->accept(visitor);
171         return true;
172     }
173
174 private:
175     std::string _value;
176 };
177
178 class HLAStringData {
179 public:
180     HLAStringData() :
181         _value(new HLAStringDataElement(0))
182     { }
183     HLAStringData(const std::string& value) :
184         _value(new HLAStringDataElement(0))
185     { _value->setValue(value); }
186
187     operator const std::string&() const
188     { return _value->getValue(); }
189     HLAStringData& operator=(const std::string& value)
190     { _value->setValue(value); return *this; }
191
192     const std::string& getValue() const
193     { return _value->getValue(); }
194     void setValue(const std::string& value)
195     { _value->setValue(value); }
196
197     const HLAStringDataElement* getDataElement() const
198     { return _value.get(); }
199     HLAStringDataElement* getDataElement()
200     { return _value.get(); }
201
202     const HLAArrayDataType* getDataType() const
203     { return _value->getDataType(); }
204     void setDataType(const HLAArrayDataType* dataType)
205     { _value->setDataType(dataType); }
206
207 private:
208     SGSharedPtr<HLAStringDataElement> _value;
209 };
210
211 template<typename T>
212 class HLAVec2DataElement : public HLAAbstractArrayDataElement {
213 public:
214     HLAVec2DataElement(const HLAArrayDataType* dataType = 0) :
215         HLAAbstractArrayDataElement(dataType),
216         _value(SGVec2<T>::zeros())
217     {}
218     HLAVec2DataElement(const HLAArrayDataType* dataType, const SGVec2<T>& value) :
219         HLAAbstractArrayDataElement(dataType),
220         _value(value)
221     {}
222     const SGVec2<T>& getValue() const
223     { return _value; }
224     void setValue(const SGVec2<T>& value)
225     { _value = value; setDirty(true); }
226
227     virtual bool setNumElements(unsigned count)
228     {
229         for (unsigned i = count; i < 2; ++i)
230             _value[i] = 0;
231         return true;
232     }
233     virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
234     {
235         if (i < 2) {
236             HLATemplateDecodeVisitor<typename SGVec2<T>::value_type> visitor(stream);
237             getElementDataType()->accept(visitor);
238             _value[i] = visitor.getValue();
239         } else {
240             HLADataTypeDecodeVisitor visitor(stream);
241             getElementDataType()->accept(visitor);
242         }
243         return true;
244     }
245
246     virtual unsigned getNumElements() const
247     {
248         return 2;
249     }
250     virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
251     {
252         if (i < 2) {
253             HLATemplateEncodeVisitor<typename SGVec2<T>::value_type> visitor(stream, _value[i]);
254             getElementDataType()->accept(visitor);
255         } else {
256             HLADataTypeEncodeVisitor visitor(stream);
257             getElementDataType()->accept(visitor);
258         }
259         return true;
260     }
261
262 private:
263     SGVec2<T> _value;
264 };
265
266 template<typename T>
267 class HLAVec2Data {
268 public:
269     HLAVec2Data() :
270         _value(new HLAVec2DataElement<T>(0))
271     { }
272     HLAVec2Data(const SGVec2<T>& value) :
273         _value(new HLAVec2DataElement<T>(0, value))
274     { }
275
276     operator const SGVec2<T>&() const
277     { return _value->getValue(); }
278     HLAVec2Data& operator=(const SGVec2<T>& value)
279     { _value->setValue(value); return *this; }
280
281     const SGVec2<T>& getValue() const
282     { return _value->getValue(); }
283     void setValue(const SGVec2<T>& value)
284     { _value->setValue(value); }
285
286     const HLAVec2DataElement<T>* getDataElement() const
287     { return _value.get(); }
288     HLAVec2DataElement<T>* getDataElement()
289     { return _value.get(); }
290
291     const HLAArrayDataType* getDataType() const
292     { return _value->getDataType(); }
293     void setDataType(const HLAArrayDataType* dataType)
294     { _value->setDataType(dataType); }
295
296 private:
297     SGSharedPtr<HLAVec2DataElement<T> > _value;
298 };
299
300 typedef HLAVec2Data<float> HLAVec2fData;
301 typedef HLAVec2Data<double> HLAVec2dData;
302
303 template<typename T>
304 class HLAVec3DataElement : public HLAAbstractArrayDataElement {
305 public:
306     HLAVec3DataElement(const HLAArrayDataType* dataType = 0) :
307         HLAAbstractArrayDataElement(dataType),
308         _value(SGVec3<T>::zeros())
309     {}
310     HLAVec3DataElement(const HLAArrayDataType* dataType, const SGVec3<T>& value) :
311         HLAAbstractArrayDataElement(dataType),
312         _value(value)
313     {}
314     const SGVec3<T>& getValue() const
315     { return _value; }
316     void setValue(const SGVec3<T>& value)
317     { _value = value; setDirty(true); }
318
319     virtual bool setNumElements(unsigned count)
320     {
321         for (unsigned i = count; i < 3; ++i)
322             _value[i] = 0;
323         return true;
324     }
325     virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
326     {
327         if (i < 3) {
328             HLATemplateDecodeVisitor<typename SGVec3<T>::value_type> visitor(stream);
329             getElementDataType()->accept(visitor);
330             _value[i] = visitor.getValue();
331         } else {
332             HLADataTypeDecodeVisitor visitor(stream);
333             getElementDataType()->accept(visitor);
334         }
335         return true;
336     }
337
338     virtual unsigned getNumElements() const
339     {
340         return 3;
341     }
342     virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
343     {
344         if (i < 3) {
345             HLATemplateEncodeVisitor<typename SGVec3<T>::value_type> visitor(stream, _value[i]);
346             getElementDataType()->accept(visitor);
347         } else {
348             HLADataTypeEncodeVisitor visitor(stream);
349             getElementDataType()->accept(visitor);
350         }
351         return true;
352     }
353
354 private:
355     SGVec3<T> _value;
356 };
357
358 template<typename T>
359 class HLAVec3Data {
360 public:
361     HLAVec3Data() :
362         _value(new HLAVec3DataElement<T>(0))
363     { }
364     HLAVec3Data(const SGVec3<T>& value) :
365         _value(new HLAVec3DataElement<T>(0, value))
366     { }
367
368     operator const SGVec3<T>&() const
369     { return _value->getValue(); }
370     HLAVec3Data& operator=(const SGVec3<T>& value)
371     { _value->setValue(value); return *this; }
372
373     const SGVec3<T>& getValue() const
374     { return _value->getValue(); }
375     void setValue(const SGVec3<T>& value)
376     { _value->setValue(value); }
377
378     const HLAVec3DataElement<T>* getDataElement() const
379     { return _value.get(); }
380     HLAVec3DataElement<T>* getDataElement()
381     { return _value.get(); }
382
383     const HLAArrayDataType* getDataType() const
384     { return _value->getDataType(); }
385     void setDataType(const HLAArrayDataType* dataType)
386     { _value->setDataType(dataType); }
387
388 private:
389     SGSharedPtr<HLAVec3DataElement<T> > _value;
390 };
391
392 typedef HLAVec3Data<float> HLAVec3fData;
393 typedef HLAVec3Data<double> HLAVec3dData;
394
395 template<typename T>
396 class HLAVec4DataElement : public HLAAbstractArrayDataElement {
397 public:
398     HLAVec4DataElement(const HLAArrayDataType* dataType = 0) :
399         HLAAbstractArrayDataElement(dataType),
400         _value(SGVec4<T>::zeros())
401     {}
402     HLAVec4DataElement(const HLAArrayDataType* dataType, const SGVec4<T>& value) :
403         HLAAbstractArrayDataElement(dataType),
404         _value(value)
405     {}
406     const SGVec4<T>& getValue() const
407     { return _value; }
408     void setValue(const SGVec4<T>& value)
409     { _value = value; setDirty(true); }
410
411     virtual bool setNumElements(unsigned count)
412     {
413         for (unsigned i = count; i < 4; ++i)
414             _value[i] = 0;
415         return true;
416     }
417     virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
418     {
419         if (i < 4) {
420             HLATemplateDecodeVisitor<typename SGVec4<T>::value_type> visitor(stream);
421             getElementDataType()->accept(visitor);
422             _value[i] = visitor.getValue();
423         } else {
424             HLADataTypeDecodeVisitor visitor(stream);
425             getElementDataType()->accept(visitor);
426         }
427         return true;
428     }
429
430     virtual unsigned getNumElements() const
431     {
432         return 4;
433     }
434     virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
435     {
436         if (i < 4) {
437             HLATemplateEncodeVisitor<typename SGVec4<T>::value_type> visitor(stream, _value[i]);
438             getElementDataType()->accept(visitor);
439         } else {
440             HLADataTypeEncodeVisitor visitor(stream);
441             getElementDataType()->accept(visitor);
442         }
443         return true;
444     }
445
446 private:
447     SGVec4<T> _value;
448 };
449
450 template<typename T>
451 class HLAVec4Data {
452 public:
453     HLAVec4Data() :
454         _value(new HLAVec4DataElement<T>(0))
455     { }
456     HLAVec4Data(const SGVec4<T>& value) :
457         _value(new HLAVec4DataElement<T>(0, value))
458     { }
459
460     operator const SGVec4<T>&() const
461     { return _value->getValue(); }
462     HLAVec4Data& operator=(const SGVec4<T>& value)
463     { _value->setValue(value); return *this; }
464
465     const SGVec4<T>& getValue() const
466     { return _value->getValue(); }
467     void setValue(const SGVec4<T>& value)
468     { _value->setValue(value); }
469
470     const HLAVec4DataElement<T>* getDataElement() const
471     { return _value.get(); }
472     HLAVec4DataElement<T>* getDataElement()
473     { return _value.get(); }
474
475     const HLAArrayDataType* getDataType() const
476     { return _value->getDataType(); }
477     void setDataType(const HLAArrayDataType* dataType)
478     { _value->setDataType(dataType); }
479
480 private:
481     SGSharedPtr<HLAVec4DataElement<T> > _value;
482 };
483
484 typedef HLAVec4Data<float> HLAVec4fData;
485 typedef HLAVec4Data<double> HLAVec4dData;
486
487 template<typename T>
488 class HLAQuatDataElement : public HLAAbstractArrayDataElement {
489 public:
490     HLAQuatDataElement(const HLAArrayDataType* dataType = 0) :
491         HLAAbstractArrayDataElement(dataType),
492         _value(SGQuat<T>::zeros())
493     {}
494     HLAQuatDataElement(const HLAArrayDataType* dataType, const SGQuat<T>& value) :
495         HLAAbstractArrayDataElement(dataType),
496         _value(value)
497     {}
498     const SGQuat<T>& getValue() const
499     { return _value; }
500     void setValue(const SGQuat<T>& value)
501     { _value = value; setDirty(true); }
502
503     virtual bool setNumElements(unsigned count)
504     {
505         for (unsigned i = count; i < 4; ++i)
506             _value[i] = 0;
507         return true;
508     }
509     virtual bool decodeElement(HLADecodeStream& stream, unsigned i)
510     {
511         if (i < 4) {
512             HLATemplateDecodeVisitor<typename SGQuat<T>::value_type> visitor(stream);
513             getElementDataType()->accept(visitor);
514             _value[i] = visitor.getValue();
515         } else {
516             HLADataTypeDecodeVisitor visitor(stream);
517             getElementDataType()->accept(visitor);
518         }
519         return true;
520     }
521
522     virtual unsigned getNumElements() const
523     {
524         return 4;
525     }
526     virtual bool encodeElement(HLAEncodeStream& stream, unsigned i) const
527     {
528         if (i < 4) {
529             HLATemplateEncodeVisitor<typename SGQuat<T>::value_type> visitor(stream, _value[i]);
530             getElementDataType()->accept(visitor);
531         } else {
532             HLADataTypeEncodeVisitor visitor(stream);
533             getElementDataType()->accept(visitor);
534         }
535         return true;
536     }
537
538 private:
539     SGQuat<T> _value;
540 };
541
542 }
543
544 #endif