CARLA
Loading...
Searching...
No Matches
SubscriberImpl.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"
15
16namespace carla {
17namespace ros2 {
18
19// SubscriberImpl mirrors PublisherImpl on the subscriber side. It delegates all
20// DDS plumbing to an ISubscriberMiddleware obtained from
21// MiddlewareFactory::CreateSubscriber<Traits>(); the middleware writes arriving
22// samples directly into the SubscriberImpl-owned _message storage and raises the
23// _new_message flag passed down in Init(). The Traits struct only has to expose a
24// `msg_type` typedef naming a carla::ros2::msg::* POD. The next ProcessMessages
25// call on the owning BaseSubscriber drains a fresh sample via GetMessage().
26template <typename Traits>
28public:
29 using msg_type = typename Traits::msg_type;
30
31 bool Init(std::string topic_name) {
32 if (!EnsureMiddleware()) {
33 return false;
34 }
35 return _middleware->Init(topic_name, &_message, &_new_message);
36 }
37
38 /// Init with an explicit per-topic QoS profile (see QosProfile.h).
39 bool Init(std::string topic_name, const QosProfile& qos) {
40 if (!EnsureMiddleware()) {
41 return false;
42 }
43 return _middleware->Init(topic_name, &_message, &_new_message, qos);
44 }
45
46 std::string GetTopicName() {
47 if (_middleware) {
48 return _middleware->GetTopicName();
49 }
50 return "";
51 }
52
53 bool IsAlive() {
54 if (_middleware) {
55 return _middleware->IsAlive();
56 }
57 return false;
58 }
59
61 _new_message = false;
62 return _message;
63 }
64
65 bool HasNewMessage() { return _new_message; }
66
67#ifdef LIBCARLA_WITH_GTEST
68 void SetMiddlewareForTesting(std::unique_ptr<ISubscriberMiddleware> middleware) {
69 _middleware = std::move(middleware);
70 }
71
72 void SimulateMessageReceiptForTesting(const msg_type &msg) {
73 _message = msg;
74 _new_message = true;
75 }
76#endif
77
78private:
80#ifdef LIBCARLA_WITH_GTEST
81 // A test may inject a fake middleware before Init(); do not overwrite it.
82 if (!_middleware) {
83#endif
84 _middleware = MiddlewareFactory::CreateSubscriber<Traits>();
85 if (!_middleware) {
86 log_error("SubscriberImpl::Init failed to create middleware subscriber");
87 return false;
88 }
89#ifdef LIBCARLA_WITH_GTEST
90 }
91#endif
92 return true;
93 }
94
95 std::unique_ptr<ISubscriberMiddleware> _middleware;
97 bool _new_message{false};
98};
99
100} // namespace ros2
101} // 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)
typename Traits::msg_type msg_type
std::unique_ptr< ISubscriberMiddleware > _middleware
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