scenario_simulator_v2 C++ API
circular_iterator.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef OPENSCENARIO_INTERPRETER__ITERATOR__CIRCULAR_ITERATOR_HPP_
16 #define OPENSCENARIO_INTERPRETER__ITERATOR__CIRCULAR_ITERATOR_HPP_
17 
18 #include <iterator>
19 #include <type_traits>
20 
22 {
23 inline namespace iterator
24 {
25 template <typename Container>
27 {
28  using ForwardIterator = typename Container::iterator;
29  using ForwardConstIterator = typename Container::const_iterator;
30 
31  const ForwardIterator begin, end;
32  ForwardIterator current;
33 
34 public:
35  using iterator_category = std::forward_iterator_tag;
36 
37  using value_type = typename std::iterator_traits<ForwardIterator>::value_type;
38 
39  using reference = typename std::add_lvalue_reference<value_type>::type;
40 
41  using const_reference = typename std::add_const<reference>::type;
42 
43  using pointer = typename std::iterator_traits<ForwardIterator>::pointer;
44 
45  using difference_type = typename std::iterator_traits<ForwardIterator>::difference_type;
46 
47  explicit CircularIterator(ForwardIterator begin, ForwardIterator end, ForwardIterator current)
48  : begin(begin), end(end), current(current)
49  {
50  }
51 
52  CircularIterator & operator=(const ForwardIterator & iterator)
53  {
54  current = iterator;
55  return *this;
56  }
57 
58  operator ForwardConstIterator() const { return current; }
59  operator ForwardIterator() { return current; }
60 
61  reference operator*() const { return *current; }
62 
63  auto & operator++()
64  {
65  if (current == end) {
66  current = begin;
67  } else if (++current == end) {
68  current = begin;
69  }
70 
71  return *this;
72  }
73 
74  auto operator++(int)
75  {
76  auto copy = *this;
77  operator++();
78  return copy;
79  }
80 };
81 } // namespace iterator
82 } // namespace openscenario_interpreter
83 
84 #endif // OPENSCENARIO_INTERPRETER__ITERATOR__CIRCULAR_ITERATOR_HPP_
Definition: circular_iterator.hpp:27
typename std::iterator_traits< ForwardIterator >::value_type value_type
Definition: circular_iterator.hpp:37
auto operator++(int)
Definition: circular_iterator.hpp:74
std::forward_iterator_tag iterator_category
Definition: circular_iterator.hpp:35
auto & operator++()
Definition: circular_iterator.hpp:63
typename std::add_lvalue_reference< value_type >::type reference
Definition: circular_iterator.hpp:39
reference operator*() const
Definition: circular_iterator.hpp:61
typename std::iterator_traits< ForwardIterator >::pointer pointer
Definition: circular_iterator.hpp:43
CircularIterator(ForwardIterator begin, ForwardIterator end, ForwardIterator current)
Definition: circular_iterator.hpp:47
CircularIterator & operator=(const ForwardIterator &iterator)
Definition: circular_iterator.hpp:52
typename std::iterator_traits< ForwardIterator >::difference_type difference_type
Definition: circular_iterator.hpp:45
typename std::add_const< reference >::type const_reference
Definition: circular_iterator.hpp:41
Definition: escape_sequence.hpp:22