fe61177aeef3954143709ccb083a1316f4e78701
[senf.git] / Packets / ListBParser.ih
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief ListBParser internal header */
25
26 #ifndef IH_SENF_Packets_ListBParser_
27 #define IH_SENF_Packets_ListBParser_ 1
28
29 // Custom includes
30 #include "ListParser.ih"
31
32 ///////////////////////////////ih.p////////////////////////////////////////
33
34 namespace senf {
35 namespace detail {
36
37     /** \brief Internal: ListPolicy defing the ListBParser parser
38         \internal
39         \see \ref ListBParser
40      */
41     template <class ElementParser, class AuxPolicy>
42     struct ListBParser_Policy
43         : public AuxPolicy
44     {
45         // This policy needs to work around a serious problem with this type of list: When we change
46         // the size of any (direct or indirect) sub-element of the list, this will change will
47         // render the list completely invalid and un-parseable since the 'byte' field will now be
48         // invalid.
49         //
50         // The solution we apply is to store the *size* (i.e. the number of elements) of the list
51         // when creating the container wrapper. We also maintain this value across insert/erase
52         // statements. Additionally we also safe the complete size of the data container (the vector
53         // holding the bytes). Since we only allow packet changes through this container while it
54         // exists, any change in the container size must be a change within this list and therefore
55         // mandates an update of the 'bytes' field.
56         //
57         // The list container wrapper will call 'update' policy member before every access to the
58         // container and also in the destructor. This gives us a chance to fix the bytes header
59         // before the invalid header is seen by anyone (This is so, since we only allow access to
60         // the list through the container wrapper ...). Since we know the number of list elements,
61         // we can always find the correct 'bytes' value by traversing the list for that number of
62         // elements.
63         //
64         // By caching the container size, all this can be made reasonably efficient and usable: The
65         // updates are done automatically by only if needed. It would of course be more efficient to
66         // just apply the size change directly to the bytes header (no need to traverse the
67         // list). However, the implementation of this approach would be much more complex and even
68         // more invasive and would probably suffer from the same restrictions to the user.
69
70         struct container_policy;
71
72         typedef PacketParserBase::data_iterator data_iterator;
73         typedef PacketParserBase::state_type state_type;
74         typedef PacketParserBase::size_type size_type;
75
76         typedef ElementParser element_type;
77         typedef ListParser< ListBParser_Policy > parser_type;
78         typedef ListParser_Container< container_policy > container_type;
79
80         static const size_type init_bytes = AuxPolicy::aux_bytes;
81
82         ListBParser_Policy();
83         template <class Arg> ListBParser_Policy(Arg const & arg);
84
85         size_type bytes  (data_iterator i, state_type s) const;
86         size_type size   (data_iterator i, state_type s) const;
87         void      init   (data_iterator i, state_type s) const;
88
89         /** \brief Internal: ListBParser container/wrapper policy */
90         struct container_policy
91             : public AuxPolicy
92         {
93             typedef PacketParserBase::data_iterator data_iterator;
94             typedef PacketParserBase::state_type state_type;
95             typedef PacketParserBase::size_type size_type;
96
97             typedef ListBParser_Policy<ElementParser, AuxPolicy> parser_policy;
98             typedef typename parser_policy::element_type element_type;
99             typedef typename parser_policy::parser_type parser_type;
100             typedef typename parser_policy::container_type container_type;
101
102             static const size_type init_bytes = parser_policy::init_bytes;
103
104             container_policy(parser_policy const & p);
105
106             size_type bytes  (data_iterator i, state_type s) const;
107             size_type size   (data_iterator i, state_type s) const;
108             void      init   (data_iterator i, state_type s);
109
110             void      erase  (container_type & c, data_iterator p);
111             void      insert (container_type & c, data_iterator p);
112             void      update (container_type const & c) const;
113
114             /** \brief Internal: ListBParser specific iterator data */
115             struct iterator_data {};
116
117             data_iterator setBegin        (container_type const & c, iterator_data & d) const;
118             data_iterator setEnd          (container_type const & c, iterator_data & d) const;
119             void          setFromPosition (container_type const & c, iterator_data & d,
120                                            data_iterator p) const;
121
122             data_iterator next            (container_type const & c, iterator_data & d) const;
123             data_iterator raw             (container_type const & c, iterator_data const & d) const;
124
125             size_type n_;
126             mutable size_type container_size_;
127         };
128     };
129
130 #ifndef DOXYGEN
131
132     template <class ElementParser, class AuxPolicy>
133     struct ListParserPolicy<ElementParser, AuxPolicy, senf::detail::auxtag::bytes>
134     {
135         typedef ListBParser_Policy<ElementParser, AuxPolicy> type;
136     };
137
138     template <class ElementParser, class AuxPolicy, class Transform>
139     struct ListParserPolicy<ElementParser, AuxPolicy, 
140                             senf::detail::auxtag::transform<Transform, 
141                                                             senf::detail::auxtag::bytes> >
142     {
143         typedef ListBParser_Policy< ElementParser,
144                                     TransformAuxParserPolicy<AuxPolicy, Transform> > type;
145     };
146
147 #endif
148
149 }}
150
151 ///////////////////////////////ih.e////////////////////////////////////////
152 #endif
153
154 \f
155 // Local Variables:
156 // mode: c++
157 // fill-column: 100
158 // comment-column: 40
159 // c-file-style: "senf"
160 // indent-tabs-mode: nil
161 // ispell-local-dictionary: "american"
162 // compile-command: "scons -u test"
163 // End: