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 
28 namespace rclcpp
29 {
30 
31 template<typename ServiceT>
33 {
34 private:
36  void (
39  )>;
41  void (
45  )>;
46 
47  SharedPtrCallback shared_ptr_callback_;
48  SharedPtrWithRequestHeaderCallback shared_ptr_with_request_header_callback_;
49 
50 public:
52  : shared_ptr_callback_(nullptr), shared_ptr_with_request_header_callback_(nullptr)
53  {}
54 
55  AnyServiceCallback(const AnyServiceCallback &) = default;
56 
57  template<
58  typename CallbackT,
59  typename std::enable_if<
61  CallbackT,
63  >::value
64  >::type * = nullptr
65  >
66  void set(CallbackT callback)
67  {
68  shared_ptr_callback_ = callback;
69  }
70 
71  template<
72  typename CallbackT,
73  typename std::enable_if<
75  CallbackT,
77  >::value
78  >::type * = nullptr
79  >
80  void set(CallbackT callback)
81  {
82  shared_ptr_with_request_header_callback_ = callback;
83  }
84 
85  void dispatch(
86  std::shared_ptr<rmw_request_id_t> request_header,
89  {
90  TRACEPOINT(callback_start, (const void *)this, false);
91  if (shared_ptr_callback_ != nullptr) {
92  (void)request_header;
93  shared_ptr_callback_(request, response);
94  } else if (shared_ptr_with_request_header_callback_ != nullptr) {
95  shared_ptr_with_request_header_callback_(request_header, request, response);
96  } else {
97  throw std::runtime_error("unexpected request without any callback set");
98  }
99  TRACEPOINT(callback_end, (const void *)this);
100  }
101 };
102 
103 } // namespace rclcpp
104 
105 #endif // RCLCPP__ANY_SERVICE_CALLBACK_HPP_
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
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:85
Definition: function_traits.hpp:161
AnyServiceCallback()
Definition: any_service_callback.hpp:51
Definition: any_service_callback.hpp:32