2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
15 \brief ListBParser internal header */
17 #ifndef IH_SENF_Packets_ListBParser_
18 #define IH_SENF_Packets_ListBParser_ 1
21 #include "ListParser.ih"
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 /** \brief Internal: ListPolicy defing the ListBParser parser
32 template <class ElementParser, class AuxPolicy>
33 struct ListBParser_Policy
36 // This policy needs to work around a serious problem with this type of list: When we change
37 // the size of any (direct or indirect) sub-element of the list, this will change will
38 // render the list completely invalid and un-parseable since the 'byte' field will now be
41 // The solution we apply is to store the *size* (i.e. the number of elements) of the list
42 // when creating the container wrapper. We also maintain this value across insert/erase
43 // statements. Additionally we also safe the complete size of the data container (the vector
44 // holding the bytes). Since we only allow packet changes through this container while it
45 // exists, any change in the container size must be a change within this list and therefore
46 // mandates an update of the 'bytes' field.
48 // The list container wrapper will call 'update' policy member before every access to the
49 // container and also in the destructor. This gives us a chance to fix the bytes header
50 // before the invalid header is seen by anyone (This is so, since we only allow access to
51 // the list through the container wrapper ...). Since we know the number of list elements,
52 // we can always find the correct 'bytes' value by traversing the list for that number of
55 // By caching the container size, all this can be made reasonably efficient and usable: The
56 // updates are done automatically by only if needed. It would of course be more efficient to
57 // just apply the size change directly to the bytes header (no need to traverse the
58 // list). However, the implementation of this approach would be much more complex and even
59 // more invasive and would probably suffer from the same restrictions to the user.
61 struct container_policy;
63 typedef PacketParserBase::data_iterator data_iterator;
64 typedef PacketParserBase::state_type state_type;
65 typedef PacketParserBase::size_type size_type;
67 typedef ElementParser element_type;
68 typedef ListParser<ListBParser_Policy> parser_type;
69 typedef ListParser_Container<container_policy> container_type;
71 static const size_type init_bytes = AuxPolicy::aux_bytes;
74 template <class Arg> ListBParser_Policy(Arg const & arg);
76 size_type bytes (data_iterator i, state_type s) const;
77 size_type size (data_iterator i, state_type s) const;
78 void init (data_iterator i, state_type s) const;
80 /** \brief Internal: ListBParser container/wrapper policy */
81 struct container_policy
82 : public AuxPolicy::WrapperPolicy
84 typedef PacketParserBase::data_iterator data_iterator;
85 typedef PacketParserBase::state_type state_type;
86 typedef PacketParserBase::size_type size_type;
88 typedef ListBParser_Policy<ElementParser, AuxPolicy> parser_policy;
89 typedef typename parser_policy::element_type element_type;
90 typedef typename parser_policy::parser_type parser_type;
91 typedef typename parser_policy::container_type container_type;
93 static const size_type init_bytes = parser_policy::init_bytes;
95 container_policy(parser_policy const & p);
97 size_type bytes (data_iterator i, state_type s) const;
98 size_type size (data_iterator i, state_type s) const;
99 void init (data_iterator i, state_type s);
101 void construct (container_type & c) const;
102 void destruct (container_type & c) const;
103 void erase (container_type & c, data_iterator p);
104 void insert (container_type & c, data_iterator p);
105 void update (container_type const & c) const;
107 /** \brief Internal: ListBParser specific iterator data */
108 struct iterator_data {};
110 data_iterator setBegin (container_type const & c, iterator_data & d) const;
111 data_iterator setEnd (container_type const & c, iterator_data & d) const;
112 void setFromPosition (container_type const & c, iterator_data & d,
113 data_iterator p) const;
115 data_iterator next (container_type const & c, iterator_data & d) const;
116 data_iterator raw (container_type const & c, iterator_data const & d) const;
119 mutable size_type container_size_;
125 template <class ElementParser, class AuxPolicy>
126 struct ListParserPolicy<ElementParser, AuxPolicy, senf::detail::auxtag::bytes>
128 typedef ListBParser_Policy<ElementParser, AuxPolicy> type;
131 template <class ElementParser, class AuxPolicy, class Transform>
132 struct ListParserPolicy<ElementParser, AuxPolicy,
133 senf::detail::auxtag::transform<Transform,
134 senf::detail::auxtag::bytes> >
136 typedef ListBParser_Policy< ElementParser,
137 TransformAuxParserPolicy<AuxPolicy, Transform> > type;
144 //-/////////////////////////////////////////////////////////////////////////////////////////////////
151 // comment-column: 40
152 // c-file-style: "senf"
153 // indent-tabs-mode: nil
154 // ispell-local-dictionary: "american"
155 // compile-command: "scons -u test"