Merged revisions 262,264-265,267-282,284-298,300-311 via svnmerge from
[senf.git] / Packets / PacketParser.mpp
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 PacketParser Boost.Preprocesser definitions */
23
24 # if !defined(MPP_PacketParser_)
25 # define MPP_PacketParser_ 1
26
27 # // Custom includes
28 # include <boost/preprocessor/seq/for_each_i.hpp>
29 # include <boost/preprocessor/seq/for_each.hpp>
30 # include <boost/preprocessor/seq/size.hpp>
31 # include <boost/preprocessor/cat.hpp>
32 # include <boost/preprocessor/inc.hpp>
33 # include <boost/preprocessor/tuple/elem.hpp>
34 # include <boost/preprocessor/facilities/expand.hpp>
35
36 # ///////////////////////////////mpp.p///////////////////////////////////////
37
38 # define SENF_PACKET_PARSER_I_UNWRAP(n,elt) (n,SENF_PACKET_PARSER_I_UNWRAP_1 elt)
39 # define SENF_PACKET_PARSER_I_UNWRAP_1(x) x,SENF_PACKET_PARSER_I_UNWRAP_2
40 # define SENF_PACKET_PARSER_I_UNWRAP_2(x,y) x,y
41
42 # define SENF_PACKET_PARSER_I_GET_NAME(elt) SENF_PACKET_PARSER_I_GET_NAME_1 elt
43 # define SENF_PACKET_PARSER_I_GET_NAME_1(x) SENF_PACKET_PARSER_I_GET_NAME_2
44 # define SENF_PACKET_PARSER_I_GET_NAME_2(name,type) name
45
46 # define SENF_PACKET_PARSER_I_GET_TYPE(elt) SENF_PACKET_PARSER_I_GET_TYPE_1 elt
47 # define SENF_PACKET_PARSER_I_GET_TYPE_1(x) SENF_PACKET_PARSER_I_GET_TYPE_2
48 # define SENF_PACKET_PARSER_I_GET_TYPE_2(name,type) type
49
50 # define SENF_PACKET_PARSER_I_DEFINE_INIT_C(_0,_1,elt)                                             \
51      SENF_PACKET_PARSER_I_GET_NAME(elt) ().init();
52
53 # define SENF_PACKET_PARSER_I_DEFINE_INIT(fields)                                                  \
54      void defaultInit() const {                                                                    \
55          BOOST_PP_SEQ_FOR_EACH( SENF_PACKET_PARSER_I_DEFINE_INIT_C, _, fields)                     \
56      }
57
58 # define SENF_PACKET_PARSER_I_FIELD_DISPATCH(n,t,name,type)                                        \
59      SENF_PACKET_PARSER_I_ ## t(n,name,type)
60
61 # define SENF_PACKET_PARSER_I_FIELD_C(_0,_1,n,elt)                                                 \
62      BOOST_PP_EXPAND(                                                                              \
63          SENF_PACKET_PARSER_I_FIELD_DISPATCH SENF_PACKET_PARSER_I_UNWRAP(n,elt))
64
65 # define SENF_PACKET_PARSER_I_INITSIZE_C(_0,_1,n,elt)                                              \
66      BOOST_PP_IF(n,+,) senf::init_bytes< SENF_PACKET_PARSER_I_GET_TYPE(elt) >::value
67 #     
68 # define SENF_PACKET_PARSER_I_DEFINE_FIELDS(fields)                                                \
69      size_type offset_0_() const { return 0; }                                                     \
70      BOOST_PP_SEQ_FOR_EACH_I(SENF_PACKET_PARSER_I_FIELD_C, _, fields)                              \
71      size_type bytes() const {                                                                     \
72          return BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_SEQ_SIZE(fields),_)) ();                \
73      }                                                                                             \
74      SENF_PACKET_PARSER_I_DEFINE_INIT(fields)                                                      \
75      static const size_type init_bytes =                                                           \
76          BOOST_PP_SEQ_FOR_EACH_I(SENF_PACKET_PARSER_I_INITSIZE_C, _, fields) ;
77
78 # define SENF_PACKET_PARSER_I_FIXED_FIELD_DISPATCH(n,t,name,type)                                  \
79      SENF_PACKET_PARSER_I_FIXED_ ## t(n,name,type)
80
81 # define SENF_PACKET_PARSER_I_FIXED_FIELD_C(_0,_1,n,elt)                                           \
82      BOOST_PP_EXPAND(                                                                              \
83          SENF_PACKET_PARSER_I_FIXED_FIELD_DISPATCH SENF_PACKET_PARSER_I_UNWRAP(n,elt))
84
85 # define SENF_PACKET_PARSER_I_DEFINE_FIXED_FIELDS(fields)                                          \
86      static const size_type offset_0_ = 0;                                                         \
87      BOOST_PP_SEQ_FOR_EACH_I(SENF_PACKET_PARSER_I_FIXED_FIELD_C, _, fields)                        \
88      static const size_type fixed_bytes =                                                          \
89          BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_SEQ_SIZE(fields),_));                          \
90      SENF_PACKET_PARSER_I_DEFINE_INIT(fields)
91
92 # ///////////////////////////////////////////////////////////////////////////
93 # // Definition of the field types
94
95 # define SENF_PACKET_PARSER_I_Field(n,name,type)                                                   \
96      type name () const {                                                                          \
97          return parse< type >( BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) () );                       \
98      }                                                                                             \
99      size_type BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_INC(n),_)) () const {                    \
100          return BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) () + senf::bytes( name () );               \
101      }
102
103 # define SENF_PACKET_PARSER_I_FIXED_Field(n,name,type)                                             \
104      type name () const {                                                                          \
105          return parse< type >( BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) );                          \
106      }                                                                                             \
107      static const size_type BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_INC(n),_)) =                \
108          BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) + type::fixed_bytes;
109
110 # define SENF_PACKET_PARSER_I_OverlayField(n,name,type)                                            \
111      type name () const {                                                                          \
112          return parse< type >( BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) () );                       \
113      }                                                                                             \
114      size_type BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_INC(n),_)) () const {                    \
115          return BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) ();                                        \
116      }
117
118 # define SENF_PACKET_PARSER_I_FIXED_OverlayField(n,name,type)                                      \
119      type name () const {                                                                          \
120          return parse< type >( BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) );                          \
121      }                                                                                             \
122      static const size_type BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_INC(n),_)) =                \
123          BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_));
124
125 # ///////////////////////////////mpp.e///////////////////////////////////////
126 # endif
127
128\f
129 # // Local Variables:
130 # // mode: c++
131 # // fill-column: 100
132 # // c-file-style: "senf"
133 # // indent-tabs-mode: nil
134 # // ispell-local-dictionary: "american"
135 # // compile-command: "scons -u test"
136 # // End: