CARLA
Array.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 #include "carla/Exception.h"
12 
13 #include <exception>
14 #include <iterator>
15 #include <type_traits>
16 
17 namespace carla {
18 namespace sensor {
19 namespace data {
20 
21  /// Base class for all the sensor data consisting of an array of items.
22  template <typename T>
23  class Array : public SensorData {
24  public:
25 
26  using value_type = T;
27  using iterator = value_type *;
28  using const_iterator = typename std::add_const<value_type>::type *;
29  using reverse_iterator = std::reverse_iterator<iterator>;
30  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
31  using size_type = size_t;
32  using pointer = value_type *;
33  using const_pointer = typename std::add_const<value_type>::type *;
34  using reference = value_type &;
35  using const_reference = typename std::add_const<value_type>::type &;
36 
38  return reinterpret_cast<iterator>(_data.begin() + _offset);
39  }
40 
42  return reinterpret_cast<const_iterator>(_data.begin() + _offset);
43  }
44 
46  return cbegin();
47  }
48 
50  return reinterpret_cast<iterator>(_data.end());
51  }
52 
53  const_iterator cend() const {
54  return reinterpret_cast<const_iterator>(_data.end());
55  }
56 
57  const_iterator end() const {
58  return cend();
59  }
60 
62  return reverse_iterator(begin());
63  }
64 
67  }
68 
70  return crbegin();
71  }
72 
74  return reverse_iterator(end());
75  }
76 
78  return const_reverse_iterator(cend());
79  }
80 
82  return crend();
83  }
84 
85  bool empty() const {
86  return begin() == end();
87  }
88 
89  size_type size() const {
90  DEBUG_ASSERT(std::distance(begin(), end()) >= 0);
91  return static_cast<size_type>(std::distance(begin(), end()));
92  }
93 
95  return begin();
96  }
97 
98  const value_type *data() const {
99  return begin();
100  }
101 
103  return data()[pos];
104  }
105 
107  return data()[pos];
108  }
109 
111  if (!(pos < size())) {
112  throw_exception(std::out_of_range("Array index out of range"));
113  }
114  return operator[](pos);
115  }
116 
118  if (!(pos < size())) {
119  throw_exception(std::out_of_range("Array index out of range"));
120  }
121  return operator[](pos);
122  }
123 
124  protected:
125 
126  template <typename FuncT>
127  explicit Array(RawData &&data, FuncT get_offset)
128  : SensorData(data),
129  _data(std::move(data)),
130  _offset(get_offset(_data)) {
132  DEBUG_ASSERT((_data.size() - _offset) % sizeof(T) == 0u);
133  DEBUG_ASSERT(begin() <= end());
134  }
135 
136  explicit Array(size_t offset, RawData &&data)
137  : Array(std::move(data), [offset](const RawData &) { return offset; }) {}
138 
139  const RawData &GetRawData() const {
140  return _data;
141  }
142 
143  private:
144 
146 
147  const size_t _offset;
148  };
149 
150 } // namespace data
151 } // namespace sensor
152 } // namespace carla
const_reverse_iterator crend() const
Definition: Array.h:77
const RawData & GetRawData() const
Definition: Array.h:139
const_reference at(size_type pos) const
Definition: Array.h:117
const size_t _offset
Definition: Array.h:147
const_reference operator[](size_type pos) const
Definition: Array.h:106
typename std::add_const< value_type >::type & const_reference
Definition: Array.h:35
const_iterator cbegin() const
Definition: Array.h:41
const_iterator end() const
Definition: Array.h:57
void throw_exception(const std::exception &e)
Definition: Carla.cpp:101
Helper class to store and serialize the data generated by a RawLidar.
const_iterator begin() const
Definition: Array.h:45
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
const_reverse_iterator rbegin() const
Definition: Array.h:69
const_reverse_iterator crbegin() const
Definition: Array.h:65
auto begin() noexcept
Begin iterator to the data generated by the sensor.
Definition: RawData.h:51
typename std::add_const< value_type >::type * const_pointer
Definition: Array.h:33
size_t size() const
Size in bytes of the data generated by the sensor.
Definition: RawData.h:82
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
bool empty() const
Definition: Array.h:85
Base class for all the objects containing data generated by a sensor.
Definition: SensorData.h:20
reverse_iterator rend()
Definition: Array.h:73
auto end() noexcept
Past-the-end iterator to the data generated by the sensor.
Definition: RawData.h:61
const_reverse_iterator rend() const
Definition: Array.h:81
const value_type * data() const
Definition: Array.h:98
std::reverse_iterator< iterator > reverse_iterator
Definition: Array.h:29
typename std::add_const< value_type >::type * const_iterator
Definition: Array.h:28
Base class for all the sensor data consisting of an array of items.
Definition: Array.h:23
Array(size_t offset, RawData &&data)
Definition: Array.h:136
reference operator[](size_type pos)
Definition: Array.h:102
const_iterator cend() const
Definition: Array.h:53
value_type * data()
Definition: Array.h:94
reverse_iterator rbegin()
Definition: Array.h:61
reference at(size_type pos)
Definition: Array.h:110
Wrapper around the raw data generated by a sensor plus some useful meta-information.
Definition: RawData.h:20
size_type size() const
Definition: Array.h:89
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: Array.h:30
Array(RawData &&data, FuncT get_offset)
Definition: Array.h:127