CARLA
Loading...
Searching...
No Matches
LibCarla/source/carla/client/ActorAttribute.h
Go to the documentation of this file.
1// Copyright (c) 2026 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
16namespace carla {
17namespace 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:
53 virtual ~ActorAttributeValueAccess() = default;
54
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 <>
91
92 template <>
94
95 template <>
97
98 template <>
100
101 template <>
103
104 template <>
106 return As<bool>();
107 }
108
109 template <>
111 return As<int>();
112 }
113
114 template <>
116 return As<float>();
117 }
118
119 template <>
123
124 template <>
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 }
151 virtual ~ActorAttributeValue() = default;
152
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;
195 virtual ~ActorAttribute() = default;
196
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 {
210 }
211
212 bool IsModifiable() const {
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
virtual const std::string & GetValue() const =0
ActorAttributeValueAccess & operator=(ActorAttributeValueAccess const &)=default
virtual const std::string & GetId() const =0
ActorAttributeValueAccess(ActorAttributeValueAccess &&)=default
virtual rpc::ActorAttributeType GetType() const =0
ActorAttributeValueAccess(ActorAttributeValueAccess const &)=default
auto As() const
Cast the value to the type specified by the enum carla::rpc::ActorAttributeType.
T As() const
Cast the value to the given type.
ActorAttributeValue(ActorAttributeValue const &)=default
ActorAttributeValue & operator=(ActorAttributeValue const &)=default
virtual ~ActorAttributeValue()=default
virtual const std::string & GetId() const override
virtual rpc::ActorAttributeType GetType() const override
virtual const std::string & GetValue() const override
ActorAttributeValue(ActorAttributeValue &&)=default
virtual const std::string & GetId() const override
virtual const std::string & GetValue() const override
ActorAttribute(ActorAttribute &&)=default
void Set(std::string value)
Set the value of this attribute.
virtual ~ActorAttribute()=default
ActorAttribute(ActorAttribute const &)=default
virtual rpc::ActorAttributeType GetType() const override
const std::vector< std::string > & GetRecommendedValues() const
ActorAttribute & operator=(ActorAttribute const &)=default
sensor::data::Color ActorAttributeValueAccess::As< sensor::data::Color >() const
std::string ActorAttributeValueAccess::As< std::string >() const
This file contains definitions of common data structures used in traffic manager.