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;
75 class Context :
public std::enable_shared_from_this<Context>
78 RCLCPP_SMART_PTR_DEFINITIONS(
Context)
134 char const *
const * argv,
202 shutdown(
const std::string & reason);
204 using OnShutdownCallback = OnShutdownCallbackHandle::ShutdownCallbackType;
264 using PreShutdownCallback = PreShutdownCallbackHandle::ShutdownCallbackType;
296 std::vector<OnShutdownCallback>
304 std::vector<PreShutdownCallback>
309 std::shared_ptr<rcl_context_t>
326 sleep_for(
const std::chrono::nanoseconds & nanoseconds);
334 template<
typename SubContext,
typename ... Args>
335 std::shared_ptr<SubContext>
338 std::lock_guard<std::recursive_mutex> lock(sub_contexts_mutex_);
340 std::type_index type_i(
typeid(SubContext));
341 std::shared_ptr<SubContext> sub_context;
342 auto it = sub_contexts_.find(type_i);
343 if (it == sub_contexts_.end()) {
345 sub_context = std::shared_ptr<SubContext>(
346 new SubContext(std::forward<Args>(args) ...),
347 [](SubContext * sub_context_ptr) {
348 delete sub_context_ptr;
350 sub_contexts_[type_i] = sub_context;
353 sub_context = std::static_pointer_cast<SubContext>(it->second);
370 mutable std::recursive_mutex init_mutex_;
371 std::shared_ptr<rcl_context_t> rcl_context_;
373 std::string shutdown_reason_;
376 std::shared_ptr<std::recursive_mutex> logging_mutex_;
378 std::unordered_map<std::type_index, std::shared_ptr<void>> sub_contexts_;
381 std::recursive_mutex sub_contexts_mutex_;
383 std::vector<std::shared_ptr<OnShutdownCallback>> on_shutdown_callbacks_;
384 mutable std::mutex on_shutdown_callbacks_mutex_;
386 std::vector<std::shared_ptr<PreShutdownCallback>> pre_shutdown_callbacks_;
387 mutable std::mutex pre_shutdown_callbacks_mutex_;
390 std::condition_variable interrupt_condition_variable_;
392 std::mutex interrupt_mutex_;
395 std::shared_ptr<WeakContextsWrapper> weak_contexts_;
397 enum class ShutdownType
403 using ShutdownCallback = ShutdownCallbackHandle::ShutdownCallbackType;
405 template<ShutdownType shutdown_type>
407 ShutdownCallbackHandle
408 add_shutdown_callback(
409 ShutdownCallback callback);
411 template<ShutdownType shutdown_type>
414 remove_shutdown_callback(
415 const ShutdownCallbackHandle & callback_handle);
417 template<ShutdownType shutdown_type>
419 std::vector<rclcpp::Context::ShutdownCallback>
420 get_shutdown_callback()
const;
428 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.
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.