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 
27 namespace rclcpp
28 {
29 
30 template<typename ServiceT>
32 {
33 private:
35  void (
38  )>;
40  void (
44  )>;
45 
46  SharedPtrCallback shared_ptr_callback_;
47  SharedPtrWithRequestHeaderCallback shared_ptr_with_request_header_callback_;
48 
49 public:
51  : shared_ptr_callback_(nullptr), shared_ptr_with_request_header_callback_(nullptr)
52  {}
53 
54  AnyServiceCallback(const AnyServiceCallback &) = default;
55 
56  template<
57  typename CallbackT,
58  typename std::enable_if<
60  CallbackT,
62  >::value
63  >::type * = nullptr
64  >
65  void set(CallbackT callback)
66  {
67  shared_ptr_callback_ = callback;
68  }
69 
70  template<
71  typename CallbackT,
72  typename std::enable_if<
74  CallbackT,
76  >::value
77  >::type * = nullptr
78  >
79  void set(CallbackT callback)
80  {
81  shared_ptr_with_request_header_callback_ = callback;
82  }
83 
84  void dispatch(
85  std::shared_ptr<rmw_request_id_t> request_header,
88  {
89  if (shared_ptr_callback_ != nullptr) {
90  (void)request_header;
91  shared_ptr_callback_(request, response);
92  } else if (shared_ptr_with_request_header_callback_ != nullptr) {
93  shared_ptr_with_request_header_callback_(request_header, request, response);
94  } else {
95  throw std::runtime_error("unexpected request without any callback set");
96  }
97  }
98 };
99 
100 } // namespace rclcpp
101 
102 #endif // RCLCPP__ANY_SERVICE_CALLBACK_HPP_
This header provides the get_node_topics_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:84
Definition: function_traits.hpp:143
AnyServiceCallback()
Definition: any_service_callback.hpp:50
Definition: any_service_callback.hpp:31