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