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 "ParserBase.hh"
29 #include <boost/cstdint.hpp>
30 #include <boost/static_assert.hpp>
31 #include <boost/integer/integer_mask.hpp>
33 //#include "ParseInt.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35 #include "ParseInt.ih"
40 template <class Iterator=nil, class IPacket=nil>
42 : public impl::ParseIntOps<Parse_Int8<Iterator,IPacket>,boost::int8_t>,
43 public ParserBase<Iterator,IPacket>
45 template <class I=nil, class P=nil>
46 struct rebind { typedef Parse_Int8<I,P> parser; };
47 typedef Iterator byte_iterator;
49 static unsigned bytes() { return 1; }
52 explicit Parse_Int8(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
54 ///////////////////////////////////////////////////////////////////////////
56 typedef boost::int8_t value_type;
58 value_type value() const { return this->i()[0]; }
59 void value(value_type v) { this->i()[0] = v; }
60 Parse_Int8 const & operator= (value_type other) { value(other); return *this; }
62 template <class Iterator, class IPacket>
63 std::ostream & operator<<(std::ostream & os, Parse_Int8<Iterator,IPacket> const & i)
64 { os << i.value(); return os; }
66 template <class Iterator=nil, class IPacket=nil>
68 : public impl::ParseIntOps<Parse_UInt8<Iterator,IPacket>,boost::uint8_t>,
69 public ParserBase<Iterator,IPacket>
71 template <class I=nil, class P=nil>
72 struct rebind { typedef Parse_UInt8<I,P> parser; };
73 typedef Iterator byte_iterator;
75 static unsigned bytes() { return 1; }
78 explicit Parse_UInt8(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
80 ///////////////////////////////////////////////////////////////////////////
82 typedef boost::uint8_t value_type;
84 value_type value() const { return this->i()[0]; }
85 void value(value_type v) { this->i()[0] = v; }
86 Parse_UInt8 const & operator= (value_type other) { value(other); return *this; }
88 template <class Iterator, class IPacket>
89 std::ostream & operator<<(std::ostream & os, Parse_UInt8<Iterator,IPacket> const & i)
90 { os << i.value(); return os; }
92 template <class Iterator=nil, class IPacket=nil>
94 : public impl::ParseIntOps<Parse_Int16<Iterator,IPacket>,boost::int16_t>,
95 public ParserBase<Iterator,IPacket>
97 template <class I=nil, class P=nil>
98 struct rebind { typedef Parse_Int16<I,P> parser; };
99 typedef Iterator byte_iterator;
101 static unsigned bytes() { return 2; }
104 explicit Parse_Int16(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
106 ///////////////////////////////////////////////////////////////////////////
108 typedef boost::int16_t value_type;
110 value_type value() const { return impl::parse_uint16(this->i()); }
111 void value(value_type v) { impl::write_uint16(this->i(),v); }
112 Parse_Int16 const & operator= (value_type other) { value(other); return *this; }
114 template <class Iterator, class IPacket>
115 std::ostream & operator<<(std::ostream & os, Parse_Int16<Iterator,IPacket> const & i)
116 { os << i.value(); return os; }
118 template <class Iterator=nil, class IPacket=nil>
120 : public impl::ParseIntOps<Parse_UInt16<Iterator,IPacket>,boost::uint16_t>,
121 public ParserBase<Iterator,IPacket>
123 template <class I=nil, class P=nil>
124 struct rebind { typedef Parse_UInt16<I,P> parser; };
125 typedef Iterator byte_iterator;
127 static unsigned bytes() { return 2; }
130 explicit Parse_UInt16(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
132 ///////////////////////////////////////////////////////////////////////////
134 typedef boost::uint16_t value_type;
136 value_type value() const { return impl::parse_uint16(this->i()); }
137 void value(value_type v) { impl::write_uint16(this->i(),v); }
138 Parse_UInt16 const & operator= (value_type other) { value(other); return *this; }
140 template <class Iterator, class IPacket>
141 std::ostream & operator<<(std::ostream & os, Parse_UInt16<Iterator,IPacket> const & i)
142 { os << i.value(); return os; }
144 template <class Iterator=nil, class IPacket=nil>
146 : public impl::ParseIntOps<Parse_Int24<Iterator,IPacket>,boost::int32_t>,
147 public ParserBase<Iterator,IPacket>
149 template <class I=nil, class P=nil>
150 struct rebind { typedef Parse_Int24<I,P> parser; };
151 typedef Iterator byte_iterator;
153 static unsigned bytes() { return 3; }
156 explicit Parse_Int24(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
158 ///////////////////////////////////////////////////////////////////////////
160 typedef boost::int32_t value_type;
162 value_type value() const {
163 value_type v (impl::parse_uint24(this->i())); return v&0x800000 ? v|0xff000000 : v; }
164 void value(value_type v) { impl::write_uint24(this->i(),v); }
165 Parse_Int24 const & operator= (value_type other) { value(other); return *this; }
167 template <class Iterator, class IPacket>
168 std::ostream & operator<<(std::ostream & os, Parse_Int24<Iterator,IPacket> const & i)
169 { os << i.value(); return os; }
171 template <class Iterator=nil, class IPacket=nil>
173 : public impl::ParseIntOps<Parse_UInt24<Iterator,IPacket>,boost::uint32_t>,
174 public ParserBase<Iterator,IPacket>
176 template <class I=nil, class P=nil>
177 struct rebind { typedef Parse_UInt24<I,P> parser; };
178 typedef Iterator byte_iterator;
180 static unsigned bytes() { return 3; }
183 explicit Parse_UInt24(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
185 ///////////////////////////////////////////////////////////////////////////
187 typedef boost::uint32_t value_type;
189 value_type value() const { return impl::parse_uint24(this->i()); }
190 void value(value_type v) { impl::write_uint24(this->i(),v); }
191 Parse_UInt24 const & operator= (value_type other) { value(other); return *this; }
193 template <class Iterator, class IPacket>
194 std::ostream & operator<<(std::ostream & os, Parse_UInt24<Iterator,IPacket> const & i)
195 { os << i.value(); return os; }
197 template <class Iterator=nil, class IPacket=nil>
199 : public impl::ParseIntOps<Parse_Int32<Iterator,IPacket>,boost::int32_t>,
200 public ParserBase<Iterator,IPacket>
202 template <class I=nil, class P=nil>
203 struct rebind { typedef Parse_Int32<I,P> parser; };
204 typedef Iterator byte_iterator;
206 static unsigned bytes() { return 4; }
209 explicit Parse_Int32(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
211 ///////////////////////////////////////////////////////////////////////////
213 typedef boost::int32_t value_type;
215 value_type value() const { return impl::parse_uint32(this->i()); }
216 void value(value_type v) { impl::write_uint32(this->i(),v); }
217 Parse_Int32 const & operator= (value_type other) { value(other); return *this; }
219 template <class Iterator, class IPacket>
220 std::ostream & operator<<(std::ostream & os, Parse_Int32<Iterator,IPacket> const & i)
221 { os << i.value(); return os; }
223 template <class Iterator=nil, class IPacket=nil>
225 : public impl::ParseIntOps<Parse_UInt32<Iterator,IPacket>,boost::uint32_t>,
226 public ParserBase<Iterator,IPacket>
228 template <class I=nil, class P=nil>
229 struct rebind { typedef Parse_UInt32<I,P> parser; };
230 typedef Iterator byte_iterator;
232 static unsigned bytes() { return 4; }
235 explicit Parse_UInt32(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
237 ///////////////////////////////////////////////////////////////////////////
239 typedef boost::uint32_t value_type;
241 value_type value() const { return impl::parse_uint32(this->i()); }
242 void value(value_type v) { impl::write_uint32(this->i(),v); }
243 Parse_UInt32 const & operator= (value_type other) { value(other); return *this; }
245 template <class Iterator, class IPacket>
246 std::ostream & operator<<(std::ostream & os, Parse_UInt32<Iterator,IPacket> const & i)
247 { os << i.value(); return os; }
249 template <unsigned start, unsigned end, class Iterator=nil, class IPacket=nil>
250 struct Parse_IntField
251 : public impl::ParseIntOps<Parse_IntField<start,end,Iterator,IPacket>,boost::int32_t>,
252 public ParserBase<Iterator,IPacket>
254 template <class I=nil, class P=nil>
255 struct rebind { typedef Parse_IntField<start,end,I,P> parser; };
256 typedef Iterator byte_iterator;
258 static unsigned bytes() { return (end-1)/8+1; }
261 explicit Parse_IntField(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
263 ///////////////////////////////////////////////////////////////////////////
265 typedef boost::int32_t value_type;
267 value_type value() const {
268 value_type v (impl::parse_bitfield<Iterator,start,end>::parse(this->i()));
269 return v&boost::high_bit_mask_t<end-start-1>::high_bit ?
270 v | ~boost::low_bits_mask_t<end-start>::sig_bits : v;
272 void value(value_type v) { impl::parse_bitfield<Iterator,start,end>::write(this->i(),v); }
273 Parse_IntField const & operator= (value_type other) { value(other); return *this; }
276 BOOST_STATIC_ASSERT( start<end );
277 BOOST_STATIC_ASSERT( end-start<=32 );
279 template <unsigned start, unsigned end, class Iterator, class IPacket>
280 std::ostream & operator<<(std::ostream & os, Parse_IntField<start,end,Iterator,IPacket> const & i)
281 { os << i.value(); return os; }
283 template <unsigned start, unsigned end, class Iterator=nil, class IPacket=nil>
284 struct Parse_UIntField
285 : public impl::ParseIntOps<Parse_UIntField<start,end,Iterator,IPacket>,boost::uint32_t>,
286 public ParserBase<Iterator,IPacket>
288 template <class I=nil, class P=nil>
289 struct rebind { typedef Parse_UIntField<start,end,I,P> parser; };
290 typedef Iterator byte_iterator;
292 static unsigned bytes() { return (end-1)/8+1; }
295 explicit Parse_UIntField(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
297 ///////////////////////////////////////////////////////////////////////////
299 typedef boost::uint32_t value_type;
301 value_type value() const { return impl::parse_bitfield<Iterator,start,end>::parse(this->i()); }
302 void value(value_type v) { impl::parse_bitfield<Iterator,start,end>::write(this->i(),v); }
303 Parse_UIntField const & operator= (value_type other) { value(other); return *this; }
306 BOOST_STATIC_ASSERT( start<end );
307 BOOST_STATIC_ASSERT( end-start<=32 );
309 template <unsigned start, unsigned end, class Iterator, class IPacket>
310 std::ostream & operator<<(std::ostream & os, Parse_UIntField<start,end,Iterator,IPacket> const & i)
311 { os << i.value(); return os; }
313 template <unsigned bit, class Iterator=nil, class IPacket=nil>
315 : public impl::ParseIntOps<Parse_Flag<bit,Iterator,IPacket>,bool>,
316 public ParserBase<Iterator,IPacket>
318 template <class I=nil, class P=nil>
319 struct rebind { typedef Parse_Flag<bit,I,P> parser; };
320 typedef Iterator byte_iterator;
322 static unsigned bytes() { return 1; }
325 explicit Parse_Flag(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
327 ///////////////////////////////////////////////////////////////////////////
329 typedef bool value_type;
331 value_type value() const { return this->i()[bit/8] & (1<<(7-(bit%8))); }
332 void value(value_type v) {
333 if (v) this->i()[0] |= 1<<(7-(bit%8));
334 else this->i()[0] &= ~(1<<(7-(bit%8)));
336 Parse_Flag const & operator= (value_type other) { value(other); return *this; }
338 template <unsigned bit, class Iterator, class IPacket>
339 std::ostream & operator<<(std::ostream & os, Parse_Flag<bit,Iterator,IPacket> const & i)
340 { os << i.value(); return os; }
344 ///////////////////////////////hh.e////////////////////////////////////////
345 //#include "ParseInt.cci"
346 //#include "ParseInt.ct"
347 //#include "ParseInt.cti"
353 // c-file-style: "senf"