Packets: Complete reimplementation of 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 <boost/preprocessor/facilities/expand.hpp>
30 # include "../Utils/mpl.hh"
31 #
32 # ////////////////////////////////ih.p///////////////////////////////////////
33 #
34 # define SENF_PARSER_I_FIELD_INTRO(name, type)                                                    \
35         typedef type BOOST_PP_CAT(name, _t);                                                      \
36         static size_type const BOOST_PP_CAT(name,_index) = SENF_MPL_SLOT_GET(index)+1;            \
37     private:                                                                                      \
38         SENF_MPL_SLOT_SET(index, BOOST_PP_CAT(name,_index));                                      \
39     public:
40 #
41 # define SENF_PARSER_I_FIELD_INIT_rw(name, type)                                                  \
42     private:                                                                                      \
43         void init_chain(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) {                              \
44             init_chain(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0));              \
45             name().init();                                                                        \
46         }                                                                                         \
47     public:
48 #
49 # define SENF_PARSER_I_FIELD_INIT_ro(name, type)                                                  \
50     private:                                                                                      \
51         void init_chain(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) {                              \
52             init_chain(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0));              \
53         }                                                                                         \
54     public:
55 #
56 # define SENF_PARSER_I_SIZE_var(name, type) senf::bytes(name())
57 # define SENF_PARSER_I_INITBYTES_var(name, type) senf::init_bytes<type>::value
58 #
59 # define SENF_PARSER_I_ADVANCE_OFS_var(name, type, size, isize)                                   \
60     private:                                                                                      \
61         size_type BOOST_PP_CAT(name, _next_offset)() const {                                      \
62             return BOOST_PP_CAT(name,_offset)() + size;                                           \
63         }                                                                                         \
64         size_type field_offset_(senf::mpl::rv<BOOST_PP_CAT(name,_index)>*) const {                \
65             return BOOST_PP_CAT(name, _next_offset)();                                            \
66         }                                                                                         \
67         SENF_MPL_SLOT_SET(init_bytes, SENF_MPL_SLOT_GET(init_bytes) + isize);                     \
68     public:
69 #
70 # define SENF_PARSER_I_FIELD_OFS_var(name, type)                                                  \
71         size_type BOOST_PP_CAT(name,_offset)() const {                                            \
72             return field_offset_(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0));    \
73         }
74 #
75 # define SENF_PARSER_I_FIELD_OFS_AFTER_var(name, type, prev)                                      \
76         size_type BOOST_PP_CAT(name,_offset)() const {                                            \
77             return BOOST_PP_CAT(prev,_next_offset)();                                             \
78         }
79 #
80 # define SENF_PARSER_I_SIZE_fix(name, type) type::fixed_bytes
81 # define SENF_PARSER_I_INITBYTES_fix(name, type) void
82 #
83 # define SENF_PARSER_I_ADVANCE_OFS_fix(name, type, size, isize)                                   \
84     private:                                                                                      \
85         static size_type const BOOST_PP_CAT(name, _next_offset) =                                 \
86             BOOST_PP_CAT(name, _offset) + size;                                                   \
87         SENF_MPL_SLOT_SET(offset, BOOST_PP_CAT(name, _next_offset));                              \
88     public:
89 #
90 # define SENF_PARSER_I_FIELD_OFS_fix(name, type)                                                  \
91         static size_type const BOOST_PP_CAT(name, _offset) = SENF_MPL_SLOT_GET(offset);
92 #
93 # define SENF_PARSER_I_FIELD_OFS_AFTER_fix(name, type, prev)                                      \
94         static size_type const BOOST_PP_CAT(name, _offset) = BOOST_PP_CAT(prev, _next_offset);
95 #
96 # define SENF_PARSER_I_MAYBECALL_var() ()
97 # define SENF_PARSER_I_MAYBECALL_fix()
98 #
99 # define SENF_PARSER_I_FIELD_VAL_rw(name,type,ofstype)                                            \
100         BOOST_PP_CAT(name, _t) name() const {                                                     \
101             return parse<type>(                                                                   \
102                 BOOST_PP_CAT(name,_offset) BOOST_PP_CAT(SENF_PARSER_I_MAYBECALL_, ofstype)());    \
103         }
104 #
105 # define SENF_PARSER_I_FIELD_VAL_ro(name,type,ofstype)                                            \
106         BOOST_PP_CAT(name, _t)::value_type name() const {                                         \
107             return parse<type>(                                                                   \
108                 BOOST_PP_CAT(name,_offset) BOOST_PP_CAT(SENF_PARSER_I_MAYBECALL_, ofstype)()      \
109                     .value()                                                                      \
110         }
111 #
112 # define SENF_PARSER_FIELD_I(name, type, ofstype, rwtype)                                         \
113         SENF_PARSER_I_BITFIELD_RESET()                                                            \
114         SENF_PARSER_I_FIELD_INTRO(name, type)                                                     \
115         BOOST_PP_CAT(SENF_PARSER_I_FIELD_INIT_, rwtype) (name, type)                              \
116         BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type)                              \
117         BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (                                       \
118             name, type,                                                                           \
119             BOOST_PP_CAT(SENF_PARSER_I_SIZE_, ofstype) (name, type),                              \
120             BOOST_PP_CAT(SENF_PARSER_I_INITBYTES_, ofstype) (name, type) )                        \
121         BOOST_PP_CAT(SENF_PARSER_I_FIELD_VAL_, rwtype) (name, type, ofstype)
122 #
123 # define SENF_PARSER_FIELD_I_AFTER(name, type, prev, ofstype, rwtype)                             \
124         SENF_PARSER_I_BITFIELD_RESET()                                                            \
125         SENF_PARSER_I_FIELD_INTRO(name, type)                                                     \
126         BOOST_PP_CAT(SENF_PARSER_I_FIELD_INIT_, rwtype) (name, type)                              \
127         BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_AFTER_, ofstype) (name, type, prev);                 \
128         BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (                                       \
129             name, type,                                                                           \
130             BOOST_PP_CAT(SENF_PARSER_I_SIZE_, ofstype) (name, type),                              \
131             BOOST_PP_CAT(SENF_PARSER_I_INITBYTES_, ofstype) (name, type) )                        \
132         BOOST_PP_CAT(SENF_PARSER_I_FIELD_VAL_, rwtype) (name, type, ofstype)
133 #
134 # define SENF_PARSER_CUSTOM_FIELD_I(name, type, size, isize, ofstype)                             \
135         SENF_PARSER_I_BITFIELD_RESET()                                                            \
136         SENF_PARSER_I_FIELD_INTRO(name, type)                                                     \
137         SENF_PARSER_I_FIELD_INIT_ro(name, type)                                                   \
138         BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_, ofstype) (name, type)                              \
139         BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (name, type, size, isize)               \
140         BOOST_PP_CAT(name, _t) name() const
141 #
142 # define SENF_PARSER_CUSTOM_FIELD_I_AFTER(name, type, size, isize, prev, ofstype)                 \
143         SENF_PARSER_I_BITFIELD_RESET()                                                            \
144         SENF_PARSER_I_FIELD_INTRO(name, type)                                                     \
145         SENF_PARSER_I_FIELD_INIT_ro(name, type)                                                   \
146         BOOST_PP_CAT(SENF_PARSER_I_FIELD_OFS_AFTER, ofstype) (name, type, prev)                   \
147         BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (name, type, size)                      \
148         BOOST_PP_CAT(name, _t) name() const
149 #
150 # define SENF_PARSER_I_BITFIELD_RESET()                                                           \
151         SENF_MPL_SLOT_SET(bit, 0);                                                                \
152         SENF_MPL_SLOT_SET(bitfield_size, 0);
153 #
154 # define SENF_PARSER_BITFIELD_I(name, bits, type, ofstype, rwtype)                                \
155         static size_type const BOOST_PP_CAT(name, _bit) = SENF_MPL_SLOT_GET(bit);                 \
156     private:                                                                                      \
157         SENF_MPL_SLOT_SET(bit, BOOST_PP_CAT(name, _bit) + bits);                                  \
158         typedef BOOST_PP_CAT(SENF_PARSER_BITFIELD_TYPE_, type)( BOOST_PP_CAT(name, _bit), bits )  \
159              BOOST_PP_CAT(name,_bit_t );                                                          \
160     public:                                                                                       \
161         SENF_PARSER_BITFIELD_II( name, bits, BOOST_PP_CAT(name, _bit_t), ofstype, rwtype)
162 #
163 # define SENF_PARSER_BITFIELD_TYPE_signed(start, bits)   senf::Parse_IntField<start, start+bits>
164 # define SENF_PARSER_BITFIELD_TYPE_unsigned(start, bits) senf::Parse_UIntField<start, start+bits>
165 # define SENF_PARSER_BITFIELD_TYPE_bool(start, bits)     senf::Parse_Flag<start>
166 #
167 # define SENF_PARSER_BITFIELD_II(name, bits, type, ofstype, rwtype)                               \
168          SENF_PARSER_I_FIELD_INTRO(name, type)                                                    \
169          SENF_PARSER_I_FIELD_INIT_ro(name, type)                                                  \
170          BOOST_PP_CAT(SENF_PARSER_I_BITFIELD_OFS_, ofstype) (name, type)                          \
171          BOOST_PP_CAT(SENF_PARSER_I_ADVANCE_OFS_, ofstype) (                                      \
172              name, type,                                                                          \
173              BOOST_PP_CAT(name, _t)::fixed_bytes, BOOST_PP_CAT(name, _t)::fixed_bytes )           \
174     private:                                                                                      \
175          SENF_MPL_SLOT_SET(bitfield_size, BOOST_PP_CAT(name, _t)::fixed_bytes);                   \
176     public:                                                                                       \
177          BOOST_PP_CAT(SENF_PARSER_I_FIELD_VAL_, rwtype) (name, type, ofstype)
178 #
179 # define SENF_PARSER_I_BITFIELD_OFS_var(name, type)                                               \
180         size_type BOOST_PP_CAT(name,_offset)() const {                                            \
181             return field_offset_(static_cast<senf::mpl::rv<BOOST_PP_CAT(name,_index)-1>*>(0))     \
182                 - SENF_MPL_SLOT_GET(bitfield_size);                                               \
183         }
184 #
185 # define SENF_PARSER_I_BITFIELD_OFS_fix(name, type)                                               \
186         static size_type const BOOST_PP_CAT(name, _offset) = SENF_MPL_SLOT_GET(offset)            \
187             - SENF_MPL_SLOT_GET(bitfield_size);
188 #
189 # define SENF_PARSER_INHERIT_I(name)                                                              \
190     private:                                                                                      \
191         SENF_MPL_SLOT_SET(index, 1);                                                              \
192         SENF_MPL_SLOT_SET(init_bytes, senf::init_bytes<name>::value);                             \
193         size_type field_offset_(senf::mpl::rv<1>*) const {                                        \
194             return senf::bytes( *static_cast<name*>(this) );                                      \
195         }                                                                                         \
196         void init_chain(senf::mpl::rv<inherit_index_>*) {                                         \
197             name::init();                                                                         \
198         }                                                                                         \
199     public:
200 #
201 # define SENF_PARSER_FIXED_INHERIT_I(name)                                                        \
202     private:                                                                                      \
203         SENF_MPL_SLOT_SET(offset, name::fixed_bytes);                                             \
204         SENF_MPL_SLOT_SET(index, 1);                                                              \
205         void init_chain(senf::mpl::rv<1>*) {                                                      \
206             name::init();                                                                         \
207         }                                                                                         \
208     public:
209 #
210 #
211 # ////////////////////////////////ih.e///////////////////////////////////////
212 # endif
213 #
214 #
215 # // Local Variables:
216 # // mode: c++
217 # // fill-column: 100
218 # // c-file-style: "senf"
219 # // indent-tabs-mode: nil
220 # // ispell-local-dictionary: "american"
221 # // compile-command: "scons -u test"
222 # // End: