4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief ListBParser internal header */
31 #ifndef IH_SENF_Packets_ListBParser_
32 #define IH_SENF_Packets_ListBParser_ 1
35 #include "ListParser.ih"
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 /** \brief Internal: ListPolicy defing the ListBParser parser
46 template <class ElementParser, class AuxPolicy>
47 struct ListBParser_Policy
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
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.
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
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.
75 struct container_policy;
77 typedef PacketParserBase::data_iterator data_iterator;
78 typedef PacketParserBase::state_type state_type;
79 typedef PacketParserBase::size_type size_type;
81 typedef ElementParser element_type;
82 typedef ListParser< ListBParser_Policy > parser_type;
83 typedef ListParser_Container< container_policy > container_type;
85 static const size_type init_bytes = AuxPolicy::aux_bytes;
88 template <class Arg> ListBParser_Policy(Arg const & arg);
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;
94 /** \brief Internal: ListBParser container/wrapper policy */
95 struct container_policy
96 : public AuxPolicy::WrapperPolicy
98 typedef PacketParserBase::data_iterator data_iterator;
99 typedef PacketParserBase::state_type state_type;
100 typedef PacketParserBase::size_type size_type;
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;
107 static const size_type init_bytes = parser_policy::init_bytes;
109 container_policy(parser_policy const & p);
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);
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;
121 /** \brief Internal: ListBParser specific iterator data */
122 struct iterator_data {};
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;
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;
133 mutable size_type container_size_;
139 template <class ElementParser, class AuxPolicy>
140 struct ListParserPolicy<ElementParser, AuxPolicy, senf::detail::auxtag::bytes>
142 typedef ListBParser_Policy<ElementParser, AuxPolicy> type;
145 template <class ElementParser, class AuxPolicy, class Transform>
146 struct ListParserPolicy<ElementParser, AuxPolicy,
147 senf::detail::auxtag::transform<Transform,
148 senf::detail::auxtag::bytes> >
150 typedef ListBParser_Policy< ElementParser,
151 TransformAuxParserPolicy<AuxPolicy, Transform> > type;
158 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"