ListBParser.ih
Go to the documentation of this file.
1 //
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
6 //
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
11 //
12 
13 
14 /** \file
15  \brief ListBParser internal header */
16 
17 #ifndef IH_SENF_Packets_ListBParser_
18 #define IH_SENF_Packets_ListBParser_ 1
19 
20 // Custom includes
21 #include "ListParser.ih"
22 
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 
25 namespace senf {
26 namespace detail {
27 
28  /** \brief Internal: ListPolicy defing the ListBParser parser
29  \internal
30  \see \ref ListParser
31  */
32  template <class ElementParser, class AuxPolicy>
33  struct ListBParser_Policy
34  : public AuxPolicy
35  {
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
39  // invalid.
40  //
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.
47  //
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
53  // elements.
54  //
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.
60 
61  struct container_policy;
62 
63  typedef PacketParserBase::data_iterator data_iterator;
64  typedef PacketParserBase::state_type state_type;
65  typedef PacketParserBase::size_type size_type;
66 
67  typedef ElementParser element_type;
68  typedef ListParser<ListBParser_Policy> parser_type;
69  typedef ListParser_Container<container_policy> container_type;
70 
71  static const size_type init_bytes = AuxPolicy::aux_bytes;
72 
73  ListBParser_Policy();
74  template <class Arg> ListBParser_Policy(Arg const & arg);
75 
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;
79 
80  /** \brief Internal: ListBParser container/wrapper policy */
81  struct container_policy
82  : public AuxPolicy::WrapperPolicy
83  {
84  typedef PacketParserBase::data_iterator data_iterator;
85  typedef PacketParserBase::state_type state_type;
86  typedef PacketParserBase::size_type size_type;
87 
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;
92 
93  static const size_type init_bytes = parser_policy::init_bytes;
94 
95  container_policy(parser_policy const & p);
96 
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);
100 
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;
106 
107  /** \brief Internal: ListBParser specific iterator data */
108  struct iterator_data {};
109 
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;
114 
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;
117 
118  size_type n_;
119  mutable size_type container_size_;
120  };
121  };
122 
123 #ifndef DOXYGEN
124 
125  template <class ElementParser, class AuxPolicy>
126  struct ListParserPolicy<ElementParser, AuxPolicy, senf::detail::auxtag::bytes>
127  {
128  typedef ListBParser_Policy<ElementParser, AuxPolicy> type;
129  };
130 
131  template <class ElementParser, class AuxPolicy, class Transform>
132  struct ListParserPolicy<ElementParser, AuxPolicy,
133  senf::detail::auxtag::transform<Transform,
134  senf::detail::auxtag::bytes> >
135  {
136  typedef ListBParser_Policy< ElementParser,
137  TransformAuxParserPolicy<AuxPolicy, Transform> > type;
138  };
139 
140 #endif
141 
142 }}
143 
144 //-/////////////////////////////////////////////////////////////////////////////////////////////////
145 #endif
146 
147 
148 // Local Variables:
149 // mode: c++
150 // fill-column: 100
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"
156 // End: