CARLA
Loading...
Searching...
No Matches
MiddlewareFactory.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 <memory>
8#include <string>
9
13#include "carla/Logging.h"
14
15#if defined(CARLA_ROS2_MIDDLEWARE_FASTDDS) && !defined(CARLA_ROS2_MIDDLEWARE_TESTING)
18#endif
19
20#if defined(CARLA_ROS2_MIDDLEWARE_CYCLONEDDS) && !defined(CARLA_ROS2_MIDDLEWARE_TESTING)
23#endif
24
25#if defined(CARLA_ROS2_MIDDLEWARE_ZENOH) && !defined(CARLA_ROS2_MIDDLEWARE_TESTING)
28#endif
29
30namespace carla {
31namespace ros2 {
32
33/// Factory that creates publisher/subscriber middleware based on the active middleware selection.
34/// The middleware is set once at startup via SetMiddleware() before any entities are created.
35/// After the first entity is created, changing the middleware has undefined behavior.
37 public:
38 /// Select the middleware for all subsequent publisher/subscriber creation.
39 /// Must be called before any publisher or subscriber is initialized.
40 static void SetMiddleware(Middleware middleware) {
41 GetActiveMiddleware() = middleware;
42 }
43
44 /// @return The currently selected middleware.
46 return GetActiveMiddleware();
47 }
48
49 /// Check whether a specific middleware was compiled into this binary.
50 static bool IsMiddlewareAvailable(Middleware middleware) {
51 switch (middleware) {
53#if defined(CARLA_ROS2_MIDDLEWARE_FASTDDS)
54 return true;
55#else
56 return false;
57#endif
59#if defined(CARLA_ROS2_MIDDLEWARE_CYCLONEDDS)
60 return true;
61#else
62 return false;
63#endif
65#if defined(CARLA_ROS2_MIDDLEWARE_ZENOH)
66 return true;
67#else
68 return false;
69#endif
70 }
71 return false;
72 }
73
74 /// Result of middleware resolution — whether resolution succeeded and which middleware to use.
79
80 /// Resolve the requested middleware strictly — no fallback to other middleware.
81 /// Returns {true, requested} if available, {false, requested} otherwise.
83 if (IsMiddlewareAvailable(requested)) {
84 return {true, requested};
85 }
86 return {false, requested};
87 }
88
89 /// Return a readable list of middleware implementations compiled into this binary.
90 /// Delegates to the free function in Middleware.h.
94
95 /// Create a publisher middleware for traits type T.
96 /// T must provide:
97 /// T::msg_type — the message type
98 template<typename T>
99 static std::unique_ptr<IPublisherMiddleware> CreatePublisher() {
100 switch (GetActiveMiddleware()) {
102#if defined(CARLA_ROS2_MIDDLEWARE_FASTDDS) && !defined(CARLA_ROS2_MIDDLEWARE_TESTING)
103 return std::make_unique<FastDDSPublisherMiddleware<T>>();
104#else
105 log_error("MiddlewareFactory: FastDDS not compiled in");
106 return nullptr;
107#endif
109#if defined(CARLA_ROS2_MIDDLEWARE_CYCLONEDDS) && !defined(CARLA_ROS2_MIDDLEWARE_TESTING)
110 return std::make_unique<CycloneDDSPublisherMiddleware<T>>();
111#else
112 log_error("MiddlewareFactory: CycloneDDS not compiled in");
113 return nullptr;
114#endif
116#if defined(CARLA_ROS2_MIDDLEWARE_ZENOH) && !defined(CARLA_ROS2_MIDDLEWARE_TESTING)
117 return std::make_unique<ZenohPublisherMiddleware<T>>();
118#else
119 log_error("MiddlewareFactory: Zenoh not compiled in");
120 return nullptr;
121#endif
122 }
123 return nullptr;
124 }
125
126 /// Create a subscriber middleware for traits type S.
127 /// S must provide:
128 /// S::msg_type — the message type
129 template<typename S>
130 static std::unique_ptr<ISubscriberMiddleware> CreateSubscriber() {
131 switch (GetActiveMiddleware()) {
133#if defined(CARLA_ROS2_MIDDLEWARE_FASTDDS) && !defined(CARLA_ROS2_MIDDLEWARE_TESTING)
134 return std::make_unique<FastDDSSubscriberMiddleware<S>>();
135#else
136 log_error("MiddlewareFactory: FastDDS not compiled in");
137 return nullptr;
138#endif
140#if defined(CARLA_ROS2_MIDDLEWARE_CYCLONEDDS) && !defined(CARLA_ROS2_MIDDLEWARE_TESTING)
141 return std::make_unique<CycloneDDSSubscriberMiddleware<S>>();
142#else
143 log_error("MiddlewareFactory: CycloneDDS not compiled in");
144 return nullptr;
145#endif
147#if defined(CARLA_ROS2_MIDDLEWARE_ZENOH) && !defined(CARLA_ROS2_MIDDLEWARE_TESTING)
148 return std::make_unique<ZenohSubscriberMiddleware<S>>();
149#else
150 log_error("MiddlewareFactory: Zenoh not compiled in");
151 return nullptr;
152#endif
153 }
154 return nullptr;
155 }
156
157 private:
158 /// Returns reference to the active middleware selection
159 /// (function-local static for C++11 thread safety).
161 static Middleware active_middleware = Middleware::FastDDS;
162 return active_middleware;
163 }
164};
165
166} // namespace ros2
167} // namespace carla
Factory that creates publisher/subscriber middleware based on the active middleware selection.
static void SetMiddleware(Middleware middleware)
Select the middleware for all subsequent publisher/subscriber creation.
static std::string GetAvailableMiddlewareString()
Return a readable list of middleware implementations compiled into this binary.
static MiddlewareResolution ResolveMiddleware(Middleware requested)
Resolve the requested middleware strictly — no fallback to other middleware.
static std::unique_ptr< ISubscriberMiddleware > CreateSubscriber()
Create a subscriber middleware for traits type S.
static std::unique_ptr< IPublisherMiddleware > CreatePublisher()
Create a publisher middleware for traits type T.
static bool IsMiddlewareAvailable(Middleware middleware)
Check whether a specific middleware was compiled into this binary.
static Middleware & GetActiveMiddleware()
Returns reference to the active middleware selection (function-local static for C++11 thread safety).
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
This file contains definitions of common data structures used in traffic manager.
static void log_error(Args &&... args)
Definition Logging.h:110
Result of middleware resolution — whether resolution succeeded and which middleware to use.