]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLABasicDataElement.hxx
hla: Fix buffer overrun in SGMath vector types.
[simgear.git] / simgear / hla / HLABasicDataElement.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 HLABasicDataElement_hxx
19 #define HLABasicDataElement_hxx
20
21 #include "HLABasicDataType.hxx"
22 #include "HLADataElement.hxx"
23
24 namespace simgear {
25
26 class HLABasicDataElement : public HLADataElement {
27 public:
28     HLABasicDataElement(const HLABasicDataType* dataType);
29     virtual ~HLABasicDataElement();
30
31     virtual void accept(HLADataElementVisitor& visitor);
32     virtual void accept(HLAConstDataElementVisitor& visitor) const;
33
34     virtual bool encode(HLAEncodeStream& stream) const = 0;
35     virtual bool decode(HLADecodeStream& stream) = 0;
36
37     virtual const HLABasicDataType* getDataType() const;
38     virtual bool setDataType(const HLADataType* dataType);
39     void setDataType(const HLABasicDataType* dataType);
40
41 protected:
42     SGSharedPtr<const HLABasicDataType> _dataType;
43 };
44
45 #define TYPED_HLA_BASIC_DATA_ELEMENT(type, ctype)                             \
46 class HLAAbstract##type##DataElement : public HLABasicDataElement {           \
47 public:                                                                       \
48     HLAAbstract##type##DataElement(const HLABasicDataType* dataType = 0);     \
49     virtual ~HLAAbstract##type##DataElement();                                \
50                                                                               \
51     virtual bool encode(HLAEncodeStream& stream) const;                       \
52     virtual bool decode(HLADecodeStream& stream);                             \
53                                                                               \
54     virtual ctype getValue() const = 0;                                       \
55     virtual void setValue(ctype) = 0;                                         \
56 };                                                                            \
57 class HLA##type##DataElement : public HLAAbstract##type##DataElement {        \
58 public:                                                                       \
59     HLA##type##DataElement(const HLABasicDataType* dataType = 0);             \
60     HLA##type##DataElement(const HLABasicDataType* dataType,                  \
61                            const ctype& value);                               \
62     virtual ~HLA##type##DataElement();                                        \
63     virtual ctype getValue() const;                                           \
64     virtual void setValue(ctype value);                                       \
65 private:                                                                      \
66     ctype _value;                                                             \
67 };                                                                            \
68 class HLA##type##Data {                                                       \
69 public:                                                                       \
70     HLA##type##Data() :                                                       \
71         _value(new HLA##type##DataElement(0))                                 \
72     { }                                                                       \
73     HLA##type##Data(const ctype& value) :                                     \
74         _value(new HLA##type##DataElement(0, value))                          \
75     { }                                                                       \
76     operator ctype() const                                                    \
77     { return _value->getValue(); }                                            \
78     HLA##type##Data& operator=(const ctype& value)                            \
79     { _value->setValue(value); return *this; }                                \
80     ctype getValue() const                                                    \
81     { return _value->getValue(); }                                            \
82     void setValue(const ctype& value)                                         \
83     { _value->setValue(value); }                                              \
84     const HLA##type##DataElement* getDataElement() const                      \
85     { return _value.get(); }                                                  \
86     HLA##type##DataElement* getDataElement()                                  \
87     { return _value.get(); }                                                  \
88     const HLABasicDataType* getDataType() const                               \
89     { return _value->getDataType(); }                                         \
90     void setDataType(const HLABasicDataType* dataType)                        \
91     { _value->setDataType(dataType); }                                        \
92                                                                               \
93 private:                                                                      \
94     SGSharedPtr<HLA##type##DataElement> _value;                               \
95 };
96
97
98 TYPED_HLA_BASIC_DATA_ELEMENT(Char, char);
99 TYPED_HLA_BASIC_DATA_ELEMENT(WChar, wchar_t);
100 TYPED_HLA_BASIC_DATA_ELEMENT(SChar, signed char);
101 TYPED_HLA_BASIC_DATA_ELEMENT(UChar, unsigned char);
102 TYPED_HLA_BASIC_DATA_ELEMENT(Short, short);
103 TYPED_HLA_BASIC_DATA_ELEMENT(UShort, unsigned short);
104 TYPED_HLA_BASIC_DATA_ELEMENT(Int, int);
105 TYPED_HLA_BASIC_DATA_ELEMENT(UInt, unsigned int);
106 TYPED_HLA_BASIC_DATA_ELEMENT(Long, long);
107 TYPED_HLA_BASIC_DATA_ELEMENT(ULong, unsigned long);
108 TYPED_HLA_BASIC_DATA_ELEMENT(Float, float);
109 TYPED_HLA_BASIC_DATA_ELEMENT(Double, double);
110
111 #undef TYPED_HLA_BASIC_DATA_ELEMENT
112
113 }
114
115 #endif