ROS 2 rclcpp + rcl - humble  humble
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 <functional>
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  RCLCPP_PUBLIC
43  NodeBase(
44  const std::string & node_name,
45  const std::string & namespace_,
46  rclcpp::Context::SharedPtr context,
47  const rcl_node_options_t & rcl_node_options,
48  bool use_intra_process_default,
49  bool enable_topic_statistics_default);
50 
51  RCLCPP_PUBLIC
52  virtual
53  ~NodeBase();
54 
55  RCLCPP_PUBLIC
56  const char *
57  get_name() const override;
58 
59  RCLCPP_PUBLIC
60  const char *
61  get_namespace() const override;
62 
63  RCLCPP_PUBLIC
64  const char *
65  get_fully_qualified_name() const override;
66 
67  RCLCPP_PUBLIC
68  rclcpp::Context::SharedPtr
69  get_context() override;
70 
71  RCLCPP_PUBLIC
72  rcl_node_t *
73  get_rcl_node_handle() override;
74 
75  RCLCPP_PUBLIC
76  const rcl_node_t *
77  get_rcl_node_handle() const override;
78 
79  RCLCPP_PUBLIC
80  std::shared_ptr<rcl_node_t>
81  get_shared_rcl_node_handle() override;
82 
83  RCLCPP_PUBLIC
84  std::shared_ptr<const rcl_node_t>
85  get_shared_rcl_node_handle() const override;
86 
87  RCLCPP_PUBLIC
88  rclcpp::CallbackGroup::SharedPtr
90  rclcpp::CallbackGroupType group_type,
91  bool automatically_add_to_executor_with_node = true) override;
92 
93  RCLCPP_PUBLIC
94  rclcpp::CallbackGroup::SharedPtr
95  get_default_callback_group() override;
96 
97  RCLCPP_PUBLIC
98  bool
99  callback_group_in_node(rclcpp::CallbackGroup::SharedPtr group) override;
100 
102 
108  RCLCPP_PUBLIC
109  void
110  for_each_callback_group(const CallbackGroupFunction & func) override;
111 
112  RCLCPP_PUBLIC
113  std::atomic_bool &
115 
116  RCLCPP_PUBLIC
118  get_notify_guard_condition() override;
119 
120  RCLCPP_PUBLIC
121  bool
122  get_use_intra_process_default() const override;
123 
124  bool
125  get_enable_topic_statistics_default() const override;
126 
127  std::string
129  const std::string & name, bool is_service, bool only_expand = false) const override;
130 
131 private:
132  RCLCPP_DISABLE_COPY(NodeBase)
133 
134  rclcpp::Context::SharedPtr context_;
135  bool use_intra_process_default_;
136  bool enable_topic_statistics_default_;
137 
138  std::shared_ptr<rcl_node_t> node_handle_;
139 
140  rclcpp::CallbackGroup::SharedPtr default_callback_group_;
141  std::mutex callback_groups_mutex_;
142  std::vector<rclcpp::CallbackGroup::WeakPtr> callback_groups_;
143 
144  std::atomic_bool associated_with_executor_;
145 
147  mutable std::recursive_mutex notify_guard_condition_mutex_;
148  rclcpp::GuardCondition notify_guard_condition_;
149  bool notify_guard_condition_is_valid_;
150 };
151 
152 } // namespace node_interfaces
153 } // namespace rclcpp
154 
155 #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:149
bool get_enable_topic_statistics_default() const override
Return the default preference for enabling topic statistics collection.
Definition: node_base.cpp:262
RCLCPP_PUBLIC bool get_use_intra_process_default() const override
Return the default preference for using intra process communication.
Definition: node_base.cpp:256
RCLCPP_PUBLIC rclcpp::CallbackGroup::SharedPtr get_default_callback_group() override
Return the default callback group.
Definition: node_base.cpp:210
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:197
RCLCPP_PUBLIC rcl_node_t * get_rcl_node_handle() override
Return the rcl_node_t node handle (non-const version).
Definition: node_base.cpp:173
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:268
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:246
RCLCPP_PUBLIC const char * get_namespace() const override
Return the namespace of the node.
Definition: node_base.cpp:155
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:185
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:228
RCLCPP_PUBLIC rclcpp::Context::SharedPtr get_context() override
Return the context of the node.
Definition: node_base.cpp:167
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:216
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:240
RCLCPP_PUBLIC const char * get_fully_qualified_name() const override
Return the fully qualified name of the node.
Definition: node_base.cpp:161
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:42