rclcpp  master
C++ ROS Client Library API
get_node_topics_interface.hpp
Go to the documentation of this file.
1 // Copyright 2019 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__NODE_INTERFACES__GET_NODE_TOPICS_INTERFACE_HPP_
16 #define RCLCPP__NODE_INTERFACES__GET_NODE_TOPICS_INTERFACE_HPP_
17 
18 #include <memory>
19 #include <utility>
20 #include <type_traits>
21 
23 
25 
35 namespace rclcpp
36 {
37 namespace node_interfaces
38 {
39 
40 namespace detail
41 {
42 
43 // This is a meta-programming checker for if a given Node-like object has a
44 // getter called get_node_topics_interface() which returns various types,
45 // e.g. const pointer or a shared pointer.
46 template<typename NodeType, typename ReturnType>
48 {
49 private:
50  template<typename T>
51  static constexpr
52  auto
53  check(T *)->typename std::is_same<
54  decltype(std::declval<T>().get_node_topics_interface()),
55  ReturnType
56  >::type;
57 
58  template<typename>
59  static constexpr
61  check(...);
62 
63 public:
64  using type = decltype(check<NodeType>(nullptr));
65  static constexpr bool value = type::value;
66 };
67 
68 // If NodeType is a pointer to NodeTopicsInterface already (just normal function overload).
69 inline
72 {
73  return pointer;
74 }
75 
76 // If NodeType has a method called get_node_topics_interface() which returns a shared pointer.
77 template<
78  typename NodeType,
82  >::value, int>::type = 0
83 >
86 {
87  return node_pointer->get_node_topics_interface().get();
88 }
89 
90 // If NodeType has a method called get_node_topics_interface() which returns a pointer.
91 template<
92  typename NodeType,
94  typename std::remove_pointer<NodeType>::type,
96  >::value, int>::type = 0
97 >
99 get_node_topics_interface_from_pointer(NodeType node_pointer)
100 {
101  return node_pointer->get_node_topics_interface();
102 }
103 
104 // Forward shared_ptr's to const node pointer signatures.
105 template<
106  typename NodeType,
107  typename std::enable_if<std::is_same<
108  NodeType,
110  >::value, int>::type = 0
111 >
113 get_node_topics_interface_from_pointer(NodeType node_shared_pointer)
114 {
115  return get_node_topics_interface_from_pointer(node_shared_pointer->get());
116 }
117 
118 } // namespace detail
119 
121 template<
122  typename NodeType,
124 >
126 get_node_topics_interface(NodeType && node_pointer)
127 {
128  // Forward pointers to detail implmentation directly.
129  return detail::get_node_topics_interface_from_pointer(std::forward<NodeType>(node_pointer));
130 }
131 
133 template<
134  typename NodeType,
135  typename std::enable_if<!std::is_pointer<NodeType>::value, int>::type = 0
136 >
138 get_node_topics_interface(NodeType && node_reference)
139 {
140  // Forward references to detail implmentation as a pointer.
141  return detail::get_node_topics_interface_from_pointer(&node_reference);
142 }
143 
144 } // namespace node_interfaces
145 } // namespace rclcpp
146 
147 #endif // RCLCPP__NODE_INTERFACES__GET_NODE_TOPICS_INTERFACE_HPP_
This header provides the get_node_topics_interface() template function.
Definition: allocator_common.hpp:24
decltype(check< NodeType >(nullptr)) type
Definition: get_node_topics_interface.hpp:64
rclcpp::node_interfaces::NodeTopicsInterface * get_node_topics_interface(NodeType &&node_pointer)
Get the NodeTopicsInterface as a pointer from a pointer to a "Node like" object.
Definition: get_node_topics_interface.hpp:126
Pure virtual interface class for the NodeTopics part of the Node API.
Definition: node_topics_interface.hpp:38
rclcpp::node_interfaces::NodeTopicsInterface * get_node_topics_interface_from_pointer(rclcpp::node_interfaces::NodeTopicsInterface *pointer)
Definition: get_node_topics_interface.hpp:71
static constexpr bool value
Definition: get_node_topics_interface.hpp:65
Definition: get_node_topics_interface.hpp:47