CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
ros2
middleware
MiddlewareConfig.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 <cstdlib>
8
9
namespace
carla
{
10
namespace
ros2
{
11
12
/// Sentinel domain id meaning "no domain id was configured on the command line";
13
/// each middleware falls back to its own native default.
14
constexpr
int
kUnsetDomainId
= -1;
15
16
/// Lowest valid ROS 2 domain id.
17
constexpr
int
kMinDomainId
= 0;
18
19
/// Highest valid ROS 2 domain id (RTPS maximum).
20
constexpr
int
kMaxDomainId
= 232;
21
22
/// Domain id used when neither the command line nor the environment provides one.
23
constexpr
int
kDefaultDomainId
= 0;
24
25
/// Name of the standard ROS 2 environment variable that selects the domain id.
26
constexpr
char
kRosDomainIdEnvVar
[] =
"ROS_DOMAIN_ID"
;
27
28
/// @return true if @a id is a valid ROS 2 domain id (0..232).
29
/// The unset sentinel is intentionally NOT valid: callers test for it explicitly.
30
inline
bool
IsValidDomainId
(
int
id
) {
31
return
id
>=
kMinDomainId
&&
id
<=
kMaxDomainId
;
32
}
33
34
/// Where the effective domain id came from, in priority order.
35
enum class
DomainIdSource
{
36
CommandLine
,
///< From the --ros-domain-id command line option.
37
Environment
,
///< From the ROS_DOMAIN_ID environment variable.
38
Default
///< Neither was set; the default domain (0) is used.
39
};
40
41
/// The resolved domain id together with the source it came from.
42
struct
ResolvedDomainId
{
43
int
id
;
44
DomainIdSource
source
;
45
};
46
47
/// Parse a ROS_DOMAIN_ID-style string into a valid domain id.
48
/// @return true and writes @a out only when @a text is a base-10 integer that is
49
/// a valid domain id (0..232). nullptr, empty, non-numeric, trailing-garbage and
50
/// out-of-range inputs return false and leave @a out untouched.
51
inline
bool
TryParseDomainId
(
const
char
* text,
int
& out) {
52
if
(text ==
nullptr
) {
53
return
false
;
54
}
55
char
* end =
nullptr
;
56
const
long
value = std::strtol(text, &end, 10);
57
if
(end == text) {
58
return
false
;
// No digits were consumed.
59
}
60
while
(*end ==
' '
|| *end ==
'\t'
|| *end ==
'\n'
|| *end ==
'\r'
) {
61
++end;
62
}
63
if
(*end !=
'\0'
) {
64
return
false
;
// Trailing non-numeric characters.
65
}
66
if
(value < kMinDomainId || value >
kMaxDomainId
) {
67
return
false
;
68
}
69
out =
static_cast<
int
>
(value);
70
return
true
;
71
}
72
73
/// Resolve the effective domain id from a command line value and an environment
74
/// value, in priority order:
75
/// 1. @a cli_value, when it is a valid domain id (0..232).
76
/// 2. @a env_value (e.g. ROS_DOMAIN_ID), when it parses to a valid domain id.
77
/// 3. kDefaultDomainId (0).
78
/// This function is pure (no environment access), so the precedence rules can be
79
/// unit-tested by passing the environment value as a string.
80
inline
ResolvedDomainId
ResolveDomainId
(
int
cli_value,
const
char
* env_value) {
81
if
(
IsValidDomainId
(cli_value)) {
82
return
{cli_value,
DomainIdSource::CommandLine
};
83
}
84
int
parsed = 0;
85
if
(
TryParseDomainId
(env_value, parsed)) {
86
return
{parsed,
DomainIdSource::Environment
};
87
}
88
return
{
kDefaultDomainId
,
DomainIdSource::Default
};
89
}
90
91
/// Process-wide runtime configuration shared by every ROS 2 middleware
92
/// implementation (FastDDS, CycloneDDS, and future ones such as Zenoh).
93
///
94
/// This belongs to the middleware abstraction layer, not to any single vendor:
95
/// each concrete middleware reads this configuration when it establishes its
96
/// transport context (a DDS DomainParticipant, a Zenoh session, ...) and
97
/// translates the abstract values into its own mechanism, mapping
98
/// kUnsetDomainId to its native default.
99
///
100
/// Set once at startup (via ROS2::Enable) before any publisher or subscriber is
101
/// created. Reads after that point are stable. Because it is set before any
102
/// entity exists, no synchronisation is required.
103
class
MiddlewareConfig
{
104
public
:
105
/// Configure the ROS 2 domain id for all subsequent transport contexts.
106
/// Pass kUnsetDomainId to keep each middleware's native default.
107
static
void
SetDomainId
(
int
id
) {
108
Storage
().
domain_id
= id;
109
}
110
111
/// @return The configured domain id, or kUnsetDomainId if none was set.
112
static
int
GetDomainId
() {
113
return
Storage
().
domain_id
;
114
}
115
116
/// Resolve the effective domain id and its source from the configured value,
117
/// the ROS_DOMAIN_ID environment variable, and the default. See ResolveDomainId
118
/// for the priority order.
119
static
ResolvedDomainId
ResolveEffective
() {
120
return
ResolveDomainId
(
GetDomainId
(), std::getenv(
kRosDomainIdEnvVar
));
121
}
122
123
/// @return The effective domain id every middleware should use, after applying
124
/// the command-line value, the ROS_DOMAIN_ID environment variable and the
125
/// default in that order.
126
static
int
GetEffectiveDomainId
() {
127
return
ResolveEffective
().
id
;
128
}
129
130
private
:
131
struct
Data
{
132
int
domain_id
=
kUnsetDomainId
;
133
};
134
135
/// Function-local static: a single instance shared across all translation units.
136
static
Data
&
Storage
() {
137
static
Data
data;
138
return
data;
139
}
140
};
141
142
}
// namespace ros2
143
}
// namespace carla
ECarlaLightFlags::Default
@ Default
Definition
CarlaLight.h:45
carla::ros2::MiddlewareConfig
Process-wide runtime configuration shared by every ROS 2 middleware implementation (FastDDS,...
Definition
MiddlewareConfig.h:103
carla::ros2::MiddlewareConfig::GetEffectiveDomainId
static int GetEffectiveDomainId()
Definition
MiddlewareConfig.h:126
carla::ros2::MiddlewareConfig::GetDomainId
static int GetDomainId()
Definition
MiddlewareConfig.h:112
carla::ros2::MiddlewareConfig::ResolveEffective
static ResolvedDomainId ResolveEffective()
Resolve the effective domain id and its source from the configured value, the ROS_DOMAIN_ID environme...
Definition
MiddlewareConfig.h:119
carla::ros2::MiddlewareConfig::SetDomainId
static void SetDomainId(int id)
Configure the ROS 2 domain id for all subsequent transport contexts.
Definition
MiddlewareConfig.h:107
carla::ros2::MiddlewareConfig::Storage
static Data & Storage()
Function-local static: a single instance shared across all translation units.
Definition
MiddlewareConfig.h:136
carla::ros2
Definition
AutowareSteeringCompensation.h:15
carla::ros2::IsValidDomainId
bool IsValidDomainId(int id)
Definition
MiddlewareConfig.h:30
carla::ros2::kMinDomainId
constexpr int kMinDomainId
Lowest valid ROS 2 domain id.
Definition
MiddlewareConfig.h:17
carla::ros2::kDefaultDomainId
constexpr int kDefaultDomainId
Domain id used when neither the command line nor the environment provides one.
Definition
MiddlewareConfig.h:23
carla::ros2::kMaxDomainId
constexpr int kMaxDomainId
Highest valid ROS 2 domain id (RTPS maximum).
Definition
MiddlewareConfig.h:20
carla::ros2::ResolveDomainId
ResolvedDomainId ResolveDomainId(int cli_value, const char *env_value)
Resolve the effective domain id from a command line value and an environment value,...
Definition
MiddlewareConfig.h:80
carla::ros2::kUnsetDomainId
constexpr int kUnsetDomainId
Sentinel domain id meaning "no domain id was configured on the command line"; each middleware falls b...
Definition
MiddlewareConfig.h:14
carla::ros2::TryParseDomainId
bool TryParseDomainId(const char *text, int &out)
Parse a ROS_DOMAIN_ID-style string into a valid domain id.
Definition
MiddlewareConfig.h:51
carla::ros2::DomainIdSource
DomainIdSource
Where the effective domain id came from, in priority order.
Definition
MiddlewareConfig.h:35
carla::ros2::DomainIdSource::Environment
@ Environment
From the ROS_DOMAIN_ID environment variable.
Definition
MiddlewareConfig.h:37
carla::ros2::DomainIdSource::Default
@ Default
Neither was set; the default domain (0) is used.
Definition
MiddlewareConfig.h:38
carla::ros2::DomainIdSource::CommandLine
@ CommandLine
From the –ros-domain-id command line option.
Definition
MiddlewareConfig.h:36
carla::ros2::kRosDomainIdEnvVar
constexpr char kRosDomainIdEnvVar[]
Name of the standard ROS 2 environment variable that selects the domain id.
Definition
MiddlewareConfig.h:26
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::ros2::MiddlewareConfig::Data
Definition
MiddlewareConfig.h:131
carla::ros2::MiddlewareConfig::Data::domain_id
int domain_id
Definition
MiddlewareConfig.h:132
carla::ros2::ResolvedDomainId
The resolved domain id together with the source it came from.
Definition
MiddlewareConfig.h:42
carla::ros2::ResolvedDomainId::source
DomainIdSource source
Definition
MiddlewareConfig.h:44
carla::ros2::ResolvedDomainId::id
int id
Definition
MiddlewareConfig.h:43
Generated by
1.18.0