ROS 2 rclcpp + rcl - kilted  kilted
ROS 2 C++ Client Library with ROS Client Library
node_base.hpp
1 // Copyright 2016 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__NODE_INTERFACES__NODE_BASE_HPP_
16 #define RCLCPP__NODE_INTERFACES__NODE_BASE_HPP_
17 
18 #include <atomic>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "rcl/node.h"
25 #include "rclcpp/callback_group.hpp"
26 #include "rclcpp/context.hpp"
27 #include "rclcpp/macros.hpp"
28 #include "rclcpp/node_interfaces/node_base_interface.hpp"
29 #include "rclcpp/visibility_control.hpp"
30 
31 namespace rclcpp
32 {
33 namespace node_interfaces
34 {
35 
37 class NodeBase : public NodeBaseInterface, public std::enable_shared_from_this<NodeBase>
38 {
39 public:
40  RCLCPP_SMART_PTR_ALIASES_ONLY(NodeBase)
41 
42 
49  RCLCPP_PUBLIC
50  NodeBase(
51  const std::string & node_name,
52  const std::string & namespace_,
53  rclcpp::Context::SharedPtr context,
54  const rcl_node_options_t & rcl_node_options,
55  bool use_intra_process_default,
56  bool enable_topic_statistics_default,
57  rclcpp::CallbackGroup::SharedPtr default_callback_group = nullptr);
58 
59  RCLCPP_PUBLIC
60  virtual
61  ~NodeBase();
62 
63  RCLCPP_PUBLIC
64  const char *
65  get_name() const override;
66 
67  RCLCPP_PUBLIC
68  const char *
69  get_namespace() const override;
70 
71  RCLCPP_PUBLIC
72  const char *
73  get_fully_qualified_name() const override;
74 
75  RCLCPP_PUBLIC
76  rclcpp::Context::SharedPtr
77  get_context() override;
78 
79  RCLCPP_PUBLIC
80  rcl_node_t *
81  get_rcl_node_handle() override;
82 
83  RCLCPP_PUBLIC
84  const rcl_node_t *
85  get_rcl_node_handle() const override;
86 
87  RCLCPP_PUBLIC
88  std::shared_ptr<rcl_node_t>
89  get_shared_rcl_node_handle() override;
90 
91  RCLCPP_PUBLIC
92  std::shared_ptr<const rcl_node_t>
93  get_shared_rcl_node_handle() const override;
94 
95  RCLCPP_PUBLIC
96  rclcpp::CallbackGroup::SharedPtr
98  rclcpp::CallbackGroupType group_type,
99  bool automatically_add_to_executor_with_node = true) override;
100 
101  RCLCPP_PUBLIC
102  rclcpp::CallbackGroup::SharedPtr
103  get_default_callback_group() override;
104 
105  RCLCPP_PUBLIC
106  bool
107  callback_group_in_node(rclcpp::CallbackGroup::SharedPtr group) override;
108 
110 
116  RCLCPP_PUBLIC
117  void
118  for_each_callback_group(const CallbackGroupFunction & func) override;
119 
120  RCLCPP_PUBLIC
121  std::atomic_bool &
123 
124  [[deprecated("Use get_shared_notify_guard_condition or trigger_notify_guard_condition instead")]]
125  RCLCPP_PUBLIC
127  get_notify_guard_condition() override;
128 
129  RCLCPP_PUBLIC
130  rclcpp::GuardCondition::SharedPtr
132 
133  RCLCPP_PUBLIC
134  void
136 
137  RCLCPP_PUBLIC
138  bool
139  get_use_intra_process_default() const override;
140 
141  bool
142  get_enable_topic_statistics_default() const override;
143 
144  std::string
146  const std::string & name, bool is_service, bool only_expand = false) const override;
147 
148 private:
149  RCLCPP_DISABLE_COPY(NodeBase)
150 
151  rclcpp::Context::SharedPtr context_;
152  bool use_intra_process_default_;
153  bool enable_topic_statistics_default_;
154 
155  std::shared_ptr<rcl_node_t> node_handle_;
156 
157  rclcpp::CallbackGroup::SharedPtr default_callback_group_;
158  std::mutex callback_groups_mutex_;
159  std::vector<rclcpp::CallbackGroup::WeakPtr> callback_groups_;
160 
161  std::atomic_bool associated_with_executor_;
162 
164  mutable std::recursive_mutex notify_guard_condition_mutex_;
165  std::shared_ptr<rclcpp::GuardCondition> notify_guard_condition_;
166  bool notify_guard_condition_is_valid_;
167 };
168 
169 } // namespace node_interfaces
170 } // namespace rclcpp
171 
172 #endif // RCLCPP__NODE_INTERFACES__NODE_BASE_HPP_
A condition that can be waited on in a single wait set and asynchronously triggered.
Pure virtual interface class for the NodeBase part of the Node API.
Implementation of the NodeBase part of the Node API.
Definition: node_base.hpp:38
RCLCPP_PUBLIC const char * get_name() const override
Return the name of the node.
Definition: node_base.cpp:171
bool get_enable_topic_statistics_default() const override
Return the default preference for enabling topic statistics collection.
Definition: node_base.cpp:315
RCLCPP_PUBLIC bool get_use_intra_process_default() const override
Return the default preference for using intra process communication.
Definition: node_base.cpp:309
RCLCPP_PUBLIC rclcpp::GuardCondition::SharedPtr get_shared_notify_guard_condition() override
Return a guard condition that should be notified when the internal node state changes.
Definition: node_base.cpp:289
RCLCPP_PUBLIC rclcpp::CallbackGroup::SharedPtr get_default_callback_group() override
Return the default callback group.
Definition: node_base.cpp:243
RCLCPP_PUBLIC rclcpp::CallbackGroup::SharedPtr create_callback_group(rclcpp::CallbackGroupType group_type, bool automatically_add_to_executor_with_node=true) override
Create and return a callback group.
Definition: node_base.cpp:219
RCLCPP_PUBLIC rcl_node_t * get_rcl_node_handle() override
Return the rcl_node_t node handle (non-const version).
Definition: node_base.cpp:195
std::string resolve_topic_or_service_name(const std::string &name, bool is_service, bool only_expand=false) const override
Expand and remap a given topic or service name.
Definition: node_base.cpp:321
RCLCPP_PUBLIC rclcpp::GuardCondition & get_notify_guard_condition() override
Return a guard condition that should be notified when the internal node state changes.
Definition: node_base.cpp:279
RCLCPP_PUBLIC const char * get_namespace() const override
Return the namespace of the node.
Definition: node_base.cpp:177
RCLCPP_PUBLIC std::shared_ptr< rcl_node_t > get_shared_rcl_node_handle() override
Return the rcl_node_t node handle in a std::shared_ptr.
Definition: node_base.cpp:207
RCLCPP_PUBLIC void trigger_notify_guard_condition() override
Trigger the guard condition that notifies of internal node state changes.
Definition: node_base.cpp:299
RCLCPP_PUBLIC void for_each_callback_group(const CallbackGroupFunction &func) override
Iterate over the stored callback groups, calling the given function on each valid one.
Definition: node_base.cpp:261
RCLCPP_PUBLIC rclcpp::Context::SharedPtr get_context() override
Return the context of the node.
Definition: node_base.cpp:189
RCLCPP_PUBLIC bool callback_group_in_node(rclcpp::CallbackGroup::SharedPtr group) override
Return true if the given callback group is associated with this node.
Definition: node_base.cpp:249
RCLCPP_PUBLIC NodeBase(const std::string &node_name, const std::string &namespace_, rclcpp::Context::SharedPtr context, const rcl_node_options_t &rcl_node_options, bool use_intra_process_default, bool enable_topic_statistics_default, rclcpp::CallbackGroup::SharedPtr default_callback_group=nullptr)
Constructor.
Definition: node_base.cpp:37
RCLCPP_PUBLIC std::atomic_bool & get_associated_with_executor_atomic() override
Return the atomic bool which is used to ensure only one executor is used.
Definition: node_base.cpp:273
RCLCPP_PUBLIC const char * get_fully_qualified_name() const override
Return the fully qualified name of the node.
Definition: node_base.cpp:183
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Structure which encapsulates the options for creating a rcl_node_t.
Definition: node_options.h:35
Structure which encapsulates a ROS Node.
Definition: node.h:45