ROS 2 rclcpp + rcl - jazzy  jazzy
ROS 2 C++ Client Library with ROS Client Library
get_message_type_support_handle.hpp
1 // Copyright 2021 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__GET_MESSAGE_TYPE_SUPPORT_HANDLE_HPP_
16 #define RCLCPP__GET_MESSAGE_TYPE_SUPPORT_HANDLE_HPP_
17 
18 #include <type_traits>
19 
20 #include "rosidl_runtime_cpp/traits.hpp"
21 #include "rosidl_runtime_cpp/message_type_support_decl.hpp"
22 #include "rosidl_typesupport_cpp/message_type_support.hpp"
23 
24 #include "rclcpp/type_adapter.hpp"
25 
27 
28 namespace rclcpp
29 {
30 
31 #ifdef DOXYGEN_ONLY
32 
34 
37 template<typename MessageT>
38 constexpr const rosidl_message_type_support_t & get_message_type_support_handle();
39 
40 #else
41 
42 template<typename MessageT>
43 constexpr
44 typename std::enable_if_t<
45  rosidl_generator_traits::is_message<MessageT>::value,
46  const rosidl_message_type_support_t &
47 >
48 get_message_type_support_handle()
49 {
50  auto handle = rosidl_typesupport_cpp::get_message_type_support_handle<MessageT>();
51  if (!handle) {
52  throw std::runtime_error("Type support handle unexpectedly nullptr");
53  }
54  return *handle;
55 }
56 
57 template<typename AdaptedType>
58 constexpr
59 typename std::enable_if_t<
60  !rosidl_generator_traits::is_message<AdaptedType>::value &&
62  const rosidl_message_type_support_t &
63 >
64 get_message_type_support_handle()
65 {
66  auto handle = rosidl_typesupport_cpp::get_message_type_support_handle<
67  typename TypeAdapter<AdaptedType>::ros_message_type
68  >();
69  if (!handle) {
70  throw std::runtime_error("Type support handle unexpectedly nullptr");
71  }
72  return *handle;
73 }
74 
75 // This specialization is a pass through runtime check, which allows a better
76 // static_assert to catch this issue further down the line.
77 // This should never get to be called in practice, and is purely defensive.
78 template<
79  typename AdaptedType
80 >
81 constexpr
82 typename std::enable_if_t<
83  !rosidl_generator_traits::is_message<AdaptedType>::value &&
84  !TypeAdapter<AdaptedType>::is_specialized::value,
85  const rosidl_message_type_support_t &
86 >
87 get_message_type_support_handle()
88 {
89  throw std::runtime_error(
90  "this specialization of rclcpp::get_message_type_support_handle() "
91  "should never be called");
92 }
93 
94 #endif
95 
96 } // namespace rclcpp
97 
98 #endif // RCLCPP__GET_MESSAGE_TYPE_SUPPORT_HANDLE_HPP_
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Template structure used to adapt custom, user-defined types to ROS types.