Utils/Logger: Implement Area and Stream registry
[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(offset,fields)                                        \
69      size_type offset_0_() const { return offset; }                                               \
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(offset,fields)                                  \
86      static const size_type offset_0_ = offset;                                                   \
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      typedef type BOOST_PP_CAT(name,_t) ;                                                         \
97      type name () const {                                                                         \
98          return parse< type >( BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) () );                      \
99      }                                                                                            \
100      size_type BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_INC(n),_)) () const {                   \
101          return BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) () + senf::bytes( name () );              \
102      }                                                                                            \
103      size_type BOOST_PP_CAT(name, _offset) () const {                                             \
104          return BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) () ;                                      \
105      }
106
107 # define SENF_PACKET_PARSER_I_FIXED_Field(n,name,type)                                            \
108      typedef type BOOST_PP_CAT(name,_t) ;                                                         \
109      type name () const {                                                                         \
110          return parse< type >( BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) );                         \
111      }                                                                                            \
112      static const size_type BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_INC(n),_)) =               \
113          BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) + type::fixed_bytes;                             \
114      static const size_type BOOST_PP_CAT(name,_offset) =                                          \
115          BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_));
116
117 # define SENF_PACKET_PARSER_I_OverlayField(n,name,type)                                           \
118      typedef type BOOST_PP_CAT(name,_t) ;                                                         \
119      type name () const {                                                                         \
120          return parse< type >( BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) () );                      \
121      }                                                                                            \
122      size_type BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_INC(n),_)) () const {                   \
123          return BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) ();                                       \
124      }                                                                                            \
125      size_type BOOST_PP_CAT(name, _offset) () const {                                             \
126          return BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) () ;                        \
127      }
128
129 # define SENF_PACKET_PARSER_I_FIXED_OverlayField(n,name,type)                                     \
130      typedef type BOOST_PP_CAT(name,_t) ;                                                         \
131      type name () const {                                                                         \
132          return parse< type >( BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_)) );                         \
133      }                                                                                            \
134      static const size_type BOOST_PP_CAT(offset_,BOOST_PP_CAT(BOOST_PP_INC(n),_)) =               \
135          BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_));                                                 \
136      static const size_type BOOST_PP_CAT(name,_offset) =                                          \
137          BOOST_PP_CAT(offset_,BOOST_PP_CAT(n,_));
138
139 # ///////////////////////////////mpp.e///////////////////////////////////////
140 # endif
141
142\f
143 # // Local Variables:
144 # // mode: c++
145 # // fill-column: 100
146 # // c-file-style: "senf"
147 # // indent-tabs-mode: nil
148 # // ispell-local-dictionary: "american"
149 # // compile-command: "scons -u test"
150 # // End: