CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
ros2
publishers
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
"
13
#include "
carla/ros2/middleware/MiddlewareFactory.h
"
14
15
namespace
carla
{
16
namespace
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().
27
template
<
typename
Traits>
28
class
PublisherImpl
{
29
public
:
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
61
msg_type
*
GetMessage
() {
return
&
_message
; }
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
77
private
:
78
bool
EnsureMiddleware
() {
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
;
95
msg_type
_message
{};
96
};
97
98
}
// namespace ros2
99
}
// namespace carla
Logging.h
MiddlewareFactory.h
carla::ros2::MiddlewareFactory::CreatePublisher
static std::unique_ptr< IPublisherMiddleware > CreatePublisher()
Create a publisher middleware for traits type T.
Definition
MiddlewareFactory.h:99
carla::ros2::PublisherImpl
Definition
PublisherImpl.h:28
carla::ros2::PublisherImpl::_message
msg_type _message
Definition
PublisherImpl.h:95
carla::ros2::PublisherImpl::Init
bool Init(std::string topic_name, const QosProfile &qos)
Init with an explicit per-topic QoS profile (see QosProfile.h).
Definition
PublisherImpl.h:40
carla::ros2::PublisherImpl::EnsureMiddleware
bool EnsureMiddleware()
Definition
PublisherImpl.h:78
carla::ros2::PublisherImpl::GetMessage
msg_type * GetMessage()
Definition
PublisherImpl.h:61
carla::ros2::PublisherImpl::Publish
bool Publish()
Definition
PublisherImpl.h:63
carla::ros2::PublisherImpl::Init
bool Init(std::string topic_name)
Definition
PublisherImpl.h:32
carla::ros2::PublisherImpl::_middleware
std::unique_ptr< IPublisherMiddleware > _middleware
Definition
PublisherImpl.h:94
carla::ros2::PublisherImpl::GetTopicName
std::string GetTopicName()
Definition
PublisherImpl.h:47
carla::ros2::PublisherImpl::IsAlive
bool IsAlive()
Definition
PublisherImpl.h:54
carla::ros2::PublisherImpl::msg_type
typename Traits::msg_type msg_type
Definition
PublisherImpl.h:30
carla::ros2
Definition
AutowareSteeringCompensation.h:15
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::ros2::QosProfile
Middleware-neutral QoS profile passed to IPublisherMiddleware::Init / ISubscriberMiddleware::Init whe...
Definition
QosProfile.h:27
Generated by
1.18.0