15 #ifndef RCLCPP__CONTEXT_HPP_
16 #define RCLCPP__CONTEXT_HPP_
18 #include <condition_variable>
25 #include <unordered_map>
26 #include <unordered_set>
34 #include "rclcpp/init_options.hpp"
35 #include "rclcpp/macros.hpp"
36 #include "rclcpp/visibility_control.hpp"
46 : std::runtime_error(
"context is already initialized") {}
57 using ShutdownCallbackType = std::function<void ()>;
60 std::weak_ptr<ShutdownCallbackType> callback;
72 class Context :
public std::enable_shared_from_this<Context>
75 RCLCPP_SMART_PTR_DEFINITIONS(
Context)
131 char const *
const * argv,
194 shutdown(
const std::string & reason);
196 using OnShutdownCallback = OnShutdownCallbackHandle::ShutdownCallbackType;
256 using PreShutdownCallback = PreShutdownCallbackHandle::ShutdownCallbackType;
288 std::vector<OnShutdownCallback>
296 std::vector<PreShutdownCallback>
301 std::shared_ptr<rcl_context_t>
318 sleep_for(
const std::chrono::nanoseconds & nanoseconds);
327 template<
typename SubContext,
typename ... Args>
328 std::shared_ptr<SubContext>
331 std::lock_guard<std::recursive_mutex> lock(sub_contexts_mutex_);
333 std::type_index type_i(
typeid(SubContext));
334 std::shared_ptr<SubContext> sub_context;
335 auto it = sub_contexts_.find(type_i);
336 if (it == sub_contexts_.end()) {
338 sub_context = std::shared_ptr<SubContext>(
339 new SubContext(std::forward<Args>(args) ...),
340 [](SubContext * sub_context_ptr) {
341 delete sub_context_ptr;
343 sub_contexts_[type_i] = sub_context;
346 sub_context = std::static_pointer_cast<SubContext>(it->second);
364 mutable std::recursive_mutex init_mutex_;
365 std::shared_ptr<rcl_context_t> rcl_context_;
367 std::string shutdown_reason_;
370 std::shared_ptr<std::recursive_mutex> logging_mutex_;
372 std::unordered_map<std::type_index, std::shared_ptr<void>> sub_contexts_;
375 std::recursive_mutex sub_contexts_mutex_;
377 std::unordered_set<std::shared_ptr<OnShutdownCallback>> on_shutdown_callbacks_;
378 mutable std::mutex on_shutdown_callbacks_mutex_;
380 std::unordered_set<std::shared_ptr<PreShutdownCallback>> pre_shutdown_callbacks_;
381 mutable std::mutex pre_shutdown_callbacks_mutex_;
384 std::condition_variable interrupt_condition_variable_;
386 std::mutex interrupt_mutex_;
389 std::shared_ptr<WeakContextsWrapper> weak_contexts_;
391 enum class ShutdownType
397 using ShutdownCallback = ShutdownCallbackHandle::ShutdownCallbackType;
400 ShutdownCallbackHandle
401 add_shutdown_callback(
402 ShutdownType shutdown_type,
403 ShutdownCallback callback);
407 remove_shutdown_callback(
408 ShutdownType shutdown_type,
409 const ShutdownCallbackHandle & callback_handle);
411 std::vector<rclcpp::Context::ShutdownCallback>
412 get_shutdown_callback(ShutdownType shutdown_type)
const;
420 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.
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.
virtual 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(OnShutdownCallback callback)
Add a on_shutdown callback to be called when shutdown is called for this context.
virtual RCLCPP_PUBLIC OnShutdownCallbackHandle add_on_shutdown_callback(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 PreShutdownCallbackHandle add_pre_shutdown_callback(PreShutdownCallback callback)
Add a pre_shutdown callback to be called before shutdown is called for this context.
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(std::function< void()> callback, rclcpp::Context::SharedPtr context=nullptr)
Register a function to be called when shutdown is called on the context.