CARLA
CachedActorList.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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.
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  boost::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::lock_guard<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::lock_guard<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::lock_guard<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 boost::optional<rpc::Actor> CachedActorList::GetActorById(ActorId id) const {
97  std::lock_guard<std::mutex> lock(_mutex);
98  auto it = _actors.find(id);
99  if (it != _actors.end()) {
100  return it->second;
101  }
102  return boost::none;
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::lock_guard<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::lock_guard<std::mutex> lock(_mutex);
121  _actors.clear();
122  }
123 
124 } // namespace detail
125 } // namespace client
126 } // namespace carla
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
Keeps a list of actor descriptions to avoid requesting each time the descriptions to the server...
std::vector< rpc::Actor > GetActorsById(const RangeT &range) const
Retrieve the actors matching the ids in range.
boost::optional< rpc::Actor > GetActorById(ActorId id) const
Retrieve the actor matching id, or empty optional if actor is not cached.
void Insert(rpc::Actor actor)
Inserts an actor into the list.
void InsertRange(RangeT range)
Inserts a range containing actors.
Inherit (privately) to suppress copy construction and assignment.
carla::ActorId ActorId
std::vector< ActorId > GetMissingIds(const RangeT &range) const
Return the actor ids present in range that haven&#39;t been added to this list.
std::unordered_map< ActorId, rpc::Actor > _actors