4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #define HH_ParseInt_ 1
28 #include <boost/cstdint.hpp>
29 #include <boost/static_assert.hpp>
30 #include <boost/integer/integer_mask.hpp>
31 #include "PacketParser.hh"
33 //#include "ParseInt.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35 #include "ParseInt.ih"
40 : public detail::packet::ParseIntOps<Parse_Int8,boost::int8_t>,
41 public PacketParserBase
43 Parse_Int8(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
45 ///////////////////////////////////////////////////////////////////////////
47 typedef boost::int8_t value_type;
48 static size_type const fixed_bytes = 1;
50 value_type value() const { return i()[0]; }
51 void value(value_type v) { i()[0] = v; }
52 Parse_Int8 const & operator= (value_type other) { value(other); return *this; }
54 inline std::ostream & operator<<(std::ostream & os, Parse_Int8 const & i)
55 { os << i.value(); return os; }
58 : public detail::packet::ParseIntOps<Parse_UInt8,boost::uint8_t>,
59 public PacketParserBase
61 Parse_UInt8(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
63 ///////////////////////////////////////////////////////////////////////////
65 typedef boost::uint8_t value_type;
66 static size_type const fixed_bytes = 1;
68 value_type value() const { return i()[0]; }
69 void value(value_type v) { i()[0] = v; }
70 Parse_UInt8 const & operator= (value_type other) { value(other); return *this; }
72 inline std::ostream & operator<<(std::ostream & os, Parse_UInt8 const & i)
73 { os << i.value(); return os; }
76 : public detail::packet::ParseIntOps<Parse_Int16,boost::int16_t>,
77 public PacketParserBase
79 Parse_Int16(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
81 ///////////////////////////////////////////////////////////////////////////
83 typedef boost::int16_t value_type;
84 static size_type const fixed_bytes = 2;
86 value_type value() const { return detail::packet::parse_uint16(i()); }
87 void value(value_type v) { detail::packet::write_uint16(i(),v); }
88 Parse_Int16 const & operator= (value_type other) { value(other); return *this; }
90 inline std::ostream & operator<<(std::ostream & os, Parse_Int16 const & i)
91 { os << i.value(); return os; }
94 : public detail::packet::ParseIntOps<Parse_UInt16,boost::uint16_t>,
95 public PacketParserBase
97 Parse_UInt16(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
99 ///////////////////////////////////////////////////////////////////////////
101 typedef boost::uint16_t value_type;
102 static size_type const fixed_bytes = 2;
104 value_type value() const { return detail::packet::parse_uint16(i()); }
105 void value(value_type v) { detail::packet::write_uint16(i(),v); }
106 Parse_UInt16 const & operator= (value_type other) { value(other); return *this; }
108 inline std::ostream & operator<<(std::ostream & os, Parse_UInt16 const & i)
109 { os << i.value(); return os; }
112 : public detail::packet::ParseIntOps<Parse_Int24,boost::int32_t>,
113 public PacketParserBase
115 Parse_Int24(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
117 ///////////////////////////////////////////////////////////////////////////
119 typedef boost::int32_t value_type;
120 static size_type const fixed_bytes = 3;
122 value_type value() const {
123 value_type v (detail::packet::parse_uint24(i())); return v&0x800000 ? v|0xff000000 : v; }
124 void value(value_type v) { detail::packet::write_uint24(i(),v); }
125 Parse_Int24 const & operator= (value_type other) { value(other); return *this; }
127 inline std::ostream & operator<<(std::ostream & os, Parse_Int24 const & i)
128 { os << i.value(); return os; }
131 : public detail::packet::ParseIntOps<Parse_UInt24,boost::uint32_t>,
132 public PacketParserBase
134 Parse_UInt24(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
136 ///////////////////////////////////////////////////////////////////////////
138 typedef boost::uint32_t value_type;
139 static size_type const fixed_bytes = 3;
141 value_type value() const { return detail::packet::parse_uint24(i()); }
142 void value(value_type v) { detail::packet::write_uint24(i(),v); }
143 Parse_UInt24 const & operator= (value_type other) { value(other); return *this; }
145 inline std::ostream & operator<<(std::ostream & os, Parse_UInt24 const & i)
146 { os << i.value(); return os; }
149 : public detail::packet::ParseIntOps<Parse_Int32,boost::int32_t>,
150 public PacketParserBase
152 Parse_Int32(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
154 ///////////////////////////////////////////////////////////////////////////
156 typedef boost::int32_t value_type;
157 static size_type const fixed_bytes = 4;
159 value_type value() const { return detail::packet::parse_uint32(i()); }
160 void value(value_type v) { detail::packet::write_uint32(i(),v); }
161 Parse_Int32 const & operator= (value_type other) { value(other); return *this; }
163 inline std::ostream & operator<<(std::ostream & os, Parse_Int32 const & i)
164 { os << i.value(); return os; }
167 : public detail::packet::ParseIntOps<Parse_UInt32,boost::uint32_t>,
168 public PacketParserBase
170 Parse_UInt32(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
172 ///////////////////////////////////////////////////////////////////////////
174 typedef boost::uint32_t value_type;
175 static size_type const fixed_bytes = 4;
177 value_type value() const { return detail::packet::parse_uint32(i()); }
178 void value(value_type v) { detail::packet::write_uint32(i(),v); }
179 Parse_UInt32 const & operator= (value_type other) { value(other); return *this; }
181 inline std::ostream & operator<<(std::ostream & os, Parse_UInt32 const & i)
182 { os << i.value(); return os; }
184 template <unsigned Start, unsigned End>
185 struct Parse_IntField
186 : public detail::packet::ParseIntOps<Parse_IntField<Start,End>,boost::int32_t>,
187 public PacketParserBase
189 Parse_IntField(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
191 ///////////////////////////////////////////////////////////////////////////
193 typedef boost::int32_t value_type;
194 static size_type const fixed_bytes = (End-1)/8+1;
196 value_type value() const {
197 value_type v (detail::packet::parse_bitfield<Start,End>::parse(i()));
198 return v&boost::high_bit_mask_t<End-Start-1>::high_bit ?
199 v | ~boost::low_bits_mask_t<End-Start>::sig_bits : v;
201 void value(value_type v) { detail::packet::parse_bitfield<Start,End>::write(i(),v); }
202 Parse_IntField const & operator= (value_type other) { value(other); return *this; }
205 BOOST_STATIC_ASSERT( Start<End );
206 BOOST_STATIC_ASSERT( End-Start<=32 );
208 template <unsigned Start, unsigned End>
209 inline std::ostream & operator<<(std::ostream & os, Parse_IntField<Start,End> const & i)
210 { os << i.value(); return os; }
212 template <unsigned Start, unsigned End>
213 struct Parse_UIntField
214 : public detail::packet::ParseIntOps<Parse_UIntField<Start,End>,boost::uint32_t>,
215 public PacketParserBase
217 Parse_UIntField(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
219 ///////////////////////////////////////////////////////////////////////////
221 typedef boost::uint32_t value_type;
222 static size_type const fixed_bytes = (End-1)/8+1;
224 value_type value() const { return detail::packet::parse_bitfield<Start,End>::parse(i()); }
225 void value(value_type v) { detail::packet::parse_bitfield<Start,End>::write(i(),v); }
226 Parse_UIntField const & operator= (value_type other) { value(other); return *this; }
229 BOOST_STATIC_ASSERT( Start<End );
230 BOOST_STATIC_ASSERT( End-Start<=32 );
232 template <unsigned Start, unsigned End>
233 inline std::ostream & operator<<(std::ostream & os, Parse_UIntField<Start,End> const & i)
234 { os << i.value(); return os; }
236 template <unsigned bit>
238 : public detail::packet::ParseIntOps<Parse_Flag<bit>,bool>,
239 public PacketParserBase
241 Parse_Flag(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
243 ///////////////////////////////////////////////////////////////////////////
245 typedef bool value_type;
246 static size_type const fixed_bytes = bit/8+1;
248 value_type value() const { return i()[bit/8] & (1<<(7-(bit%8))); }
249 void value(value_type v) {
250 if (v) i()[0] |= 1<<(7-(bit%8));
251 else i()[0] &= ~(1<<(7-(bit%8)));
253 Parse_Flag const & operator= (value_type other) { value(other); return *this; }
255 template <unsigned bit>
256 inline std::ostream & operator<<(std::ostream & os, Parse_Flag<bit> const & i)
257 { os << i.value(); return os; }
261 ///////////////////////////////hh.e////////////////////////////////////////
263 #if !defined(SENF_PACKETS_DECL_ONLY) && !defined(HH_ParseInt_i_)
264 #define HH_ParseInt_i_
265 //#include "ParseInt.cci"
266 //#include "ParseInt.ct"
267 //#include "ParseInt.cti"
274 // c-file-style: "senf"
275 // indent-tabs-mode: nil
276 // ispell-local-dictionary: "american"
277 // compile-command: "scons -u test"
278 // comment-column: 40