CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
client
detail
CachedActorList.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 "
carla/NonCopyable.h
"
10
#include "
carla/rpc/Actor.h
"
11
12
#include <boost/iterator/transform_iterator.hpp>
13
14
#include <algorithm>
15
#include <iterator>
16
#include <mutex>
17
18
namespace
carla
{
19
namespace
client
{
20
namespace
detail
{
21
22
// ===========================================================================
23
// -- CachedActorList --------------------------------------------------------
24
// ===========================================================================
25
26
/// Keeps a list of actor descriptions to avoid requesting each time the
27
/// descriptions to the server.
28
///
29
/// @todo Dead actors are never removed from the list.
30
class
CachedActorList
:
private
MovableNonCopyable
{
31
public
:
32
33
/// Inserts an actor into the list.
34
void
Insert
(
rpc::Actor
actor);
35
36
/// Inserts a @a range containing actors.
37
template
<
typename
RangeT>
38
void
InsertRange
(RangeT range);
39
40
/// Return the actor ids present in @a range that haven't been added to this
41
/// list.
42
template
<
typename
RangeT>
43
std::vector<ActorId>
GetMissingIds
(
const
RangeT &range)
const
;
44
45
/// Retrieve the actor matching @a id, or empty optional if actor is not
46
/// cached.
47
std::optional<rpc::Actor>
GetActorById
(
ActorId
id
)
const
;
48
49
/// Retrieve the actors matching the ids in @a range.
50
template
<
typename
RangeT>
51
std::vector<rpc::Actor>
GetActorsById
(
const
RangeT &range)
const
;
52
53
void
Clear
();
54
55
private
:
56
57
mutable
std::mutex
_mutex
;
58
59
std::unordered_map<ActorId, rpc::Actor>
_actors
;
60
};
61
62
// ===========================================================================
63
// -- CachedActorList implementation -----------------------------------------
64
// ===========================================================================
65
66
inline
void
CachedActorList::Insert
(
rpc::Actor
actor) {
67
std::scoped_lock<std::mutex> lock(
_mutex
);
68
auto
id
= actor.
id
;
69
_actors
.emplace(
id
, std::move(actor));
70
}
71
72
template
<
typename
RangeT>
73
inline
void
CachedActorList::InsertRange
(RangeT range) {
74
auto
make_a_pair = [](
rpc::Actor
actor) {
75
auto
id
= actor.id;
76
return
std::make_pair(
id
, std::move(actor));
77
};
78
auto
make_iterator = [&make_a_pair](
auto
it) {
79
return
boost::make_transform_iterator(std::make_move_iterator(it), make_a_pair);
80
};
81
std::scoped_lock<std::mutex> lock(
_mutex
);
82
_actors
.insert(make_iterator(std::begin(range)), make_iterator(std::end(range)));
83
}
84
85
template
<
typename
RangeT>
86
inline
std::vector<ActorId>
CachedActorList::GetMissingIds
(
const
RangeT &range)
const
{
87
std::vector<ActorId> result;
88
result.reserve(range.size());
89
std::scoped_lock<std::mutex> lock(
_mutex
);
90
std::copy_if(std::begin(range), std::end(range), std::back_inserter(result), [
this
](
auto
id
) {
91
return
_actors
.find(
id
) ==
_actors
.end();
92
});
93
return
result;
94
}
95
96
inline
std::optional<rpc::Actor>
CachedActorList::GetActorById
(
ActorId
id
)
const
{
97
std::scoped_lock<std::mutex> lock(
_mutex
);
98
auto
it =
_actors
.find(
id
);
99
if
(it !=
_actors
.end()) {
100
return
it->second;
101
}
102
return
std::nullopt;
103
}
104
105
template
<
typename
RangeT>
106
inline
std::vector<rpc::Actor>
CachedActorList::GetActorsById
(
const
RangeT &range)
const
{
107
std::vector<rpc::Actor> result;
108
result.reserve(range.size());
109
std::scoped_lock<std::mutex> lock(
_mutex
);
110
for
(
auto
&&
id
: range) {
111
auto
it =
_actors
.find(
id
);
112
if
(it !=
_actors
.end()) {
113
result.emplace_back(it->second);
114
}
115
}
116
return
result;
117
}
118
119
inline
void
CachedActorList::Clear
() {
120
std::scoped_lock<std::mutex> lock(
_mutex
);
121
_actors
.clear();
122
}
123
124
}
// namespace detail
125
}
// namespace client
126
}
// namespace carla
NonCopyable.h
carla::MovableNonCopyable::MovableNonCopyable
MovableNonCopyable()=default
carla::client::detail::CachedActorList
Keeps a list of actor descriptions to avoid requesting each time the descriptions to the server.
Definition
CachedActorList.h:30
carla::client::detail::CachedActorList::InsertRange
void InsertRange(RangeT range)
Inserts a range containing actors.
Definition
CachedActorList.h:73
carla::client::detail::CachedActorList::_mutex
std::mutex _mutex
Definition
CachedActorList.h:57
carla::client::detail::CachedActorList::Clear
void Clear()
Definition
CachedActorList.h:119
carla::client::detail::CachedActorList::Insert
void Insert(rpc::Actor actor)
Inserts an actor into the list.
Definition
CachedActorList.h:66
carla::client::detail::CachedActorList::_actors
std::unordered_map< ActorId, rpc::Actor > _actors
Definition
CachedActorList.h:59
carla::client::detail::CachedActorList::GetActorById
std::optional< rpc::Actor > GetActorById(ActorId id) const
Retrieve the actor matching id, or empty optional if actor is not cached.
Definition
CachedActorList.h:96
carla::client::detail::CachedActorList::GetActorsById
std::vector< rpc::Actor > GetActorsById(const RangeT &range) const
Retrieve the actors matching the ids in range.
Definition
CachedActorList.h:106
carla::client::detail::CachedActorList::GetMissingIds
std::vector< ActorId > GetMissingIds(const RangeT &range) const
Return the actor ids present in range that haven't been added to this list.
Definition
CachedActorList.h:86
carla::rpc::Actor
Definition
rpc/Actor.h:20
carla::rpc::Actor::id
ActorId id
Definition
rpc/Actor.h:25
carla::client::detail
Definition
AtomicList.h:18
carla::client
Definition
BasicAgent.h:24
carla::traffic_manager::ActorId
carla::ActorId ActorId
Definition
AtomicActorSet.h:21
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
Actor.h
Generated by
1.18.0