CARLA
Loading...
Searching...
No Matches
AtomicActorSet.h
Go to the documentation of this file.
1// Copyright (c) 2026 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
24
25 private:
26
28 std::map<ActorId, ActorPtr> actor_set;
30 /// Removed actors, kept alive until TakeReleased() so they die outside TM locks.
31 std::vector<ActorPtr> released;
32
33 public:
34
36
37 std::vector<ActorPtr> GetList() {
38
39 std::scoped_lock<std::mutex> lock(modification_mutex);
40 std::vector<ActorPtr> actor_list;
41 for (auto it = actor_set.begin(); it != actor_set.end(); ++it) {
42 actor_list.push_back(it->second);
43 }
44 return actor_list;
45 }
46
47 std::vector<ActorId> GetIDList() {
48
49 std::scoped_lock<std::mutex> lock(modification_mutex);
50 std::vector<ActorId> actor_list;
51 for (auto it = actor_set.begin(); it != actor_set.end(); ++it) {
52 actor_list.push_back(it->first);
53 }
54 return actor_list;
55 }
56
57 void Insert(std::vector<ActorPtr> actor_list) {
58
59 std::scoped_lock<std::mutex> lock(modification_mutex);
60 for (auto &actor: actor_list) {
61 actor_set.insert({actor->GetId(), actor});
62 }
64 }
65
66 void Remove(std::vector<ActorId> actor_id_list) {
67
68 std::scoped_lock<std::mutex> lock(modification_mutex);
69 for (auto& actor_id: actor_id_list) {
70 auto it = actor_set.find(actor_id);
71 if (it != actor_set.end()){
72 released.push_back(std::move(it->second));
73 actor_set.erase(it);
74 }
75 }
77 }
78
79 void Destroy(ActorId actor_id) {
80
81 std::scoped_lock<std::mutex> lock(modification_mutex);
82 if (actor_set.find(actor_id) != actor_set.end()) {
83 ActorPtr actor = actor_set.at(actor_id);
84 actor->Destroy();
85 actor_set.erase(actor_id);
86 released.push_back(std::move(actor));
88 }
89 }
90
91 int GetState() {
92
93 std::scoped_lock<std::mutex> lock(modification_mutex);
94 return state_counter;
95 }
96
97 bool Contains(ActorId id) {
98
99 std::scoped_lock<std::mutex> lock(modification_mutex);
100 return actor_set.find(id) != actor_set.end();
101 }
102
103 size_t Size() {
104
105 std::scoped_lock<std::mutex> lock(modification_mutex);
106 return actor_set.size();
107 }
108
109 void Clear() {
110
111 std::scoped_lock<std::mutex> lock(modification_mutex);
112 for (auto &entry : actor_set) {
113 released.push_back(std::move(entry.second));
114 }
115 actor_set.clear();
116 }
117
118 /// Actors removed since the last call; drop them with no lock held.
119 std::vector<ActorPtr> TakeReleased() {
120
121 std::scoped_lock<std::mutex> lock(modification_mutex);
122 std::vector<ActorPtr> result;
123 result.swap(released);
124 return result;
125 }
126
127 };
128
129} // namespace traffic_manager
130} // namespace carla
std::vector< ActorPtr > TakeReleased()
Actors removed since the last call; drop them with no lock held.
void Remove(std::vector< ActorId > actor_id_list)
std::vector< ActorPtr > released
Removed actors, kept alive until TakeReleased() so they die outside TM locks.
std::map< ActorId, ActorPtr > actor_set
std::vector< ActorPtr > GetList()
void Insert(std::vector< ActorPtr > actor_list)
carla::SharedPtr< cc::Actor > ActorPtr
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15
rpc::ActorId ActorId
Definition ActorId.h:18