rclcpp  beta1
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 namespace any_service_callback
31 {
32 
33 template<typename ServiceT>
35 {
36 private:
37  using SharedPtrCallback = std::function<void(
40  )>;
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  if (shared_ptr_callback_ != nullptr) {
91  (void)request_header;
92  shared_ptr_callback_(request, response);
93  } else if (shared_ptr_with_request_header_callback_ != nullptr) {
94  shared_ptr_with_request_header_callback_(request_header, request, response);
95  } else {
96  throw std::runtime_error("unexpected request without any callback set");
97  }
98  }
99 };
100 
101 } // namespace any_service_callback
102 } // namespace rclcpp
103 
104 #endif // RCLCPP__ANY_SERVICE_CALLBACK_HPP_
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
AnyServiceCallback()
Definition: any_service_callback.hpp:51
Definition: allocator_common.hpp:24
Definition: function_traits.hpp:141
Definition: any_service_callback.hpp:34