4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief ListBParser internal header */
26 #ifndef IH_SENF_Packets_ListBParser_
27 #define IH_SENF_Packets_ListBParser_ 1
30 #include "ListParser.ih"
32 ///////////////////////////////ih.p////////////////////////////////////////
37 /** \brief Internal: ListPolicy defing the ListBParser parser
41 template <class ElementParser, class AuxPolicy>
42 struct ListBParser_Policy
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
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.
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
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.
70 struct container_policy;
72 typedef PacketParserBase::data_iterator data_iterator;
73 typedef PacketParserBase::state_type state_type;
74 typedef PacketParserBase::size_type size_type;
76 typedef ElementParser element_type;
77 typedef ListParser< ListBParser_Policy > parser_type;
78 typedef ListParser_Container< container_policy > container_type;
80 static const size_type init_bytes = AuxPolicy::aux_bytes;
83 template <class Arg> ListBParser_Policy(Arg const & arg);
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;
89 /** \brief Internal: ListBParser container/wrapper policy */
90 struct container_policy
93 typedef PacketParserBase::data_iterator data_iterator;
94 typedef PacketParserBase::state_type state_type;
95 typedef PacketParserBase::size_type size_type;
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;
102 static const size_type init_bytes = parser_policy::init_bytes;
104 container_policy(parser_policy const & p);
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);
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;
114 /** \brief Internal: ListBParser specific iterator data */
115 struct iterator_data {};
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;
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;
126 mutable size_type container_size_;
132 template <class ElementParser, class AuxPolicy>
133 struct ListParserPolicy<ElementParser, AuxPolicy, senf::detail::auxtag::bytes>
135 typedef ListBParser_Policy<ElementParser, AuxPolicy> type;
138 template <class ElementParser, class AuxPolicy, class Transform>
139 struct ListParserPolicy<ElementParser, AuxPolicy,
140 senf::detail::auxtag::transform<Transform,
141 senf::detail::auxtag::bytes> >
143 typedef ListBParser_Policy< ElementParser,
144 TransformAuxParserPolicy<AuxPolicy, Transform> > type;
151 ///////////////////////////////ih.e////////////////////////////////////////
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"