voidpublish(const MessageT & msg){ // RCUTILS_LOG_WARN_NAMED(ROS_PACKAGE_NAME_RCLCPP, // "[PUB]publish msg START for topic: %s of node: %s", // get_topic_name(), node_name_.c_str()); auto start = std::chrono::steady_clock::now(); // Avoid allocating when not using intra process. if (!intra_process_is_enabled_) { // In this case we're not using intra process. returnthis->do_inter_process_publish(msg); } // Otherwise we have to allocate memory in a unique_ptr and pass it along. // As the message is not const, a copy should be made. // A shared_ptr<const MessageT> could also be constructed here. auto ptr = MessageAllocatorTraits::allocate(*message_allocator_.get(), 1); MessageAllocatorTraits::construct(*message_allocator_.get(), ptr, msg); MessageUniquePtr unique_msg(ptr, message_deleter_); this->publish(std::move(unique_msg)); ... }
void publish(std::unique_ptr<MessageT, MessageDeleter> msg) { ... if (!intra_process_is_enabled_) { this->do_inter_process_publish(*msg); return; } // If an interprocess subscription exist, then the unique_ptr is promoted // to a shared_ptr and published. // This allows doing the intraprocess publish first and then doing the // interprocess publish, resulting in lower publish-to-subscribe latency. // It's not possible to do that with an unique_ptr, // as do_intra_process_publish takes the ownership of the message. bool inter_process_publish_needed = get_subscription_count() > get_intra_process_subscription_count();
if (inter_process_publish_needed) { auto shared_msg = this->do_intra_process_publish_and_return_shared(std::move(msg)); this->do_inter_process_publish(*shared_msg); } else { this->do_intra_process_publish(std::move(msg)); } ...