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 HLABasicDataType_hxx 19 #define HLABasicDataType_hxx 20 21 #include <string> 22 #include "HLADataType.hxx" 23 24 namespace simgear { 25 26 class HLABasicDataType : public HLADataType { 27 public: 28 virtual ~HLABasicDataType(); 29 virtual void accept(HLADataTypeVisitor& visitor) const; 30 31 virtual const HLABasicDataType* toBasicDataType() const; 32 33 protected: 34 HLABasicDataType(const std::string& name); 35 }; 36 37 class HLAInt8DataType : public HLABasicDataType { 38 public: 39 HLAInt8DataType(const std::string& name = "HLAInt8DataType"); 40 virtual ~HLAInt8DataType(); 41 42 virtual void accept(HLADataTypeVisitor& visitor) const; 43 skip(HLADecodeStream & stream) const44 bool skip(HLADecodeStream& stream) const 45 { 46 if (!stream.alignOffsetForSize(getAlignment())) 47 return false; 48 return stream.skip(1); 49 } skip(HLAEncodeStream & stream) const50 bool skip(HLAEncodeStream& stream) const 51 { 52 if (!stream.alignOffsetForSize(getAlignment())) 53 return false; 54 return stream.skip(1); 55 } decode(HLADecodeStream & stream,int8_t & value) const56 bool decode(HLADecodeStream& stream, int8_t& value) const 57 { 58 if (!stream.alignOffsetForSize(getAlignment())) 59 return false; 60 return stream.decodeInt8(value); 61 } encode(HLAEncodeStream & stream,const int8_t & value) const62 bool encode(HLAEncodeStream& stream, const int8_t& value) const 63 { 64 if (!stream.alignOffsetForSize(getAlignment())) 65 return false; 66 return stream.encodeInt8(value); 67 } 68 }; 69 70 class HLAUInt8DataType : public HLABasicDataType { 71 public: 72 HLAUInt8DataType(const std::string& name = "HLAUInt8DataType"); 73 virtual ~HLAUInt8DataType(); 74 75 virtual void accept(HLADataTypeVisitor& visitor) const; 76 skip(HLADecodeStream & stream) const77 bool skip(HLADecodeStream& stream) const 78 { 79 if (!stream.alignOffsetForSize(getAlignment())) 80 return false; 81 return stream.skip(1); 82 } skip(HLAEncodeStream & stream) const83 bool skip(HLAEncodeStream& stream) const 84 { 85 if (!stream.alignOffsetForSize(getAlignment())) 86 return false; 87 return stream.skip(1); 88 } decode(HLADecodeStream & stream,uint8_t & value) const89 bool decode(HLADecodeStream& stream, uint8_t& value) const 90 { 91 if (!stream.alignOffsetForSize(getAlignment())) 92 return false; 93 return stream.decodeUInt8(value); 94 } encode(HLAEncodeStream & stream,const uint8_t & value) const95 bool encode(HLAEncodeStream& stream, const uint8_t& value) const 96 { 97 if (!stream.alignOffsetForSize(getAlignment())) 98 return false; 99 return stream.encodeUInt8(value); 100 } 101 }; 102 103 #define BASIC_DATATYPE_IMPLEMENTATION(type, name) \ 104 class HLA##name##DataType : public HLABasicDataType { \ 105 public: \ 106 virtual ~HLA##name##DataType(); \ 107 \ 108 virtual void accept(HLADataTypeVisitor& visitor) const; \ 109 \ 110 bool skip(HLADecodeStream& stream) const \ 111 { \ 112 if (!stream.alignOffsetForSize(getAlignment())) \ 113 return false; \ 114 return stream.skip(sizeof(type)); \ 115 } \ 116 bool skip(HLAEncodeStream& stream) const \ 117 { \ 118 if (!stream.alignOffsetForSize(getAlignment())) \ 119 return false; \ 120 return stream.skip(sizeof(type)); \ 121 } \ 122 virtual bool decode(HLADecodeStream& stream, type& value) const = 0; \ 123 virtual bool encode(HLAEncodeStream& stream, const type& value) const = 0;\ 124 protected: \ 125 HLA##name##DataType(const std::string& name); \ 126 }; \ 127 class HLA##name##LEDataType : public HLA##name##DataType { \ 128 public: \ 129 HLA##name##LEDataType(const std::string& name = "HLA" #name "LEDataType");\ 130 virtual ~HLA##name##LEDataType(); \ 131 virtual bool decode(HLADecodeStream& stream, type& value) const; \ 132 virtual bool encode(HLAEncodeStream& stream, const type& value) const; \ 133 }; \ 134 class HLA##name##BEDataType : public HLA##name##DataType { \ 135 public: \ 136 HLA##name##BEDataType(const std::string& name = "HLA" #name "BEDataType");\ 137 virtual ~HLA##name##BEDataType(); \ 138 virtual bool decode(HLADecodeStream& stream, type& value) const; \ 139 virtual bool encode(HLAEncodeStream& stream, const type& value) const; \ 140 }; 141 142 BASIC_DATATYPE_IMPLEMENTATION(int16_t, Int16) 143 BASIC_DATATYPE_IMPLEMENTATION(uint16_t, UInt16) 144 BASIC_DATATYPE_IMPLEMENTATION(int32_t, Int32) 145 BASIC_DATATYPE_IMPLEMENTATION(uint32_t, UInt32) 146 BASIC_DATATYPE_IMPLEMENTATION(int64_t, Int64) 147 BASIC_DATATYPE_IMPLEMENTATION(uint64_t, UInt64) 148 BASIC_DATATYPE_IMPLEMENTATION(float, Float32) 149 BASIC_DATATYPE_IMPLEMENTATION(double, Float64) 150 151 #undef BASIC_DATATYPE_IMPLEMENTATION 152 153 } // namespace simgear 154 155 #endif 156