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