CARLA
 
载入中...
搜索中...
未找到
RoadInfoIterator.h
浏览该文件的文档.
1// Copyright (c) 2019 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/road/element/RoadInfoVisitor.h" // 包含道路信息访问者的头文件
11
12#include <iterator> // 包含迭代器相关的头文件
13#include <memory> // 包含智能指针相关的头文件
14
15namespace carla { // 定义 carla 命名空间
16namespace road { // 定义 road 命名空间
17namespace element { // 定义 element 命名空间
18
19 template <typename T, typename IT> // 模板类,T 是值类型,IT 是迭代器类型
20 class RoadInfoIterator : private RoadInfoVisitor { // 道路信息迭代器类,继承自 RoadInfoVisitor
21 public:
22
23 static_assert(std::is_same<std::unique_ptr<RoadInfo>, typename IT::value_type>::value, "Not compatible."); // 静态断言,确保迭代器值类型与 unique_ptr<RoadInfo> 兼容
24
25 using value_type = T; // 定义值类型
26 using difference_type = typename IT::difference_type; // 定义差值类型
27 using pointer = T *; // 定义指针类型
28 using reference = T &; // 定义引用类型
29
30 RoadInfoIterator(IT begin, IT end) // 构造函数,接受迭代器的开始和结束
31 : _it(begin), // 初始化当前迭代器
32 _end(end) { // 初始化结束迭代器
33 _success = false; // 初始化成功标志为 false
34 for (; !IsAtEnd(); ++_it) { // 遍历迭代器直到结束
35 DEBUG_ASSERT((*_it) != nullptr); // 断言当前迭代器指向的对象不为空
36 (*_it)->AcceptVisitor(*this); // 调用接受访问者的方法
37 if (_success) { // 如果成功,则退出循环
38 break;
39 }
40 }
41 }
42
43 RoadInfoIterator &operator++() { // 重载前缀递增运算符
44 _success = false; // 重置成功标志为 false
45 while (!_success) { // 循环直到成功
46 ++_it; // 移动到下一个迭代器元素
47 if (IsAtEnd()) { // 检查是否到达结束
48 break; // 到达结束则退出
49 }
50 DEBUG_ASSERT((*_it) != nullptr); // 断言当前迭代器指向的对象不为空
51 (*_it)->AcceptVisitor(*this); // 调用接受访问者的方法
52 }
53 return *this; // 返回自身
54 }
55
56 reference operator*() const { // 重载解引用运算符
57 DEBUG_ASSERT((*_it) != nullptr); // 断言当前迭代器指向的对象不为空
58 return static_cast<T &>(**_it); // 返回当前迭代器指向的对象的引用
59 }
60
61 pointer operator->() const { // 重载箭头运算符
62 DEBUG_ASSERT((*_it) != nullptr); // 断言当前迭代器指向的对象不为空
63 return static_cast<T *>(_it->get()); // 返回当前迭代器指向的对象的指针
64 }
65
66 bool operator!=(const RoadInfoIterator &rhs) const { // 重载不等于运算符
67 return _it != rhs._it; // 判断当前迭代器与右侧迭代器是否不相等
68 }
69
70 bool operator==(const RoadInfoIterator &rhs) const { // 重载等于运算符
71 return !((*this) != rhs); // 判断当前迭代器与右侧迭代器是否相等
72 }
73
74 bool IsAtEnd() const { // 检查是否到达结束
75 return _it == _end; // 返回当前迭代器是否等于结束迭代器
76 }
77
78 private:
79
80 void Visit(T &) { // 访问方法,接受一个类型为 T 的对象
81 _success = true; // 设置成功标志为 true
82 }
83
84 IT _it; // 当前迭代器
85 IT _end; // 结束迭代器
86 bool _success; // 成功标志
87 };
88
89 template <typename T, typename Container> // 模板函数,接受值类型和容器类型
90 static auto MakeRoadInfoIterator(const Container &c) { // 创建道路信息迭代器的函数
91 auto begin = std::begin(c); // 获取容器的开始迭代器
92 auto end = std::end(c); // 获取容器的结束迭代器
93 return RoadInfoIterator<T, decltype(begin)>(begin, end); // 返回新的道路信息迭代器
94 }
95
96 template <typename T, typename IT> // 模板函数,接受值类型和迭代器类型
97 static auto MakeRoadInfoIterator(IT begin, IT end) { // 创建道路信息迭代器的函数
98 return RoadInfoIterator<T, decltype(begin)>(begin, end); // 返回新的道路信息迭代器
99 }
100
101} // namespace element
102} // namespace road
103} // namespace carla
auto end() const noexcept
auto begin() const noexcept
名称范围迭代支持
#define DEBUG_ASSERT(predicate)
Definition Debug.h:68
bool operator!=(const RoadInfoIterator &rhs) const
bool operator==(const RoadInfoIterator &rhs) const
typename IT::difference_type difference_type
static auto MakeRoadInfoIterator(const Container &c)
CARLA模拟器的主命名空间。
Definition Carla.cpp:139