Implementation:CARLA simulator Carla RPC Response
| Knowledge Sources | |
|---|---|
| Domains | RPC Communication, Error Handling |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Response is a template class that wraps RPC call results in an error-or-value variant, providing type-safe error handling for the CARLA client-server communication protocol.
Description
Defined in the carla::rpc namespace, this file (~150 lines) provides:
ResponseError: a simple wrapper around a string error message (defaulting to "unknown error"), MsgPack-serializable.
Response<T>: a template class using std::variant<error_type, value_type> to hold either an error or a successful result. Key methods include HasError(), SetError(), GetError(), Get() (returns the value), and operator bool() (returns true if no error).
Response<void>: a template specialization for void return types, using std::optional<error_type> instead of variant. It provides a static Success() factory method.
Both specializations support MsgPack serialization via MSGPACK_DEFINE_ARRAY.
Usage
This type is used internally by the CARLA RPC framework to wrap function return values with error handling capability, enabling the client to check whether an RPC call succeeded or returned an error.
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/rpc/Response.h
Signature
namespace carla {
namespace rpc {
class ResponseError {
public:
ResponseError() = default;
explicit ResponseError(std::string message);
const std::string &What() const;
MSGPACK_DEFINE_ARRAY(_what)
private:
std::string _what{"unknown error"};
};
template <typename T>
class Response {
public:
using value_type = T;
using error_type = ResponseError;
Response() = default;
template <typename TValue> Response(TValue &&value);
bool HasError() const;
template <typename... Ts> void SetError(Ts &&... args);
const error_type &GetError() const;
value_type &Get();
operator bool() const;
MSGPACK_DEFINE_ARRAY(_data)
private:
std::variant<error_type, value_type> _data;
};
template <>
class Response<void> { /* specialization using std::optional */ };
} // namespace rpc
} // namespace carla
Import
#include "carla/rpc/Response.h"
I/O Contract
| Method | Return Type | Description |
|---|---|---|
HasError() |
bool |
Returns true if response contains an error |
GetError() |
const ResponseError & |
Returns the error (asserts !HasError is false) |
Get() |
T & |
Returns the value (asserts HasError is false) |
operator bool() |
bool |
Returns true if response has a valid value |
SetError(args...) |
void |
Sets an error with the given message |
Usage Examples
#include "carla/rpc/Response.h"
carla::rpc::Response<int> response;
// Check result
if (response) {
int value = response.Get();
} else {
std::cerr << "Error: " << response.GetError().What() << std::endl;
}
// Void response
auto void_response = carla::rpc::Response<void>::Success();