rclcpp  master
C++ ROS Client Library API
any_service_callback.hpp
Go to the documentation of this file.
1 // Copyright 2015 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__ANY_SERVICE_CALLBACK_HPP_
16 #define RCLCPP__ANY_SERVICE_CALLBACK_HPP_
17 
18 #include <functional>
19 #include <memory>
20 #include <stdexcept>
21 #include <type_traits>
22 
25 #include "rmw/types.h"
26 #include "tracetools/tracetools.h"
27 #include "tracetools/utils.hpp"
28 
29 namespace rclcpp
30 {
31 
32 template<typename ServiceT>
34 {
35 private:
37  void (
40  )>;
42  void (
46  )>;
47 
48  SharedPtrCallback shared_ptr_callback_;
49  SharedPtrWithRequestHeaderCallback shared_ptr_with_request_header_callback_;
50 
51 public:
53  : shared_ptr_callback_(nullptr), shared_ptr_with_request_header_callback_(nullptr)
54  {}
55 
56  AnyServiceCallback(const AnyServiceCallback &) = default;
57 
58  template<
59  typename CallbackT,
60  typename std::enable_if<
62  CallbackT,
63  SharedPtrCallback
64  >::value
65  >::type * = nullptr
66  >
67  void set(CallbackT callback)
68  {
69  shared_ptr_callback_ = callback;
70  }
71 
72  template<
73  typename CallbackT,
74  typename std::enable_if<
76  CallbackT,
77  SharedPtrWithRequestHeaderCallback
78  >::value
79  >::type * = nullptr
80  >
81  void set(CallbackT callback)
82  {
83  shared_ptr_with_request_header_callback_ = callback;
84  }
85 
86  void dispatch(
87  std::shared_ptr<rmw_request_id_t> request_header,
90  {
91  TRACEPOINT(callback_start, static_cast<const void *>(this), false);
92  if (shared_ptr_callback_ != nullptr) {
93  (void)request_header;
94  shared_ptr_callback_(request, response);
95  } else if (shared_ptr_with_request_header_callback_ != nullptr) {
96  shared_ptr_with_request_header_callback_(request_header, request, response);
97  } else {
98  throw std::runtime_error("unexpected request without any callback set");
99  }
100  TRACEPOINT(callback_end, static_cast<const void *>(this));
101  }
102 
104  {
105 #ifndef TRACETOOLS_DISABLED
106  if (shared_ptr_callback_) {
107  TRACEPOINT(
108  rclcpp_callback_register,
109  static_cast<const void *>(this),
110  tracetools::get_symbol(shared_ptr_callback_));
111  } else if (shared_ptr_with_request_header_callback_) {
112  TRACEPOINT(
113  rclcpp_callback_register,
114  static_cast<const void *>(this),
115  tracetools::get_symbol(shared_ptr_with_request_header_callback_));
116  }
117 #endif // TRACETOOLS_DISABLED
118  }
119 };
120 
121 } // namespace rclcpp
122 
123 #endif // RCLCPP__ANY_SERVICE_CALLBACK_HPP_
rclcpp::AnyServiceCallback::dispatch
void dispatch(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< typename ServiceT::Request > request, std::shared_ptr< typename ServiceT::Response > response)
Definition: any_service_callback.hpp:86
rclcpp::AnyServiceCallback
Definition: any_service_callback.hpp:33
rclcpp::AnyServiceCallback::set
void set(CallbackT callback)
Definition: any_service_callback.hpp:67
std::shared_ptr
std::function< void(const std::shared_ptr< typename ServiceT::Request >, std::shared_ptr< typename ServiceT::Response >)>
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
std::enable_if
rclcpp::AnyServiceCallback::AnyServiceCallback
AnyServiceCallback()
Definition: any_service_callback.hpp:52
std::runtime_error
rclcpp::function_traits::same_arguments
Definition: function_traits.hpp:159
function_traits.hpp
visibility_control.hpp
rclcpp::AnyServiceCallback::register_callback_for_tracing
void register_callback_for_tracing()
Definition: any_service_callback.hpp:103
types.h