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 <unordered_set>
27 #include <utility>
28 #include <vector>
29 
30 #include "rcl/context.h"
31 #include "rcl/guard_condition.h"
32 #include "rcl/wait.h"
33 #include "rclcpp/init_options.hpp"
34 #include "rclcpp/macros.hpp"
36 
37 namespace rclcpp
38 {
39 
42 {
43 public:
45  : std::runtime_error("context is already initialized") {}
46 };
47 
49 class WeakContextsWrapper;
50 
52 {
53  friend class Context;
54 
55 public:
57 
58 private:
60 };
61 
63 
68 class Context : public std::enable_shared_from_this<Context>
69 {
70 public:
72 
73 
74 
81  Context();
82 
84  virtual
85  ~Context();
86 
88 
123  virtual
124  void
125  init(
126  int argc,
127  char const * const argv[],
128  const rclcpp::InitOptions & init_options = rclcpp::InitOptions());
129 
131 
141  bool
142  is_valid() const;
143 
146  const rclcpp::InitOptions &
147  get_init_options() const;
148 
153 
156  size_t
157  get_domain_id() const;
158 
160 
165  shutdown_reason() const;
166 
168 
188  virtual
189  bool
190  shutdown(const std::string & reason);
191 
193 
195 
214  virtual
217 
219 
238  virtual
241 
243 
248  virtual
249  bool
250  remove_on_shutdown_callback(const OnShutdownCallbackHandle & callback_handle);
251 
253 
259 
263  get_rcl_context();
264 
266 
278  bool
280 
283  virtual
284  void
286 
288  template<typename SubContext, typename ... Args>
290  get_sub_context(Args && ... args)
291  {
292  std::lock_guard<std::recursive_mutex> lock(sub_contexts_mutex_);
293 
294  std::type_index type_i(typeid(SubContext));
295  std::shared_ptr<SubContext> sub_context;
296  auto it = sub_contexts_.find(type_i);
297  if (it == sub_contexts_.end()) {
298  // It doesn't exist yet, make it
299  sub_context = std::shared_ptr<SubContext>(
300  new SubContext(std::forward<Args>(args) ...),
301  [](SubContext * sub_context_ptr) {
302  delete sub_context_ptr;
303  });
304  sub_contexts_[type_i] = sub_context;
305  } else {
306  // It exists, get it out and cast it.
307  sub_context = std::static_pointer_cast<SubContext>(it->second);
308  }
309  return sub_context;
310  }
311 
312 protected:
313  // Called by constructor and destructor to clean up by finalizing the
314  // shutdown rcl context and preparing for a new init cycle.
316  virtual
317  void
318  clean_up();
319 
320 private:
322 
323  // This mutex is recursive so that the destructor can ensure atomicity
324  // between is_initialized and shutdown.
325  mutable std::recursive_mutex init_mutex_;
326  std::shared_ptr<rcl_context_t> rcl_context_;
327  rclcpp::InitOptions init_options_;
328  std::string shutdown_reason_;
329 
330  // Keep shared ownership of the global logging mutex.
332 
334  // This mutex is recursive so that the constructor of a sub context may
335  // attempt to acquire another sub context.
336  std::recursive_mutex sub_contexts_mutex_;
337 
339  mutable std::mutex on_shutdown_callbacks_mutex_;
340 
342  std::condition_variable interrupt_condition_variable_;
344  std::mutex interrupt_mutex_;
345 
348 };
349 
351 
356 get_contexts();
357 
358 } // namespace rclcpp
359 
360 #endif // RCLCPP__CONTEXT_HPP_
rclcpp::Context::get_domain_id
size_t get_domain_id() const
Return actual domain id.
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::remove_on_shutdown_callback
virtual bool remove_on_shutdown_callback(const OnShutdownCallbackHandle &callback_handle)
Remove an registered on_shutdown callbacks.
rclcpp::Context::get_on_shutdown_callbacks
std::vector< OnShutdownCallback > get_on_shutdown_callbacks() const
Return the shutdown callbacks.
std::string
std::shared_ptr< rcl_context_t >
guard_condition.h
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rclcpp::OnShutdownCallbackHandle::OnShutdownCallbackType
std::function< void()> OnShutdownCallbackType
Definition: context.hpp:56
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.
std::unordered_set
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.
std::vector
std::unordered_map::find
T find(T... args)
wait.h
std::chrono::nanoseconds
std::type_index
std::recursive_mutex
std::lock_guard
std::function
rclcpp::OnShutdownCallbackHandle
Definition: context.hpp:51
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:41
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::Context::add_on_shutdown_callback
virtual OnShutdownCallbackHandle add_on_shutdown_callback(OnShutdownCallback callback)
Add a on_shutdown callback to be called when shutdown is called for this context.
rclcpp::ContextAlreadyInitialized::ContextAlreadyInitialized
ContextAlreadyInitialized()
Definition: context.hpp:44
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
std::runtime_error
rclcpp::Context::~Context
virtual ~Context()
rclcpp::InitOptions
Encapsulation of options for initializing rclcpp.
Definition: init_options.hpp:28
std::weak_ptr
rclcpp::Context
Context which encapsulates shared state between nodes and other similar entities.
Definition: context.hpp:68
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
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:290
context.h
rclcpp::Context::shutdown_reason
std::string shutdown_reason() const
Return the shutdown reason, or empty string if not shutdown.