first small steps to MPEG/DVB support...
[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 <boost/cstdint.hpp>
29 #include <boost/static_assert.hpp>
30 #include <boost/integer/integer_mask.hpp>
31 #include "PacketParser.hh"
32
33 //#include "ParseInt.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35 #include "ParseInt.ih"
36
37 namespace senf {
38
39     struct Parse_Int8
40         : public detail::packet::ParseIntOps<Parse_Int8,boost::int8_t>,
41           public PacketParserBase
42     {
43         Parse_Int8(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
44
45         ///////////////////////////////////////////////////////////////////////////
46
47         typedef boost::int8_t value_type;
48         static size_type const fixed_bytes = 1;
49
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; }
53     };
54     inline std::ostream & operator<<(std::ostream & os, Parse_Int8 const & i)
55     { os << i.value(); return os; }
56
57     struct Parse_UInt8
58         : public detail::packet::ParseIntOps<Parse_UInt8,boost::uint8_t>,
59           public PacketParserBase
60     {
61         Parse_UInt8(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
62
63         ///////////////////////////////////////////////////////////////////////////
64
65         typedef boost::uint8_t value_type;
66         static size_type const fixed_bytes = 1;
67
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; }
71     };
72     inline std::ostream & operator<<(std::ostream & os, Parse_UInt8 const & i)
73     { os << i.value(); return os; }
74
75     struct Parse_Int16
76         : public detail::packet::ParseIntOps<Parse_Int16,boost::int16_t>,
77           public PacketParserBase
78     {
79         Parse_Int16(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
80
81         ///////////////////////////////////////////////////////////////////////////
82
83         typedef boost::int16_t value_type;
84         static size_type const fixed_bytes = 2;
85
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; }
89     };
90     inline std::ostream & operator<<(std::ostream & os, Parse_Int16 const & i)
91     { os << i.value(); return os; }
92
93     struct Parse_UInt16
94         : public detail::packet::ParseIntOps<Parse_UInt16,boost::uint16_t>,
95           public PacketParserBase
96     {
97         Parse_UInt16(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
98
99         ///////////////////////////////////////////////////////////////////////////
100
101         typedef boost::uint16_t value_type;
102         static size_type const fixed_bytes = 2;
103
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; }
107     };
108     inline std::ostream & operator<<(std::ostream & os, Parse_UInt16 const & i)
109     { os << i.value(); return os; }
110
111     struct Parse_Int24
112         : public detail::packet::ParseIntOps<Parse_Int24,boost::int32_t>,
113           public PacketParserBase
114     {
115         Parse_Int24(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
116
117         ///////////////////////////////////////////////////////////////////////////
118
119         typedef boost::int32_t value_type;
120         static size_type const fixed_bytes = 3;
121
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; }
126     };
127     inline std::ostream & operator<<(std::ostream & os, Parse_Int24 const & i)
128     { os << i.value(); return os; }
129
130     struct Parse_UInt24
131         : public detail::packet::ParseIntOps<Parse_UInt24,boost::uint32_t>,
132           public PacketParserBase
133     {
134         Parse_UInt24(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
135
136         ///////////////////////////////////////////////////////////////////////////
137
138         typedef boost::uint32_t value_type;
139         static size_type const fixed_bytes = 3;
140
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; }
144     };
145     inline std::ostream & operator<<(std::ostream & os, Parse_UInt24 const & i)
146     { os << i.value(); return os; }
147
148     struct Parse_Int32
149         : public detail::packet::ParseIntOps<Parse_Int32,boost::int32_t>,
150           public PacketParserBase
151     {
152         Parse_Int32(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
153
154         ///////////////////////////////////////////////////////////////////////////
155
156         typedef boost::int32_t value_type;
157         static size_type const fixed_bytes = 4;
158
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; }
162     };
163     inline std::ostream & operator<<(std::ostream & os, Parse_Int32 const & i)
164     { os << i.value(); return os; }
165
166     struct Parse_UInt32
167         : public detail::packet::ParseIntOps<Parse_UInt32,boost::uint32_t>,
168           public PacketParserBase
169     {
170         Parse_UInt32(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
171
172         ///////////////////////////////////////////////////////////////////////////
173
174         typedef boost::uint32_t value_type;
175         static size_type const fixed_bytes = 4;
176
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; }
180     };
181     inline std::ostream & operator<<(std::ostream & os, Parse_UInt32 const & i)
182     { os << i.value(); return os; }
183
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
188     {
189         Parse_IntField(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
190
191         ///////////////////////////////////////////////////////////////////////////
192
193         typedef boost::int32_t value_type;
194         static size_type const fixed_bytes = (End-1)/8+1;
195
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;
200         }
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; }
203
204     private:
205         BOOST_STATIC_ASSERT( Start<End );
206         BOOST_STATIC_ASSERT( End-Start<=32 );
207     };
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; }
211
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
216     {
217         Parse_UIntField(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
218
219         ///////////////////////////////////////////////////////////////////////////
220
221         typedef boost::uint32_t value_type;
222         static size_type const fixed_bytes = (End-1)/8+1;
223
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; }
227
228     private:
229         BOOST_STATIC_ASSERT( Start<End );
230         BOOST_STATIC_ASSERT( End-Start<=32 );
231     };
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; }
235
236     template <unsigned bit>
237     struct Parse_Flag
238         : public detail::packet::ParseIntOps<Parse_Flag<bit>,bool>,
239           public PacketParserBase
240     {
241         Parse_Flag(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
242
243         ///////////////////////////////////////////////////////////////////////////
244
245         typedef bool value_type;
246         static size_type const fixed_bytes = bit/8+1;
247
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)));
252         }
253         Parse_Flag const & operator= (value_type other) { value(other); return *this; }
254     };
255     template <unsigned bit>
256     inline std::ostream & operator<<(std::ostream & os, Parse_Flag<bit> const & i)
257     { os << i.value(); return os; }
258
259 }
260
261 ///////////////////////////////hh.e////////////////////////////////////////
262 //#include "ParseInt.cci"
263 //#include "ParseInt.ct"
264 //#include "ParseInt.cti"
265 #endif
266
267 \f
268 // Local Variables:
269 // mode: c++
270 // fill-column: 100
271 // c-file-style: "senf"
272 // indent-tabs-mode: nil
273 // ispell-local-dictionary: "american"
274 // compile-command: "scons -u test"
275 // comment-column: 40
276 // End: