CARLA
AtomicActorSet.h
Go to the documentation of this file.
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 
16 namespace carla {
17 namespace traffic_manager {
18 
19  namespace cc = carla::client;
22 
24 
25  private:
26 
27  std::mutex modification_mutex;
28  std::map<ActorId, ActorPtr> actor_set;
30 
31  public:
32 
33  AtomicActorSet(): state_counter(0) {}
34 
35  std::vector<ActorPtr> GetList() {
36 
37  std::lock_guard<std::mutex> lock(modification_mutex);
38  std::vector<ActorPtr> actor_list;
39  for (auto it = actor_set.begin(); it != actor_set.end(); ++it) {
40  actor_list.push_back(it->second);
41  }
42  return actor_list;
43  }
44 
45  std::vector<ActorId> GetIDList() {
46 
47  std::lock_guard<std::mutex> lock(modification_mutex);
48  std::vector<ActorId> actor_list;
49  for (auto it = actor_set.begin(); it != actor_set.end(); ++it) {
50  actor_list.push_back(it->first);
51  }
52  return actor_list;
53  }
54 
55  void Insert(std::vector<ActorPtr> actor_list) {
56 
57  std::lock_guard<std::mutex> lock(modification_mutex);
58  for (auto &actor: actor_list) {
59  actor_set.insert({actor->GetId(), actor});
60  }
61  ++state_counter;
62  }
63 
64  void Remove(std::vector<ActorId> actor_id_list) {
65 
66  std::lock_guard<std::mutex> lock(modification_mutex);
67  for (auto& actor_id: actor_id_list) {
68  if (actor_set.find(actor_id) != actor_set.end()){
69  actor_set.erase(actor_id);
70  }
71  }
72  ++state_counter;
73  }
74 
75  void Destroy(ActorId actor_id) {
76 
77  std::lock_guard<std::mutex> lock(modification_mutex);
78  if (actor_set.find(actor_id) != actor_set.end()) {
79  ActorPtr actor = actor_set.at(actor_id);
80  actor->Destroy();
81  actor_set.erase(actor_id);
82  ++state_counter;
83  }
84  }
85 
86  int GetState() {
87 
88  std::lock_guard<std::mutex> lock(modification_mutex);
89  return state_counter;
90  }
91 
92  bool Contains(ActorId id) {
93 
94  std::lock_guard<std::mutex> lock(modification_mutex);
95  return actor_set.find(id) != actor_set.end();
96  }
97 
98  size_t Size() {
99 
100  std::lock_guard<std::mutex> lock(modification_mutex);
101  return actor_set.size();
102  }
103 
104  void Clear() {
105 
106  std::lock_guard<std::mutex> lock(modification_mutex);
107  return actor_set.clear();
108  }
109 
110  };
111 
112 } // namespace traffic_manager
113 } // namespace carla
carla::SharedPtr< cc::Actor > ActorPtr
rpc::ActorId ActorId
Definition: ActorId.h:18
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::vector< ActorId > GetIDList()
std::map< ActorId, ActorPtr > actor_set
std::vector< ActorPtr > GetList()
void Insert(std::vector< ActorPtr > actor_list)
carla::ActorId ActorId
void Remove(std::vector< ActorId > actor_id_list)