CARLA
ActorVariant.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/Debug.h"
10 #include "carla/Memory.h"
11 #include "carla/client/Actor.h"
13 #include "carla/rpc/Actor.h"
14 
15 #include <boost/variant.hpp>
16 
17 namespace carla {
18 namespace client {
19 namespace detail {
20 
21  /// Holds an Actor, but only instantiates it when needed.
22  class ActorVariant {
23  public:
24 
26  : _value(actor) {}
27 
29  : _value(actor) {}
30 
32  _value = actor;
33  return *this;
34  }
35 
37  _value = actor;
38  return *this;
39  }
40 
42  if (_value.which() == 0u) {
43  MakeActor(episode);
44  }
45  DEBUG_ASSERT(_value.which() == 1u);
46  return boost::get<SharedPtr<client::Actor>>(_value);
47  }
48 
49  const rpc::Actor &Serialize() const {
50  return boost::apply_visitor(Visitor(), _value);
51  }
52 
53  ActorId GetId() const {
54  return Serialize().id;
55  }
56 
57  ActorId GetParentId() const {
58  return Serialize().parent_id;
59  }
60 
61  const std::string &GetTypeId() const {
62  return Serialize().description.id;
63  }
64 
65  bool operator==(ActorVariant rhs) const {
66  return GetId() == rhs.GetId();
67  }
68 
69  bool operator!=(ActorVariant rhs) const {
70  return !(*this == rhs);
71  }
72 
73  private:
74 
75  struct Visitor {
76  const rpc::Actor &operator()(const rpc::Actor &actor) const {
77  return actor;
78  }
80  return actor->Serialize();
81  }
82  };
83 
84  void MakeActor(EpisodeProxy episode) const;
85 
86  mutable boost::variant<rpc::Actor, SharedPtr<client::Actor>> _value;
87  };
88 
89 } // namespace detail
90 } // namespace client
91 } // namespace carla
const rpc::Actor & operator()(const rpc::Actor &actor) const
Definition: ActorVariant.h:76
void MakeActor(EpisodeProxy episode) const
const rpc::Actor & Serialize() const
Definition: ActorVariant.h:49
ActorVariant & operator=(SharedPtr< client::Actor > actor)
Definition: ActorVariant.h:36
Holds an Actor, but only instantiates it when needed.
Definition: ActorVariant.h:22
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
boost::variant< rpc::Actor, SharedPtr< client::Actor > > _value
Definition: ActorVariant.h:86
ActorDescription description
Definition: rpc/Actor.h:29
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
ActorVariant(SharedPtr< client::Actor > actor)
Definition: ActorVariant.h:28
bool operator==(ActorVariant rhs) const
Definition: ActorVariant.h:65
carla::ActorId ActorId
ActorVariant & operator=(rpc::Actor actor)
Definition: ActorVariant.h:31
bool operator!=(ActorVariant rhs) const
Definition: ActorVariant.h:69
SharedPtr< client::Actor > Get(EpisodeProxy episode) const
Definition: ActorVariant.h:41
const rpc::Actor & operator()(const SharedPtr< const client::Actor > &actor) const
Definition: ActorVariant.h:79
ActorId parent_id
Definition: rpc/Actor.h:27
const std::string & GetTypeId() const
Definition: ActorVariant.h:61