CARLA
Loading...
Searching...
No Matches
Middleware.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB).
2// This work is licensed under the terms of the MIT license.
3// For a copy, see <https://opensource.org/licenses/MIT>.
4
5#pragma once
6
7#include <string>
8
9namespace carla {
10namespace ros2 {
11
12/// Enumeration of available middleware implementations.
13/// Passed to ROS2::Enable() to select the middleware at startup.
14/// Once set, the middleware cannot be changed without restarting.
15enum class Middleware {
16 FastDDS,
18 Zenoh
19};
20
21/// Convert a Middleware enum value to a readable string.
22inline const char* MiddlewareToString(Middleware middleware) {
23 switch (middleware) {
25 return "FastDDS";
27 return "CycloneDDS";
29 return "Zenoh";
30 }
31 return "Unknown";
32}
33
34/// Result of parsing a middleware name string.
39
40/// Parse a middleware name string (lowercase). Returns {true, middleware} on match,
41/// {false, FastDDS} for unrecognized values.
42inline MiddlewareParseResult MiddlewareFromString(const std::string& name) {
43 if (name == "fastdds") {
44 return {true, Middleware::FastDDS};
45 }
46 if (name == "cyclonedds") {
47 return {true, Middleware::CycloneDDS};
48 }
49 if (name == "zenoh") {
50 return {true, Middleware::Zenoh};
51 }
52 return {false, Middleware::FastDDS};
53}
54
55/// Return a readable list of middleware implementations compiled into this binary.
56inline std::string GetAvailableMiddlewareString() {
57 std::string result;
58#if defined(CARLA_ROS2_MIDDLEWARE_FASTDDS)
59 result += "FastDDS";
60#endif
61#if defined(CARLA_ROS2_MIDDLEWARE_CYCLONEDDS)
62 if (!result.empty()) {
63 result += ", ";
64 }
65 result += "CycloneDDS";
66#endif
67#if defined(CARLA_ROS2_MIDDLEWARE_ZENOH)
68 if (!result.empty()) {
69 result += ", ";
70 }
71 result += "Zenoh";
72#endif
73 if (result.empty()) {
74 result = "none";
75 }
76 return result;
77}
78
79/// Mangle a type name into the ROS2-compatible format.
80/// "sensor_msgs::msg::Image" becomes "sensor_msgs::msg::dds_::Image_".
81/// A bare name like "Image" becomes "dds_::Image_".
82inline std::string ToROS2TypeName(const std::string& type_name) {
83 auto pos = type_name.rfind("::");
84 if (pos == std::string::npos) {
85 return "dds_::" + type_name + "_";
86 }
87 return type_name.substr(0, pos) +
88 "::dds_::" + type_name.substr(pos + 2) + "_";
89}
90
91} // namespace ros2
92} // namespace carla
const char * MiddlewareToString(Middleware middleware)
Convert a Middleware enum value to a readable string.
Definition Middleware.h:22
Middleware
Enumeration of available middleware implementations.
Definition Middleware.h:15
std::string GetAvailableMiddlewareString()
Return a readable list of middleware implementations compiled into this binary.
Definition Middleware.h:56
std::string ToROS2TypeName(const std::string &type_name)
Mangle a type name into the ROS2-compatible format.
Definition Middleware.h:82
MiddlewareParseResult MiddlewareFromString(const std::string &name)
Parse a middleware name string (lowercase).
Definition Middleware.h:42
This file contains definitions of common data structures used in traffic manager.
Result of parsing a middleware name string.
Definition Middleware.h:35