CARLA
 
载入中...
搜索中...
未找到
AtomicActorSet.h
浏览该文件的文档.
1// Copyright (c) 2020 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 <mutex>
10#include <map>
11#include <vector>
12
13#include "carla/client/Actor.h"
14#include "carla/Memory.h"
15
16namespace carla {
17namespace traffic_manager {
18// 使用命名空间别名简化类型名称
19 namespace cc = carla::client;
22 // 定义名为 AtomicActorSet 的类
24
25 private:
26 // 互斥锁,用于保护对 actor_set 的并发访问
28 // 存储 ActorId 和对应的 ActorPtr 的映射
29 std::map<ActorId, ActorPtr> actor_set;
30 // 状态计数器,用于跟踪集合的状态变化
32
33 public:
34
36// 获取当前集合中的所有 ActorPtr 组成的向量
37 std::vector<ActorPtr> GetList() {
38
39 std::lock_guard<std::mutex> lock(modification_mutex);
40 std::vector<ActorPtr> actor_list;
41 // 遍历 actor_set,将每个 ActorPtr 添加到向量中
42 for (auto it = actor_set.begin(); it != actor_set.end(); ++it) {
43 actor_list.push_back(it->second);
44 }
45 return actor_list;
46 }
47// 获取当前集合中所有 Actor 的 ID 组成的向量
48 std::vector<ActorId> GetIDList() {
49
50 std::lock_guard<std::mutex> lock(modification_mutex);
51 std::vector<ActorId> actor_list;
52 for (auto it = actor_set.begin(); it != actor_set.end(); ++it) {
53 actor_list.push_back(it->first);
54 }//将 actor_set 集合中的所有元素的 second 值提取并添加到 actor_list 列表中
55 return actor_list;
56 }
57// 将一个 ActorPtr 向量中的所有元素插入到集合中
58 void Insert(std::vector<ActorPtr> actor_list) {
59
60 std::lock_guard<std::mutex> lock(modification_mutex);
61 // 遍历输入的向量,将每个 ActorPtr 插入到 actor_set 中
62 for (auto &actor: actor_list) {
63 actor_set.insert({actor->GetId(), actor});
64 }
66 }
67// 从集合中移除指定的 ActorId 列表中的元素
68 void Remove(std::vector<ActorId> actor_id_list) {
69
70 std::lock_guard<std::mutex> lock(modification_mutex);
71 // 遍历要移除的 ActorId 列表
72 for (auto& actor_id: actor_id_list) {
73 // 如果在 actor_set 中找到对应的 ActorId,则移除该元素
74 if (actor_set.find(actor_id) != actor_set.end()){
75 actor_set.erase(actor_id);
76 }
77 }
79 }
80// 销毁指定 ActorId 对应的 Actor
81 void Destroy(ActorId actor_id) {
82
83 std::lock_guard<std::mutex> lock(modification_mutex);
84 // 如果在 actor_set 中找到对应的 ActorId
85 if (actor_set.find(actor_id) != actor_set.end()) {
86 // 获取对应的 ActorPtr
87 ActorPtr actor = actor_set.at(actor_id);
88 // 销毁该 Actor
89 actor->Destroy();
90 // 从集合中移除该元素
91 actor_set.erase(actor_id);
93 }
94 }
95// 获取当前集合的状态计数器值
96 int GetState() {
97
98 std::lock_guard<std::mutex> lock(modification_mutex);
99 return state_counter;
100 }
101 // 检查集合中是否包含指定的 ActorId
102 bool Contains(ActorId id) {
103
104 std::lock_guard<std::mutex> lock(modification_mutex);
105 // 判断 actor_set 中是否存在指定的 ActorId
106 return actor_set.find(id) != actor_set.end();
107 }
108// 获取集合中元素的数量
109 size_t Size() {
110
111 std::lock_guard<std::mutex> lock(modification_mutex);
112 return actor_set.size();
113 }
114// 清空集合
115 void Clear() {
116
117 std::lock_guard<std::mutex> lock(modification_mutex);
118 return actor_set.clear();
119 }
120
121 };
122
123} // namespace traffic_manager
124} // namespace carla
void Remove(std::vector< ActorId > actor_id_list)
std::map< ActorId, ActorPtr > actor_set
std::vector< ActorPtr > GetList()
void Insert(std::vector< ActorPtr > actor_list)
包含客户端相关类和定义的命名空间。
Definition AtomicList.h:17
carla::SharedPtr< cc::Actor > ActorPtr
使用别名简化代码中的命名
carla::ActorId ActorId
参与者的智能指针类型
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
boost::shared_ptr< T > SharedPtr
使用这个SharedPtr(boost::shared_ptr)以保持与boost::python的兼容性, 但未来如果可能的话,我们希望能为std::shared_ptr制作一个Python适配器。
Definition Memory.h:19
rpc::ActorId ActorId
Definition ActorId.h:26