ROS 2 rclcpp + rcl - kilted  kilted
ROS 2 C++ Client Library with ROS Client Library
utilities.hpp
1 // Copyright 2014 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__UTILITIES_HPP_
16 #define RCLCPP__UTILITIES_HPP_
17 
18 #include <chrono>
19 #include <functional>
20 #include <limits>
21 #include <string>
22 #include <vector>
23 
24 #include "rclcpp/context.hpp"
25 #include "rclcpp/init_options.hpp"
26 #include "rclcpp/visibility_control.hpp"
27 
28 #ifdef ANDROID
29 #include <sstream>
30 
31 namespace std
32 {
33 template<typename T>
34 std::string to_string(T value)
35 {
36  std::ostringstream os;
37  os << value;
38  return os.str();
39 }
40 }
41 #endif
42 
43 namespace rclcpp
44 {
45 
48 {
50  All,
52  SigInt,
54  SigTerm,
56  None,
57 };
58 
60 
73 RCLCPP_PUBLIC
74 void
75 init(
76  int argc,
77  char const * const * argv,
78  const InitOptions & init_options = InitOptions(),
79  SignalHandlerOptions signal_handler_options = SignalHandlerOptions::All);
80 
82 
96 RCLCPP_PUBLIC
97 bool
99 
101 RCLCPP_PUBLIC
102 bool
104 
106 
109 RCLCPP_PUBLIC
112 
114 
126 RCLCPP_PUBLIC
127 bool
129 
131 
138 RCLCPP_PUBLIC
139 std::vector<std::string>
141  int argc,
142  char const * const * argv,
143  const InitOptions & init_options = InitOptions());
144 
146 
158 RCLCPP_PUBLIC
159 std::vector<std::string>
160 remove_ros_arguments(int argc, char const * const * argv);
161 
163 
174 RCLCPP_PUBLIC
175 bool
176 ok(rclcpp::Context::SharedPtr context = nullptr);
177 
179 
192 RCLCPP_PUBLIC
193 bool
195  rclcpp::Context::SharedPtr context = nullptr,
196  const std::string & reason = "user called rclcpp::shutdown()");
197 
199 
213 RCLCPP_PUBLIC
214 void
215 on_shutdown(std::function<void()> callback, rclcpp::Context::SharedPtr context = nullptr);
216 
218 
230 RCLCPP_PUBLIC
231 bool
233  const std::chrono::nanoseconds & nanoseconds,
234  rclcpp::Context::SharedPtr context = nullptr);
235 
237 
246 template<typename T>
247 bool
248 add_will_overflow(const T x, const T y)
249 {
250  return (y > 0) && (x > (std::numeric_limits<T>::max() - y));
251 }
252 
254 
263 template<typename T>
264 bool
265 add_will_underflow(const T x, const T y)
266 {
267  return (y < 0) && (x < (std::numeric_limits<T>::min() - y));
268 }
269 
271 
280 template<typename T>
281 bool
282 sub_will_overflow(const T x, const T y)
283 {
284  return (y < 0) && (x > (std::numeric_limits<T>::max() + y));
285 }
286 
288 
297 template<typename T>
298 bool
299 sub_will_underflow(const T x, const T y)
300 {
301  return (y > 0) && (x < (std::numeric_limits<T>::min() + y));
302 }
303 
305 
311 RCLCPP_PUBLIC
312 const char *
313 get_c_string(const char * string_in);
314 
316 
320 RCLCPP_PUBLIC
321 const char *
322 get_c_string(const std::string & string_in);
323 
325 
329 RCLCPP_PUBLIC
330 std::vector<const char *>
331 get_c_vector_string(const std::vector<std::string> & strings_in);
332 
333 } // namespace rclcpp
334 
335 #endif // RCLCPP__UTILITIES_HPP_
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC void init(int argc, char const *const *argv, const InitOptions &init_options=InitOptions(), SignalHandlerOptions signal_handler_options=SignalHandlerOptions::All)
Initialize communications via the rmw implementation and set up a global signal handler.
Definition: utilities.cpp:34
RCLCPP_PUBLIC std::string to_string(const FutureReturnCode &future_return_code)
String conversion function for FutureReturnCode.
SignalHandlerOptions
Option to indicate which signal handlers rclcpp should install.
Definition: utilities.hpp:48
@ SigTerm
Install only a sigterm handler.
@ None
Do not install any signal handler.
@ All
Install both sigint and sigterm, this is the default behavior.
@ SigInt
Install only a sigint handler.
bool add_will_underflow(const T x, const T y)
Safely check if addition will underflow.
Definition: utilities.hpp:265
RCLCPP_PUBLIC bool signal_handlers_installed()
Return true if the signal handlers are installed, otherwise false.
Definition: utilities.cpp:53
RCLCPP_PUBLIC void on_shutdown(std::function< void()> callback, rclcpp::Context::SharedPtr context=nullptr)
Register a function to be called when shutdown is called on the context.
RCLCPP_PUBLIC bool sleep_for(const std::chrono::nanoseconds &nanoseconds, rclcpp::Context::SharedPtr context=nullptr)
Use the global condition variable to block for the specified amount of time.
RCLCPP_PUBLIC std::vector< std::string > remove_ros_arguments(int argc, char const *const *argv)
Remove ROS-specific arguments from argument vector.
Definition: utilities.cpp:126
RCLCPP_PUBLIC std::vector< std::string > init_and_remove_ros_arguments(int argc, char const *const *argv, const InitOptions &init_options=InitOptions())
Initialize communications via the rmw implementation and set up a global signal handler.
Definition: utilities.cpp:113
RCLCPP_PUBLIC std::vector< const char * > get_c_vector_string(const std::vector< std::string > &strings_in)
Return the std::vector of C string from the given std::vector<std::string>.
Definition: utilities.cpp:218
RCLCPP_PUBLIC bool install_signal_handlers(SignalHandlerOptions signal_handler_options=SignalHandlerOptions::All)
Install the global signal handler for rclcpp.
Definition: utilities.cpp:47
RCLCPP_PUBLIC bool shutdown(rclcpp::Context::SharedPtr context=nullptr, const std::string &reason="user called rclcpp::shutdown()")
Shutdown rclcpp context, invalidating it for derived entities.
bool sub_will_overflow(const T x, const T y)
Safely check if subtraction will overflow.
Definition: utilities.hpp:282
RCLCPP_PUBLIC const char * get_c_string(const char *string_in)
Return the given string.
Definition: utilities.cpp:206
RCLCPP_PUBLIC bool ok(rclcpp::Context::SharedPtr context=nullptr)
Check rclcpp's status.
RCLCPP_PUBLIC SignalHandlerOptions get_current_signal_handler_options()
Get the current signal handler options.
Definition: utilities.cpp:59
RCLCPP_PUBLIC bool uninstall_signal_handlers()
Uninstall the global signal handler for rclcpp.
Definition: utilities.cpp:66
bool add_will_overflow(const T x, const T y)
Safely check if addition will overflow.
Definition: utilities.hpp:248
bool sub_will_underflow(const T x, const T y)
Safely check if subtraction will underflow.
Definition: utilities.hpp:299