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
27 #include "ParserBase.hh"
28 #include <boost/cstdint.hpp>
29 #include <boost/static_assert.hpp>
30 #include <boost/integer/integer_mask.hpp>
32 //#include "ParseInt.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
34 #include "ParseInt.ih"
39 template <class Iterator=nil, class IPacket=nil>
41 : public impl::ParseIntOps<Parse_Int8<Iterator,IPacket>,boost::int8_t>,
42 public ParserBase<Iterator,IPacket>
44 template <class I=nil, class P=nil>
45 struct rebind { typedef Parse_Int8<I,P> parser; };
46 typedef Iterator byte_iterator;
48 static unsigned bytes() { return 1; }
51 explicit Parse_Int8(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
53 ///////////////////////////////////////////////////////////////////////////
55 typedef boost::int8_t value_type;
57 value_type value() const { return this->i()[0]; }
58 void value(value_type v) { this->i()[0] = v; }
59 Parse_Int8 const & operator= (value_type other) { value(other); return *this; }
62 template <class Iterator=nil, class IPacket=nil>
64 : public impl::ParseIntOps<Parse_UInt8<Iterator,IPacket>,boost::uint8_t>,
65 public ParserBase<Iterator,IPacket>
67 template <class I=nil, class P=nil>
68 struct rebind { typedef Parse_UInt8<I,P> parser; };
69 typedef Iterator byte_iterator;
71 static unsigned bytes() { return 1; }
74 explicit Parse_UInt8(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
76 ///////////////////////////////////////////////////////////////////////////
78 typedef boost::uint8_t value_type;
80 value_type value() const { return this->i()[0]; }
81 void value(value_type v) { this->i()[0] = v; }
82 Parse_UInt8 const & operator= (value_type other) { value(other); return *this; }
85 template <class Iterator=nil, class IPacket=nil>
87 : public impl::ParseIntOps<Parse_Int16<Iterator,IPacket>,boost::int16_t>,
88 public ParserBase<Iterator,IPacket>
90 template <class I=nil, class P=nil>
91 struct rebind { typedef Parse_Int16<I,P> parser; };
92 typedef Iterator byte_iterator;
94 static unsigned bytes() { return 2; }
97 explicit Parse_Int16(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
99 ///////////////////////////////////////////////////////////////////////////
101 typedef boost::int16_t value_type;
103 value_type value() const { return impl::parse_uint16(this->i()); }
104 void value(value_type v) { impl::write_uint16(this->i(),v); }
105 Parse_Int16 const & operator= (value_type other) { value(other); return *this; }
108 template <class Iterator=nil, class IPacket=nil>
110 : public impl::ParseIntOps<Parse_UInt16<Iterator,IPacket>,boost::uint16_t>,
111 public ParserBase<Iterator,IPacket>
113 template <class I=nil, class P=nil>
114 struct rebind { typedef Parse_UInt16<I,P> parser; };
115 typedef Iterator byte_iterator;
117 static unsigned bytes() { return 2; }
120 explicit Parse_UInt16(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
122 ///////////////////////////////////////////////////////////////////////////
124 typedef boost::uint16_t value_type;
126 value_type value() const { return impl::parse_uint16(this->i()); }
127 void value(value_type v) { impl::write_uint16(this->i(),v); }
128 Parse_UInt16 const & operator= (value_type other) { value(other); return *this; }
131 template <class Iterator=nil, class IPacket=nil>
133 : public impl::ParseIntOps<Parse_Int24<Iterator,IPacket>,boost::int32_t>,
134 public ParserBase<Iterator,IPacket>
136 template <class I=nil, class P=nil>
137 struct rebind { typedef Parse_Int24<I,P> parser; };
138 typedef Iterator byte_iterator;
140 static unsigned bytes() { return 3; }
143 explicit Parse_Int24(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
145 ///////////////////////////////////////////////////////////////////////////
147 typedef boost::int32_t value_type;
149 value_type value() const {
150 value_type v (impl::parse_uint24(this->i())); return v&0x800000 ? v|0xff000000 : v; }
151 void value(value_type v) { impl::write_uint24(this->i(),v); }
152 Parse_Int24 const & operator= (value_type other) { value(other); return *this; }
155 template <class Iterator=nil, class IPacket=nil>
157 : public impl::ParseIntOps<Parse_UInt24<Iterator,IPacket>,boost::uint32_t>,
158 public ParserBase<Iterator,IPacket>
160 template <class I=nil, class P=nil>
161 struct rebind { typedef Parse_UInt24<I,P> parser; };
162 typedef Iterator byte_iterator;
164 static unsigned bytes() { return 3; }
167 explicit Parse_UInt24(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
169 ///////////////////////////////////////////////////////////////////////////
171 typedef boost::uint32_t value_type;
173 value_type value() const { return impl::parse_uint24(this->i()); }
174 void value(value_type v) { impl::write_uint24(this->i(),v); }
175 Parse_UInt24 const & operator= (value_type other) { value(other); return *this; }
178 template <class Iterator=nil, class IPacket=nil>
180 : public impl::ParseIntOps<Parse_Int32<Iterator,IPacket>,boost::int32_t>,
181 public ParserBase<Iterator,IPacket>
183 template <class I=nil, class P=nil>
184 struct rebind { typedef Parse_Int32<I,P> parser; };
185 typedef Iterator byte_iterator;
187 static unsigned bytes() { return 4; }
190 explicit Parse_Int32(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
192 ///////////////////////////////////////////////////////////////////////////
194 typedef boost::int32_t value_type;
196 value_type value() const { return impl::parse_uint32(this->i()); }
197 void value(value_type v) { impl::write_uint32(this->i(),v); }
198 Parse_Int32 const & operator= (value_type other) { value(other); return *this; }
201 template <class Iterator=nil, class IPacket=nil>
203 : public impl::ParseIntOps<Parse_UInt32<Iterator,IPacket>,boost::uint32_t>,
204 public ParserBase<Iterator,IPacket>
206 template <class I=nil, class P=nil>
207 struct rebind { typedef Parse_UInt32<I,P> parser; };
208 typedef Iterator byte_iterator;
210 static unsigned bytes() { return 4; }
213 explicit Parse_UInt32(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
215 ///////////////////////////////////////////////////////////////////////////
217 typedef boost::uint32_t value_type;
219 value_type value() const { return impl::parse_uint32(this->i()); }
220 void value(value_type v) { impl::write_uint32(this->i(),v); }
221 Parse_UInt32 const & operator= (value_type other) { value(other); return *this; }
224 template <unsigned start, unsigned end, class Iterator=nil, class IPacket=nil>
225 struct Parse_IntField
226 : public impl::ParseIntOps<Parse_IntField<start,end,Iterator,IPacket>,boost::int32_t>,
227 public ParserBase<Iterator,IPacket>
229 template <class I=nil, class P=nil>
230 struct rebind { typedef Parse_IntField<start,end,I,P> parser; };
231 typedef Iterator byte_iterator;
233 static unsigned bytes() { return (end-1)/8+1; }
236 explicit Parse_IntField(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
238 ///////////////////////////////////////////////////////////////////////////
240 typedef boost::int32_t value_type;
242 value_type value() const {
243 value_type v (impl::parse_bitfield<Iterator,start,end>::parse(this->i()));
244 return v&boost::high_bit_mask_t<end-start-1>::high_bit ?
245 v | ~boost::low_bits_mask_t<end-start>::sig_bits : v;
247 void value(value_type v) { impl::parse_bitfield<Iterator,start,end>::write(this->i(),v); }
248 Parse_IntField const & operator= (value_type other) { value(other); return *this; }
251 BOOST_STATIC_ASSERT( start<end );
252 BOOST_STATIC_ASSERT( end-start<=32 );
255 template <unsigned start, unsigned end, class Iterator=nil, class IPacket=nil>
256 struct Parse_UIntField
257 : public impl::ParseIntOps<Parse_UIntField<start,end,Iterator,IPacket>,boost::uint32_t>,
258 public ParserBase<Iterator,IPacket>
260 template <class I=nil, class P=nil>
261 struct rebind { typedef Parse_UIntField<start,end,I,P> parser; };
262 typedef Iterator byte_iterator;
264 static unsigned bytes() { return (end-1)/8+1; }
267 explicit Parse_UIntField(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
269 ///////////////////////////////////////////////////////////////////////////
271 typedef boost::uint32_t value_type;
273 value_type value() const { return impl::parse_bitfield<Iterator,start,end>::parse(this->i()); }
274 void value(value_type v) { impl::parse_bitfield<Iterator,start,end>::write(this->i(),v); }
275 Parse_UIntField const & operator= (value_type other) { value(other); return *this; }
278 BOOST_STATIC_ASSERT( start<end );
279 BOOST_STATIC_ASSERT( end-start<=32 );
282 template <unsigned bit, class Iterator=nil, class IPacket=nil>
284 : public impl::ParseIntOps<Parse_Flag<bit,Iterator,IPacket>,bool>,
285 public ParserBase<Iterator,IPacket>
287 template <class I=nil, class P=nil>
288 struct rebind { typedef Parse_Flag<bit,I,P> parser; };
289 typedef Iterator byte_iterator;
291 static unsigned bytes() { return 1; }
294 explicit Parse_Flag(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
296 ///////////////////////////////////////////////////////////////////////////
298 typedef bool value_type;
300 value_type value() const { return this->i()[bit/8] & (1<<(7-(bit%8))); }
301 void value(value_type v) {
302 if (v) this->i()[0] |= 1<<(7-(bit%8));
303 else this->i()[0] &= ~(1<<(7-(bit%8)));
305 Parse_Flag const & operator= (value_type other) { value(other); return *this; }
310 ///////////////////////////////hh.e////////////////////////////////////////
311 //#include "ParseInt.cci"
312 //#include "ParseInt.ct"
313 //#include "ParseInt.cti"
319 // c-file-style: "senf"