Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CARLA simulator Carla MsgPackAdaptors

From Leeroopedia
Knowledge Sources
Domains Serialization, RPC
Last Updated 2026-02-15 05:00 GMT

Overview

MsgPackAdaptors provides MsgPack serialization adaptors for std::optional and std::variant, enabling these C++ standard library types to be used in CARLA's RPC communication protocol.

Description

The MsgPackAdaptors.h header extends the clmdep_msgpack library (CARLA's vendored MsgPack implementation) with custom adaptors for two C++ standard library types that are not natively supported:

std::optional<T> adaptors:

  • convert: Deserializes from a MsgPack array. A 1-element array represents std::nullopt (the element is false). A 2-element array represents a present value ([true, value]).
  • pack: Serializes by packing a 2-element array [true, value] for engaged optionals, or a 1-element array [false] for disengaged ones.
  • object_with_zone: Creates MsgPack objects with zone-allocated memory following the same encoding scheme.

std::variant<Ts...> adaptors:

  • convert: Deserializes from a 2-element MsgPack array [index, value]. Uses copy_to_variant_impl with std::index_sequence expansion to dispatch to the correct variant alternative based on the stored index. The index is read as uint64_t and matched using an std::initializer_list expansion trick (similar to CompositeSerializer). A workaround using std::get(std::tuple<Ts...>{}) is used for type deduction.
  • pack: Serializes as [variant.index(), value] using std::visit to access the active alternative.
  • object_with_zone: Creates zone-allocated MsgPack objects with the [index, value] encoding.

The file handles MSVC compiler warnings (C4583, C4582) around variant/optional construction by wrapping the <variant> include in pragma blocks.

Usage

Include this header in any context where std::optional or std::variant types need to be serialized/deserialized through CARLA's MsgPack-based RPC system. It is included transitively by many RPC type definitions.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/MsgPackAdaptors.h

Signature

namespace clmdep_msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {

    // std::optional adaptors
    template<typename T> struct convert<std::optional<T>>;
    template<typename T> struct pack<std::optional<T>>;
    template<typename T> struct object_with_zone<std::optional<T>>;

    // std::variant adaptors
    template<typename... Ts> struct convert<std::variant<Ts...>>;
    template<typename... Ts> struct pack<std::variant<Ts...>>;
    template<typename... Ts> struct object_with_zone<std::variant<Ts...>>;

} // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE
} // namespace clmdep_msgpack

Import

#include "carla/MsgPackAdaptors.h"

I/O Contract

Inputs

Name Type Required Description
o (convert) const clmdep_msgpack::object& Yes MsgPack object to deserialize from
v (convert) std::optional<T>& or std::variant<Ts...>& Yes Target C++ object to populate
v (pack) const std::optional<T>& or const std::variant<Ts...>& Yes Source C++ object to serialize

Outputs

Name Type Description
(convert result) std::optional<T> or std::variant<Ts...> Deserialized C++ object from MsgPack data
(pack result) MsgPack stream Serialized MsgPack representation of the C++ object

Usage Examples

#include "carla/MsgPackAdaptors.h"
#include "carla/MsgPack.h"

// Serializing an optional
std::optional<int> maybeValue = 42;
auto packed = clmdep_msgpack::pack(maybeValue);
// Produces: [true, 42]

std::optional<int> empty = std::nullopt;
auto packedEmpty = clmdep_msgpack::pack(empty);
// Produces: [false]

// Serializing a variant
std::variant<int, std::string> v = std::string("hello");
auto packedVariant = clmdep_msgpack::pack(v);
// Produces: [1, "hello"] (index 1 = string alternative)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment