15 #ifndef RCLCPP__CONTEXT_HPP_
16 #define RCLCPP__CONTEXT_HPP_
18 #include <condition_variable>
25 #include <unordered_map>
33 #include "rclcpp/init_options.hpp"
34 #include "rclcpp/macros.hpp"
35 #include "rclcpp/visibility_control.hpp"
39 namespace graph_listener
49 : std::runtime_error(
"context is already initialized") {}
60 using ShutdownCallbackType = std::function<void ()>;
63 std::weak_ptr<ShutdownCallbackType> callback;
78 class Context :
public std::enable_shared_from_this<Context>
81 RCLCPP_SMART_PTR_DEFINITIONS(
Context)
137 char const *
const * argv,
205 shutdown(
const std::string & reason);
207 using OnShutdownCallback = OnShutdownCallbackHandle::ShutdownCallbackType;
267 using PreShutdownCallback = PreShutdownCallbackHandle::ShutdownCallbackType;
299 std::vector<OnShutdownCallback>
307 std::vector<PreShutdownCallback>
312 std::shared_ptr<rcl_context_t>
316 std::shared_ptr<rclcpp::graph_listener::GraphListener>
317 get_graph_listener();
333 sleep_for(
const std::chrono::nanoseconds & nanoseconds);
341 template<
typename SubContext,
typename ... Args>
342 std::shared_ptr<SubContext>
345 std::lock_guard<std::recursive_mutex> lock(sub_contexts_mutex_);
347 std::type_index type_i(
typeid(SubContext));
348 std::shared_ptr<SubContext> sub_context;
349 auto it = sub_contexts_.find(type_i);
350 if (it == sub_contexts_.end()) {
352 sub_context = std::shared_ptr<SubContext>(
353 new SubContext(std::forward<Args>(args) ...),
354 [](SubContext * sub_context_ptr) {
355 delete sub_context_ptr;
357 sub_contexts_[type_i] = sub_context;
360 sub_context = std::static_pointer_cast<SubContext>(it->second);
377 mutable std::recursive_mutex init_mutex_;
378 std::shared_ptr<rcl_context_t> rcl_context_;
380 std::string shutdown_reason_;
383 std::shared_ptr<std::recursive_mutex> logging_mutex_;
385 std::unordered_map<std::type_index, std::shared_ptr<void>> sub_contexts_;
388 std::recursive_mutex sub_contexts_mutex_;
390 std::vector<std::shared_ptr<OnShutdownCallback>> on_shutdown_callbacks_;
391 mutable std::recursive_mutex on_shutdown_callbacks_mutex_;
393 std::vector<std::shared_ptr<PreShutdownCallback>> pre_shutdown_callbacks_;
394 mutable std::recursive_mutex pre_shutdown_callbacks_mutex_;
397 std::condition_variable interrupt_condition_variable_;
399 std::mutex interrupt_mutex_;
402 std::shared_ptr<rclcpp::graph_listener::GraphListener> graph_listener_;
405 std::shared_ptr<WeakContextsWrapper> weak_contexts_;
407 enum class ShutdownType
413 using ShutdownCallback = ShutdownCallbackHandle::ShutdownCallbackType;
415 template<ShutdownType shutdown_type>
417 ShutdownCallbackHandle
418 add_shutdown_callback(
419 const ShutdownCallback & callback);
421 template<ShutdownType shutdown_type>
424 remove_shutdown_callback(
425 const ShutdownCallbackHandle & callback_handle);
427 template<ShutdownType shutdown_type>
429 std::vector<rclcpp::Context::ShutdownCallback>
430 get_shutdown_callback()
const;
438 std::vector<Context::SharedPtr>
Thrown when init is called on an already initialized context.
Context which encapsulates shared state between nodes and other similar entities.
virtual RCLCPP_PUBLIC PreShutdownCallbackHandle add_pre_shutdown_callback(const PreShutdownCallback &callback)
Add a pre_shutdown callback to be called before shutdown is called for this context.
virtual RCLCPP_PUBLIC OnShutdownCallbackHandle add_on_shutdown_callback(const OnShutdownCallback &callback)
Add a on_shutdown callback to be called when shutdown is called for this context.
std::shared_ptr< SubContext > get_sub_context(Args &&... args)
Return a singleton instance for the SubContext type, constructing one if necessary.
virtual RCLCPP_PUBLIC void init(int argc, char const *const *argv, const rclcpp::InitOptions &init_options=rclcpp::InitOptions())
Initialize the context, and the underlying elements like the rcl context.
RCLCPP_PUBLIC std::vector< OnShutdownCallback > get_on_shutdown_callbacks() const
Return the shutdown callbacks.
RCLCPP_PUBLIC std::vector< PreShutdownCallback > get_pre_shutdown_callbacks() const
Return the pre-shutdown callbacks.
RCLCPP_PUBLIC Context()
Default constructor, after which the Context is still not "initialized".
RCLCPP_PUBLIC size_t get_domain_id() const
Return actual domain id.
RCLCPP_PUBLIC std::string shutdown_reason() const
Return the shutdown reason, or empty string if not shutdown.
RCLCPP_PUBLIC const rclcpp::InitOptions & get_init_options() const
Return the init options used during init.
RCLCPP_PUBLIC bool sleep_for(const std::chrono::nanoseconds &nanoseconds)
Sleep for a given period of time or until shutdown() is called.
RCLCPP_PUBLIC void interrupt_all_sleep_for()
Interrupt any blocking sleep_for calls, causing them to return immediately and return true.
virtual RCLCPP_PUBLIC OnShutdownCallback on_shutdown(const OnShutdownCallback &callback)
Add a on_shutdown callback to be called when shutdown is called for this context.
virtual RCLCPP_PUBLIC bool remove_pre_shutdown_callback(const PreShutdownCallbackHandle &callback_handle)
Remove an registered pre_shutdown callback.
RCLCPP_PUBLIC bool is_valid() const
Return true if the context is valid, otherwise false.
virtual RCLCPP_PUBLIC bool remove_on_shutdown_callback(const OnShutdownCallbackHandle &callback_handle)
Remove an registered on_shutdown callbacks.
virtual RCLCPP_PUBLIC bool shutdown(const std::string &reason)
Shutdown the context, making it uninitialized and therefore invalid for derived entities.
RCLCPP_PUBLIC std::shared_ptr< rcl_context_t > get_rcl_context()
Return the internal rcl context.
Encapsulation of options for initializing rclcpp.
Class to manage vector of weak pointers to all created contexts.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC std::vector< Context::SharedPtr > get_contexts()
Return a copy of the list of context shared pointers.
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.