CARLA
ActorRegistry.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 
10 
11 #include "Containers/Map.h"
12 
14 #include <carla/Iterator.h>
16 
17 #include <unordered_map>
18 
19 /// A registry of all the Carla actors.
21 {
22 private:
23 
24  using DatabaseType = std::unordered_map<FActorView::IdType, FActorView>;
25 
26 public:
27 
28  using IdType = DatabaseType::key_type;
29 
30  // ===========================================================================
31  /// @name Actor registry functions
32  // ===========================================================================
33  /// @{
34 public:
35 
36  /// Register the @a Actor in the database. A new ID will be assign to this
37  /// actor.
38  ///
39  /// @warning Undefined if an actor is registered more than once.
40  FActorView Register(AActor &Actor, FActorDescription Description, IdType DesiredId = 0);
41 
42  void Deregister(IdType Id);
43 
44  void Deregister(AActor *Actor);
45 
46  /// @}
47  // ===========================================================================
48  /// @name Look up functions
49  // ===========================================================================
50  /// @{
51 
52  int32 Num() const
53  {
54  return Actors.Num();
55  }
56 
57  bool IsEmpty() const
58  {
59  return ActorDatabase.empty();
60  }
61 
62  bool Contains(uint32 Id) const
63  {
64  return ActorDatabase.find(Id) != ActorDatabase.end();
65  }
66 
68  {
69  auto it = ActorDatabase.find(Id);
70  return it != ActorDatabase.end() ? it->second : FActorView();
71  }
72 
73  FActorView Find(const AActor *Actor) const
74  {
75  auto PtrToId = Ids.Find(Actor);
76  return PtrToId != nullptr ? Find(*PtrToId) : FActorView();
77  }
78 
79  /// If the actor is not found in the registry, create a fake actor view. The
80  /// returned FActorView has some information about the @a Actor but will have
81  /// an invalid id.
83 
84  /// @}
85  // ===========================================================================
86  /// @name Range iteration support
87  // ===========================================================================
88  /// @{
89 public:
90 
91  using value_type = DatabaseType::mapped_type;
92 
93  auto begin() const noexcept
94  {
96  }
97 
98  auto end() const noexcept
99  {
101  }
102 
103  /// @}
104 private:
105 
106  FActorView MakeView(IdType Id, AActor &Actor, FActorDescription Description) const;
107 
108  TMap<IdType, AActor *> Actors;
109 
110  TMap<AActor *, IdType> Ids;
111 
113 };
void Deregister(IdType Id)
TMap< IdType, AActor * > Actors
auto end() const noexcept
Definition: ActorRegistry.h:98
A registry of all the Carla actors.
Definition: ActorRegistry.h:20
A view over an actor and its properties.
Definition: ActorView.h:14
DatabaseType ActorDatabase
FActorView Register(AActor &Actor, FActorDescription Description, IdType DesiredId=0)
Register the Actor in the database.
auto begin() const noexcept
Definition: ActorRegistry.h:93
TMap< AActor *, IdType > Ids
bool IsEmpty() const
Definition: ActorRegistry.h:57
DatabaseType::mapped_type value_type
Definition: ActorRegistry.h:91
int32 Num() const
Definition: ActorRegistry.h:52
carla::SharedPtr< cc::Actor > Actor
static auto make_map_values_const_iterator(It it)
Creates an iterator over const references to the values of a map.
Definition: Iterator.h:43
bool Contains(uint32 Id) const
Definition: ActorRegistry.h:62
std::unordered_map< FActorView::IdType, FActorView > DatabaseType
Definition: ActorRegistry.h:24
A description of a Carla Actor with all its variation.
DatabaseType::key_type IdType
Definition: ActorRegistry.h:28
FActorView FindOrFake(AActor *Actor) const
If the actor is not found in the registry, create a fake actor view.
FActorView Find(const AActor *Actor) const
Definition: ActorRegistry.h:73
FActorView MakeView(IdType Id, AActor &Actor, FActorDescription Description) const
FActorView Find(IdType Id) const
Definition: ActorRegistry.h:67