CARLA
 
载入中...
搜索中...
未找到
Response.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#include "carla/MsgPack.h" // 引入MsgPack库头文件
10#include "carla/MsgPackAdaptors.h" // 引入MsgPack适配器头文件
11
12#include <boost/optional.hpp> // 引入Boost选项库
13
14#ifdef _MSC_VER // 如果是MSVC编译器
15#pragma warning(push) // 保存当前警告状态
16#pragma warning(disable:4583) // 禁用特定警告
17#pragma warning(disable:4582) // 禁用特定警告
18#include <boost/variant2/variant.hpp> // 引入Boost变体库
19#pragma warning(pop) // 恢复之前的警告状态
20#else // 如果不是MSVC编译器
21#include <boost/variant2/variant.hpp> // 引入Boost变体库
22#endif
23
24#include <string> // 引入字符串库
25
26namespace carla { // 定义carla命名空间
27namespace rpc { // 定义rpc子命名空间
28
29 class ResponseError { // 定义响应错误类
30 public:
31
32 ResponseError() = default; // 默认构造函数
33
34 explicit ResponseError(std::string message) // 带字符串参数的构造函数
35 : _what(std::move(message)) {} // 移动构造字符串
36
37 const std::string &What() const { // 获取错误消息
38 return _what; // 返回错误消息
39 }
40
41 MSGPACK_DEFINE_ARRAY(_what) // 定义MsgPack数组序列化
42
43 private:
44
45 /// @todo 需要初始化,空字符串会导致对nullptr调用memcpy。
46 /// 可能是MsgPack的bug,但也可能是我们对变体的特化问题
47 std::string _what{"unknown error"}; // 错误消息,默认值为“未知错误”
48 };
49
50 template <typename T> // 模板类,类型为T
51 class Response { // 定义响应类
52 public:
53
54 using value_type = T; // 使用类型T作为value_type
55
56 using error_type = ResponseError; // 使用ResponseError作为error_type
57
58 Response() = default; // 默认构造函数
59
60 template <typename TValue> // 模板构造函数,接收任意类型的值
61 Response(TValue &&value) : _data(std::forward<TValue>(value)) {} // 移动构造数据
62
63 template <typename TValue> // 模板函数,重置数据
64 void Reset(TValue &&value) {
65 _data = std::forward<TValue>(value); // 移动赋值新数据
66 }
67
68 bool HasError() const { // 检查是否有错误
69 return _data.index() == 0; // 如果第一个索引是0,则表示有错误
70 }
71
72 template <typename... Ts> // 模板函数,设置错误
73 void SetError(Ts &&... args) {
74 _data = error_type(std::forward<Ts>(args)...); // 移动构造错误对象
75 }
76
77 const error_type &GetError() const { // 获取错误
78 DEBUG_ASSERT(HasError()); // 确保有错误
79 return boost::variant2::get<error_type>(_data); // 返回错误对象
80 }
81
82 value_type &Get() { // 获取有效数据
83 DEBUG_ASSERT(!HasError()); // 确保没有错误
84 return boost::variant2::get<value_type>(_data); // 返回数据
85 }
86
87 const value_type &Get() const { // 获取有效数据(常量版本)
88 DEBUG_ASSERT(!HasError()); // 确保没有错误
89 return boost::variant2::get<value_type>(_data); // 返回数据
90 }
91
92 operator bool() const { // 重载布尔转换运算符
93 return !HasError(); // 如果没有错误返回true
94 }
95
96 MSGPACK_DEFINE_ARRAY(_data) // 定义MsgPack数组序列化
97
98 private:
99
100 boost::variant2::variant<error_type, value_type> _data; // 存储错误或有效数据的变体
101 };
102
103 template <> // 对于特殊化的模板
104 class Response<void> { // 特化响应类,类型为void
105 public:
106
107 using value_type = void; // 使用void作为value_type
108
109 using error_type = ResponseError; // 使用ResponseError作为error_type
110
111 static Response Success() { // 静态函数,返回成功的响应
112 return success_flag{}; // 返回成功标志
113 }
114
115 Response() : _data(error_type{}) {} // 默认构造函数,初始化为错误对象
116
117 Response(ResponseError error) : _data(std::move(error)) {} // 带错误参数的构造函数
118
119 bool HasError() const { // 检查是否有错误
120 return _data.has_value(); // 如果有值则表示有错误
121 }
122
123 template <typename... Ts> // 模板函数,设置错误
124 void SetError(Ts &&... args) {
125 _data = error_type(std::forward<Ts>(args)...); // 移动构造错误对象
126 }
127
128 const error_type &GetError() const { // 获取错误
129 DEBUG_ASSERT(HasError()); // 确保有错误
130 return *_data; // 返回错误对象
131 }
132
133 operator bool() const { // 重载布尔转换运算符
134 return !HasError(); // 如果没有错误返回true
135 }
136
137 MSGPACK_DEFINE_ARRAY(_data) // 定义MsgPack数组序列化
138
139 private:
140
141 struct success_flag {}; // 成功标志的内部结构体
142
143 Response(success_flag) {} // 带成功标志的私有构造函数
144
145 boost::optional<error_type> _data; // 可选的错误对象
146 };
147
148} // namespace rpc
149} // namespace carla
#define DEBUG_ASSERT(predicate)
Definition Debug.h:68
const std::string & What() const
Definition Response.h:37
ResponseError(std::string message)
Definition Response.h:34
Response(ResponseError error)
Definition Response.h:117
static Response Success()
Definition Response.h:111
void SetError(Ts &&... args)
Definition Response.h:124
boost::optional< error_type > _data
Definition Response.h:145
const error_type & GetError() const
Definition Response.h:128
void Reset(TValue &&value)
Definition Response.h:64
Response(TValue &&value)
Definition Response.h:61
value_type & Get()
Definition Response.h:82
const value_type & Get() const
Definition Response.h:87
boost::variant2::variant< error_type, value_type > _data
Definition Response.h:100
void SetError(Ts &&... args)
Definition Response.h:73
bool HasError() const
Definition Response.h:68
ResponseError error_type
Definition Response.h:56
const error_type & GetError() const
Definition Response.h:77
CARLA模拟器的主命名空间。
Definition Carla.cpp:139