iceoryx_doc  1.0.1
publisher.hpp
1 // Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2020 - 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_TYPED_PUBLISHER_HPP
19 #define IOX_POSH_POPO_TYPED_PUBLISHER_HPP
20 
21 #include "iceoryx_posh/popo/base_publisher.hpp"
22 #include "iceoryx_posh/popo/sample.hpp"
23 #include "iceoryx_utils/cxx/type_traits.hpp"
24 
25 namespace iox
26 {
27 namespace popo
28 {
34 template <typename T, typename H>
36 {
37  public:
38  virtual void publish(Sample<T, H>&& sample) noexcept = 0;
39 
40  virtual ~PublisherInterface(){};
41 
42  protected:
43  PublisherInterface() = default;
44 };
45 template <typename T, typename H = mepoo::NoUserHeader, typename BasePublisher_t = BasePublisher<>>
46 class PublisherImpl : public BasePublisher_t, public PublisherInterface<T, H>
47 {
48  static_assert(!std::is_void<T>::value, "The type `T` must not be void. Use the UntypedPublisher for void types.");
49  static_assert(!std::is_void<H>::value, "The user-header `H` must not be void.");
50 
51  static_assert(!std::is_const<T>::value, "The type `T` must not be const.");
52  static_assert(!std::is_reference<T>::value, "The type `T` must not be a reference.");
53  static_assert(!std::is_pointer<T>::value, "The type `T` must not be a pointer.");
54 
55  static_assert(!std::is_const<H>::value, "The user-header `H` must not be const.");
56  static_assert(!std::is_reference<H>::value, "The user-header `H` must not be a reference.");
57  static_assert(!std::is_pointer<H>::value, "The user-header must `H` not be a pointer.");
58 
59  public:
61  const PublisherOptions& publisherOptions = PublisherOptions());
62  PublisherImpl(const PublisherImpl& other) = delete;
63  PublisherImpl& operator=(const PublisherImpl&) = delete;
64  PublisherImpl(PublisherImpl&& rhs) = default;
65  PublisherImpl& operator=(PublisherImpl&& rhs) = default;
66  virtual ~PublisherImpl() = default;
67 
75  template <typename... Args>
76  cxx::expected<Sample<T, H>, AllocationError> loan(Args&&... args) noexcept;
77 
82  void publish(Sample<T, H>&& sample) noexcept override;
83 
89  cxx::expected<AllocationError> publishCopyOf(const T& val) noexcept;
96  template <typename Callable, typename... ArgTypes>
97  cxx::expected<AllocationError> publishResultOf(Callable c, ArgTypes... args) noexcept;
98 
99  protected:
100  using BasePublisher_t::port;
101 
102  private:
103  Sample<T, H> convertChunkHeaderToSample(mepoo::ChunkHeader* const header) noexcept;
104 
105  cxx::expected<Sample<T, H>, AllocationError> loanSample() noexcept;
106 
108  PublisherSampleDeleter m_sampleDeleter{port()};
109 };
110 
111 template <typename T, typename H = mepoo::NoUserHeader>
113 
114 } // namespace popo
115 } // namespace iox
116 
117 #include "iceoryx_posh/internal/popo/publisher.inl"
118 
119 #endif // IOX_POSH_POPO_TYPED_PUBLISHER_HPP
class for the identification of a communication event including information on the service,...
Definition: service_description.hpp:86
const port_t & port() const noexcept
port
Definition: base_publisher.inl:77
Definition: publisher.hpp:47
void publish(Sample< T, H > &&sample) noexcept override
publish Publishes the given sample and then releases its loan.
Definition: publisher.inl:84
cxx::expected< AllocationError > publishCopyOf(const T &val) noexcept
publishCopyOf Copy the provided value into a loaned shared memory chunk and publish it.
Definition: publisher.inl:59
cxx::expected< Sample< T, H >, AllocationError > loan(Args &&... args) noexcept
loan Get a sample from loaned shared memory and consctruct the data with the given arguments.
Definition: publisher.inl:36
cxx::expected< AllocationError > publishResultOf(Callable c, ArgTypes... args) noexcept
publishResultOf Loan a sample from memory, execute the provided callable to write to it,...
Definition: publisher.inl:43
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
Definition: service_description.hpp:29
Definition: chunk_header.hpp:42
This struct is used to configure the publisher.
Definition: publisher_options.hpp:30