Skip to content

Commit

Permalink
get_zero_initialized_xxx functions return zero initialized structure. (
Browse files Browse the repository at this point in the history
…#380)

* get_zero_initialized_xxx functions return zero initialized structure.

Signed-off-by: Tomoya Fujita <[email protected]>

* introduce RMW_EVENT_TYPE_MAX in rmw_event_type_t.

Signed-off-by: Tomoya Fujita <[email protected]>

* add a comment and more tests for rmw_event_type.

Signed-off-by: Tomoya Fujita <[email protected]>

---------

Signed-off-by: Tomoya Fujita <[email protected]>
  • Loading branch information
fujitatomoya authored Feb 5, 2025
1 parent 2a95768 commit f8b3563
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 26 deletions.
5 changes: 4 additions & 1 deletion rmw/include/rmw/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ extern "C"
/// Define publisher/subscription events
typedef enum rmw_event_type_e
{
// must be zero or rmw_get_zero_initialized_event will break
RMW_EVENT_INVALID = 0,

// subscription events
RMW_EVENT_LIVELINESS_CHANGED,
RMW_EVENT_REQUESTED_DEADLINE_MISSED,
Expand All @@ -48,7 +51,7 @@ typedef enum rmw_event_type_e
RMW_EVENT_PUBLICATION_MATCHED,

// sentinel value
RMW_EVENT_INVALID
RMW_EVENT_TYPE_MAX
} rmw_event_type_t;

/// Encapsulate the RMW event implementation, data, and type.
Expand Down
9 changes: 2 additions & 7 deletions rmw/src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ extern "C" {
rmw_event_t
rmw_get_zero_initialized_event(void)
{
// TODO(@fujitatomoya): This is not exatly zero initialized structure.
/// We should introduce xxx_get_default_event to return the default values.
static const rmw_event_t event = {
.implementation_identifier = NULL,
.data = NULL,
.event_type = RMW_EVENT_INVALID
};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_event_t event;
return event;
}

Expand Down
10 changes: 3 additions & 7 deletions rmw/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ extern "C"
rmw_context_t
rmw_get_zero_initialized_context(void)
{
return (const rmw_context_t) {
.instance_id = 0,
.implementation_identifier = NULL,
.options = rmw_get_zero_initialized_init_options(),
.actual_domain_id = 0u,
.impl = NULL
}; // NOLINT(readability/braces): false positive
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_context_t context;
return context;
}

#ifdef __cplusplus
Expand Down
13 changes: 2 additions & 11 deletions rmw/src/init_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,8 @@ extern "C"
rmw_init_options_t
rmw_get_zero_initialized_init_options(void)
{
// TODO(@fujitatomoya): This is not exatly zero initialized structure.
/// We should introduce xxx_get_default_init_optionst to return the default values.
static const rmw_init_options_t init_option = {
.domain_id = RMW_DEFAULT_DOMAIN_ID,
.discovery_options = {RMW_AUTOMATIC_DISCOVERY_RANGE_NOT_SET, 0},
.implementation_identifier = NULL,
.impl = NULL,
.instance_id = 0,
.enclave = NULL,
.security_options = {RMW_SECURITY_ENFORCEMENT_PERMISSIVE, NULL},
};
// All members are initialized to 0 or NULL by C99 6.7.8/10.
static const rmw_init_options_t init_option;
return init_option;
}

Expand Down
1 change: 1 addition & 0 deletions rmw/test/test_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ TEST(rmw_event, get_zero_initialized_event)
const rmw_event_t actual = rmw_get_zero_initialized_event();
EXPECT_EQ(nullptr, actual.implementation_identifier);
EXPECT_EQ(nullptr, actual.data);
EXPECT_EQ(0, RMW_EVENT_INVALID);
EXPECT_EQ(RMW_EVENT_INVALID, actual.event_type);
}

Expand Down
2 changes: 2 additions & 0 deletions rmw/test/test_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ TEST(rmw_init_options, get_zero_initialized_init_options)
{
const rmw_context_t context = rmw_get_zero_initialized_context();
EXPECT_EQ(context.instance_id, 0u);
EXPECT_EQ(context.implementation_identifier, nullptr);
EXPECT_EQ(context.actual_domain_id, 0u);
EXPECT_EQ(context.impl, nullptr);
}
10 changes: 10 additions & 0 deletions rmw/test/test_init_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
TEST(rmw_init_options, get_zero_initialized_init_options)
{
const rmw_init_options_t options = rmw_get_zero_initialized_init_options();
EXPECT_EQ(options.domain_id, 0u);
EXPECT_EQ(options.instance_id, 0u);
EXPECT_EQ(options.implementation_identifier, nullptr);
EXPECT_EQ(options.impl, nullptr);
EXPECT_EQ(options.enclave, nullptr);
EXPECT_EQ(
options.security_options.enforce_security,
RMW_SECURITY_ENFORCEMENT_PERMISSIVE);
EXPECT_EQ(
options.security_options.security_root_path, nullptr);
EXPECT_EQ(
options.discovery_options.automatic_discovery_range,
RMW_AUTOMATIC_DISCOVERY_RANGE_NOT_SET);
}

0 comments on commit f8b3563

Please sign in to comment.