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