CARLA
Loading...
Searching...
No Matches
PublisherImpl.h
Go to the documentation of this file.
1// Copyright (c) 2026 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 <memory>
10#include <string>
11
12#include "carla/Logging.h"
14
15namespace carla {
16namespace ros2 {
17
18// PublisherImpl owns the transport-neutral publish path for a single message
19// type. It delegates all DDS plumbing to an IPublisherMiddleware obtained from
20// MiddlewareFactory::CreatePublisher<Traits>(), so the concrete middleware
21// (FastDDS, CycloneDDS, ...) is selected at runtime without any vendor header
22// reaching this template. The Traits struct only has to expose a `msg_type`
23// typedef naming a carla::ros2::msg::* POD; the CDR type name/hash/size come
24// from the CdrTopicInfo<msg_type> specialisation consumed inside the middleware.
25// Concrete publishers hold a std::shared_ptr<PublisherImpl<Traits>>, populate
26// the owned message in place via GetMessage(), then call Publish().
27template <typename Traits>
29public:
30 using msg_type = typename Traits::msg_type;
31
32 bool Init(std::string topic_name) {
33 if (!EnsureMiddleware()) {
34 return false;
35 }
36 return _middleware->Init(topic_name);
37 }
38
39 /// Init with an explicit per-topic QoS profile (see QosProfile.h).
40 bool Init(std::string topic_name, const QosProfile& qos) {
41 if (!EnsureMiddleware()) {
42 return false;
43 }
44 return _middleware->Init(topic_name, qos);
45 }
46
47 std::string GetTopicName() {
48 if (_middleware) {
49 return _middleware->GetTopicName();
50 }
51 return "";
52 }
53
54 bool IsAlive() {
55 if (_middleware) {
56 return _middleware->IsAlive();
57 }
58 return false;
59 }
60
62
63 bool Publish() {
64 if (!_middleware) {
65 log_error("PublisherImpl::Publish called before Init");
66 return false;
67 }
68 return _middleware->Publish(&_message);
69 }
70
71#ifdef LIBCARLA_WITH_GTEST
72 void SetMiddlewareForTesting(std::unique_ptr<IPublisherMiddleware> middleware) {
73 _middleware = std::move(middleware);
74 }
75#endif
76
77private:
79#ifdef LIBCARLA_WITH_GTEST
80 // A test may inject a fake middleware before Init(); do not overwrite it.
81 if (!_middleware) {
82#endif
83 _middleware = MiddlewareFactory::CreatePublisher<Traits>();
84 if (!_middleware) {
85 log_error("PublisherImpl::Init failed to create middleware publisher");
86 return false;
87 }
88#ifdef LIBCARLA_WITH_GTEST
89 }
90#endif
91 return true;
92 }
93
94 std::unique_ptr<IPublisherMiddleware> _middleware;
96};
97
98} // namespace ros2
99} // namespace carla
bool Init(std::string topic_name, const QosProfile &qos)
Init with an explicit per-topic QoS profile (see QosProfile.h).
bool Init(std::string topic_name)
std::unique_ptr< IPublisherMiddleware > _middleware
typename Traits::msg_type msg_type
This file contains definitions of common data structures used in traffic manager.
static void log_error(Args &&... args)
Definition Logging.h:110
Middleware-neutral QoS profile passed to IPublisherMiddleware::Init / ISubscriberMiddleware::Init whe...
Definition QosProfile.h:27