ROS 2 rclcpp + rcl - rolling  rolling-e615c7c3
ROS 2 C++ Client Library with ROS Client Library
create_intra_process_buffer.hpp
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__EXPERIMENTAL__CREATE_INTRA_PROCESS_BUFFER_HPP_
16 #define RCLCPP__EXPERIMENTAL__CREATE_INTRA_PROCESS_BUFFER_HPP_
17 
18 #include <memory>
19 #include <stdexcept>
20 #include <utility>
21 
22 #include "rclcpp/experimental/buffers/intra_process_buffer.hpp"
23 #include "rclcpp/experimental/buffers/ring_buffer_implementation.hpp"
24 #include "rclcpp/intra_process_buffer_type.hpp"
25 #include "rclcpp/qos.hpp"
26 
27 namespace rclcpp
28 {
29 namespace experimental
30 {
31 
32 template<
33  typename MessageT,
34  typename Alloc = std::allocator<void>,
35  typename Deleter = std::default_delete<MessageT>>
37 create_intra_process_buffer(
38  IntraProcessBufferType buffer_type,
39  const rclcpp::QoS & qos,
40  std::shared_ptr<Alloc> allocator)
41 {
42  using MessageSharedPtr = std::shared_ptr<const MessageT>;
43  using MessageUniquePtr = std::unique_ptr<MessageT, Deleter>;
44 
45  size_t buffer_size = qos.depth();
46 
48  typename IntraProcessBuffer<MessageT, Alloc, Deleter>::UniquePtr buffer;
49 
50  switch (buffer_type) {
52  {
53  using BufferT = MessageSharedPtr;
54 
55  auto buffer_implementation =
56  std::make_unique<rclcpp::experimental::buffers::RingBufferImplementation<BufferT>>(
57  buffer_size);
58 
59  // Construct the intra_process_buffer
60  buffer =
61  std::make_unique<rclcpp::experimental::buffers::TypedIntraProcessBuffer<MessageT, Alloc,
62  Deleter, BufferT>>(
63  std::move(buffer_implementation),
64  allocator);
65 
66  break;
67  }
69  {
70  using BufferT = MessageUniquePtr;
71 
72  auto buffer_implementation =
73  std::make_unique<rclcpp::experimental::buffers::RingBufferImplementation<BufferT>>(
74  buffer_size);
75 
76  // Construct the intra_process_buffer
77  buffer =
78  std::make_unique<rclcpp::experimental::buffers::TypedIntraProcessBuffer<MessageT, Alloc,
79  Deleter, BufferT>>(
80  std::move(buffer_implementation),
81  allocator);
82 
83  break;
84  }
85  default:
86  {
87  throw std::runtime_error("Unrecognized IntraProcessBufferType value");
88  break;
89  }
90  }
91 
92  return buffer;
93 }
94 
95 } // namespace experimental
96 } // namespace rclcpp
97 
98 #endif // RCLCPP__EXPERIMENTAL__CREATE_INTRA_PROCESS_BUFFER_HPP_
Encapsulation of Quality of Service settings.
Definition: qos.hpp:116
size_t depth() const
Get the history depth.
Definition: qos.cpp:283
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
@ SharedPtr
Set the data type used in the intra-process buffer as std::shared_ptr<MessageT>
@ UniquePtr
Set the data type used in the intra-process buffer as std::unique_ptr<MessageT>