ROS 2 rclcpp + rcl - lyrical  lyrical
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/contexts/default_context.hpp"
26 #include "rclcpp/init_options.hpp"
27 #include "rclcpp/visibility_control.hpp"
28 
29 #ifdef ANDROID
30 #include <sstream>
31 
32 namespace std
33 {
34 template<typename T>
35 std::string to_string(T value)
36 {
37  std::ostringstream os;
38  os << value;
39  return os.str();
40 }
41 }
42 #endif
43 
44 namespace rclcpp
45 {
46 
49 {
51  All,
53  SigInt,
55  SigTerm,
57  None,
58 };
59 
61 
74 RCLCPP_PUBLIC
75 void
76 init(
77  int argc,
78  char const * const * argv,
79  const InitOptions & init_options = InitOptions(),
80  SignalHandlerOptions signal_handler_options = SignalHandlerOptions::All);
81 
83 
97 RCLCPP_PUBLIC
98 bool
100 
102 RCLCPP_PUBLIC
103 bool
105 
107 
110 RCLCPP_PUBLIC
113 
115 
127 RCLCPP_PUBLIC
128 bool
130 
132 
139 RCLCPP_PUBLIC
140 std::vector<std::string>
142  int argc,
143  char const * const * argv,
144  const InitOptions & init_options = InitOptions());
145 
147 
159 RCLCPP_PUBLIC
160 std::vector<std::string>
161 remove_ros_arguments(int argc, char const * const * argv);
162 
164 
175 RCLCPP_PUBLIC
176 bool
177 ok(const rclcpp::Context::SharedPtr & context = rclcpp::contexts::get_global_default_context());
178 
180 
193 RCLCPP_PUBLIC
194 bool
196  const rclcpp::Context::SharedPtr & context = rclcpp::contexts::get_global_default_context(),
197  const std::string & reason = "user called rclcpp::shutdown()");
198 
200 
214 RCLCPP_PUBLIC
215 void
217  const std::function<void()> & callback,
218  const rclcpp::Context::SharedPtr & context = rclcpp::contexts::get_global_default_context());
219 
221 
233 RCLCPP_PUBLIC
234 bool
236  const std::chrono::nanoseconds & nanoseconds,
237  const rclcpp::Context::SharedPtr & context = rclcpp::contexts::get_global_default_context());
238 
240 
249 template<typename T>
250 bool
251 add_will_overflow(const T x, const T y)
252 {
253  return (y > 0) && (x > (std::numeric_limits<T>::max() - y));
254 }
255 
257 
266 template<typename T>
267 bool
268 add_will_underflow(const T x, const T y)
269 {
270  return (y < 0) && (x < (std::numeric_limits<T>::min() - y));
271 }
272 
274 
283 template<typename T>
284 bool
285 sub_will_overflow(const T x, const T y)
286 {
287  return (y < 0) && (x > (std::numeric_limits<T>::max() + y));
288 }
289 
291 
300 template<typename T>
301 bool
302 sub_will_underflow(const T x, const T y)
303 {
304  return (y > 0) && (x < (std::numeric_limits<T>::min() + y));
305 }
306 
308 
314 RCLCPP_PUBLIC
315 const char *
316 get_c_string(const char * string_in);
317 
319 
323 RCLCPP_PUBLIC
324 const char *
325 get_c_string(const std::string & string_in);
326 
328 
332 RCLCPP_PUBLIC
333 std::vector<const char *>
334 get_c_vector_string(const std::vector<std::string> & strings_in);
335 
336 } // namespace rclcpp
337 
338 #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:49
@ 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.
RCLCPP_PUBLIC bool ok(const rclcpp::Context::SharedPtr &context=rclcpp::contexts::get_global_default_context())
Check rclcpp's status.
bool add_will_underflow(const T x, const T y)
Safely check if addition will underflow.
Definition: utilities.hpp:268
RCLCPP_PUBLIC bool signal_handlers_installed()
Return true if the signal handlers are installed, otherwise false.
Definition: utilities.cpp:53
RCLCPP_PUBLIC bool sleep_for(const std::chrono::nanoseconds &nanoseconds, const rclcpp::Context::SharedPtr &context=rclcpp::contexts::get_global_default_context())
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:214
RCLCPP_PUBLIC bool install_signal_handlers(SignalHandlerOptions signal_handler_options=SignalHandlerOptions::All)
Install the global signal handler for rclcpp.
Definition: utilities.cpp:47
bool sub_will_overflow(const T x, const T y)
Safely check if subtraction will overflow.
Definition: utilities.hpp:285
RCLCPP_PUBLIC const char * get_c_string(const char *string_in)
Return the given string.
Definition: utilities.cpp:202
RCLCPP_PUBLIC SignalHandlerOptions get_current_signal_handler_options()
Get the current signal handler options.
Definition: utilities.cpp:59
RCLCPP_PUBLIC void on_shutdown(const std::function< void()> &callback, const rclcpp::Context::SharedPtr &context=rclcpp::contexts::get_global_default_context())
Register a function to be called when shutdown is called on the context.
RCLCPP_PUBLIC bool uninstall_signal_handlers()
Uninstall the global signal handler for rclcpp.
Definition: utilities.cpp:66
RCLCPP_PUBLIC bool shutdown(const rclcpp::Context::SharedPtr &context=rclcpp::contexts::get_global_default_context(), const std::string &reason="user called rclcpp::shutdown()")
Shutdown rclcpp context, invalidating it for derived entities.
bool add_will_overflow(const T x, const T y)
Safely check if addition will overflow.
Definition: utilities.hpp:251
bool sub_will_underflow(const T x, const T y)
Safely check if subtraction will underflow.
Definition: utilities.hpp:302