CARLA
LibCarla/source/carla/ListView.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "carla/Debug.h"
10 
11 #include <type_traits>
12 #include <iterator>
13 
14 namespace carla {
15 
16  /// A view over a range of elements in a container. Basically a pair of begin
17  /// and end iterators.
18  template<typename IT>
19  class ListView {
20  public:
21 
22  using iterator = IT;
23  using const_iterator = typename std::add_const<IT>::type;
24  using size_type = size_t;
25  using difference_type = typename std::iterator_traits<iterator>::difference_type;
26  using value_type = typename std::iterator_traits<iterator>::value_type;
27  using pointer = typename std::iterator_traits<iterator>::pointer;
28  using reference = typename std::iterator_traits<iterator>::reference;
29 
31  : _begin(begin), _end(end) {
32  DEBUG_ASSERT(std::distance(_begin, _end) >= 0);
33  }
34 
35  ListView(const ListView &) = default;
36  ListView &operator=(const ListView &) = delete;
37 
39  return _begin;
40  }
41 
43  return _begin;
44  }
45 
47  return _begin;
48  }
49 
51  return _end;
52  }
53 
54  const_iterator end() const {
55  return _end;
56  }
57 
58  const_iterator cend() const {
59  return _end;
60  }
61 
62  bool empty() const {
63  return _begin == _end;
64  }
65 
66  size_type size() const {
67  return static_cast<size_t>(std::distance(begin(), end()));
68  }
69 
70  private:
71 
73 
74  const iterator _end;
75  };
76 
77  template <typename Iterator>
78  static inline auto MakeListView(Iterator begin, Iterator end) {
79  return ListView<Iterator>(begin, end);
80  }
81 
82  template <typename Container>
83  static inline auto MakeListView(Container &c) {
84  return MakeListView(std::begin(c), std::end(c));
85  }
86 
87 } // namespace carla
const_iterator begin() const
typename std::iterator_traits< iterator >::difference_type difference_type
const_iterator cbegin() const
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
typename std::add_const< IT >::type const_iterator
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
const_iterator cend() const
typename std::iterator_traits< iterator >::reference reference
A view over a range of elements in a container.
const_iterator end() const
typename std::iterator_traits< iterator >::pointer pointer
ListView & operator=(const ListView &)=delete
ListView(iterator begin, iterator end)
static auto MakeListView(Iterator begin, Iterator end)
typename std::iterator_traits< iterator >::value_type value_type