a69e6f9ee1e7bda34e881ef75695024c2dd23529
[senf.git] / senf / 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 ListParser
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::WrapperPolicy
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      construct (container_type & c) const;
111             void      destruct  (container_type & c) const;
112             void      erase     (container_type & c, data_iterator p);
113             void      insert    (container_type & c, data_iterator p);
114             void      update    (container_type const & c) const;
115
116             /** \brief Internal: ListBParser specific iterator data */
117             struct iterator_data {};
118
119             data_iterator setBegin        (container_type const & c, iterator_data & d) const;
120             data_iterator setEnd          (container_type const & c, iterator_data & d) const;
121             void          setFromPosition (container_type const & c, iterator_data & d,
122                                            data_iterator p) const;
123
124             data_iterator next            (container_type const & c, iterator_data & d) const;
125             data_iterator raw             (container_type const & c, iterator_data const & d) const;
126
127             size_type n_;
128             mutable size_type container_size_;
129         };
130     };
131
132 #ifndef DOXYGEN
133
134     template <class ElementParser, class AuxPolicy>
135     struct ListParserPolicy<ElementParser, AuxPolicy, senf::detail::auxtag::bytes>
136     {
137         typedef ListBParser_Policy<ElementParser, AuxPolicy> type;
138     };
139
140     template <class ElementParser, class AuxPolicy, class Transform>
141     struct ListParserPolicy<ElementParser, AuxPolicy,
142                             senf::detail::auxtag::transform<Transform,
143                                                             senf::detail::auxtag::bytes> >
144     {
145         typedef ListBParser_Policy< ElementParser,
146                                     TransformAuxParserPolicy<AuxPolicy, Transform> > type;
147     };
148
149 #endif
150
151 }}
152
153 ///////////////////////////////ih.e////////////////////////////////////////
154 #endif
155
156 \f
157 // Local Variables:
158 // mode: c++
159 // fill-column: 100
160 // comment-column: 40
161 // c-file-style: "senf"
162 // indent-tabs-mode: nil
163 // ispell-local-dictionary: "american"
164 // compile-command: "scons -u test"
165 // End: