1 # // Copyright (C) 2007
2 # // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 # // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 # // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
22 # \brief ParseHelpers internal header */
24 # if !defined(IH_ParseHelpers_)
25 # define IH_ParseHelpers_ 1
28 # include <boost/preprocessor/cat.hpp>
29 # include "../Utils/mpl.hh"
31 # ////////////////////////////////ih.p///////////////////////////////////////
33 # ///////////////////////////////////////////////////////////////////////////
34 # // SENF_PARSER_INITIALIZE
36 # define SENF_PARSER_INITIALIZE BOOST_PP_CAT( SENF_PARSER_INITIALIZE_ , SENF_PARSER_TYPE )
38 # define SENF_PARSER_INITIALIZE_fix() \
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>*) {} \
47 # define SENF_PARSER_INITIALIZE_var() \
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; } \
57 # ///////////////////////////////////////////////////////////////////////////
58 # // SENF_PARSER_INHERIT_*
60 # define SENF_PARSER_INHERIT_var(name) \
61 typedef name parser_base_type; \
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) ); \
68 void init_chain(senf::mpl::rv<1>*) { \
73 # define SENF_PARSER_INHERIT_fix(name) \
74 typedef name parser_base_type; \
76 SENF_MPL_SLOT_SET(offset, name::fixed_bytes); \
77 SENF_MPL_SLOT_SET(index, 1); \
78 void init_chain(senf::mpl::rv<1>*) { \
83 # ///////////////////////////////////////////////////////////////////////////
84 # // SENF_PARSER_FIELD*
85 # // SENF_PARSER_P_FIELD_*
87 # define SENF_PARSER_FIELD_var(name, type) SENF_PARSER_FIELD_I(name, type, var, rw, public)
88 # define SENF_PARSER_FIELD_RO_var(name, type) SENF_PARSER_FIELD_I(name, type, var, ro, public)
89 # define SENF_PARSER_FIELD_fix(name, type) SENF_PARSER_FIELD_I(name, type, fix, rw, public)
90 # define SENF_PARSER_FIELD_RO_fix(name, type) SENF_PARSER_FIELD_I(name, type, fix, ro, public)
92 # define SENF_PARSER_P_FIELD_var(name, type) SENF_PARSER_FIELD_I(name, type, var, rw, private)
93 # define SENF_PARSER_P_FIELD_RO_var(name, type) SENF_PARSER_FIELD_I(name, type, var, ro, private)
94 # define SENF_PARSER_P_FIELD_fix(name, type) SENF_PARSER_FIELD_I(name, type, fix, rw, private)
95 # define SENF_PARSER_P_FIELD_RO_fix(name, type) SENF_PARSER_FIELD_I(name, type, fix, ro, private)
97 # define SENF_PARSER_FIELD_I(name, type, ofstype, rwtype, access) \
99 SENF_PARSER_I_BITFIELD_RESET() \
100 SENF_PARSER_I_FIELD_INTRO(name, type, access) \
101 BOOST_PP_CAT(SENF_PARSER_I_FIELD_INIT_, rwtype) (name, type, access) \
102 BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type, access) \
103 BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) ( \
105 BOOST_PP_CAT(SENF_PARSER_I_SIZE_, ofstype) (name, type), \
106 BOOST_PP_CAT(SENF_PARSER_I_INITBYTES_, ofstype) (name, type), \
108 BOOST_PP_CAT(SENF_PARSER_I_FIELD_VAL_, rwtype) (name, type, access) \
111 # ////////////////////////////////////////
112 # // SENF_PARSER_I_FIELD_INTRO
114 # define SENF_PARSER_I_FIELD_INTRO(name, type, access) \
115 typedef type BOOST_PP_CAT(name, _t); \
116 static size_type const BOOST_PP_CAT(name,_index) = SENF_MPL_SLOT_GET(index)+1; \
118 SENF_MPL_SLOT_SET(index, BOOST_PP_CAT(name,_index)); \
121 # ////////////////////////////////////////
122 # // SENF_PARSER_I_FIELD_INIT_*
124 # define SENF_PARSER_I_FIELD_INIT_rw(name, type, access) \
126 void init_chain(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) { \
127 init_chain(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0)); \
132 # define SENF_PARSER_I_FIELD_INIT_ro(name, type, access) \
134 void init_chain(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) { \
135 init_chain(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0)); \
139 # ////////////////////////////////////////
140 # // SENF_PARSER_I_FIELD_OFS_*
142 # define SENF_PARSER_I_FIELD_OFS_var(name, type, access) \
143 size_type BOOST_PP_CAT(name,_offset)() const { \
144 return field_offset_(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0)); \
147 # define SENF_PARSER_I_FIELD_OFS_fix(name, type, access) \
148 static size_type const BOOST_PP_CAT(name, _offset) = SENF_MPL_SLOT_GET(offset);
150 # ////////////////////////////////////////
151 # // SENF_PARSER_I_ADVANCE_OFS_*
153 # // Can't call 'name()' here if 'name' is an ro field ...
154 # define SENF_PARSER_I_SIZE_var(name, type) senf::bytes(parse<type>(BOOST_PP_CAT(name,_offset)()))
155 # define SENF_PARSER_I_INITBYTES_var(name, type) senf::init_bytes<type>::value
157 # define SENF_PARSER_I_SIZE_fix(name, type) type::fixed_bytes
158 # define SENF_PARSER_I_INITBYTES_fix(name, type) void
160 # define SENF_PARSER_I_ADVANCE_OFS_var(name, type, size, isize, access) \
161 size_type BOOST_PP_CAT(name, _next_offset)() const { \
162 return BOOST_PP_CAT(name,_offset)() + size; \
164 static size_type const BOOST_PP_CAT(name, _init_bytes) = SENF_MPL_SLOT_GET(init_bytes); \
165 static size_type const BOOST_PP_CAT(name, _next_init_bytes) = \
166 BOOST_PP_CAT(name, _init_bytes) + isize; \
168 size_type field_offset_(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) const { \
169 return BOOST_PP_CAT(name, _next_offset)(); \
171 SENF_MPL_SLOT_SET(init_bytes, BOOST_PP_CAT(name,_next_init_bytes)); \
174 # define SENF_PARSER_I_ADVANCE_OFS_fix(name, type, size, isize, access) \
175 static size_type const BOOST_PP_CAT(name, _next_offset) = \
176 BOOST_PP_CAT(name, _offset) + size; \
178 SENF_MPL_SLOT_SET(offset, BOOST_PP_CAT(name, _next_offset)); \
181 # ////////////////////////////////////////
182 # // SENF_PARSER_I_FIELD_VAL_*
184 # define SENF_PARSER_I_FIELD_VAL_rw(name, type, access) \
185 BOOST_PP_CAT(name, _t) name() const { \
186 return parse<type>( SENF_PARSER_OFFSET(name) ); \
189 # define SENF_PARSER_I_FIELD_VAL_ro(name, type, access) \
190 BOOST_PP_CAT(name, _t)::value_type name() const { \
191 return parse<type>( SENF_PARSER_OFFSET(name) ).value(); \
194 # ///////////////////////////////////////////////////////////////////////////
195 # // SENF_PARSER_CUSTOM_FIELD_*
197 # define SENF_PARSER_CUSTOM_FIELD_var(name, type, size, isize) \
198 SENF_PARSER_CUSTOM_FIELD_I(name, type, size, isize, var)
199 # define SENF_PARSER_CUSTOM_FIELD_fix(name, type, size) \
200 SENF_PARSER_CUSTOM_FIELD_I(name, type, size, size, fix)
202 # define SENF_PARSER_CUSTOM_FIELD_I(name, type, size, isize, ofstype) \
203 SENF_PARSER_I_BITFIELD_RESET() \
204 SENF_PARSER_I_FIELD_INTRO(name, type, public) \
205 SENF_PARSER_I_FIELD_INIT_ro(name, type, public) \
206 BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type, public) \
207 BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (name, type, size, isize, public) \
208 BOOST_PP_CAT(name, _t) name() const
210 # ///////////////////////////////////////////////////////////////////////////
211 # // SENF_PARSER_BITFIELD_*
212 # // SENF_PARSER_P_BITFIELD_*
214 # define SENF_PARSER_BITFIELD_var(name, bits, type) \
215 SENF_PARSER_BITFIELD_I(name, bits, type, var, rw, public)
216 # define SENF_PARSER_BITFIELD_RO_var(name, bits, type) \
217 SENF_PARSER_BITFIELD_I(name, bits, type, var, ro, public)
218 # define SENF_PARSER_BITFIELD_fix(name, bits, type) \
219 SENF_PARSER_BITFIELD_I(name, bits, type, fix, rw, public)
220 # define SENF_PARSER_BITFIELD_RO_fix(name, bits, type) \
221 SENF_PARSER_BITFIELD_I(name, bits, type, fix, ro, public)
223 # define SENF_PARSER_P_BITFIELD_var(name, bits, type) \
224 SENF_PARSER_BITFIELD_I(name, bits, type, var, rw, private)
225 # define SENF_PARSER_P_BITFIELD_RO_var(name, bits, type) \
226 SENF_PARSER_BITFIELD_I(name, bits, type, var, ro, private)
227 # define SENF_PARSER_P_BITFIELD_fix(name, bits, type) \
228 SENF_PARSER_BITFIELD_I(name, bits, type, fix, rw, private)
229 # define SENF_PARSER_P_BITFIELD_RO_fix(name, bits, type) \
230 SENF_PARSER_BITFIELD_I(name, bits, type, fix, ro, private)
232 # ////////////////////////////////////////
233 # // SENF_PARSER_BITFIELD_I
235 # define SENF_PARSER_BITFIELD_TYPE_signed(start, bits) senf::Parse_IntField<start, start+bits>
236 # define SENF_PARSER_BITFIELD_TYPE_unsigned(start, bits) senf::Parse_UIntField<start, start+bits>
237 # define SENF_PARSER_BITFIELD_TYPE_bool(start, bits) senf::Parse_Flag<start>
239 # define SENF_PARSER_BITFIELD_I(name, bits, type, ofstype, rwtype, access) \
241 static size_type const BOOST_PP_CAT(name, _bit) = SENF_MPL_SLOT_GET(bit); \
243 SENF_MPL_SLOT_SET(bit, BOOST_PP_CAT(name, _bit) + bits); \
244 typedef BOOST_PP_CAT(SENF_PARSER_BITFIELD_TYPE_, type)( BOOST_PP_CAT(name, _bit), bits ) \
245 BOOST_PP_CAT(name,_bit_t ); \
247 SENF_PARSER_BITFIELD_II( name, bits, BOOST_PP_CAT(name, _bit_t), ofstype, rwtype, access) \
250 # define SENF_PARSER_BITFIELD_II(name, bits, type, ofstype, rwtype, access) \
251 SENF_PARSER_I_FIELD_INTRO(name, type, access) \
252 SENF_PARSER_I_FIELD_INIT_ro(name, type, access) \
253 BOOST_PP_CAT(SENF_PARSER_I_BITFIELD_OFS_, ofstype) (name, type, access) \
254 BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) ( \
256 BOOST_PP_CAT(name, _t)::fixed_bytes, BOOST_PP_CAT(name, _t)::fixed_bytes, \
259 SENF_MPL_SLOT_SET(bitfield_size, BOOST_PP_CAT(name, _t)::fixed_bytes); \
261 BOOST_PP_CAT(SENF_PARSER_I_FIELD_VAL_, rwtype) (name, type, access) \
264 # ////////////////////////////////////////
265 # // SENF_PARSER_I_BITFIELD_OFS_*
267 # define SENF_PARSER_I_BITFIELD_OFS_var(name, type, access) \
268 size_type BOOST_PP_CAT(name,_offset)() const { \
269 return field_offset_(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0)) \
270 - SENF_MPL_SLOT_GET(bitfield_size); \
273 # define SENF_PARSER_I_BITFIELD_OFS_fix(name, type, access) \
274 static size_type const BOOST_PP_CAT(name, _offset) = SENF_MPL_SLOT_GET(offset) \
275 - SENF_MPL_SLOT_GET(bitfield_size);
277 # ////////////////////////////////////////
278 # // SENF_PARSER_I_BITFIELD_RESET
280 # define SENF_PARSER_I_BITFIELD_RESET() \
281 SENF_MPL_SLOT_SET(bit, 0); \
282 SENF_MPL_SLOT_SET(bitfield_size, 0);
284 # ///////////////////////////////////////////////////////////////////////////
285 # // SENF_PARSER_SKIP_*
287 # define SENF_PARSER_SKIP_var(bytes, ibytes) \
288 SENF_PARSER_I_SKIP( BOOST_PP_CAT(senf_anon_, __LINE__), bytes, ibytes, var)
290 # define SENF_PARSER_SKIP_fix(bytes) \
291 SENF_PARSER_I_SKIP( BOOST_PP_CAT(senf_anon_, __LINE__), bytes, bytes, fix)
293 # define SENF_PARSER_I_SKIP(name, bytes, ibytes, ofstype) \
295 SENF_PARSER_I_BITFIELD_RESET() \
296 SENF_PARSER_I_FIELD_INTRO(name, void, private) \
297 SENF_PARSER_I_FIELD_INIT_ro(name, void, private) \
298 BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type, access) \
299 BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (name, void, bytes, ibytes, private) \
302 # ///////////////////////////////////////////////////////////////////////////
303 # // SENF_PARSER_SKIP_BITS_*
305 # define SENF_PARSER_SKIP_BITS_var(bits) SENF_PARSER_I_SKIP_BITS(bits, var)
306 # define SENF_PARSER_SKIP_BITS_fix(bits) SENF_PARSER_I_SKIP_BITS(bits, fix)
308 # define SENF_PARSER_I_SKIP_BITS(bits, ofstype) \
309 SENF_MPL_SLOT_SET(bit, SENF_MPL_SLOT_GET(bit) + bits)
311 # ///////////////////////////////////////////////////////////////////////////
312 # // SENF_PARSER_GOTO_*
314 # define SENF_PARSER_GOTO_var(name) \
315 SENF_PARSER_I_GOTO( BOOST_PP_CAT(senf_anon_, __LINE__), \
316 BOOST_PP_CAT(name, _offset)(), \
317 BOOST_PP_CAT(name, _init_bytes), \
320 # define SENF_PARSER_GOTO_fix(name) \
321 SENF_PARSER_I_GOTO( BOOST_PP_CAT(senf_anon_, __LINE__), \
322 BOOST_PP_CAT(name, _offset), \
323 BOOST_PP_CAT(name, _offset), \
326 # define SENF_PARSER_GOTO_OFFSET_var(offset, isize) \
327 SENF_PARSER_I_GOTO( BOOST_PP_CAT(senf_anon_, __LINE__), offset, isize, var )
329 # define SENF_PARSER_GOTO_OFFSET_fix(offset) \
330 SENF_PARSER_I_GOTO( BOOST_PP_CAT(senf_anon_, __LINE__), offset, offset, fix )
332 # define SENF_PARSER_I_GOTO(name, offset, initsize, ofstype) \
334 SENF_PARSER_I_BITFIELD_RESET() \
335 SENF_PARSER_I_FIELD_INTRO(name, void, private) \
336 SENF_PARSER_I_FIELD_INIT_ro(name, void, private) \
337 BOOST_PP_CAT( SENF_PARSER_I_GOTO_SET_OFS_, ofstype ) (name, offset, initsize) \
340 # define SENF_PARSER_I_GOTO_SET_OFS_var(name, offs, initsize) \
341 size_type field_offset_(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) const { \
344 SENF_MPL_SLOT_SET(init_bytes, initsize);
346 # define SENF_PARSER_I_GOTO_SET_OFS_fix(name, offs, initsize) \
347 SENF_MPL_SLOT_SET(offset, offs);
349 # ///////////////////////////////////////////////////////////////////////////
350 # // SENF_PARSER_LABEL_*
352 # define SENF_PARSER_LABEL_var(name) SENF_PARSER_I_LABEL( name, var, public )
353 # define SENF_PARSER_LABEL_fix(name) SENF_PARSER_I_LABEL( name, fix, public )
355 # define SENF_PARSER_I_LABEL(name, ofstype, access) \
357 SENF_PARSER_I_BITFIELD_RESET() \
358 SENF_PARSER_I_FIELD_INTRO(name, void, access) \
359 SENF_PARSER_I_FIELD_INIT_ro(name, void, access) \
360 BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type, access) \
361 BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (name, void, 0, 0,access) \
364 # ///////////////////////////////////////////////////////////////////////////
365 # // SENF_PARSER_OFFSET_*
367 # define SENF_PARSER_OFFSET_fix(name) BOOST_PP_CAT(name, _offset)
368 # define SENF_PARSER_OFFSET_var(name) BOOST_PP_CAT(name, _offset)()
370 # ///////////////////////////////////////////////////////////////////////////
371 # // SENF_PARSER_FIXED_OFFSET_*
373 # define SENF_PARSER_FIXED_OFFSET_fix(name) BOOST_PP_CAT(name, _offset)
374 # define SENF_PARSER_FIXED_OFFSET_var(name) BOOST_PP_CAT(name, _init_bytes)
376 # ///////////////////////////////////////////////////////////////////////////
377 # // SENF_PARSER_FIXED_OFFSET_*
379 # define SENF_PARSER_CURRENT_FIXED_OFFSET_fix() SENF_MPL_SLOT_GET(offset)
380 # define SENF_PARSER_CURRENT_FIXED_OFFSET_var() SENF_MPL_SLOT_GET(init_bytes)
382 # ///////////////////////////////////////////////////////////////////////////
383 # // SENF_PARSER_FINALIZE_*
385 # define SENF_PARSER_FINALIZE_var(name) \
386 SENF_PARSER_FINALIZE_GENERIC(name) \
387 size_type bytes() const { \
388 return field_offset_(static_cast<senf::mpl::rv<SENF_MPL_SLOT_GET(index)>*>(0)); \
390 static size_type const init_bytes = SENF_MPL_SLOT_GET(init_bytes)
392 # define SENF_PARSER_FINALIZE_fix(name) \
393 SENF_PARSER_FINALIZE_GENERIC(name) \
394 static size_type const fixed_bytes = SENF_MPL_SLOT_GET(offset);
396 # define SENF_PARSER_FINALIZE_GENERIC(name) \
397 void defaultInit() { \
398 init_chain(static_cast<senf::mpl::rv<SENF_MPL_SLOT_GET(index)>*>(0)); \
400 name(data_iterator i, state_type s) : parser_base_type(i,s) {} \
402 template <class T> void init(T) { defaultInit(); } \
404 void init() { init(0); }
406 # ////////////////////////////////ih.e///////////////////////////////////////
410 # // Local Variables:
412 # // fill-column: 100
413 # // c-file-style: "senf"
414 # // indent-tabs-mode: nil
415 # // ispell-local-dictionary: "american"
416 # // compile-command: "scons -u test"