CARLA
 
载入中...
搜索中...
未找到
Array.h
浏览该文件的文档.
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
17namespace carla {
18namespace sensor {
19namespace 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;
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;
33 using const_pointer = typename std::add_const<value_type>::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
54 return reinterpret_cast<const_iterator>(_data.end());
55 }
56
58 return cend();
59 }
60
64
68
70 return crbegin();
71 }
72
74 return reverse_iterator(end());
75 }
76
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
#define DEBUG_ASSERT(predicate)
Definition Debug.h:66
Wrapper around the raw data generated by a sensor plus some useful meta-information.
Definition RawData.h:21
auto end() noexcept
Past-the-end iterator to the data generated by the sensor.
Definition RawData.h:62
auto begin() noexcept
Begin iterator to the data generated by the sensor.
Definition RawData.h:52
size_t size() const
Size in bytes of the data generated by the sensor.
Definition RawData.h:83
Base class for all the objects containing data generated by a sensor.
Definition SensorData.h:22
Base class for all the sensor data consisting of an array of items.
Definition Array.h:23
const_iterator end() const
Definition Array.h:57
Array(RawData &&data, FuncT get_offset)
Definition Array.h:127
const_reference at(size_type pos) const
Definition Array.h:117
value_type * iterator
Definition Array.h:27
const value_type * data() const
Definition Array.h:98
const_iterator cbegin() const
Definition Array.h:41
typename std::add_const< value_type >::type & const_reference
Definition Array.h:35
const size_t _offset
Definition Array.h:147
bool empty() const
Definition Array.h:85
const_iterator begin() const
Definition Array.h:45
value_type * pointer
Definition Array.h:32
const_reverse_iterator crbegin() const
Definition Array.h:65
const_reverse_iterator rbegin() const
Definition Array.h:69
std::reverse_iterator< iterator > reverse_iterator
Definition Array.h:29
const_reference operator[](size_type pos) const
Definition Array.h:106
reference operator[](size_type pos)
Definition Array.h:102
typename std::add_const< value_type >::type * const_pointer
Definition Array.h:33
reverse_iterator rend()
Definition Array.h:73
const RawData & GetRawData() const
Definition Array.h:139
Array(size_t offset, RawData &&data)
Definition Array.h:136
size_type size() const
Definition Array.h:89
value_type * data()
Definition Array.h:94
typename std::add_const< value_type >::type * const_iterator
Definition Array.h:28
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition Array.h:30
reference at(size_type pos)
Definition Array.h:110
value_type & reference
Definition Array.h:34
const_reverse_iterator rend() const
Definition Array.h:81
const_reverse_iterator crend() const
Definition Array.h:77
reverse_iterator rbegin()
Definition Array.h:61
const_iterator cend() const
Definition Array.h:53
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133
void throw_exception(const std::exception &e)
Definition Carla.cpp:135