CARLA
 
载入中...
搜索中...
未找到
LibCarla/source/carla/ListView.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
11#include <type_traits> // 包含类型特征相关的头文件
12#include <iterator> // 包含迭代器相关的头文件
13
14
15namespace carla {
16
17 /// 代表容器中一段元素的视图,基本上是一对起始和结束迭代器。
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
30 // 构造函数,接受开始和结束迭代器
32 : _begin(begin), _end(end) { // 初始化成员变量
33 DEBUG_ASSERT(std::distance(_begin, _end) >= 0); // 确保范围有效
34 }
35
36 ListView(const ListView &) = default; // 默认复制构造函数
37 ListView &operator=(const ListView &) = delete; // 删除赋值运算符
38
39 iterator begin() { // 返回迭代器的开始位置
40 return _begin;
41 }
42
43 const_iterator begin() const { // 返回常量迭代器的开始位置
44 return _begin;
45 }
46
47 const_iterator cbegin() const { // 返回常量迭代器的开始位置(常量版本)
48 return _begin;
49 }
50
51 iterator end() { // 返回迭代器的结束位置
52 return _end;
53 }
54
55 const_iterator end() const { // 返回常量迭代器的结束位置
56
57 return _end;
58 }
59
60 const_iterator cend() const { // 返回常量迭代器的结束位置(常量版本)
61 return _end;
62 }
63
64 bool empty() const { // 检查视图是否为空
65 return _begin == _end; // 如果开始和结束迭代器相等则为空
66 }
67
68 size_type size() const { // 返回视图中的元素数量
69 return static_cast<size_t>(std::distance(begin(), end())); // 计算开始和结束之间的距离
70 }
71
72 private:
73
74 const iterator _begin; // 成员变量,表示开始迭代器
75
76 const iterator _end; // 成员变量,表示结束迭代器
77 };
78
79 template <typename Iterator> // 模板函数,接受迭代器类型
80 static inline auto MakeListView(Iterator begin, Iterator end) { // 创建 ListView 实例
81 return ListView<Iterator>(begin, end); // 返回新的 ListView
82 }
83
84 template <typename Container> // 模板函数,接受容器类型
85 static inline auto MakeListView(Container &c) { // 创建 ListView 实例
86 return MakeListView(std::begin(c), std::end(c)); // 使用容器的 begin 和 end 创建 ListView
87 }
88
89} // namespace carla
auto end() const noexcept
auto begin() const noexcept
名称范围迭代支持
Traits::size_t size_t
#define DEBUG_ASSERT(predicate)
Definition Debug.h:68
代表容器中一段元素的视图,基本上是一对起始和结束迭代器。
ListView & operator=(const ListView &)=delete
const_iterator cend() const
typename std::add_const< IT >::type const_iterator
typename std::iterator_traits< iterator >::difference_type difference_type
const_iterator begin() const
typename std::iterator_traits< iterator >::pointer pointer
ListView(iterator begin, iterator end)
typename std::iterator_traits< iterator >::value_type value_type
const_iterator cbegin() const
ListView(const ListView &)=default
const_iterator end() const
typename std::iterator_traits< iterator >::reference reference
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
static auto MakeListView(Iterator begin, Iterator end)