CARLA
LibCarla/source/carla/client/ActorAttribute.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 
11 
12 #include <cstdlib>
13 #include <exception>
14 #include <type_traits>
15 
16 namespace carla {
17 namespace client {
18 
19  // ===========================================================================
20  // -- InvalidAttributeValue --------------------------------------------------
21  // ===========================================================================
22 
23  /// Exception thrown when the value given to an ActorAttribute cannot be
24  /// converted to its type.
25  class InvalidAttributeValue : public std::invalid_argument {
26  public:
27 
28  InvalidAttributeValue(const std::string &what) : std::invalid_argument(what) {}
29  };
30 
31  // ===========================================================================
32  // -- BadAttributeCast -------------------------------------------------------
33  // ===========================================================================
34 
35  /// Exception thrown when the value of an ActorAttribute cannot be cast to the
36  /// requested type.
37  class BadAttributeCast : public std::logic_error {
38  public:
39 
40  BadAttributeCast(const std::string &what) : std::logic_error(what) {}
41  };
42 
43  // ===========================================================================
44  // -- ActorAttribute ---------------------------------------------------------
45  // ===========================================================================
46 
48  {
49  public:
50  ActorAttributeValueAccess() = default;
53  virtual ~ActorAttributeValueAccess() = default;
54 
55  ActorAttributeValueAccess & operator= (ActorAttributeValueAccess const & ) = default;
56  ActorAttributeValueAccess & operator= (ActorAttributeValueAccess && ) = default;
57 
58  virtual const std::string &GetId() const = 0;
59 
60  virtual rpc::ActorAttributeType GetType() const = 0;
61 
62  /// Cast the value to the given type.
63  ///
64  /// @throw BadAttributeCast if the cast fails.
65  template <typename T>
66  T As() const;
67 
68  /// Cast the value to the type specified by the enum
69  /// carla::rpc::ActorAttributeType.
70  ///
71  /// @throw BadAttributeCast if the cast fails.
72  template <rpc::ActorAttributeType Type>
73  auto As() const;
74 
75  template <typename T>
76  bool operator==(const T &rhs) const;
77 
78  template <typename T>
79  bool operator!=(const T &rhs) const {
80  return !(*this == rhs);
81  }
82 
83  protected:
84  virtual const std::string &GetValue() const = 0;
85 
86  void Validate() const;
87  };
88 
89  template <>
90  bool ActorAttributeValueAccess::As<bool>() const;
91 
92  template <>
93  int ActorAttributeValueAccess::As<int>() const;
94 
95  template <>
96  float ActorAttributeValueAccess::As<float>() const;
97 
98  template <>
99  std::string ActorAttributeValueAccess::As<std::string>() const;
100 
101  template <>
102  sensor::data::Color ActorAttributeValueAccess::As<sensor::data::Color>() const;
103 
104  template <>
105  inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::Bool>() const {
106  return As<bool>();
107  }
108 
109  template <>
110  inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::Int>() const {
111  return As<int>();
112  }
113 
114  template <>
115  inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::Float>() const {
116  return As<float>();
117  }
118 
119  template <>
120  inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::String>() const {
121  return As<std::string>();
122  }
123 
124  template <>
125  inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::RGBColor>() const {
126  return As<sensor::data::Color>();
127  }
128 
129  template <typename T>
130  inline bool ActorAttributeValueAccess::operator==(const T &rhs) const {
131  return As<T>() == rhs;
132  }
133 
134  template <>
136  return
137  (GetType() == rhs.GetType()) &&
138  (GetValue() == rhs.GetValue());
139  }
140 
142 
143  public:
145  _attribute(std::move(attribute))
146  {
147  Validate();
148  }
149  ActorAttributeValue(ActorAttributeValue const &) = default;
151  virtual ~ActorAttributeValue() = default;
152 
153  ActorAttributeValue & operator= (ActorAttributeValue const & ) = default;
154  ActorAttributeValue & operator= (ActorAttributeValue && ) = default;
155 
156  virtual const std::string &GetId() const override {
157  return _attribute.id;
158  }
159 
160  virtual rpc::ActorAttributeType GetType() const override {
161  return _attribute.type;
162  }
163 
164  /// Serialize this object as a carla::rpc::ActorAttributeValue.
165  operator rpc::ActorAttributeValue() const{
166  return _attribute;
167  }
168 
169  virtual const std::string &GetValue() const override {
170  return _attribute.value;
171  }
172 
173  private:
174 
176  };
177 
178  template <>
180  return rhs.operator==(*this);
181  }
182 
183  /// An attribute of an ActorBlueprint.
185 
186  public:
189  _attribute(std::move(attribute)) {
190  Validate();
191  }
192 
193  ActorAttribute(ActorAttribute const &) = default;
194  ActorAttribute(ActorAttribute &&) = default;
195  virtual ~ActorAttribute() = default;
196 
197  ActorAttribute & operator= (ActorAttribute const & ) = default;
198  ActorAttribute & operator= (ActorAttribute && ) = default;
199 
200  virtual const std::string &GetId() const override {
201  return _attribute.id;
202  }
203 
204  virtual rpc::ActorAttributeType GetType() const override {
205  return _attribute.type;
206  }
207 
208  const std::vector<std::string> &GetRecommendedValues() const {
209  return _attribute.recommended_values;
210  }
211 
212  bool IsModifiable() const {
213  return _attribute.is_modifiable;
214  }
215 
216  /// Set the value of this attribute.
217  ///
218  /// @throw InvalidAttributeValue if attribute is not modifiable.
219  /// @throw InvalidAttributeValue if format does not match this type.
220  void Set(std::string value);
221 
222  /// Serialize this object as a carla::rpc::ActorAttributeValue.
223  operator rpc::ActorAttributeValue() const {
224  return _attribute;
225  }
226 
227  virtual const std::string &GetValue() const override {
228  return _attribute.value;
229  }
230 
231  private:
233  };
234 
235  template <>
237  return rhs.operator==(*this);
238  }
239 
240 } // namespace client
241 } // namespace carla
Exception thrown when the value given to an ActorAttribute cannot be converted to its type...
const std::vector< std::string > & GetRecommendedValues() const
virtual const std::string & GetId() const override
A 32-bit BGRA color.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
virtual rpc::ActorAttributeType GetType() const override
virtual rpc::ActorAttributeType GetType() const =0
static bool operator==(const Buffer &lhs, const Buffer &rhs)
Definition: test/Buffer.h:53
virtual const std::string & GetValue() const override
virtual const std::string & GetValue() const override
virtual const std::string & GetValue() const =0
Exception thrown when the value of an ActorAttribute cannot be cast to the requested type...
static LaneMarking::Type GetType(std::string str)
Definition: LaneMarking.cpp:17
virtual rpc::ActorAttributeType GetType() const override
virtual const std::string & GetId() const override