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