Skip to content

Commit

Permalink
Apply changes
Browse files Browse the repository at this point in the history
Signed-off-by: Irene Bandera <irenebandera@eprosima.com>
  • Loading branch information
irenebm committed Nov 3, 2023
1 parent 8fe9072 commit 0af87a7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 29 deletions.
98 changes: 69 additions & 29 deletions cpp_utils/include/cpp_utils/ros2_mangling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,104 +23,144 @@ namespace eprosima {
namespace utils {

/**
* @param[in] name string that will be stripped from prefix
* @param[in] prefix prefix to be stripped
* @brief Remove a specified prefix from a string.
*
* @return name stripped of prefix, or
* @return "" if name doesn't start with prefix
* This function takes a string and a prefix as input. It checks if the string starts with the specified prefix
* followed by a forward slash ('/'). If the prefix exists at the beginning of the string, it removes the prefix
* and the following slash and returns the modified string. If the prefix is not found at the beginning of the
* string, an empty string is returned.
*
* @param[in] name The input string from which the prefix will be removed.
* @param[in] prefix The prefix to be removed from the input string.
*
* @return The modified string with the specified prefix removed, or an empty string if the prefix is not present
* at the beginning of the input string.
*/
CPP_UTILS_DllAPI
std::string remove_prefix(
const std::string& name,
const std::string& prefix);

/**
* @param[in] name string to be prefixed with "prefix"
* @param[in] prefix prefix to be added
* @brief Add a specified prefix to a string.
*
* This function takes a string and a prefix as input. It concatenates the prefix with the input string
* and returns the resulting string with the prefix added.
*
* @return Returns the name with the prefix added "prefixname"
* @param[in] name The input string to which the prefix will be added.
* @param[in] prefix The prefix to be added to the input string.
*
* @return The modified string with the specified prefix added.
*/
CPP_UTILS_DllAPI
std::string add_prefix(
const std::string& name,
const std::string& prefix);

/**
* @param[in] name string to be suffixed with "suffix"
* @param[in] suffix suffix to be added
* @brief Add a specified suffix to a string.
*
* This function takes a string and a suffix as input. It concatenates the suffix with the input string
* and returns the resulting string with the suffix added.
*
* @return Returns the name with the suffix added "namesuffix"
* @param[in] name The input string to which the suffix will be added.
* @param[in] suffix The suffix to be added to the input string.
*
* @return The modified string with the specified suffix added.
*/
CPP_UTILS_DllAPI
std::string add_suffix(
const std::string& name,
const std::string& suffix);

/**
* @param[in] topic_name The name of the topic to be processed.
* @brief Get a ROS prefix if it exists at the beginning of a topic name.
*
* This function takes a topic name as input and checks if it starts with any of the ROS prefixes.
* If a matching ROS prefix is found at the beginning of the topic name, the prefix is returned.
* If no matching prefix is found, an empty string is returned.
*
* @param[in] topic_name The input topic name to be checked for a ROS prefix.
*
* @return Returns the ROS specific prefix if present, otherwise "".
* @return The ROS prefix found at the beginning of the topic name, or an empty string if no prefix exists.
*/
CPP_UTILS_DllAPI
std::string get_ros_prefix_if_exists(
const std::string& topic_name);

/**
* @brief Remove the ROS specific prefix from the \c topic_name if it exists.
* @brief Remove a ROS prefix if it exists at the beginning of a topic name.
*
* This function takes a topic name as input and checks if it starts with any of the ROS prefixes.
* If a matching ROS prefix is found at the beginning of the topic name, the prefix is removed,
* and the modified topic name is returned. If no matching prefix is found, the original topic name is returned unchanged.
*
* @param[in] topic_name The name of the topic to be processed.
*
* @return Returns the topic name stripped of a ROS specific prefix if present.
* @return The topic name with the ROS prefix removed, or the original topic name if no prefix exists.
*/
CPP_UTILS_DllAPI
std::string remove_ros_prefix_if_exists(
const std::string& topic_name);

/**
* @brief Add the ROS Topic prefix in the \c topic_name.
* @brief Add the ROS topic prefix to a given topic name.
*
* @param[in] topic_name The name of the topic to be processed.
* This function takes a topic name as input and adds the ROS topic prefix to it.
* The modified topic name with the prefix added is returned.
*
* @param[in] topic_name The input topic name to which the ROS topic prefix will be added.
*
* @return Returns the topic name with the ROS Topic prefix.
* @return The modified topic name with the ROS topic prefix added.
*/
CPP_UTILS_DllAPI
std::string add_ros_topic_prefix(
const std::string& topic_name);

/**
* @brief Add the ROS Service Requester prefix in the \c topic_name.
* @brief Add the ROS service requester prefix to a given topic name.
*
* @param[in] topic_name The name of the topic to be processed.
* This function takes a topic name as input and adds the ROS service requester prefix to it.
* The modified topic name with the prefix added is returned.
*
* @param[in] topic_name The input topic name to which the ROS service requester prefix will be added.
*
* @return Returns the topic name with the ROS Requester prefix.
* @return The modified topic name with the ROS service requester prefix added.
*/
CPP_UTILS_DllAPI
std::string add_ros_service_requester_prefix(
const std::string& topic_name);

/**
* @brief Add the ROS Service Response prefix in the \c topic_name.
* @brief Add the ROS service response prefix to a given topic name.
*
* @param[in] topic_name The name of the topic to be processed.
* This function takes a topic name as input and adds the ROS service response prefix to it.
* The modified topic name with the prefix added is returned.
*
* @param[in] topic_name The input topic name to which the ROS service response prefix will be added.
*
* @return Returns the topic name with the ROS Response prefix.
* @return The modified topic name with the ROS service response prefix added.
*/
CPP_UTILS_DllAPI
std::string add_ros_service_response_prefix(
const std::string& topic_name);

/**
* @return Returns \c ros_prefixes_.
* @brief Get a reference to the collection of all ROS prefixes.
*
* This function returns a reference to the collection of ROS prefixes stored in the `ros_prefixes_` variable.
*
* @return A reference to the collection of all ROS prefixes.
*/
CPP_UTILS_DllAPI
const std::vector<std::string>& get_all_ros_prefixes();

/**
* @brief Obtain the ROS topic associated with the provided \c topic_name,
* excluding the ROS prefix.
* @brief Demangle a ROS topic name by removing the ROS prefix if it exists.
*
* If the topic_name begins with a ROS prefix (i.e., starts with ROS prefix + "/").
* If the input topic name does not contain a ROS prefix + "/", it is returned unchanged.
* This function takes a ROS topic name as input and attempts to demangle it by removing the ROS prefix.
* If the input topic name contains a ROS prefix, it is removed. If there is no ROS prefix, the topic name
* is returned unchanged.
*
* @param[in] topic_name The name of the topic to be processed.
*
Expand Down Expand Up @@ -205,7 +245,7 @@ CPP_UTILS_DllAPI
std::string demangle_ros_service_prefix_from_topic(
const std::string& topic_name);
/**
* @brief Demangle a ROS service request topic name by removing the requester prefix.
* @brief Demangle a ROS service request topic name by removing the requester prefix and "Request" suffix.
*
* This function takes a ROS service request topic name as input and removes the specified requester prefix and the common "Request" suffix.
* If the input topic name does not contain both the requester prefix
Expand Down
5 changes: 5 additions & 0 deletions cpp_utils/test/unittest/ros2_mangling/ROS2ManglingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ TEST(ROS2ManglingTest, demangle_service_type_only)
* - DDS Service Name: "rt/hello" Return: ""
* - DDS Service Name: "rq/srv/hello" Return: "rq::srv::dds_::hello_Request_"
* - DDS Service Name: "rr/srv/hello" Return: "rr::srv::dds_::hello_Response_"
* - DDS Service Name: "/srv/hello" Return: ""
* - DDS Service Name: "/srv/hello" Return: ""
* - DDS Service Name: "rq::dds_::hello" Return: ""
* - DDS Service Name: "rr::dds_::hello" Return: ""
* - DDS Service Name: "rt::dds_::hello" Return: ""
Expand All @@ -491,6 +493,9 @@ TEST(ROS2ManglingTest, mangle_service_type_only)
EXPECT_EQ("rq::srv::dds_::hello_Request_", mangle_service_type_only("rq/srv/hello"));
EXPECT_EQ("rr::srv::dds_::hello_Response_", mangle_service_type_only("rr/srv/hello"));

EXPECT_EQ("", mangle_service_type_only("/srv/hello"));
EXPECT_EQ("", mangle_service_type_only("/srv/hello"));

EXPECT_EQ("", mangle_service_type_only("rq::dds_::hello"));
EXPECT_EQ("", mangle_service_type_only("rr::dds_::hello"));
EXPECT_EQ("", mangle_service_type_only("rt::dds_::hello"));
Expand Down

0 comments on commit 0af87a7

Please sign in to comment.