Fixed whitespace in all files (no tabs)
[senf.git] / Packets / ParseInt.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
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.
12 //
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.
17 //
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.
22
23 #ifndef HH_ParseInt_
24 #define HH_ParseInt_ 1
25
26 // Custom includes
27 #include <iostream>
28 #include "ParserBase.hh"
29 #include <boost/cstdint.hpp>
30 #include <boost/static_assert.hpp>
31 #include <boost/integer/integer_mask.hpp>
32
33 //#include "ParseInt.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35 #include "ParseInt.ih"
36
37 namespace senf {
38
39
40     template <class Iterator=nil, class IPacket=nil>
41     struct Parse_Int8
42         : public impl::ParseIntOps<Parse_Int8<Iterator,IPacket>,boost::int8_t>,
43           public ParserBase<Iterator,IPacket>
44     {
45         template <class I=nil, class P=nil>
46         struct rebind { typedef Parse_Int8<I,P> parser; };
47         typedef Iterator byte_iterator;
48
49         static unsigned bytes() { return 1; }
50
51         Parse_Int8() {}
52         explicit Parse_Int8(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
53
54         ///////////////////////////////////////////////////////////////////////////
55
56         typedef boost::int8_t value_type;
57
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; }
61     };
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; }
65
66     template <class Iterator=nil, class IPacket=nil>
67     struct Parse_UInt8
68         : public impl::ParseIntOps<Parse_UInt8<Iterator,IPacket>,boost::uint8_t>,
69           public ParserBase<Iterator,IPacket>
70     {
71         template <class I=nil, class P=nil>
72         struct rebind { typedef Parse_UInt8<I,P> parser; };
73         typedef Iterator byte_iterator;
74
75         static unsigned bytes() { return 1; }
76
77         Parse_UInt8() {}
78         explicit Parse_UInt8(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
79
80         ///////////////////////////////////////////////////////////////////////////
81
82         typedef boost::uint8_t value_type;
83
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; }
87     };
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; }
91
92     template <class Iterator=nil, class IPacket=nil>
93     struct Parse_Int16
94         : public impl::ParseIntOps<Parse_Int16<Iterator,IPacket>,boost::int16_t>,
95           public ParserBase<Iterator,IPacket>
96     {
97         template <class I=nil, class P=nil>
98         struct rebind { typedef Parse_Int16<I,P> parser; };
99         typedef Iterator byte_iterator;
100
101         static unsigned bytes() { return 2; }
102
103         Parse_Int16() {}
104         explicit Parse_Int16(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
105
106         ///////////////////////////////////////////////////////////////////////////
107
108         typedef boost::int16_t value_type;
109
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; }
113     };
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; }
117
118     template <class Iterator=nil, class IPacket=nil>
119     struct Parse_UInt16
120         : public impl::ParseIntOps<Parse_UInt16<Iterator,IPacket>,boost::uint16_t>,
121           public ParserBase<Iterator,IPacket>
122     {
123         template <class I=nil, class P=nil>
124         struct rebind { typedef Parse_UInt16<I,P> parser; };
125         typedef Iterator byte_iterator;
126
127         static unsigned bytes() { return 2; }
128
129         Parse_UInt16() {}
130         explicit Parse_UInt16(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
131
132         ///////////////////////////////////////////////////////////////////////////
133
134         typedef boost::uint16_t value_type;
135
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; }
139     };
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; }
143
144     template <class Iterator=nil, class IPacket=nil>
145     struct Parse_Int24
146         : public impl::ParseIntOps<Parse_Int24<Iterator,IPacket>,boost::int32_t>,
147           public ParserBase<Iterator,IPacket>
148     {
149         template <class I=nil, class P=nil>
150         struct rebind { typedef Parse_Int24<I,P> parser; };
151         typedef Iterator byte_iterator;
152
153         static unsigned bytes() { return 3; }
154
155         Parse_Int24() {}
156         explicit Parse_Int24(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
157
158         ///////////////////////////////////////////////////////////////////////////
159
160         typedef boost::int32_t value_type;
161
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; }
166     };
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; }
170
171     template <class Iterator=nil, class IPacket=nil>
172     struct Parse_UInt24
173         : public impl::ParseIntOps<Parse_UInt24<Iterator,IPacket>,boost::uint32_t>,
174           public ParserBase<Iterator,IPacket>
175     {
176         template <class I=nil, class P=nil>
177         struct rebind { typedef Parse_UInt24<I,P> parser; };
178         typedef Iterator byte_iterator;
179
180         static unsigned bytes() { return 3; }
181
182         Parse_UInt24() {}
183         explicit Parse_UInt24(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
184
185         ///////////////////////////////////////////////////////////////////////////
186
187         typedef boost::uint32_t value_type;
188
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; }
192     };
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; }
196
197     template <class Iterator=nil, class IPacket=nil>
198     struct Parse_Int32
199         : public impl::ParseIntOps<Parse_Int32<Iterator,IPacket>,boost::int32_t>,
200           public ParserBase<Iterator,IPacket>
201     {
202         template <class I=nil, class P=nil>
203         struct rebind { typedef Parse_Int32<I,P> parser; };
204         typedef Iterator byte_iterator;
205
206         static unsigned bytes() { return 4; }
207
208         Parse_Int32() {}
209         explicit Parse_Int32(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
210
211         ///////////////////////////////////////////////////////////////////////////
212
213         typedef boost::int32_t value_type;
214
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; }
218     };
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; }
222
223     template <class Iterator=nil, class IPacket=nil>
224     struct Parse_UInt32
225         : public impl::ParseIntOps<Parse_UInt32<Iterator,IPacket>,boost::uint32_t>,
226           public ParserBase<Iterator,IPacket>
227     {
228         template <class I=nil, class P=nil>
229         struct rebind { typedef Parse_UInt32<I,P> parser; };
230         typedef Iterator byte_iterator;
231
232         static unsigned bytes() { return 4; }
233
234         Parse_UInt32() {}
235         explicit Parse_UInt32(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
236
237         ///////////////////////////////////////////////////////////////////////////
238
239         typedef boost::uint32_t value_type;
240
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; }
244     };
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; }
248
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>
253     {
254         template <class I=nil, class P=nil>
255         struct rebind { typedef Parse_IntField<start,end,I,P> parser; };
256         typedef Iterator byte_iterator;
257
258         static unsigned bytes() { return (end-1)/8+1; }
259
260         Parse_IntField() {}
261         explicit Parse_IntField(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
262
263         ///////////////////////////////////////////////////////////////////////////
264
265         typedef boost::int32_t value_type;
266
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;
271         }
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; }
274
275     private:
276         BOOST_STATIC_ASSERT( start<end );
277         BOOST_STATIC_ASSERT( end-start<=32 );
278     };
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; }
282
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>
287     {
288         template <class I=nil, class P=nil>
289         struct rebind { typedef Parse_UIntField<start,end,I,P> parser; };
290         typedef Iterator byte_iterator;
291
292         static unsigned bytes() { return (end-1)/8+1; }
293
294         Parse_UIntField() {}
295         explicit Parse_UIntField(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
296
297         ///////////////////////////////////////////////////////////////////////////
298
299         typedef boost::uint32_t value_type;
300
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; }
304
305     private:
306         BOOST_STATIC_ASSERT( start<end );
307         BOOST_STATIC_ASSERT( end-start<=32 );
308     };
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; }
312
313     template <unsigned bit, class Iterator=nil, class IPacket=nil>
314     struct Parse_Flag
315         : public impl::ParseIntOps<Parse_Flag<bit,Iterator,IPacket>,bool>,
316           public ParserBase<Iterator,IPacket>
317     {
318         template <class I=nil, class P=nil>
319         struct rebind { typedef Parse_Flag<bit,I,P> parser; };
320         typedef Iterator byte_iterator;
321
322         static unsigned bytes() { return 1; }
323
324         Parse_Flag() {}
325         explicit Parse_Flag(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
326
327         ///////////////////////////////////////////////////////////////////////////
328
329         typedef bool value_type;
330
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)));
335         }
336         Parse_Flag const & operator= (value_type other) { value(other); return *this; }
337     };
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; }
341
342 }
343
344 ///////////////////////////////hh.e////////////////////////////////////////
345 //#include "ParseInt.cci"
346 //#include "ParseInt.ct"
347 //#include "ParseInt.cti"
348 #endif
349
350 \f
351 // Local Variables:
352 // mode: c++
353 // fill-column: 100
354 // c-file-style: "senf"
355 // indent-tabs-mode: nil
356 // ispell-local-dictionary: "american"
357 // End: