ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
init_options.cpp
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 #include "rclcpp/init_options.hpp"
16 
17 #include "rclcpp/exceptions.hpp"
18 #include "rclcpp/logging.hpp"
19 
20 namespace rclcpp
21 {
22 
24 : init_options_(new rcl_init_options_t)
25 {
26  *init_options_ = rcl_get_zero_initialized_init_options();
27  rcl_ret_t ret = rcl_init_options_init(init_options_.get(), allocator);
28  if (RCL_RET_OK != ret) {
29  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to initialize rcl init options");
30  }
31 }
32 
34 : init_options_(new rcl_init_options_t)
35 {
36  *init_options_ = rcl_get_zero_initialized_init_options();
37  rcl_ret_t ret = rcl_init_options_copy(&init_options, init_options_.get());
38  if (RCL_RET_OK != ret) {
39  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to copy rcl init options");
40  }
41 }
42 
44 : InitOptions(*other.get_rcl_init_options())
45 {
47  initialize_logging_ = other.initialize_logging_;
48 }
49 
50 bool
52 {
53  return initialize_logging_;
54 }
55 
57 InitOptions::auto_initialize_logging(bool initialize_logging)
58 {
59  initialize_logging_ = initialize_logging;
60  return *this;
61 }
62 
65 {
66  if (this != &other) {
67  std::lock_guard<std::mutex> init_options_lock(init_options_mutex_);
68  this->finalize_init_options_impl();
69  rcl_ret_t ret = rcl_init_options_copy(other.get_rcl_init_options(), init_options_.get());
70  if (RCL_RET_OK != ret) {
71  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to copy rcl init options");
72  }
74  this->initialize_logging_ = other.initialize_logging_;
75  }
76  return *this;
77 }
78 
79 InitOptions::~InitOptions()
80 {
81  this->finalize_init_options();
82 }
83 
84 void
85 InitOptions::finalize_init_options()
86 {
87  std::lock_guard<std::mutex> init_options_lock(init_options_mutex_);
88  this->finalize_init_options_impl();
89 }
90 
91 void
92 InitOptions::finalize_init_options_impl()
93 {
94  if (init_options_) {
95  rcl_ret_t ret = rcl_init_options_fini(init_options_.get());
96  if (RCL_RET_OK != ret) {
97  RCLCPP_ERROR(
98  rclcpp::get_logger("rclcpp"),
99  "failed to finalize rcl init options: %s", rcl_get_error_string().str);
100  rcl_reset_error();
101  }
102  *init_options_ = rcl_get_zero_initialized_init_options();
103  }
104 }
105 
106 const rcl_init_options_t *
108 {
109  return init_options_.get();
110 }
111 
112 void
114 {
115  size_t domain_id = RCL_DEFAULT_DOMAIN_ID;
116  rcl_ret_t ret = rcl_get_default_domain_id(&domain_id);
117  if (RCL_RET_OK != ret) {
118  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to get default domain id");
119  }
120  set_domain_id(domain_id);
121 }
122 
123 void
124 InitOptions::set_domain_id(size_t domain_id)
125 {
126  std::lock_guard<std::mutex> init_options_lock(init_options_mutex_);
127  rcl_ret_t ret = rcl_init_options_set_domain_id(init_options_.get(), domain_id);
128  if (RCL_RET_OK != ret) {
129  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to set domain id to rcl init options");
130  }
131 }
132 
133 size_t
135 {
136  std::lock_guard<std::mutex> init_options_lock(init_options_mutex_);
137  size_t domain_id;
138  rcl_ret_t ret = rcl_init_options_get_domain_id(init_options_.get(), &domain_id);
139  if (RCL_RET_OK != ret) {
140  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to get domain id from rcl init options");
141  }
142 
143  return domain_id;
144 }
145 
146 } // namespace rclcpp
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
Encapsulation of options for initializing rclcpp.
RCLCPP_PUBLIC const rcl_init_options_t * get_rcl_init_options() const
Return the rcl init options.
RCLCPP_PUBLIC bool auto_initialize_logging() const
Return true if logging should be initialized when rclcpp::Context::init is called.
RCLCPP_PUBLIC InitOptions & operator=(const InitOptions &other)
Assignment operator.
RCLCPP_PUBLIC void use_default_domain_id()
Retrieve default domain id and set.
RCLCPP_PUBLIC size_t get_domain_id() const
Return domain id.
bool shutdown_on_signal
If true, the context will be shutdown on SIGINT by the signal handler (if it was installed).
RCLCPP_PUBLIC InitOptions(rcl_allocator_t allocator=rcl_get_default_allocator())
Constructor.
RCLCPP_PUBLIC void set_domain_id(size_t domain_id)
Set the domain id.
RCL_PUBLIC rcl_ret_t rcl_get_default_domain_id(size_t *domain_id)
Determine the default domain ID, based on the environment.
Definition: domain_id.c:28
#define RCL_DEFAULT_DOMAIN_ID
The default domain ID used by RCL.
Definition: domain_id.h:32
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_init(rcl_init_options_t *init_options, rcl_allocator_t allocator)
Initialize given init_options with the default values and implementation specific values.
Definition: init_options.c:54
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_set_domain_id(rcl_init_options_t *init_options, size_t domain_id)
Set a domain id in the init options provided.
Definition: init_options.c:152
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_copy(const rcl_init_options_t *src, rcl_init_options_t *dst)
Copy the given source init_options to the destination init_options.
Definition: init_options.c:82
RCL_PUBLIC RCL_WARN_UNUSED rcl_init_options_t rcl_get_zero_initialized_init_options(void)
Return a zero initialized rcl_init_options_t struct.
Definition: init_options.c:30
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_get_domain_id(const rcl_init_options_t *init_options, size_t *domain_id)
Return the domain_id stored in the init options.
Definition: init_options.c:142
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_init_options_fini(rcl_init_options_t *init_options)
Finalize the given init_options.
Definition: init_options.c:126
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.
Definition: logger.cpp:27
Encapsulation of init options and implementation defined init options.
Definition: init_options.h:36
#define RCL_RET_OK
Success return code.
Definition: types.h:26
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:23