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