rclcpp  master
C++ ROS Client Library API
loaned_message.hpp
Go to the documentation of this file.
1 // Copyright 2019 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__LOANED_MESSAGE_HPP_
16 #define RCLCPP__LOANED_MESSAGE_HPP_
17 
18 #include <memory>
19 #include <utility>
20 
22 #include "rclcpp/logging.hpp"
24 
25 #include "rcl/allocator.h"
26 #include "rcl/publisher.h"
27 
28 namespace rclcpp
29 {
30 
31 template<typename MessageT, typename AllocatorT = std::allocator<void>>
33 {
34  using MessageAllocatorTraits = rclcpp::allocator::AllocRebind<MessageT, AllocatorT>;
35  using MessageAllocator = typename MessageAllocatorTraits::allocator_type;
36 
37 public:
39 
59  const rclcpp::PublisherBase & pub,
60  std::allocator<MessageT> allocator)
61  : pub_(pub),
62  message_(nullptr),
63  message_allocator_(std::move(allocator))
64  {
65  if (pub_.can_loan_messages()) {
66  void * message_ptr = nullptr;
67  auto ret = rcl_borrow_loaned_message(
69  rosidl_typesupport_cpp::get_message_type_support_handle<MessageT>(),
70  &message_ptr);
71  if (RCL_RET_OK != ret) {
73  }
74  message_ = static_cast<MessageT *>(message_ptr);
75  } else {
77  rclcpp::get_logger("rclcpp"),
78  "Currently used middleware can't loan messages. Local allocator will be used.");
79  message_ = message_allocator_.allocate(1);
80  new (message_) MessageT();
81  }
82  }
83 
85 
105  const rclcpp::PublisherBase * pub,
107  : LoanedMessage(*pub, *allocator)
108  {}
109 
112  : pub_(std::move(other.pub_)),
113  message_(std::move(other.message_)),
115  {}
116 
118 
129  virtual ~LoanedMessage()
130  {
131  auto error_logger = rclcpp::get_logger("LoanedMessage");
132 
133  if (message_ == nullptr) {
134  return;
135  }
136 
137  if (pub_.can_loan_messages()) {
138  // return allocated memory to the middleware
139  auto ret =
141  if (ret != RCL_RET_OK) {
142  RCLCPP_ERROR(
143  error_logger, "rcl_deallocate_loaned_message failed: %s", rcl_get_error_string().str);
144  rcl_reset_error();
145  }
146  } else {
147  // call destructor before deallocating
148  message_->~MessageT();
149  message_allocator_.deallocate(message_, 1);
150  }
151  message_ = nullptr;
152  }
153 
155 
162  bool is_valid() const
163  {
164  return message_ != nullptr;
165  }
166 
168 
175  MessageT & get() const
176  {
177  return *message_;
178  }
179 
181 
187  MessageT * release()
188  {
189  auto msg = message_;
190  message_ = nullptr;
191  return msg;
192  }
193 
194 protected:
196 
197  MessageT * message_;
198 
199  MessageAllocator message_allocator_;
200 
202  LoanedMessage(const LoanedMessage<MessageT> & other) = delete;
203 };
204 
205 } // namespace rclcpp
206 
207 #endif // RCLCPP__LOANED_MESSAGE_HPP_
rcl_ret_t rcl_borrow_loaned_message(const rcl_publisher_t *publisher, const rosidl_message_type_support_t *type_support, void **ros_message)
#define rcl_get_error_string
MessageAllocator message_allocator_
Definition: loaned_message.hpp:199
#define rcl_reset_error
Logger get_logger(const std::string &name)
Return a named logger.
const rclcpp::PublisherBase & pub_
Definition: loaned_message.hpp:195
Definition: publisher_base.hpp:55
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
MessageT * message_
Definition: loaned_message.hpp:197
#define RCL_RET_OK
void throw_from_rcl_error(rcl_ret_t ret, const std::string &prefix="", const rcl_error_state_t *error_state=nullptr, void(*reset_error)()=rcl_reset_error)
Throw a C++ std::exception which was created based on an rcl error.
bool can_loan_messages() const
Check if publisher instance can loan messages.
bool is_valid() const
Validate if the message was correctly allocated.
Definition: loaned_message.hpp:162
MessageT * release()
Release ownership of the ROS message instance.
Definition: loaned_message.hpp:187
typename std::allocator_traits< Alloc >::template rebind_traits< T > AllocRebind
Definition: allocator_common.hpp:30
rcl_ret_t rcl_return_loaned_message_from_publisher(const rcl_publisher_t *publisher, void *loaned_message)
#define RCLCPP_ERROR(logger,...)
Definition: logging.hpp:1418
rcl_publisher_t * get_publisher_handle()
Get the rcl publisher handle.
virtual ~LoanedMessage()
Destructor of the LoanedMessage class.
Definition: loaned_message.hpp:129
LoanedMessage(const rclcpp::PublisherBase &pub, std::allocator< MessageT > allocator)
Constructor of the LoanedMessage class.
Definition: loaned_message.hpp:58
#define RCLCPP_INFO_ONCE(logger,...)
Definition: logging.hpp:558
LoanedMessage(LoanedMessage< MessageT > &&other)
Move semantic for RVO.
Definition: loaned_message.hpp:111
LoanedMessage(const rclcpp::PublisherBase *pub, std::shared_ptr< std::allocator< MessageT >> allocator)
Constructor of the LoanedMessage class.
Definition: loaned_message.hpp:104
Definition: loaned_message.hpp:32