rclcpp  master
C++ ROS Client Library API
context.hpp
Go to the documentation of this file.
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__CONTEXT_HPP_
16 #define RCLCPP__CONTEXT_HPP_
17 
18 #include <condition_variable>
19 #include <functional>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <typeindex>
24 #include <typeinfo>
25 #include <unordered_map>
26 #include <utility>
27 #include <vector>
28 
29 #include "rcl/context.h"
30 #include "rcl/guard_condition.h"
31 #include "rcl/wait.h"
32 #include "rclcpp/init_options.hpp"
33 #include "rclcpp/macros.hpp"
35 
36 namespace rclcpp
37 {
38 
41 {
42 public:
44  : std::runtime_error("context is already initialized") {}
45 };
46 
48 class WeakContextsWrapper;
49 
51 
56 class Context : public std::enable_shared_from_this<Context>
57 {
58 public:
60 
61 
62 
69  Context();
70 
72  virtual
73  ~Context();
74 
76 
111  virtual
112  void
113  init(
114  int argc,
115  char const * const argv[],
116  const rclcpp::InitOptions & init_options = rclcpp::InitOptions());
117 
119 
129  bool
130  is_valid() const;
131 
134  const rclcpp::InitOptions &
135  get_init_options() const;
136 
141 
143 
148  shutdown_reason();
149 
151 
171  virtual
172  bool
173  shutdown(const std::string & reason);
174 
176 
178 
197  virtual
200 
202 
209 
211 
218 
222  get_rcl_context();
223 
225 
237  bool
239 
242  virtual
243  void
245 
247 
274 
276 
293  void
295 
298  void
300 
303  virtual
304  void
306 
308  template<typename SubContext, typename ... Args>
310  get_sub_context(Args && ... args)
311  {
312  std::lock_guard<std::recursive_mutex> lock(sub_contexts_mutex_);
313 
314  std::type_index type_i(typeid(SubContext));
315  std::shared_ptr<SubContext> sub_context;
316  auto it = sub_contexts_.find(type_i);
317  if (it == sub_contexts_.end()) {
318  // It doesn't exist yet, make it
319  sub_context = std::shared_ptr<SubContext>(
320  new SubContext(std::forward<Args>(args) ...),
321  [](SubContext * sub_context_ptr) {
322  delete sub_context_ptr;
323  });
324  sub_contexts_[type_i] = sub_context;
325  } else {
326  // It exists, get it out and cast it.
327  sub_context = std::static_pointer_cast<SubContext>(it->second);
328  }
329  return sub_context;
330  }
331 
332 protected:
333  // Called by constructor and destructor to clean up by finalizing the
334  // shutdown rcl context and preparing for a new init cycle.
336  virtual
337  void
338  clean_up();
339 
340 private:
342 
343  // This mutex is recursive so that the destructor can ensure atomicity
344  // between is_initialized and shutdown.
345  std::recursive_mutex init_mutex_;
346  std::shared_ptr<rcl_context_t> rcl_context_;
347  rclcpp::InitOptions init_options_;
348  std::string shutdown_reason_;
349 
350  // Keep shared ownership of the global logging mutex.
352 
354  // This mutex is recursive so that the constructor of a sub context may
355  // attempt to acquire another sub context.
356  std::recursive_mutex sub_contexts_mutex_;
357 
358  std::vector<OnShutdownCallback> on_shutdown_callbacks_;
359  std::mutex on_shutdown_callbacks_mutex_;
360 
362  std::condition_variable interrupt_condition_variable_;
364  std::mutex interrupt_mutex_;
365 
367  std::mutex interrupt_guard_cond_handles_mutex_;
370 
373 };
374 
376 
381 get_contexts();
382 
383 } // namespace rclcpp
384 
385 #endif // RCLCPP__CONTEXT_HPP_
rclcpp::Context::get_on_shutdown_callbacks
const std::vector< OnShutdownCallback > & get_on_shutdown_callbacks() const
Return the shutdown callbacks as const.
rclcpp::Context::Context
Context()
Default constructor, after which the Context is still not "initialized".
rclcpp::Context::on_shutdown
virtual OnShutdownCallback on_shutdown(OnShutdownCallback callback)
Add a on_shutdown callback to be called when shutdown is called for this context.
rclcpp::Context::interrupt_all_wait_sets
virtual void interrupt_all_wait_sets()
Interrupt any blocking executors, or wait sets associated with this context.
std::string
std::shared_ptr< rcl_context_t >
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rclcpp::Context::get_interrupt_guard_condition
rcl_guard_condition_t * get_interrupt_guard_condition(rcl_wait_set_t *wait_set)
Get a handle to the guard condition which is triggered when interrupted.
rclcpp::Context::interrupt_all_sleep_for
virtual void interrupt_all_sleep_for()
Interrupt any blocking sleep_for calls, causing them to return immediately and return true.
rclcpp::Context::init
virtual 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::Context::shutdown_reason
std::string shutdown_reason()
Return the shutdown reason, or empty string if not shutdown.
std::vector
std::unordered_map::find
T find(T... args)
std::chrono::nanoseconds
std::type_index
std::recursive_mutex
std::lock_guard
rcl_guard_condition_t
std::function< void()>
rclcpp::Context::get_init_options
const rclcpp::InitOptions & get_init_options() const
Return the init options used during init.
rclcpp::ContextAlreadyInitialized
Thrown when init is called on an already initialized context.
Definition: context.hpp:40
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::ContextAlreadyInitialized::ContextAlreadyInitialized
ContextAlreadyInitialized()
Definition: context.hpp:43
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::Context::is_valid
bool is_valid() const
Return true if the context is valid, otherwise false.
RCLCPP_SMART_PTR_DEFINITIONS
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
macros.hpp
rclcpp::Context::sleep_for
bool sleep_for(const std::chrono::nanoseconds &nanoseconds)
Sleep for a given period of time or until shutdown() is called.
std::enable_shared_from_this
rcl_wait_set_t
std::runtime_error
rclcpp::Context::~Context
virtual ~Context()
rclcpp::InitOptions
Encapsulation of options for initializing rclcpp.
Definition: init_options.hpp:27
rclcpp::Context
Context which encapsulates shared state between nodes and other similar entities.
Definition: context.hpp:56
rclcpp::Context::get_rcl_context
std::shared_ptr< rcl_context_t > get_rcl_context()
Return the internal rcl context.
rclcpp::Context::shutdown
virtual bool shutdown(const std::string &reason)
Shutdown the context, making it uninitialized and therefore invalid for derived entities.
rclcpp::Context::clean_up
virtual void clean_up()
visibility_control.hpp
std
rclcpp::Context::release_interrupt_guard_condition
void release_interrupt_guard_condition(rcl_wait_set_t *wait_set)
Release the previously allocated guard condition which is triggered when interrupted.
std::nothrow_t
std::condition_variable
init_options.hpp
std::mutex
std::unordered_map::end
T end(T... args)
rclcpp::get_contexts
std::vector< Context::SharedPtr > get_contexts()
Return a copy of the list of context shared pointers.
std::unordered_map
rclcpp::Context::get_sub_context
std::shared_ptr< SubContext > get_sub_context(Args &&... args)
Return a singleton instance for the SubContext type, constructing one if necessary.
Definition: context.hpp:310