iceoryx_doc  1.0.1
sample.hpp
1 // Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 
18 #ifndef IOX_POSH_POPO_SAMPLE_HPP
19 #define IOX_POSH_POPO_SAMPLE_HPP
20 
21 #include "iceoryx_posh/mepoo/chunk_header.hpp"
22 #include "iceoryx_utils/cxx/type_traits.hpp"
23 #include "iceoryx_utils/cxx/unique_ptr.hpp"
24 
25 namespace iox
26 {
27 namespace popo
28 {
29 template <typename T, typename H>
30 class PublisherInterface;
31 
32 namespace internal
33 {
35 template <typename T, typename H>
37 {
38  SamplePrivateData(cxx::unique_ptr<T>&& sampleUniquePtr, PublisherInterface<T, H>& publisher) noexcept;
39 
40  SamplePrivateData(SamplePrivateData&& rhs) noexcept = default;
41  SamplePrivateData& operator=(SamplePrivateData&& rhs) noexcept = default;
42 
43  SamplePrivateData(const SamplePrivateData&) = delete;
44  SamplePrivateData& operator=(const SamplePrivateData&) = delete;
45 
46  cxx::unique_ptr<T> sampleUniquePtr;
47  std::reference_wrapper<PublisherInterface<T, H>> publisherRef;
48 };
49 
51 template <typename T, typename H>
52 struct SamplePrivateData<const T, H>
53 {
54  SamplePrivateData(cxx::unique_ptr<const T>&& sampleUniquePtr) noexcept;
55 
56  SamplePrivateData(SamplePrivateData&& rhs) noexcept = default;
57  SamplePrivateData& operator=(SamplePrivateData&& rhs) noexcept = default;
58 
59  SamplePrivateData(const SamplePrivateData&) = delete;
60  SamplePrivateData& operator=(const SamplePrivateData&) = delete;
61 
62  cxx::unique_ptr<const T> sampleUniquePtr;
63 };
64 } // namespace internal
65 
70 template <typename T, typename H = cxx::add_const_conditionally_t<mepoo::NoUserHeader, T>>
71 class Sample
72 {
73  static_assert(std::is_const<T>::value == std::is_const<H>::value,
74  "The type `T` and the user-header `H` must be equal in their const qualifier to ensure the same "
75  "access restrictions for the user-header as for the sample data!");
76 
78  template <typename S, typename TT>
79  using ForPublisherOnly = std::enable_if_t<std::is_same<S, TT>::value && !std::is_const<TT>::value, S>;
80 
82  template <typename S, typename TT>
83  using ForSubscriberOnly = std::enable_if_t<std::is_same<S, TT>::value && std::is_const<TT>::value, S>;
84 
86  template <typename R, typename HH>
87  using HasUserHeader =
88  std::enable_if_t<std::is_same<R, HH>::value && !std::is_same<R, mepoo::NoUserHeader>::value, R>;
89 
90  public:
95  template <typename S = T, typename = ForPublisherOnly<S, T>>
96  Sample(cxx::unique_ptr<T>&& sampleUniquePtr, PublisherInterface<T, H>& publisher) noexcept;
97 
101  template <typename S = T, typename = ForSubscriberOnly<S, T>>
102  Sample(cxx::unique_ptr<T>&& sampleUniquePtr) noexcept;
103 
104  ~Sample() noexcept = default;
105 
106  Sample<T, H>& operator=(Sample<T, H>&& rhs) noexcept = default;
107  Sample(Sample<T, H>&& rhs) noexcept = default;
108 
109  Sample(const Sample<T, H>&) = delete;
110  Sample<T, H>& operator=(const Sample<T, H>&) = delete;
111 
116  T* operator->() noexcept;
117 
122  const T* operator->() const noexcept;
123 
128  T& operator*() noexcept;
129 
134  const T& operator*() const noexcept;
135 
140  operator bool() const noexcept;
141 
146  T* get() noexcept;
147 
152  const T* get() const noexcept;
153 
156  using ConditionalConstChunkHeader_t = cxx::add_const_conditionally_t<mepoo::ChunkHeader, T>;
162 
167  const mepoo::ChunkHeader* getChunkHeader() const noexcept;
168 
173  template <typename R = H, typename = HasUserHeader<R, H>>
174  R& getUserHeader() noexcept;
175 
180  template <typename R = H, typename = HasUserHeader<R, H>>
181  const R& getUserHeader() const noexcept;
182 
188  template <typename S = T, typename = ForPublisherOnly<S, T>>
189  void publish() noexcept;
190 
191  private:
192  template <typename, typename, typename>
193  friend class PublisherImpl;
194 
197  T* release() noexcept;
198 
199  private:
200  internal::SamplePrivateData<T, H> m_members;
201 };
202 
203 } // namespace popo
204 } // namespace iox
205 
206 #include "iceoryx_posh/internal/popo/sample.inl"
207 
208 #endif // IOX_POSH_POPO_SAMPLE_HPP
Definition: publisher.hpp:47
The PublisherInterface class defines the publisher interface used by the Sample class to make it gene...
Definition: publisher.hpp:36
The Sample class is a mutable abstraction over types which are written to loaned shared memory....
Definition: sample.hpp:72
T * operator->() noexcept
Transparent access to the encapsulated type.
Definition: sample.inl:57
void publish() noexcept
Publish the sample via the publisher from which it was loaned and automatically release ownership to ...
Definition: sample.inl:126
T * get() noexcept
Mutable access to the encapsulated type loaned to the sample.
Definition: sample.inl:87
R & getUserHeader() noexcept
Retrieve the user-header of the underlying memory chunk loaned to the sample.
Definition: sample.inl:112
T & operator*() noexcept
Provides a reference to the encapsulated type.
Definition: sample.inl:69
Sample(cxx::unique_ptr< T > &&sampleUniquePtr, PublisherInterface< T, H > &publisher) noexcept
Constructor for a Sample used by the Publisher.
Definition: sample.inl:44
ConditionalConstChunkHeader_t * getChunkHeader() noexcept
Retrieve the ChunkHeader of the underlying memory chunk loaned to the sample.
Definition: sample.inl:99
cxx::add_const_conditionally_t< mepoo::ChunkHeader, T > ConditionalConstChunkHeader_t
Helper type to ensure the access to the ChunkHeader has the same const qualifier as the access to the...
Definition: sample.hpp:156
Definition: service_description.hpp:29
helper struct for sample
Definition: sample.hpp:37