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 "HLABasicDataType.hxx"
25 
26 #include "HLADataType.hxx"
27 #include "HLADataTypeVisitor.hxx"
28 
29 namespace simgear {
30 
HLABasicDataType(const std::string & name)31 HLABasicDataType::HLABasicDataType(const std::string& name) :
32     HLADataType(name)
33 {
34 }
35 
~HLABasicDataType()36 HLABasicDataType::~HLABasicDataType()
37 {
38 }
39 
40 void
accept(HLADataTypeVisitor & visitor) const41 HLABasicDataType::accept(HLADataTypeVisitor& visitor) const
42 {
43     visitor.apply(*this);
44 }
45 
46 const HLABasicDataType*
toBasicDataType() const47 HLABasicDataType::toBasicDataType() const
48 {
49     return this;
50 }
51 
52 //////////////////////////////////////////////////////////////////////////////
53 
HLAInt8DataType(const std::string & name)54 HLAInt8DataType::HLAInt8DataType(const std::string& name) :
55     HLABasicDataType(name)
56 {
57     setAlignment(sizeof(int8_t));
58 }
59 
~HLAInt8DataType()60 HLAInt8DataType::~HLAInt8DataType()
61 {
62 }
63 
64 void
accept(HLADataTypeVisitor & visitor) const65 HLAInt8DataType::accept(HLADataTypeVisitor& visitor) const
66 {
67     visitor.apply(*this);
68 }
69 
70 //////////////////////////////////////////////////////////////////////////////
71 
HLAUInt8DataType(const std::string & name)72 HLAUInt8DataType::HLAUInt8DataType(const std::string& name) :
73     HLABasicDataType(name)
74 {
75     setAlignment(sizeof(uint8_t));
76 }
77 
~HLAUInt8DataType()78 HLAUInt8DataType::~HLAUInt8DataType()
79 {
80 }
81 
82 void
accept(HLADataTypeVisitor & visitor) const83 HLAUInt8DataType::accept(HLADataTypeVisitor& visitor) const
84 {
85     visitor.apply(*this);
86 }
87 
88 //////////////////////////////////////////////////////////////////////////////
89 
90 #define BASIC_DATATYPE_IMPLEMENTATION(type, name)                       \
91 HLA##name##DataType::HLA##name##DataType(const std::string& name) :     \
92     HLABasicDataType(name)                                              \
93 {                                                                       \
94     setAlignment(sizeof(type));                                         \
95 }                                                                       \
96                                                                         \
97 HLA##name##DataType::~HLA##name##DataType()                             \
98 {                                                                       \
99 }                                                                       \
100                                                                         \
101 void                                                                    \
102 HLA##name##DataType::accept(HLADataTypeVisitor& visitor) const          \
103 {                                                                       \
104     visitor.apply(*this);                                               \
105 }                                                                       \
106                                                                         \
107                                                                         \
108                                                                         \
109 HLA##name##LEDataType::HLA##name##LEDataType(const std::string& name) : \
110     HLA##name##DataType(name)                                           \
111 {                                                                       \
112 }                                                                       \
113                                                                         \
114 HLA##name##LEDataType::~HLA##name##LEDataType()                         \
115 {                                                                       \
116 }                                                                       \
117                                                                         \
118 bool                                                                    \
119 HLA##name##LEDataType::decode(HLADecodeStream& stream,                  \
120                               type& value) const                        \
121 {                                                                       \
122     if (!stream.alignOffsetForSize(getAlignment()))                     \
123         return false;                                                   \
124     return stream.decode##name##LE(value);                              \
125 }                                                                       \
126                                                                         \
127 bool                                                                    \
128 HLA##name##LEDataType::encode(HLAEncodeStream& stream,                  \
129                               const type& value) const                  \
130 {                                                                       \
131     if (!stream.alignOffsetForSize(getAlignment()))                     \
132         return false;                                                   \
133     return stream.encode##name##LE(value);                              \
134 }                                                                       \
135                                                                         \
136                                                                         \
137                                                                         \
138 HLA##name##BEDataType::HLA##name##BEDataType(const std::string& name) : \
139     HLA##name##DataType(name)                                           \
140 {                                                                       \
141 }                                                                       \
142                                                                         \
143 HLA##name##BEDataType::~HLA##name##BEDataType()                         \
144 {                                                                       \
145 }                                                                       \
146                                                                         \
147 bool                                                                    \
148 HLA##name##BEDataType::decode(HLADecodeStream& stream,                  \
149                               type& value) const                        \
150 {                                                                       \
151     if (!stream.alignOffsetForSize(getAlignment()))                     \
152         return false;                                                   \
153     return stream.decode##name##BE(value);                              \
154 }                                                                       \
155                                                                         \
156 bool                                                                    \
157 HLA##name##BEDataType::encode(HLAEncodeStream& stream,                  \
158                               const type& value) const                  \
159 {                                                                       \
160     if (!stream.alignOffsetForSize(getAlignment()))                     \
161         return false;                                                   \
162     return stream.encode##name##BE(value);                              \
163 }
164 
165 BASIC_DATATYPE_IMPLEMENTATION(int16_t, Int16)
166 BASIC_DATATYPE_IMPLEMENTATION(uint16_t, UInt16)
167 BASIC_DATATYPE_IMPLEMENTATION(int32_t, Int32)
168 BASIC_DATATYPE_IMPLEMENTATION(uint32_t, UInt32)
169 BASIC_DATATYPE_IMPLEMENTATION(int64_t, Int64)
170 BASIC_DATATYPE_IMPLEMENTATION(uint64_t, UInt64)
171 BASIC_DATATYPE_IMPLEMENTATION(float, Float32)
172 BASIC_DATATYPE_IMPLEMENTATION(double, Float64)
173 
174 #undef BASIC_DATATYPE_IMPLEMENTATION
175 
176 } // namespace simgear
177