Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions rcl/include/rcl/remap.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,31 @@ rcl_remap_node_namespace(
rcl_allocator_t allocator,
char ** output_namespace);

/// Copy one remap structure into another.
/**
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | Yes
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param[in] rule The structure to be copied.
* Its allocator is used to copy memory into the new structure.
* \param[out] rule_out A zero-initialized rcl_remap_t structure to be copied into.
* \return `RCL_RET_OK` if the structure was copied successfully, or
* \return `RCL_RET_INVALID_ARGUMENT` if any function arguments are invalid, or
* \return `RCL_RET_BAD_ALLOC` if allocating memory failed, or
* \return `RCL_RET_ERROR` if an unspecified error occurs.
*/
RCL_PUBLIC
RCL_WARN_UNUSED
rcl_ret_t
rcl_remap_copy(
const rcl_remap_t * rule,
rcl_remap_t * rule_out);

/// Reclaim resources held inside rcl_remap_t structure.
/**
* <hr>
Expand Down
25 changes: 0 additions & 25 deletions rcl/src/rcl/remap_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,6 @@ typedef struct rcl_remap_impl_t
rcl_allocator_t allocator;
} rcl_remap_impl_t;

/// Copy one remap structure into another.
/**
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | Yes
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param[in] rule The structure to be copied.
* Its allocator is used to copy memory into the new structure.
* \param[out] rule_out A zero-initialized rcl_remap_t structure to be copied into.
* \return `RCL_RET_OK` if the structure was copied successfully, or
* \return `RCL_RET_INVALID_ARGUMENT` if any function arguments are invalid, or
* \return `RCL_RET_BAD_ALLOC` if allocating memory failed, or
* \return `RCL_RET_ERROR` if an unspecified error occurs.
*/
RCL_LOCAL
RCL_WARN_UNUSED
rcl_ret_t
rcl_remap_copy(
const rcl_remap_t * rule,
rcl_remap_t * rule_out);

#ifdef __cplusplus
}
#endif
Expand Down
43 changes: 41 additions & 2 deletions rcl/test/rcl/test_remap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
#include "rcl/remap.h"
#include "rcl/error_handling.h"

#include "./allocator_testing_utils.h"
#include "./arg_macros.hpp"
#include "./arguments_impl.h"
#include "./allocator_testing_utils.h"
#include "./remap_impl.h"

#ifdef RMW_IMPLEMENTATION
# define CLASSNAME_(NAME, SUFFIX) NAME ## __ ## SUFFIX
Expand Down Expand Up @@ -574,3 +573,43 @@ TEST_F(CLASSNAME(TestRemapFixture, RMW_IMPLEMENTATION), _rcl_remap_name_bad_arg)
EXPECT_EQ(RCL_RET_ERROR, ret);
rcl_reset_error();
}

TEST_F(CLASSNAME(TestRemapFixture, RMW_IMPLEMENTATION), internal_remap_use) {
// Easiest way to init a rcl_remap is through the arguments API
const char * argv[] = {
"process_name", "--ros-args", "-r", "__ns:=/namespace", "random:=arg"
};
int argc = sizeof(argv) / sizeof(const char *);
rcl_allocator_t alloc = rcl_get_default_allocator();
rcl_arguments_t parsed_args = rcl_get_zero_initialized_arguments();

rcl_ret_t ret = rcl_parse_arguments(argc, argv, alloc, &parsed_args);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
EXPECT_EQ(RCL_RET_OK, rcl_arguments_fini(&parsed_args));
});

// Bad alloc
rcl_remap_t remap_dst = rcl_get_zero_initialized_remap();
parsed_args.impl->remap_rules->impl->allocator = get_failing_allocator();
EXPECT_EQ(RCL_RET_BAD_ALLOC, rcl_remap_copy(parsed_args.impl->remap_rules, &remap_dst));
parsed_args.impl->remap_rules->impl->allocator = alloc;

// Expected usage
EXPECT_EQ(RCL_RET_OK, rcl_remap_copy(parsed_args.impl->remap_rules, &remap_dst));

// Copy twice
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_remap_copy(parsed_args.impl->remap_rules, &remap_dst));
rcl_reset_error();

// Fini
EXPECT_EQ(RCL_RET_OK, rcl_remap_fini(&remap_dst));

// Fini twice
EXPECT_EQ(RCL_RET_ERROR, rcl_remap_fini(&remap_dst));
rcl_reset_error();

// Bad fini
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rcl_remap_fini(nullptr));
}