removed some useless spaces; not very important, I know :)
[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_ListBParser_
27 #define IH_ListBParser_ 1
28
29 // Custom includes
30
31 ///////////////////////////////ih.p////////////////////////////////////////
32
33 namespace senf {
34 namespace detail {
35
36     /** \brief Internal: ListPolicy defing the ListBParser parser
37         \internal
38         \see \ref ListBParser
39      */
40     template <class ElementParser, class BytesParser>
41     struct ListBParser_Policy
42     {
43         // This policy needs to work around a serious problem with this type of list: When we change
44         // the size of any (direct or indirect) subelement of the list, This will change will render
45         // the list completely invalid and unparseable since the 'byte' field will now be invalid.
46         //
47         // The solution we apply is to store the *size* (i.e. the number of elements) of the list
48         // when creating the container wrapper. We also maintain this value accross insert/erase
49         // statements. Additionally we also safe the complete size of the data container (the vector
50         // holding the bytes). Since we only allow packet changes through this container while it
51         // exists, any change in the container size must be a change within this list and therefore
52         // mandates an update of the 'bytes' field.
53         //
54         // The list container wrapper will call 'update' policy member before every access to the
55         // container and also in the destructor. This gives us a chance to fix the bytes header
56         // before the invalid header is seen by anyone (This is so, since we only allow access to
57         // the list through the container wrapper ...). Since we know the number of list elements,
58         // we can always find the correct 'bytes' value by traversing the list for that number of
59         // elements.
60         //
61         // By caching the container size, all this can be made reasonably efficient and usable: The
62         // updates are done automatically by only if needed. It would of course be more efficient to
63         // just apply the size change directly to the bytes header (no need to traverse the
64         // list). However, the implementation of this approach would be much more complex and even
65         // more invasive and would probably suffer from the same restrictions to the user.
66
67         struct container_policy;
68
69         typedef PacketParserBase::data_iterator iterator;
70         typedef PacketParserBase::state_type state_type;
71         typedef PacketParserBase::size_type size_type;
72         typedef ElementParser element_type;
73         typedef ListParser< ListBParser_Policy > parser_type;
74         typedef ListParser_Container< container_policy > container_type;
75
76         static const size_type init_bytes = BytesParser::fixed_bytes;
77
78         size_type bytes  (iterator i, state_type s) const;
79         size_type size   (iterator i, state_type s) const;
80         void      init   (iterator i, state_type s) const;
81
82         /** \brief Internal: ListBParser_Policy's iterator policy
83             \internal
84          */
85         struct iterator_policy
86         {
87             iterator setBegin        (iterator i, state_type s);
88             iterator setEnd          (iterator i, state_type s);
89             void     setFromPosition (iterator i, state_type s, iterator p);
90             iterator next            (iterator i, state_type s);
91             iterator raw             (iterator i, state_type s) const;
92         };
93
94         /** \brief Internal: ListBParser_Policy's container policy
95             \internal
96          */
97         struct container_policy
98         {
99             typedef typename ListBParser_Policy<
100                 ElementParser,BytesParser>::iterator_policy iterator_policy;
101             typedef typename ListBParser_Policy<
102                 ElementParser,BytesParser>::parser_type parser_type;
103             typedef typename ListBParser_Policy<
104                 ElementParser,BytesParser>::element_type element_type;
105
106             static const size_type init_bytes = ListBParser_Policy<
107                 ElementParser,BytesParser>::init_bytes;
108
109             container_policy(parser_type const & list);
110
111             size_type bytes  (iterator i, state_type s) const;
112             size_type size   (iterator i, state_type s) const;
113             void      erase  (iterator i, state_type s, iterator p);
114             void      insert (iterator i, state_type s, iterator p);
115             void      init   (iterator i, state_type s);
116             void      update (iterator i, state_type s) const;
117
118             size_type n_;
119             mutable size_type container_size_;
120         };
121     };
122
123 }}
124
125 ///////////////////////////////ih.e////////////////////////////////////////
126 #endif
127
128 \f
129 // Local Variables:
130 // mode: c++
131 // fill-column: 100
132 // comment-column: 40
133 // c-file-style: "senf"
134 // indent-tabs-mode: nil
135 // ispell-local-dictionary: "american"
136 // compile-command: "scons -u test"
137 // End: