rclcpp_action  master
C++ ROS Action Client Library
create_client.hpp
Go to the documentation of this file.
1 // Copyright 2018 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_ACTION__CREATE_CLIENT_HPP_
16 #define RCLCPP_ACTION__CREATE_CLIENT_HPP_
17 
18 #include <rclcpp/node.hpp>
19 
20 #include <memory>
21 #include <string>
22 
23 #include "rclcpp_action/client.hpp"
25 
26 namespace rclcpp_action
27 {
29 
42 template<typename ActionT>
43 typename Client<ActionT>::SharedPtr
45  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface,
46  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_interface,
47  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface,
48  rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr node_waitables_interface,
49  const std::string & name,
50  rclcpp::CallbackGroup::SharedPtr group = nullptr,
51  const rcl_action_client_options_t & options = rcl_action_client_get_default_options())
52 {
54  node_waitables_interface;
55  std::weak_ptr<rclcpp::CallbackGroup> weak_group = group;
56  bool group_is_null = (nullptr == group.get());
57 
58  auto deleter = [weak_node, weak_group, group_is_null](Client<ActionT> * ptr)
59  {
60  if (nullptr == ptr) {
61  return;
62  }
63  auto shared_node = weak_node.lock();
64  if (shared_node) {
65  // API expects a shared pointer, give it one with a deleter that does nothing.
66  std::shared_ptr<Client<ActionT>> fake_shared_ptr(ptr, [](Client<ActionT> *) {});
67 
68  if (group_is_null) {
69  // Was added to default group
70  shared_node->remove_waitable(fake_shared_ptr, nullptr);
71  } else {
72  // Was added to a specific group
73  auto shared_group = weak_group.lock();
74  if (shared_group) {
75  shared_node->remove_waitable(fake_shared_ptr, shared_group);
76  }
77  }
78  }
79  delete ptr;
80  };
81 
82  std::shared_ptr<Client<ActionT>> action_client(
83  new Client<ActionT>(
84  node_base_interface,
85  node_graph_interface,
86  node_logging_interface,
87  name,
88  options),
89  deleter);
90 
91  node_waitables_interface->add_waitable(action_client, group);
92  return action_client;
93 }
94 
96 
103 template<typename ActionT, typename NodeT>
104 typename Client<ActionT>::SharedPtr
106  NodeT node,
107  const std::string & name,
108  rclcpp::CallbackGroup::SharedPtr group = nullptr,
109  const rcl_action_client_options_t & options = rcl_action_client_get_default_options())
110 {
111  return rclcpp_action::create_client<ActionT>(
112  node->get_node_base_interface(),
113  node->get_node_graph_interface(),
114  node->get_node_logging_interface(),
115  node->get_node_waitables_interface(),
116  name,
117  group,
118  options);
119 }
120 } // namespace rclcpp_action
121 
122 #endif // RCLCPP_ACTION__CREATE_CLIENT_HPP_
std::weak_ptr::lock
T lock(T... args)
client.hpp
std::string
std::shared_ptr
rclcpp_action::create_client
Client< ActionT >::SharedPtr create_client(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface, rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_interface, rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface, rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr node_waitables_interface, const std::string &name, rclcpp::CallbackGroup::SharedPtr group=nullptr, const rcl_action_client_options_t &options=rcl_action_client_get_default_options())
Create an action client.
Definition: create_client.hpp:44
node.hpp
rclcpp_action
Definition: client.hpp:46
rcl_action_client_options_t
std::weak_ptr
visibility_control.hpp
rclcpp_action::Client
Action Client.
Definition: client.hpp:262