Add rcl_node_get_fully_qualified_name (#255)

This commit is contained in:
RARvolt 2019-01-01 23:37:39 +01:00 committed by Tully Foote
parent e9279d1914
commit 586ae664c5
3 changed files with 71 additions and 0 deletions

View file

@ -352,6 +352,29 @@ RCL_WARN_UNUSED
const char * const char *
rcl_node_get_namespace(const rcl_node_t * node); rcl_node_get_namespace(const rcl_node_t * node);
/// Return the fully qualified name of the node.
/**
* This function returns the node's internal namespace and name combined string.
* This function can fail, and therefore return `NULL`, if:
* - node is `NULL`
* - node has not been initialized (the implementation is invalid)
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | Yes
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param[in] node pointer to the node
* \return fully qualified name string if successful, otherwise `NULL`
*/
RCL_PUBLIC
RCL_WARN_UNUSED
const char *
rcl_node_get_fully_qualified_name(const rcl_node_t * node);
/// Return the rcl node options. /// Return the rcl node options.
/** /**
* This function returns the node's internal options struct. * This function returns the node's internal options struct.

View file

@ -603,6 +603,31 @@ rcl_node_get_namespace(const rcl_node_t * node)
return node->impl->rmw_node_handle->namespace_; return node->impl->rmw_node_handle->namespace_;
} }
const char *
rcl_node_get_fully_qualified_name(const rcl_node_t * node)
{
if (!rcl_node_is_valid_except_context(node)) {
return NULL; // error already set
}
const char * name = rcl_node_get_name(node);
const char * ns = rcl_node_get_namespace(node);
char * fq_name = NULL;
if ('/' == ns[strlen(ns) - 1]) {
fq_name = (char *)calloc(strlen(ns) + strlen(name), sizeof(char));
strcpy(fq_name, ns);
strcat(fq_name, name);
}
else {
fq_name = (char *)calloc(strlen(ns) + strlen(name) + 1, sizeof(char));
strcpy(fq_name, ns);
strcat(fq_name, "/");
strcat(fq_name, name);
}
return fq_name;
}
const rcl_node_options_t * const rcl_node_options_t *
rcl_node_get_options(const rcl_node_t * node) rcl_node_get_options(const rcl_node_t * node)
{ {

View file

@ -16,6 +16,7 @@
#include <regex> #include <regex>
#include <string> #include <string>
#include <cstdlib>
#include "rcl/rcl.h" #include "rcl/rcl.h"
#include "rcl/node.h" #include "rcl/node.h"
@ -99,6 +100,7 @@ TEST_F(CLASSNAME(TestNodeFixture, RMW_IMPLEMENTATION), test_rcl_node_accessors)
rcl_node_t invalid_node = rcl_get_zero_initialized_node(); rcl_node_t invalid_node = rcl_get_zero_initialized_node();
const char * name = "test_rcl_node_accessors_node"; const char * name = "test_rcl_node_accessors_node";
const char * namespace_ = "/ns"; const char * namespace_ = "/ns";
const char * fq_name = "/ns/test_rcl_node_accessors_node";
rcl_node_options_t default_options = rcl_node_get_default_options(); rcl_node_options_t default_options = rcl_node_get_default_options();
default_options.domain_id = 42; // Set the domain id to something explicit. default_options.domain_id = 42; // Set the domain id to something explicit.
ret = rcl_node_init(&invalid_node, name, namespace_, &invalid_context, &default_options); ret = rcl_node_init(&invalid_node, name, namespace_, &invalid_context, &default_options);
@ -204,6 +206,27 @@ TEST_F(CLASSNAME(TestNodeFixture, RMW_IMPLEMENTATION), test_rcl_node_accessors)
if (actual_node_namespace) { if (actual_node_namespace) {
EXPECT_EQ(std::string(namespace_), std::string(actual_node_namespace)); EXPECT_EQ(std::string(namespace_), std::string(actual_node_namespace));
} }
// Test rcl_node_get_fully_qualified_name().
const char * actual_fq_node_name;
actual_fq_node_name = rcl_node_get_fully_qualified_name(nullptr);
EXPECT_EQ(nullptr, actual_fq_node_name);
rcl_reset_error();
actual_fq_node_name = rcl_node_get_fully_qualified_name(&zero_node);
EXPECT_EQ(nullptr, actual_fq_node_name);
rcl_reset_error();
actual_fq_node_name = rcl_node_get_fully_qualified_name(&invalid_node);
EXPECT_TRUE(actual_fq_node_name ? true : false);
if (actual_fq_node_name) {
EXPECT_STREQ(fq_name, actual_fq_node_name);
free((char *)actual_fq_node_name);
}
rcl_reset_error();
actual_fq_node_name = rcl_node_get_fully_qualified_name(&node);
EXPECT_TRUE(actual_fq_node_name ? true : false);
if (actual_fq_node_name) {
EXPECT_EQ(std::string(fq_name), std::string(actual_fq_node_name));
free((char *)actual_fq_node_name);
}
// Test rcl_node_get_logger_name(). // Test rcl_node_get_logger_name().
const char * actual_node_logger_name; const char * actual_node_logger_name;
actual_node_logger_name = rcl_node_get_logger_name(nullptr); actual_node_logger_name = rcl_node_get_logger_name(nullptr);