Skip to content

Commit

Permalink
Add function for getting a types fully qualified name (#514)
Browse files Browse the repository at this point in the history
As opposed the type name 'foo::msg::Bar', the new function let's us get the ROS namespaced name 'foo/msg/Bar'.

Signed-off-by: Jacob Perron <[email protected]>
  • Loading branch information
jacobperron authored Aug 17, 2020
1 parent bbc154b commit d098918
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rosidl_generator_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ if(BUILD_TESTING)
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}
)
endif()
ament_add_gtest(test_name test/test_name.cpp)
if(TARGET test_name)
add_dependencies(test_name ${PROJECT_NAME})
ament_target_dependencies(test_name
rosidl_runtime_cpp
rosidl_runtime_c)
target_include_directories(test_name PUBLIC
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}
)
endif()
ament_add_gtest(test_traits test/test_traits.cpp)
if(TARGET test_traits)
add_dependencies(test_traits ${PROJECT_NAME})
Expand Down
7 changes: 7 additions & 0 deletions rosidl_generator_cpp/resource/msg__traits.hpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from rosidl_parser.definition import AbstractSequence
from rosidl_parser.definition import UnboundedSequence

message_typename = '::'.join(message.structure.namespaced_type.namespaced_name())
message_fully_qualified_name = '/'.join(message.structure.namespaced_type.namespaced_name())
}@
@
@#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Expand Down Expand Up @@ -66,6 +67,12 @@ inline const char * data_type<@(message_typename)>()
return "@(message_typename)";
}

template<>
inline const char * name<@(message_typename)>()
{
return "@(message_fully_qualified_name)";
}

@{
fixed_template_string = 'true'
fixed_template_strings = set()
Expand Down
7 changes: 7 additions & 0 deletions rosidl_generator_cpp/resource/srv__traits.hpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TEMPLATE(

@{
service_typename = '::'.join(service.namespaced_type.namespaced_name())
service_fully_qualified_name = '/'.join(service.namespaced_type.namespaced_name())
}@
@
namespace rosidl_generator_traits
Expand All @@ -26,6 +27,12 @@ inline const char * data_type<@(service_typename)>()
return "@(service_typename)";
}

template<>
inline const char * name<@(service_typename)>()
{
return "@(service_fully_qualified_name)";
}

template<>
struct has_fixed_size<@(service_typename)>
: std::integral_constant<
Expand Down
37 changes: 37 additions & 0 deletions rosidl_generator_cpp/test/test_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>
#include "rosidl_generator_cpp/msg/empty.hpp"
#include "rosidl_generator_cpp/msg/strings.hpp"
#include "rosidl_generator_cpp/srv/basic_types.hpp"
#include "rosidl_generator_cpp/srv/empty.hpp"

TEST(Test_rosidl_generator_traits, check_msg_name) {
ASSERT_STREQ(
"rosidl_generator_cpp/msg/Strings",
rosidl_generator_traits::name<rosidl_generator_cpp::msg::Strings>());
ASSERT_STREQ(
"rosidl_generator_cpp/msg/Empty",
rosidl_generator_traits::name<rosidl_generator_cpp::msg::Empty>());
}

TEST(Test_rosidl_generator_traits, check_srv_name) {
ASSERT_STREQ(
"rosidl_generator_cpp/srv/BasicTypes",
rosidl_generator_traits::name<rosidl_generator_cpp::srv::BasicTypes>());
ASSERT_STREQ(
"rosidl_generator_cpp/srv/Empty",
rosidl_generator_traits::name<rosidl_generator_cpp::srv::Empty>());
}
3 changes: 3 additions & 0 deletions rosidl_runtime_cpp/include/rosidl_runtime_cpp/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ namespace rosidl_generator_traits
template<typename T>
inline const char * data_type();

template<typename T>
inline const char * name();

template<typename T>
struct has_fixed_size : std::false_type {};

Expand Down

0 comments on commit d098918

Please sign in to comment.