CARLA
ActorFactory.cpp
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 
8 
9 #include "carla/Logging.h"
10 #include "carla/StringUtil.h"
11 #include "carla/client/Actor.h"
14 #ifdef RSS_ENABLED
15 #include "carla/rss/RssSensor.h"
16 #endif
19 #include "carla/client/Vehicle.h"
20 #include "carla/client/Walker.h"
22 #include "carla/client/World.h"
24 
25 #include <rpc/config.h>
26 #include <rpc/rpc_error.h>
27 
28 #include <exception>
29 
30 namespace carla {
31 namespace client {
32 namespace detail {
33 
34  // A deleter cannot throw exceptions; and unlike std::unique_ptr, the deleter
35  // of (std|boost)::shared_ptr is invoked even if the managed pointer is null.
37  void operator()(::carla::client::Actor *ptr) const noexcept {
38  if ((ptr != nullptr) && ptr->IsAlive()) {
39  try {
40  ptr->Destroy();
41  delete ptr;
42  } catch (const ::rpc::timeout &timeout) {
43  log_error(timeout.what());
44  log_error(
45  "timeout while trying to garbage collect Actor",
46  ptr->GetDisplayId(),
47  "actor hasn't been removed from the simulation");
48  } catch (const std::exception &e) {
50  "exception thrown while trying to garbage collect Actor",
51  ptr->GetDisplayId(),
52  e.what());
53  std::terminate();
54  } catch (...) {
56  "unknown exception thrown while trying to garbage collect an Actor :",
57  ptr->GetDisplayId());
58  std::terminate();
59  }
60  }
61  }
62  };
63 
64  template <typename ActorT>
67  return SharedPtr<ActorT>{new ActorT(std::move(init)), GarbageCollector()};
68  }
70  return SharedPtr<ActorT>{new ActorT(std::move(init))};
71  }
72 
74  EpisodeProxy episode,
75  rpc::Actor description,
77  auto init = ActorInitializer{description, episode};
78  if (description.description.id == "sensor.other.lane_invasion") {
79  return MakeActorImpl<LaneInvasionSensor>(std::move(init), gc);
80 #ifdef RSS_ENABLED
81  } else if (description.description.id == "sensor.other.rss") {
82  return MakeActorImpl<RssSensor>(std::move(init), gc);
83 #endif
84  } else if (description.HasAStream()) {
85  return MakeActorImpl<ServerSideSensor>(std::move(init), gc);
86  } else if (StringUtil::StartsWith(description.description.id, "vehicle.")) {
87  return MakeActorImpl<Vehicle>(std::move(init), gc);
88  } else if (StringUtil::StartsWith(description.description.id, "walker.")) {
89  return MakeActorImpl<Walker>(std::move(init), gc);
90  } else if (StringUtil::StartsWith(description.description.id, "traffic.traffic_light")) {
91  return MakeActorImpl<TrafficLight>(std::move(init), gc);
92  } else if (StringUtil::StartsWith(description.description.id, "traffic.")) {
93  return MakeActorImpl<TrafficSign>(std::move(init), gc);
94  } else if (description.description.id == "controller.ai.walker") {
95  return MakeActorImpl<WalkerAIController>(std::move(init), gc);
96  }
97  return MakeActorImpl<Actor>(std::move(init), gc);
98  }
99 
100 } // namespace detail
101 } // namespace client
102 } // namespace carla
void operator()(::carla::client::Actor *ptr) const noexcept
static void log_error(Args &&... args)
Definition: Logging.h:110
static SharedPtr< Actor > MakeActor(EpisodeProxy episode, rpc::Actor actor_description, GarbageCollectionPolicy garbage_collection_policy)
Create an Actor based on the provided actor_description.
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
ActorDescription description
Definition: rpc/Actor.h:29
Used to initialize Actor classes.
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
bool HasAStream() const
Definition: rpc/Actor.h:41
static auto MakeActorImpl(ActorInitializer init, GarbageCollectionPolicy gc)
static void log_critical(Args &&... args)
Definition: Logging.h:124
Represents an actor in the simulation.
Definition: client/Actor.h:18
static bool StartsWith(const Range1T &input, const Range2T &test)
Definition: StringUtil.h:26