Packets: Add additional parse helper macros
[senf.git] / Packets / ParseHelpers.ih
1 # // Copyright (C) 2007
2 # // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 # // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 # //     Stefan Bund <g0dil@berlios.de>
5 # //
6 # // This program is free software; you can redistribute it and/or modify
7 # // it under the terms of the GNU General Public License as published by
8 # // the Free Software Foundation; either version 2 of the License, or
9 # // (at your option) any later version.
10 # //
11 # // This program is distributed in the hope that it will be useful,
12 # // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # // GNU General Public License for more details.
15 # //
16 # // You should have received a copy of the GNU General Public License
17 # // along with this program; if not, write to the
18 # // Free Software Foundation, Inc.,
19 # // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #
21 # /** \file
22 #     \brief ParseHelpers internal header */
23 #
24 # if !defined(IH_ParseHelpers_)
25 # define IH_ParseHelpers_ 1
26 #
27 # // Custom includes
28 # include <boost/preprocessor/cat.hpp>
29 # include "../Utils/mpl.hh"
30 #
31 # ////////////////////////////////ih.p///////////////////////////////////////
32 #
33 # ///////////////////////////////////////////////////////////////////////////
34 # // SENF_PARSER_INITIALIZE
35 #
36 # define SENF_PARSER_INITIALIZE  BOOST_PP_CAT( SENF_PARSER_INITIALIZE_ , SENF_PARSER_TYPE )
37 #
38 # define SENF_PARSER_INITIALIZE_fix()                                                             \
39     private:                                                                                      \
40         SENF_MPL_SLOT_DEF_ZERO(index);                                                            \
41         SENF_MPL_SLOT_DEF_ZERO(offset);                                                           \
42         SENF_MPL_SLOT_DEF_ZERO(bit);                                                              \
43         SENF_MPL_SLOT_DEF_ZERO(bitfield_size);                                                    \
44         void init_chain(senf::mpl::rv<0>*) {}                                                     \
45     public:
46 #
47 # define SENF_PARSER_INITIALIZE_var()                                                             \
48     private:                                                                                      \
49         SENF_MPL_SLOT_DEF_ZERO(index);                                                            \
50         SENF_MPL_SLOT_DEF_ZERO(init_bytes);                                                       \
51         SENF_MPL_SLOT_DEF_ZERO(bit);                                                              \
52         SENF_MPL_SLOT_DEF_ZERO(bitfield_size);                                                    \
53         void init_chain(senf::mpl::rv<0>*) {}                                                     \
54         size_type field_offset_(senf::mpl::rv<0>*) const { return 0; }                            \
55     public:
56 #
57 # ///////////////////////////////////////////////////////////////////////////
58 # // SENF_PARSER_INHERIT_*
59 #
60 # define SENF_PARSER_INHERIT_var(name)                                                            \
61         typedef name parser_base_type;                                                            \
62     private:                                                                                      \
63         SENF_MPL_SLOT_SET(index, 1);                                                              \
64         SENF_MPL_SLOT_SET(init_bytes, senf::init_bytes<name>::value);                             \
65         size_type field_offset_(senf::mpl::rv<1>*) const {                                        \
66             return senf::bytes( *static_cast<name const*>(this) );                                \
67         }                                                                                         \
68         void init_chain(senf::mpl::rv<1>*) {                                                      \
69             name::init();                                                                         \
70         }                                                                                         \
71     public:
72 #
73 # define SENF_PARSER_INHERIT_fix(name)                                                            \
74         typedef name parser_base_type;                                                            \
75     private:                                                                                      \
76         SENF_MPL_SLOT_SET(offset, name::fixed_bytes);                                             \
77         SENF_MPL_SLOT_SET(index, 1);                                                              \
78         void init_chain(senf::mpl::rv<1>*) {                                                      \
79             name::init();                                                                         \
80         }                                                                                         \
81     public:
82 #
83 # ///////////////////////////////////////////////////////////////////////////
84 # // SENF_PARSE_FIELD_*
85 #
86 # define SENF_PARSE_FIELD_var(name, type)    SENF_PARSER_FIELD_I(name, type, var, rw, public)
87 # define SENF_PARSE_FIELD_RO_var(name, type) SENF_PARSER_FIELD_I(name, type, var, ro, public)
88 #
89 # define SENF_PARSE_FIELD_fix(name, type)    SENF_PARSER_FIELD_I(name, type, fix, rw, public)
90 # define SENF_PARSE_FIELD_RO_fix(name, type) SENF_PARSER_FIELD_I(name, type, fix, ro, public)
91 #
92 # define SENF_PARSER_FIELD_I(name, type, ofstype, rwtype, access)                                 \
93     access:                                                                                       \
94         SENF_PARSER_I_BITFIELD_RESET()                                                            \
95         SENF_PARSER_I_FIELD_INTRO(name, type, access)                                             \
96         BOOST_PP_CAT(SENF_PARSER_I_FIELD_INIT_, rwtype) (name, type, access)                      \
97         BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type, access)                      \
98         BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (                                       \
99             name, type,                                                                           \
100             BOOST_PP_CAT(SENF_PARSER_I_SIZE_, ofstype) (name, type),                              \
101             BOOST_PP_CAT(SENF_PARSER_I_INITBYTES_, ofstype) (name, type),                         \
102             access )                                                                              \
103         BOOST_PP_CAT(SENF_PARSER_I_FIELD_VAL_, rwtype) (name, type, ofstype, access)              \
104     public:
105 #
106 # ////////////////////////////////////////
107 # // SENF_PARSER_I_FIELD_INTRO
108 #
109 # define SENF_PARSER_I_FIELD_INTRO(name, type, access)                                            \
110         typedef type BOOST_PP_CAT(name, _t);                                                      \
111         static size_type const BOOST_PP_CAT(name,_index) = SENF_MPL_SLOT_GET(index)+1;            \
112     private:                                                                                      \
113         SENF_MPL_SLOT_SET(index, BOOST_PP_CAT(name,_index));                                      \
114     access:
115 #
116 # ////////////////////////////////////////
117 # // SENF_PARSER_I_FIELD_INIT_*
118 #
119 # define SENF_PARSER_I_FIELD_INIT_rw(name, type, access)                                                  \
120     private:                                                                                      \
121         void init_chain(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) {                              \
122             init_chain(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0));              \
123             name().init();                                                                        \
124         }                                                                                         \
125     access:
126 #
127 # define SENF_PARSER_I_FIELD_INIT_ro(name, type, access)                                                  \
128     private:                                                                                      \
129         void init_chain(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) {                              \
130             init_chain(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0));              \
131         }                                                                                         \
132     access:
133 #
134 # ////////////////////////////////////////
135 # // SENF_PARSER_I_FIELD_OFS_*
136 #
137 # define SENF_PARSER_I_FIELD_OFS_var(name, type, access)                                          \
138         size_type BOOST_PP_CAT(name,_offset)() const {                                            \
139             return field_offset_(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0));    \
140         }
141 #
142 # define SENF_PARSER_I_FIELD_OFS_fix(name, type, access)                                          \
143         static size_type const BOOST_PP_CAT(name, _offset) = SENF_MPL_SLOT_GET(offset);
144 #
145 # ////////////////////////////////////////
146 # // SENF_PARSER_I_ADVANCE_OFS_*
147 #
148 # // Can't call 'name()' here if 'name' is an ro field ...
149 # define SENF_PARSER_I_SIZE_var(name, type) senf::bytes(parse<type>(BOOST_PP_CAT(name,_offset)()))
150 # define SENF_PARSER_I_INITBYTES_var(name, type) senf::init_bytes<type>::value
151 #
152 # define SENF_PARSER_I_SIZE_fix(name, type) type::fixed_bytes
153 # define SENF_PARSER_I_INITBYTES_fix(name, type) void
154 #
155 # define SENF_PARSER_I_ADVANCE_OFS_var(name, type, size, isize, access)                           \
156         size_type BOOST_PP_CAT(name, _next_offset)() const {                                      \
157             return BOOST_PP_CAT(name,_offset)() + size;                                           \
158         }                                                                                         \
159         static size_type const BOOST_PP_CAT(name, _init_bytes) = SENF_MPL_SLOT_GET(init_bytes);   \
160         static size_type const BOOST_PP_CAT(name, _next_init_bytes) =                             \
161             BOOST_PP_CAT(name, _init_bytes) + isize;                                              \
162     private:                                                                                      \
163         size_type field_offset_(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) const {                \
164             return BOOST_PP_CAT(name, _next_offset)();                                            \
165         }                                                                                         \
166         SENF_MPL_SLOT_SET(init_bytes, BOOST_PP_CAT(name,_next_init_bytes));                       \
167     access:
168 #
169 # define SENF_PARSER_I_ADVANCE_OFS_fix(name, type, size, isize, access)                           \
170         static size_type const BOOST_PP_CAT(name, _next_offset) =                                 \
171             BOOST_PP_CAT(name, _offset) + size;                                                   \
172     private:                                                                                      \
173         SENF_MPL_SLOT_SET(offset, BOOST_PP_CAT(name, _next_offset));                              \
174     access:
175 #
176 # ////////////////////////////////////////
177 # // SENF_PARSER_I_FIELD_VAL_*
178 #
179 # define SENF_PARSER_I_MAYBECALL_var() ()
180 # define SENF_PARSER_I_MAYBECALL_fix()
181 #
182 # define SENF_PARSER_I_FIELD_VAL_rw(name, type, ofstype, access)                                  \
183         BOOST_PP_CAT(name, _t) name() const {                                                     \
184             return parse<type>(                                                                   \
185                 BOOST_PP_CAT(name,_offset) BOOST_PP_CAT(SENF_PARSER_I_MAYBECALL_, ofstype)());    \
186         }
187 #
188 # define SENF_PARSER_I_FIELD_VAL_ro(name, type, ofstype, access)                                  \
189         BOOST_PP_CAT(name, _t)::value_type name() const {                                         \
190             return parse<type>(                                                                   \
191                 BOOST_PP_CAT(name,_offset) BOOST_PP_CAT(SENF_PARSER_I_MAYBECALL_, ofstype)())     \
192                     .value();                                                                     \
193         }
194 #
195 # ///////////////////////////////////////////////////////////////////////////
196 # // SENF_PARSE_CUSTOM_FIELD_*
197 #
198 # define SENF_PARSE_CUSTOM_FIELD_var(name, type, size, isize)                                     \
199       SENF_PARSER_CUSTOM_FIELD_I(name, type, size, isize, var)
200 # define SENF_PARSE_CUSTOM_FIELD_fix(name, type, size)                                            \
201       SENF_PARSER_CUSTOM_FIELD_I(name, type, size, size, fix)
202 #
203 # define SENF_PARSER_CUSTOM_FIELD_I(name, type, size, isize, ofstype)                             \
204         SENF_PARSER_I_BITFIELD_RESET()                                                            \
205         SENF_PARSER_I_FIELD_INTRO(name, type, public)                                             \
206         SENF_PARSER_I_FIELD_INIT_ro(name, type, public)                                           \
207         BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type, public)                      \
208         BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (name, type, size, isize, public)       \
209         BOOST_PP_CAT(name, _t) name() const
210 #
211 # ///////////////////////////////////////////////////////////////////////////
212 # // SENF_PARSE_BITFIELD_*
213 #
214 # define SENF_PARSE_BITFIELD_var(name, bits, type)                                                \
215       SENF_PARSER_BITFIELD_I(name, bits, type, var, rw, public)
216 # define SENF_PARSE_BITFIELD_RO_var(name, bits, type)                                             \
217       SENF_PARSER_BITFIELD_I(name, bits, type, var, ro, public)
218 #
219 # define SENF_PARSE_BITFIELD_fix(name, bits, type)                                                \
220       SENF_PARSER_BITFIELD_I(name, bits, type, fix, rw, public)
221 # define SENF_PARSE_BITFIELD_RO_fix(name, bits, type)                                             \
222       SENF_PARSER_BITFIELD_I(name, bits, type, fix, ro, public)
223 #
224 # ////////////////////////////////////////
225 # // SENF_PARSER_BITFIELD_I
226 #
227 # define SENF_PARSER_BITFIELD_TYPE_signed(start, bits)   senf::Parse_IntField<start, start+bits>
228 # define SENF_PARSER_BITFIELD_TYPE_unsigned(start, bits) senf::Parse_UIntField<start, start+bits>
229 # define SENF_PARSER_BITFIELD_TYPE_bool(start, bits)     senf::Parse_Flag<start>
230 #
231 # define SENF_PARSER_BITFIELD_I(name, bits, type, ofstype, rwtype, access)                        \
232     access:                                                                                       \
233         static size_type const BOOST_PP_CAT(name, _bit) = SENF_MPL_SLOT_GET(bit);                 \
234     private:                                                                                      \
235         SENF_MPL_SLOT_SET(bit, BOOST_PP_CAT(name, _bit) + bits);                                  \
236         typedef BOOST_PP_CAT(SENF_PARSER_BITFIELD_TYPE_, type)( BOOST_PP_CAT(name, _bit), bits )  \
237              BOOST_PP_CAT(name,_bit_t );                                                          \
238     access:                                                                                       \
239         SENF_PARSER_BITFIELD_II( name, bits, BOOST_PP_CAT(name, _bit_t), ofstype, rwtype, access) \
240     public:
241 #
242 # define SENF_PARSER_BITFIELD_II(name, bits, type, ofstype, rwtype, access)                       \
243          SENF_PARSER_I_FIELD_INTRO(name, type, access)                                            \
244          SENF_PARSER_I_FIELD_INIT_ro(name, type, access)                                          \
245          BOOST_PP_CAT(SENF_PARSER_I_BITFIELD_OFS_, ofstype) (name, type, access)                  \
246          BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (                                      \
247              name, type,                                                                          \
248              BOOST_PP_CAT(name, _t)::fixed_bytes, BOOST_PP_CAT(name, _t)::fixed_bytes,            \
249              access)                                                                              \
250     private:                                                                                      \
251          SENF_MPL_SLOT_SET(bitfield_size, BOOST_PP_CAT(name, _t)::fixed_bytes);                   \
252     access:                                                                                       \
253          BOOST_PP_CAT(SENF_PARSER_I_FIELD_VAL_, rwtype) (name, type, ofstype, access)             \
254     public:
255 #
256 # ////////////////////////////////////////
257 # // SENF_PARSER_I_BITFIELD_OFS_*
258 #
259 # define SENF_PARSER_I_BITFIELD_OFS_var(name, type, access)                                       \
260         size_type BOOST_PP_CAT(name,_offset)() const {                                            \
261             return field_offset_(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0))     \
262                 - SENF_MPL_SLOT_GET(bitfield_size);                                               \
263         }
264 #
265 # define SENF_PARSER_I_BITFIELD_OFS_fix(name, type, access)                                       \
266         static size_type const BOOST_PP_CAT(name, _offset) = SENF_MPL_SLOT_GET(offset)            \
267             - SENF_MPL_SLOT_GET(bitfield_size);
268 #
269 # ////////////////////////////////////////
270 # // SENF_PARSER_I_BITFIELD_RESET
271 #
272 # define SENF_PARSER_I_BITFIELD_RESET()                                                           \
273         SENF_MPL_SLOT_SET(bit, 0);                                                                \
274         SENF_MPL_SLOT_SET(bitfield_size, 0);
275 #
276 # ///////////////////////////////////////////////////////////////////////////
277 # // SENF_PARSER_FINALIZE_*
278 #
279 # define SENF_PARSER_FINALIZE_var(name)                                                           \
280     SENF_PARSER_FINALIZE_GENERIC(name)                                                            \
281     size_type bytes() const {                                                                     \
282         return field_offset_(static_cast<senf::mpl::rv<SENF_MPL_SLOT_GET(index)>*>(0));           \
283     }                                                                                             \
284     static size_type const init_bytes = SENF_MPL_SLOT_GET(init_bytes)
285 #
286 # define SENF_PARSER_FINALIZE_fix(name)                                                           \
287     SENF_PARSER_FINALIZE_GENERIC(name)                                                            \
288     static size_type const fixed_bytes = SENF_MPL_SLOT_GET(offset);
289 #
290 # define SENF_PARSER_FINALIZE_GENERIC(name)                                                       \
291          void defaultInit() {                                                                     \
292              init_chain(static_cast<senf::mpl::rv<SENF_MPL_SLOT_GET(index)>*>(0));                \
293          }                                                                                        \
294          name(data_iterator i, state_type s) : parser_base_type(i,s) {}                           \
295     private:                                                                                      \
296          template <class T> void init(T) { defaultInit(); }                                       \
297     public:                                                                                       \
298          void init() { init(0); }
299 #
300 # ///////////////////////////////////////////////////////////////////////////
301 # // SENF_PARSER_SKIP_*
302 #
303 # define SENF_PARSER_SKIP_var(bytes, ibytes)                                                      \
304       SENF_PARSER_I_SKIP( BOOST_PP_CAT(senf_anon_, __LINE__), bytes, ibytes, var)
305 #
306 # define SENF_PARSER_SKIP_fix(bytes)                                                              \
307       SENF_PARSER_I_SKIP( BOOST_PP_CAT(senf_anon_, __LINE__), bytes, bytes, fix)
308 #
309 # define SENF_PARSER_I_SKIP(name, bytes, ibytes, ofstype)                                         \
310     private:                                                                                      \
311           SENF_PARSER_I_BITFIELD_RESET()                                                          \
312           SENF_PARSER_I_FIELD_INTRO(name, void, private)                                          \
313           SENF_PARSER_I_FIELD_INIT_ro(name, void, private)                                        \
314           BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type, access)                    \
315           BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (name, void, bytes, ibytes, private)  \
316     public:
317 #
318 # ///////////////////////////////////////////////////////////////////////////
319 # // SENF_PARSER_GOTO_*
320 #
321 # define SENF_PARSER_GOTO_var(name)                                                               \
322       SENF_PARSER_I_GOTO( BOOST_PP_CAT(senf_anon_, __LINE__),                                     \
323                           BOOST_PP_CAT(name, _offset)(),                                          \
324                           BOOST_PP_CAT(name, _init_bytes),                                        \
325                           var )
326 #
327 # define SENF_PARSER_GOTO_fix(name)                                                               \
328       SENF_PARSER_I_GOTO( BOOST_PP_CAT(senf_anon_, __LINE__),                                     \
329                           BOOST_PP_CAT(name, _offset),                                            \
330                           BOOST_PP_CAT(name, _offset),                                            \
331                           fix )
332 #
333 # define SENF_PARSER_GOTO_OFFSET_var(offset, isize)                                               \
334       SENF_PARSER_I_GOTO( BOOST_PP_CAT(senf_anon_, __LINE__), offset, isize, var )
335 #
336 # define SENF_PARSER_GOTO_OFFSET_fix(offset)                                                      \
337       SENF_PARSER_I_GOTO( BOOST_PP_CAT(senf_anon_, __LINE__), offset, offset, fix )
338 #
339 # define SENF_PARSER_I_GOTO(name, offset, initsize, ofstype)                                      \
340       private:                                                                                    \
341           SENF_PARSER_I_BITFIELD_RESET()                                                          \
342           SENF_PARSER_I_FIELD_INTRO(name, void, private)                                          \
343           SENF_PARSER_I_FIELD_INIT_ro(name, void, private)                                        \
344           BOOST_PP_CAT( SENF_PARSE_I_GOTO_SET_OFS_, ofstype ) (name, offset, initsize)            \
345       public:
346 #
347 # define SENF_PARSE_I_GOTO_SET_OFS_var(name, offs, initsize)                                      \
348           size_type field_offset_(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) const {              \
349               return offs;                                                                        \
350           }                                                                                       \
351           SENF_MPL_SLOT_SET(init_bytes, initsize);
352 #
353 # define SENF_PARSE_I_GOTO_SET_OFS_fix(name, offs, initsize)                                      \
354           SENF_MPL_SLOT_SET(offset, offs);
355 #
356 # ///////////////////////////////////////////////////////////////////////////
357 # // SENF_PARSER_LABEL_*
358 #
359 # define SENF_PARSER_LABEL_var(name) SENF_PARSER_I_LABEL( name, var, public )
360 # define SENF_PARSER_LABEL_fix(name) SENF_PARSER_I_LABEL( name, fix, public )
361 #
362 # define SENF_PARSER_I_LABEL(name, ofstype, access)                                               \
363       access:                                                                                     \
364           SENF_PARSER_I_BITFIELD_RESET()                                                          \
365           SENF_PARSER_I_FIELD_INTRO(name, void, access)                                           \
366           SENF_PARSER_I_FIELD_INIT_ro(name, void, access)                                         \
367           BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type, access)                    \
368           BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (name, void, 0, 0,access)             \
369       public:
370 #
371 # ////////////////////////////////ih.e///////////////////////////////////////
372 # endif
373 #
374 #
375 # // Local Variables:
376 # // mode: c++
377 # // fill-column: 100
378 # // c-file-style: "senf"
379 # // indent-tabs-mode: nil
380 # // ispell-local-dictionary: "american"
381 # // compile-command: "scons -u test"
382 # // End: