Renamed namespaces satcom::lib and satcom::pkf to senf
[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 "ParserBase.hh"
28 #include <boost/cstdint.hpp>
29 #include <boost/static_assert.hpp>
30 #include <boost/integer/integer_mask.hpp>
31
32 //#include "ParseInt.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
34 #include "ParseInt.ih"
35
36 namespace senf {
37
38
39     template <class Iterator=nil, class IPacket=nil>
40     struct Parse_Int8
41         : public impl::ParseIntOps<Parse_Int8<Iterator,IPacket>,boost::int8_t>,
42           public ParserBase<Iterator,IPacket>
43     { 
44         template <class I=nil, class P=nil>
45         struct rebind { typedef Parse_Int8<I,P> parser; };
46         typedef Iterator byte_iterator;
47
48         static unsigned bytes() { return 1; }
49
50         Parse_Int8() {}
51         explicit Parse_Int8(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
52
53         ///////////////////////////////////////////////////////////////////////////
54
55         typedef boost::int8_t value_type;
56
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; }
60     };
61
62     template <class Iterator=nil, class IPacket=nil>
63     struct Parse_UInt8
64         : public impl::ParseIntOps<Parse_UInt8<Iterator,IPacket>,boost::uint8_t>,
65           public ParserBase<Iterator,IPacket>
66     {
67         template <class I=nil, class P=nil>
68         struct rebind { typedef Parse_UInt8<I,P> parser; };
69         typedef Iterator byte_iterator;
70
71         static unsigned bytes() { return 1; }
72         
73         Parse_UInt8() {}
74         explicit Parse_UInt8(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
75
76         ///////////////////////////////////////////////////////////////////////////
77
78         typedef boost::uint8_t value_type;
79
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; }
83     };
84
85     template <class Iterator=nil, class IPacket=nil>
86     struct Parse_Int16
87         : public impl::ParseIntOps<Parse_Int16<Iterator,IPacket>,boost::int16_t>,
88           public ParserBase<Iterator,IPacket>
89     {
90         template <class I=nil, class P=nil>
91         struct rebind { typedef Parse_Int16<I,P> parser; };
92         typedef Iterator byte_iterator;
93
94         static unsigned bytes() { return 2; }
95
96         Parse_Int16() {}
97         explicit Parse_Int16(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
98
99         ///////////////////////////////////////////////////////////////////////////
100
101         typedef boost::int16_t value_type;
102
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; }
106     };
107
108     template <class Iterator=nil, class IPacket=nil>
109     struct Parse_UInt16
110         : public impl::ParseIntOps<Parse_UInt16<Iterator,IPacket>,boost::uint16_t>,
111           public ParserBase<Iterator,IPacket>
112     {
113         template <class I=nil, class P=nil>
114         struct rebind { typedef Parse_UInt16<I,P> parser; };
115         typedef Iterator byte_iterator;
116
117         static unsigned bytes() { return 2; }
118
119         Parse_UInt16() {}
120         explicit Parse_UInt16(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
121
122         ///////////////////////////////////////////////////////////////////////////
123
124         typedef boost::uint16_t value_type;
125
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; }
129     };
130
131     template <class Iterator=nil, class IPacket=nil>
132     struct Parse_Int24
133         : public impl::ParseIntOps<Parse_Int24<Iterator,IPacket>,boost::int32_t>,
134           public ParserBase<Iterator,IPacket>
135     {
136         template <class I=nil, class P=nil>
137         struct rebind { typedef Parse_Int24<I,P> parser; };
138         typedef Iterator byte_iterator;
139
140         static unsigned bytes() { return 3; }
141
142         Parse_Int24() {}
143         explicit Parse_Int24(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
144
145         ///////////////////////////////////////////////////////////////////////////
146
147         typedef boost::int32_t value_type;
148
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; }
153     };
154
155     template <class Iterator=nil, class IPacket=nil>
156     struct Parse_UInt24
157         : public impl::ParseIntOps<Parse_UInt24<Iterator,IPacket>,boost::uint32_t>,
158           public ParserBase<Iterator,IPacket>
159     {
160         template <class I=nil, class P=nil>
161         struct rebind { typedef Parse_UInt24<I,P> parser; };
162         typedef Iterator byte_iterator;
163
164         static unsigned bytes() { return 3; }
165
166         Parse_UInt24() {}
167         explicit Parse_UInt24(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
168
169         ///////////////////////////////////////////////////////////////////////////
170
171         typedef boost::uint32_t value_type;
172
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; }
176     };
177
178     template <class Iterator=nil, class IPacket=nil>
179     struct Parse_Int32
180         : public impl::ParseIntOps<Parse_Int32<Iterator,IPacket>,boost::int32_t>,
181           public ParserBase<Iterator,IPacket>
182     {
183         template <class I=nil, class P=nil>
184         struct rebind { typedef Parse_Int32<I,P> parser; };
185         typedef Iterator byte_iterator;
186
187         static unsigned bytes() { return 4; }
188
189         Parse_Int32() {}
190         explicit Parse_Int32(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
191
192         ///////////////////////////////////////////////////////////////////////////
193
194         typedef boost::int32_t value_type;
195
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; }
199     };
200
201     template <class Iterator=nil, class IPacket=nil>
202     struct Parse_UInt32
203         : public impl::ParseIntOps<Parse_UInt32<Iterator,IPacket>,boost::uint32_t>,
204           public ParserBase<Iterator,IPacket>
205     {
206         template <class I=nil, class P=nil>
207         struct rebind { typedef Parse_UInt32<I,P> parser; };
208         typedef Iterator byte_iterator;
209
210         static unsigned bytes() { return 4; }
211
212         Parse_UInt32() {}
213         explicit Parse_UInt32(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
214
215         ///////////////////////////////////////////////////////////////////////////
216
217         typedef boost::uint32_t value_type;
218
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; }
222     };
223
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>
228     {
229         template <class I=nil, class P=nil>
230         struct rebind { typedef Parse_IntField<start,end,I,P> parser; };
231         typedef Iterator byte_iterator;
232
233         static unsigned bytes() { return (end-1)/8+1; }
234
235         Parse_IntField() {}
236         explicit Parse_IntField(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
237
238         ///////////////////////////////////////////////////////////////////////////
239
240         typedef boost::int32_t value_type;
241
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;
246         }
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; }
249
250     private:
251         BOOST_STATIC_ASSERT( start<end );
252         BOOST_STATIC_ASSERT( end-start<=32 );
253     };
254
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>
259     {
260         template <class I=nil, class P=nil>
261         struct rebind { typedef Parse_UIntField<start,end,I,P> parser; };
262         typedef Iterator byte_iterator;
263
264         static unsigned bytes() { return (end-1)/8+1; }
265
266         Parse_UIntField() {}
267         explicit Parse_UIntField(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
268
269         ///////////////////////////////////////////////////////////////////////////
270
271         typedef boost::uint32_t value_type;
272
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; }
276
277     private:
278         BOOST_STATIC_ASSERT( start<end );
279         BOOST_STATIC_ASSERT( end-start<=32 );
280     };
281
282     template <unsigned bit, class Iterator=nil, class IPacket=nil>
283     struct Parse_Flag
284         : public impl::ParseIntOps<Parse_Flag<bit,Iterator,IPacket>,bool>,
285           public ParserBase<Iterator,IPacket>
286     {
287         template <class I=nil, class P=nil>
288         struct rebind { typedef Parse_Flag<bit,I,P> parser; };
289         typedef Iterator byte_iterator;
290
291         static unsigned bytes() { return 1; }
292
293         Parse_Flag() {}
294         explicit Parse_Flag(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
295
296         ///////////////////////////////////////////////////////////////////////////
297
298         typedef bool value_type;
299
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)));
304         }
305         Parse_Flag const & operator= (value_type other) { value(other); return *this; }
306     };
307
308 }
309
310 ///////////////////////////////hh.e////////////////////////////////////////
311 //#include "ParseInt.cci"
312 //#include "ParseInt.ct"
313 //#include "ParseInt.cti"
314 #endif
315
316 \f
317 // Local Variables:
318 // mode: c++
319 // c-file-style: "senf"
320 // End: