ROS 2 rclcpp + rcl - rolling  rolling-e7363f10
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  rclcpp::CallbackGroup::SharedPtr default_callback_group = nullptr);
57 
58  RCLCPP_PUBLIC
59  virtual
60  ~NodeBase();
61 
62  RCLCPP_PUBLIC
63  const char *
64  get_name() const override;
65 
66  RCLCPP_PUBLIC
67  const char *
68  get_namespace() const override;
69 
70  RCLCPP_PUBLIC
71  const char *
72  get_fully_qualified_name() const override;
73 
74  RCLCPP_PUBLIC
75  rclcpp::Context::SharedPtr
76  get_context() override;
77 
78  RCLCPP_PUBLIC
79  rcl_node_t *
80  get_rcl_node_handle() override;
81 
82  RCLCPP_PUBLIC
83  const rcl_node_t *
84  get_rcl_node_handle() const override;
85 
86  RCLCPP_PUBLIC
87  std::shared_ptr<rcl_node_t>
88  get_shared_rcl_node_handle() override;
89 
90  RCLCPP_PUBLIC
91  std::shared_ptr<const rcl_node_t>
92  get_shared_rcl_node_handle() const override;
93 
94  RCLCPP_PUBLIC
95  rclcpp::CallbackGroup::SharedPtr
97  rclcpp::CallbackGroupType group_type,
98  bool automatically_add_to_executor_with_node = true) override;
99 
100  RCLCPP_PUBLIC
101  rclcpp::CallbackGroup::SharedPtr
102  get_default_callback_group() override;
103 
104  RCLCPP_PUBLIC
105  bool
106  callback_group_in_node(rclcpp::CallbackGroup::SharedPtr group) override;
107 
109 
115  RCLCPP_PUBLIC
116  void
117  for_each_callback_group(const CallbackGroupFunction & func) override;
118 
119  RCLCPP_PUBLIC
120  std::atomic_bool &
122 
123  RCLCPP_PUBLIC
124  rclcpp::GuardCondition::SharedPtr
126 
127  RCLCPP_PUBLIC
128  void
130 
131  RCLCPP_PUBLIC
132  bool
133  get_use_intra_process_default() const override;
134 
135  std::string
137  const std::string & name, bool is_service, bool only_expand = false) const override;
138 
139 private:
140  RCLCPP_DISABLE_COPY(NodeBase)
141 
142  rclcpp::Context::SharedPtr context_;
143  bool use_intra_process_default_;
144 
145  std::shared_ptr<rcl_node_t> node_handle_;
146 
147  rclcpp::CallbackGroup::SharedPtr default_callback_group_;
148  std::mutex callback_groups_mutex_;
149  std::vector<rclcpp::CallbackGroup::WeakPtr> callback_groups_;
150 
151  std::atomic_bool associated_with_executor_;
152 
154  mutable std::recursive_mutex notify_guard_condition_mutex_;
155  std::shared_ptr<rclcpp::GuardCondition> notify_guard_condition_;
156  bool notify_guard_condition_is_valid_;
157 };
158 
159 } // namespace node_interfaces
160 } // namespace rclcpp
161 
162 #endif // RCLCPP__NODE_INTERFACES__NODE_BASE_HPP_
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:169
RCLCPP_PUBLIC bool get_use_intra_process_default() const override
Return the default preference for using intra process communication.
Definition: node_base.cpp:297
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:277
RCLCPP_PUBLIC rclcpp::CallbackGroup::SharedPtr get_default_callback_group() override
Return the default callback group.
Definition: node_base.cpp:241
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:217
RCLCPP_PUBLIC rcl_node_t * get_rcl_node_handle() override
Return the rcl_node_t node handle (non-const version).
Definition: node_base.cpp:193
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:303
RCLCPP_PUBLIC const char * get_namespace() const override
Return the namespace of the node.
Definition: node_base.cpp:175
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, rclcpp::CallbackGroup::SharedPtr default_callback_group=nullptr)
Constructor.
Definition: node_base.cpp:37
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:205
RCLCPP_PUBLIC void trigger_notify_guard_condition() override
Trigger the guard condition that notifies of internal node state changes.
Definition: node_base.cpp:287
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:259
RCLCPP_PUBLIC rclcpp::Context::SharedPtr get_context() override
Return the context of the node.
Definition: node_base.cpp:187
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:247
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:271
RCLCPP_PUBLIC const char * get_fully_qualified_name() const override
Return the fully qualified name of the node.
Definition: node_base.cpp:181
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:41
Structure which encapsulates a ROS Node.
Definition: node.h:49