CARLA
 
载入中...
搜索中...
未找到
LibCarla/source/carla/client/ActorAttribute.h
浏览该文件的文档.
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
11// 包含与Carla RPC(远程过程调用)中的参与者属性相关的头文件
13// 包含与Carla传感器数据中的颜色相关的头文件
14
15#include <cstdlib>
16// 包含C标准库中的通用工具头文件,如一些宏定义、内存操作函数等
17#include <exception>
18// 包含C++ 异常处理相关的头文件
19#include <type_traits>
20// 包含C++ 类型特征相关的头文件,用于在编译时获取类型信息
21
22
23namespace carla {
24namespace client {
25
26
27 // ===========================================================================
28 // -- 无效属性值InvalidAttributeValue ----------------------------------------
29 // ===========================================================================
30
31
32 /// 当赋予ActorAttribute的值无法转换为其类型时抛出异常。
33 class InvalidAttributeValue : public std::invalid_argument {
34 public:
35 InvalidAttributeValue(const std::string &what) : std::invalid_argument(what) {}
36 // 构造函数,接受一个字符串参数what
37 // 调用std::invalid_argument的构造函数,将what传递给父类构造函数
38 // 用于创建一个带有特定错误信息的InvalidAttributeValue异常对象
39 };
40
41
42 // ===========================================================================
43 // -- 错误的属性转换异常BadAttributeCast --------------------------------------
44 // ===========================================================================
45
46
47 /// 当ActorAttribute的值无法转换为请求的类型时抛出异常。
48 class BadAttributeCast : public std::logic_error {
49 public:
50 BadAttributeCast(const std::string &what) : std::logic_error(what) {}
51 // 构造函数,接受一个字符串参数what
52 // 调用std::logic_error的构造函数,将what传递给父类构造函数
53 // 用于创建一个带有特定错误信息的BadAttributeCast异常对象
54 };
55
56
57 // ===========================================================================
58 // -- 参与者属性 --------------------------------------------------------------
59 // ===========================================================================
60
61
63 {
64 public:
66 // 默认构造函数,使用默认初始化方式初始化对象
68 // 拷贝构造函数,使用默认拷贝语义
70 // 移动构造函数,使用默认移动语义
71 virtual ~ActorAttributeValueAccess() = default;
72 // 析构函数,使用默认析构语义
73
75 // 拷贝赋值运算符,使用默认拷贝语义
77 // 移动赋值运算符,使用默认移动语义
78
79 virtual const std::string &GetId() const = 0;
80 // 纯虚函数,用于获取属性的唯一标识符,派生类必须实现
81 virtual rpc::ActorAttributeType GetType() const = 0;
82 // 纯虚函数,用于获取属性的类型(通过rpc::ActorAttributeType枚举),派生类必须实现
83
84 /// 将值转换为给定的类型。
85 ///
86 /// @throw 如果转换失败跑出BadAttributeCast异常。
87 template <typename T>
88 T As() const;
89 // 模板函数,用于将属性值转换为类型T,在函数体中会根据具体类型进行转换,如果失败会抛出BadAttributeCast异常
90
91 /// 将值转换为枚举carla::rpc::ActorAttributeType指定的类型。
92 ///
93 /// @throw 如果转换失败,则抛出BadAttributeCast的异常。
94 template <rpc::ActorAttributeType Type>
95 auto As() const;
96 // 模板函数,根据指定的rpc::ActorAttributeType枚举类型将属性值转换为相应类型,如果失败会抛出BadAttributeCast异常
97
98 template <typename T>
99 bool operator==(const T &rhs) const;
100 // 模板函数,用于比较当前对象与类型为T的对象rhs是否相等,具体比较逻辑在函数体中实现
101
102 template <typename T>
103 bool operator!=(const T &rhs) const {
104 return!(*this == rhs);
105 }
106 // 不等于运算符,通过调用==运算符取反得到不等于的结果
107
108
109 protected:
110 virtual const std::string &GetValue() const = 0;
111 // 纯虚函数,用于获取属性的值,派生类必须实现
112 void Validate() const;
113 // 函数用于验证属性的合法性,具体验证逻辑在派生类中实现
114 };
115
116
117 template <>
119 // 显式特化模板函数As,用于将属性值转换为bool类型,具体实现在其他地方(可能在源文件中)
120
121 template <>
123 // 显式特化模板函数As,用于将属性值转换为int类型,具体实现在其他地方(可能在源文件中)
124
125 template <>
127 // 显式特化模板函数As,用于将属性值转换为float类型,具体实现在其他地方(可能在源文件中)
128
129 template <>
130 std::string ActorAttributeValueAccess::As<std::string>() const;
131 // 显式特化模板函数As,用于将属性值转换为std::string类型,具体实现在其他地方(可能在源文件中)
132
133 template <>
134 sensor::data::Color ActorAttributeValueAccess::As<sensor::data::Color>() const;
135 // 显式特化模板函数As,用于将属性值转换为sensor::data::Color类型,具体实现在其他地方(可能在源文件中)
136
137
138 template <>
139 inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::Bool>() const {
140 return As<bool>();
141 }
142 // 显式特化模板函数As,当指定类型为rpc::ActorAttributeType::Bool时,调用As<bool>()进行转换
143
144 template <>
145 inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::Int>() const {
146 return As<int>();
147 }
148 // 显式特化模板函数As,当指定类型为rpc::ActorAttributeType::Int时,调用As<int>()进行转换
149
150 template <>
151 inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::Float>() const {
152 return As<float>();
153 }
154 // 显式特化模板函数As,当指定类型为rpc::ActorAttributeType::Float时,调用As<float>()进行转换
155
156 template <>
157 inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::String>() const {
158 return As<std::string>();
159 }
160 // 显式特化模板函数As,当指定类型为rpc::ActorAttributeType::String时,调用As<std::string>()进行转换
161
162 template <>
163 inline auto ActorAttributeValueAccess::As<rpc::ActorAttributeType::RGBColor>() const {
165 }
166 // 显式特化模板函数As,当指定类型为rpc::ActorAttributeType::RGBColor时,调用As<sensor::data::Color>()进行转换
167
168
169 template <typename T>
170 inline bool ActorAttributeValueAccess::operator==(const T &rhs) const {
171 return As<T>() == rhs;
172 }
173 // 模板函数,用于比较当前对象转换为类型T后与rhs是否相等,通过调用As<T>()得到转换后的值再进行比较
174
175
176 template <>
178 return
179 (GetType() == rhs.GetType()) &&
180 (GetValue() == rhs.GetValue());
181 }
182 // 显式特化模板函数operator==,用于比较两个ActorAttributeValueAccess对象是否相等
183 // 通过比较它们的类型(GetType())和值(GetValue())来确定是否相等
184
185
187
188
189 public:
191 _attribute(std::move(attribute))
192 {
193 Validate();
194 }
195 // 构造函数,接受一个rpc::ActorAttributeValue类型的参数attribute
196 // 使用std::move将attribute移动构造到内部成员变量_attribute中
197 // 然后调用Validate()函数验证属性的合法性
198
200 // 拷贝构造函数,使用默认拷贝语义
202 // 移动构造函数,使用默认移动语义
203 virtual ~ActorAttributeValue() = default;
204 // 析构函数,使用默认析构语义
205
207 // 拷贝赋值运算符,使用默认拷贝语义
209 // 移动赋值运算符,使用默认移动语义
210
211 virtual const std::string &GetId() const override {
212 return _attribute.id;
213 }
214 // 重写基类的GetId()函数,返回内部成员变量_attribute的id成员
215
216 virtual rpc::ActorAttributeType GetType() const override {
217 return _attribute.type;
218 }
219 // 重写基类的GetType()函数,返回内部成员变量_attribute的type成员
220
221 /// Serialize this object as a carla::rpc::ActorAttributeValue.
222 operator rpc::ActorAttributeValue() const{
223 return _attribute;
224 }
225 // 类型转换运算符,将ActorAttributeValue对象转换为rpc::ActorAttributeValue类型,直接返回内部成员变量_attribute
226
227
228 virtual const std::string &GetValue() const override {
229 return _attribute.value;
230 }
231 // 重写基类的GetValue()函数,返回内部成员变量_attribute的value成员
232
233
234 private:
235
237 // 内部成员变量,用于存储rpc::ActorAttributeValue类型的数据
238 };
239
240
241 template <>
243 return rhs.operator==(*this);
244 }
245 // 显式特化模板函数operator==,用于比较当前对象与ActorAttributeValue对象rhs是否相等
246 // 通过调用rhs的operator==函数并将当前对象作为参数传入来进行比较
247
248
249 /// An attribute of an ActorBlueprint.
251
252
253 public:
256 _attribute(std::move(attribute)) {
257 Validate();
258 }
259 // 构造函数,接受一个rpc::ActorAttribute类型的参数attribute
260 // 先调用基类的默认构造函数ActorAttributeValueAccess()
261 // 然后使用std::move将attribute移动构造到内部成员变量_attribute中
262 // 最后调用Validate()函数验证属性的合法性
263
264 ActorAttribute(ActorAttribute const &) = default;
265 // 拷贝构造函数,使用默认拷贝语义
267 // 移动构造函数,使用默认移动语义
268 virtual ~ActorAttribute() = default;
269 // 析构函数,使用默认析构语义
270
272 // 拷贝赋值运算符,使用默认拷贝语义
274 // 移动赋值运算符,使用默认移动语义
275
276 virtual const std::string &GetId() const override {
277 return _attribute.id;
278 }
279 // 重写基类的GetId()函数,返回内部成员变量_attribute的id成员
280
281 virtual rpc::ActorAttributeType GetType() const override {
282 return _attribute.type;
283 }
284 // 重写基类的GetType()函数,返回内部成员变量_attribute的type成员
285
286 const std::vector<std::string> &GetRecommendedValues() const {
288 }
289 // 函数用于获取推荐的值列表,直接返回内部成员变量_attribute的recommended_values成员
290
291 bool IsModifiable() const {
293 }
294 // 函数用于判断属性是否可修改,直接返回内部成员变量_attribute的is_modifiable成员
295
296
297 /// 设置这个属性值
298 ///
299 /// @throw 如果属性不可修改,则抛出InvalidAttributeValue异常。
300 /// @throw 如果格式不匹配这个类型,则抛出InvalidAttributeValue异常。
301 void Set(std::string value);
302 // 函数用于设置属性的值,在函数体中会进行属性可修改性和格式匹配性的检查,如果不满足则抛出InvalidAttributeValue异常
303
304
305 /// 将此对象序列化为carla::rpc::ActorAttributeValue。
306 operator rpc::ActorAttributeValue() const {
307 return _attribute;
308 }
309 // 类型转换运算符,将ActorAttribute对象转换为rpc::ActorAttributeValue类型,直接返回内部成员变量_attribute
310
311
312 virtual const std::string &GetValue() const override {
313 return _attribute.value;
314 }
315 // 重写基类的GetValue()函数,返回内部成员变量_attribute的value成员
316
317
318 private:
320 // 内部成员变量,用于存储rpc::ActorAttribute类型的数据
321 };
322
323
324 template <>
326 return rhs.operator==(*this);
327 }
328 // 显式特化模板函数operator==,用于比较当前对象与ActorAttribute对象rhs是否相等
329 // 通过调用rhs的operator==函数并将当前对象作为参数传入来进行比较
330
331
332} // namespace client
333} // 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
将值转换为枚举carla::rpc::ActorAttributeType指定的类型。
T As() const
将值转换为给定的类型。
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)
设置这个属性值
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
当ActorAttribute的值无法转换为请求的类型时抛出异常。
当赋予ActorAttribute的值无法转换为其类型时抛出异常。
sensor::data::Color ActorAttributeValueAccess::As< sensor::data::Color >() const
std::string ActorAttributeValueAccess::As< std::string >() const
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
包含CARLA客户端相关类和函数的命名空间。