Skip to content

Commit 172e867

Browse files
author
Chen Lihui
committed
make use of template using compile time instead of runtime to call the funcs
Signed-off-by: Chen Lihui <[email protected]>
1 parent dc1a403 commit 172e867

File tree

2 files changed

+90
-49
lines changed

2 files changed

+90
-49
lines changed

rclcpp/include/rclcpp/typesupport_helpers.hpp

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,52 @@ get_typesupport_library(const std::string & type, const std::string & typesuppor
4545
namespace internal
4646
{
4747

48-
/// The type support of ros idl
49-
enum class TypeSupport
50-
{
51-
UNKNOWN,
52-
MESSAGE,
53-
SERVICE,
54-
ACTION
48+
struct typesupport_message_tag {};
49+
struct typesupport_service_tag {};
50+
struct typesupport_action_tag {};
51+
52+
template <typename T>
53+
struct typesupport_traits {
54+
typedef T type;
55+
};
56+
57+
template <>
58+
struct typesupport_traits<rosidl_message_type_support_t> {
59+
typedef typesupport_message_tag type_tag;
60+
};
61+
62+
template <>
63+
struct typesupport_traits<rosidl_service_type_support_t> {
64+
typedef typesupport_service_tag type_tag;
65+
};
66+
67+
template <>
68+
struct typesupport_traits<rosidl_action_type_support_t> {
69+
typedef typesupport_action_tag type_tag;
5570
};
5671

5772
using SymbolNameErrorFuncPair = std::pair<std::string, std::function<std::string()>>;
5873

59-
/// Get the symbol name.
74+
/// Get the symbol name with different typesupport tags.
75+
RCLCPP_PUBLIC
76+
SymbolNameErrorFuncPair _get_typesupport_symbol_name(
77+
const std::string & type,
78+
const std::string & typesupport_identifier,
79+
typesupport_message_tag
80+
);
81+
82+
RCLCPP_PUBLIC
83+
SymbolNameErrorFuncPair _get_typesupport_symbol_name(
84+
const std::string & type,
85+
const std::string & typesupport_identifier,
86+
typesupport_service_tag
87+
);
88+
6089
RCLCPP_PUBLIC
61-
SymbolNameErrorFuncPair get_typesupport_symbol_name(
90+
SymbolNameErrorFuncPair _get_typesupport_symbol_name(
6291
const std::string & type,
6392
const std::string & typesupport_identifier,
64-
TypeSupport type_support
93+
typesupport_action_tag
6594
);
6695

6796
} // namespace internal
@@ -82,16 +111,8 @@ get_typesupport_handle(
82111
const std::string & typesupport_identifier,
83112
rcpputils::SharedLibrary & library)
84113
{
85-
internal::TypeSupport typesupport = internal::TypeSupport::UNKNOWN;
86-
if constexpr (std::is_same_v<T, rosidl_message_type_support_t>) {
87-
typesupport = internal::TypeSupport::MESSAGE;
88-
} else if constexpr (std::is_same_v<T, rosidl_service_type_support_t>) {
89-
typesupport = internal::TypeSupport::SERVICE;
90-
} else if constexpr (std::is_same_v<T, rosidl_action_type_support_t>) {
91-
typesupport = internal::TypeSupport::ACTION;
92-
}
93-
94-
auto ret = internal::get_typesupport_symbol_name(type, typesupport_identifier, typesupport);
114+
auto ret = internal::_get_typesupport_symbol_name(
115+
type, typesupport_identifier, typename internal::typesupport_traits<T>::type_tag());
95116
try {
96117
const T * (* get_ts)() = nullptr;
97118
// This will throw runtime_error if the symbol was not found.

rclcpp/src/rclcpp/typesupport_helpers.cpp

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,44 +96,22 @@ extract_type_identifier(const std::string & full_type)
9696
namespace internal
9797
{
9898

99-
SymbolNameErrorFuncPair get_typesupport_symbol_name(
99+
static SymbolNameErrorFuncPair _get_typesupport_symbol_name_impl(
100100
const std::string & type,
101101
const std::string & typesupport_identifier,
102-
TypeSupport type_support)
102+
const std::string & typesupport_name,
103+
const std::string & symbol_part_name,
104+
const std::string & middle_module_additional)
103105
{
104106
std::string package_name;
105107
std::string middle_module;
106108
std::string type_name;
107109
std::tie(package_name, middle_module, type_name) = extract_type_identifier(type);
108110

109-
std::string typesupport_name;
110-
std::string symbol_part_name;
111-
switch (type_support) {
112-
case TypeSupport::MESSAGE:
113-
typesupport_name = "message";
114-
symbol_part_name = "__get_message_type_support_handle__";
115-
if (middle_module.empty()) {
116-
middle_module = "msg";
117-
}
118-
break;
119-
case TypeSupport::SERVICE:
120-
typesupport_name = "service";
121-
symbol_part_name = "__get_service_type_support_handle__";
122-
if (middle_module.empty()) {
123-
middle_module = "srv";
124-
}
125-
break;
126-
case TypeSupport::ACTION:
127-
typesupport_name = "action";
128-
symbol_part_name = "__get_action_type_support_handle__";
129-
if (middle_module.empty()) {
130-
middle_module = "action";
131-
}
132-
break;
133-
default:
134-
typesupport_name = "unknown";
135-
break;
111+
if (middle_module.empty()) {
112+
middle_module = middle_module_additional;
136113
}
114+
137115
auto mk_error = [&package_name, &type_name, &typesupport_name]() {
138116
std::stringstream rcutils_dynamic_loading_error;
139117
rcutils_dynamic_loading_error <<
@@ -149,6 +127,48 @@ SymbolNameErrorFuncPair get_typesupport_symbol_name(
149127
return {std::move(symbol_name), std::move(mk_error)};
150128
}
151129

130+
SymbolNameErrorFuncPair _get_typesupport_symbol_name(
131+
const std::string & type,
132+
const std::string & typesupport_identifier,
133+
typesupport_message_tag)
134+
{
135+
std::string typesupport_name = "message";
136+
std::string symbol_part_name = "__get_message_type_support_handle__";
137+
std::string middle_module_additional = "msg";
138+
139+
return _get_typesupport_symbol_name_impl(
140+
type, typesupport_identifier, typesupport_name, symbol_part_name, middle_module_additional
141+
);
142+
}
143+
144+
SymbolNameErrorFuncPair _get_typesupport_symbol_name(
145+
const std::string & type,
146+
const std::string & typesupport_identifier,
147+
typesupport_service_tag)
148+
{
149+
std::string typesupport_name = "service";
150+
std::string symbol_part_name = "__get_service_type_support_handle__";
151+
std::string middle_module_additional = "srv";
152+
153+
return _get_typesupport_symbol_name_impl(
154+
type, typesupport_identifier, typesupport_name, symbol_part_name, middle_module_additional
155+
);
156+
}
157+
158+
SymbolNameErrorFuncPair _get_typesupport_symbol_name(
159+
const std::string & type,
160+
const std::string & typesupport_identifier,
161+
typesupport_action_tag)
162+
{
163+
std::string typesupport_name = "action";
164+
std::string symbol_part_name = "__get_action_type_support_handle__";
165+
std::string middle_module_additional = "action";
166+
167+
return _get_typesupport_symbol_name_impl(
168+
type, typesupport_identifier, typesupport_name, symbol_part_name, middle_module_additional
169+
);
170+
}
171+
152172
} // namespace internal
153173

154174
std::shared_ptr<rcpputils::SharedLibrary>

0 commit comments

Comments
 (0)