CARLA
Response.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/MsgPack.h"
10 #include "carla/MsgPackAdaptors.h"
11 
12 #include <boost/optional.hpp>
13 
14 #ifdef _MSC_VER
15 #pragma warning(push)
16 #pragma warning(disable:4583)
17 #pragma warning(disable:4582)
18 #include <boost/variant2/variant.hpp>
19 #pragma warning(pop)
20 #else
21 #include <boost/variant2/variant.hpp>
22 #endif
23 
24 #include <string>
25 
26 namespace carla {
27 namespace 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)
42 
43  private:
44 
45  /// @todo Needs initialization, empty strings end up calling memcpy on a
46  /// nullptr. Possibly a bug in MsgPack but could also be our specialization
47  /// for variants
48  std::string _what{"unknown error"};
49  };
50 
51  template <typename T>
52  class Response {
53  public:
54 
55  using value_type = T;
56 
58 
59  Response() = default;
60 
61  template <typename TValue>
62  Response(TValue &&value) : _data(std::forward<TValue>(value)) {}
63 
64  template <typename TValue>
65  void Reset(TValue &&value) {
66  _data = std::forward<TValue>(value);
67  }
68 
69  bool HasError() const {
70  return _data.index() == 0;
71  }
72 
73  template <typename... Ts>
74  void SetError(Ts &&... args) {
75  _data = error_type(std::forward<Ts>(args)...);
76  }
77 
78  const error_type &GetError() const {
79  DEBUG_ASSERT(HasError());
80  return boost::variant2::get<error_type>(_data);
81  }
82 
84  DEBUG_ASSERT(!HasError());
85  return boost::variant2::get<value_type>(_data);
86  }
87 
88  const value_type &Get() const {
89  DEBUG_ASSERT(!HasError());
90  return boost::variant2::get<value_type>(_data);
91  }
92 
93  operator bool() const {
94  return !HasError();
95  }
96 
97  MSGPACK_DEFINE_ARRAY(_data)
98 
99  private:
100 
101  boost::variant2::variant<error_type, value_type> _data;
102  };
103 
104  template <>
105  class Response<void> {
106  public:
107 
108  using value_type = void;
109 
111 
112  static Response Success() {
113  return success_flag{};
114  }
115 
116  Response() : _data(error_type{}) {}
117 
118  Response(ResponseError error) : _data(std::move(error)) {}
119 
120  bool HasError() const {
121  return _data.has_value();
122  }
123 
124  template <typename... Ts>
125  void SetError(Ts &&... args) {
126  _data = error_type(std::forward<Ts>(args)...);
127  }
128 
129  const error_type &GetError() const {
130  DEBUG_ASSERT(HasError());
131  return *_data;
132  }
133 
134  operator bool() const {
135  return !HasError();
136  }
137 
138  MSGPACK_DEFINE_ARRAY(_data)
139 
140  private:
141 
142  struct success_flag {};
143 
144  Response(success_flag) {}
145 
146  boost::optional<error_type> _data;
147  };
148 
149 } // namespace rpc
150 } // namespace carla
void SetError(Ts &&... args)
Definition: Response.h:125
void Reset(TValue &&value)
Definition: Response.h:65
Response(ResponseError error)
Definition: Response.h:118
boost::variant2::variant< error_type, value_type > _data
Definition: Response.h:101
boost::optional< error_type > _data
Definition: Response.h:146
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
value_type & Get()
Definition: Response.h:83
void SetError(Ts &&... args)
Definition: Response.h:74
const std::string & What() const
Definition: Response.h:37
const value_type & Get() const
Definition: Response.h:88
const error_type & GetError() const
Definition: Response.h:129
Response(TValue &&value)
Definition: Response.h:62
bool HasError() const
Definition: Response.h:69
static Response Success()
Definition: Response.h:112
ResponseError(std::string message)
Definition: Response.h:34
const error_type & GetError() const
Definition: Response.h:78