SafeIterator.cci
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 SafeIterator inline non-template implementation */
16 
17 #include "SafeIterator.ih"
18 
19 // Custom includes
20 
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::safe_data_iterator
26 
27 prefix_ senf::safe_data_iterator::safe_data_iterator()
28 {}
29 
30 prefix_ senf::safe_data_iterator::safe_data_iterator(PacketData & data)
31  : Base(&data)
32 {}
33 
34 prefix_ senf::safe_data_iterator::safe_data_iterator(PacketData & data, PacketData::iterator i)
35  : Base(&data, std::distance(data.begin(),i))
36 {}
37 
38 prefix_ senf::safe_data_iterator::safe_data_iterator(PacketParserBase const & parser)
39  : Base(&parser.data(), std::distance(parser.data().begin(),parser.i()))
40 {}
41 
42 prefix_ senf::safe_data_iterator & senf::safe_data_iterator::operator=(PacketData::iterator i)
43 {
44  SENF_ASSERT(data_, "Use/dereferencing of empty safe_data_iterator");
45  i_ = std::distance(data_->begin(),i);
46  return *this;
47 }
48 
49 prefix_ senf::safe_data_iterator &
50 senf::safe_data_iterator::operator=(PacketParserBase const & parser)
51 {
52  data_ = &parser.data();
53  i_ = std::distance(data_->begin(),parser.i());
54  return *this;
55 }
56 
57 prefix_ senf::PacketData & senf::safe_data_iterator::data()
58  const
59 {
60  SENF_ASSERT(data_, "Use/dereferencing of empty safe_data_iterator");
61  return *data_;
62 }
63 
64 prefix_ senf::safe_data_iterator::value_type & senf::safe_data_iterator::dereference()
65  const
66 {
67  return * static_cast<PacketData::iterator>(*this);
68 }
69 
70 prefix_ bool senf::safe_data_iterator::equal(safe_data_iterator const & other)
71  const
72 {
73  SENF_ASSERT(data_ == other.data_, "Comparing iterators of two different containers is invalid");
74  return i_ == other.i_;
75 }
76 
77 prefix_ senf::safe_data_iterator::difference_type
78 senf::safe_data_iterator::distance_to(safe_data_iterator const & other)
79  const
80 {
81  SENF_ASSERT(data_ == other.data_, "Comparing iterators of two different containers is invalid");
82  return other.i_ - i_;
83 }
84 
85 prefix_ void senf::safe_data_iterator::increment()
86 {
87  ++i_;
88 }
89 
90 prefix_ void senf::safe_data_iterator::decrement()
91 {
92  SENF_ASSERT(i_>0, "Decrementing iterator before beginning of container" );
93  --i_;
94 }
95 
96 prefix_ void senf::safe_data_iterator::advance(difference_type n)
97 {
98  SENF_ASSERT( -n < difference_type(i_), "Moving iterator to before beginning of container" );
99  i_ += n;
100 }
101 
102 //-/////////////////////////////////////////////////////////////////////////////////////////////////
103 #undef prefix_
104 
105 
106 // Local Variables:
107 // mode: c++
108 // fill-column: 100
109 // comment-column: 40
110 // c-file-style: "senf"
111 // indent-tabs-mode: nil
112 // ispell-local-dictionary: "american"
113 // compile-command: "scons -u test"
114 // End: