RTC Toolkit  1.0.0
shmPublisher.hpp
Go to the documentation of this file.
1 
11 #ifndef RTCKT_TELSUB_SHMPUBLISHER_HPP
12 #define RTCKT_TELSUB_SHMPUBLISHER_HPP
13 #include <cstdint>
14 #include <system_error>
15 #include <vector>
16 
17 #include <ipcq/writer.hpp>
18 
20 
21 namespace rtctk::telSub {
22 
30 template <class UserTopicType, class DataBlender, class ShmWriter = ipcq::Writer<UserTopicType>>
31 class ShmPublisher final : public ShmPublisherIf {
32 public:
33  ShmPublisher(ShmWriter&& shm_writer, DataBlender& blender);
34 
39  std::error_code Publish(DataSamplesView const& samples) noexcept override;
40 
41  void CloseQueue() noexcept override;
42 private:
46  UserTopicType m_topic;
47  DataBlender& m_blender;
48  ShmWriter m_shm;
49 };
50 
56 template <class UserTopicType, class DataBlender, class ShmWriter>
57 auto MakeShmPublisher(ShmWriter&& shm_writer, DataBlender& blender)
58  -> std::unique_ptr<ShmPublisher<UserTopicType, DataBlender, ShmWriter>> {
59  return std::make_unique<ShmPublisher<UserTopicType, DataBlender, ShmWriter>>(
60  std::move(shm_writer), blender);
61 }
62 
63 template <class UserTopicType, class DataBlender, class ShmWriter>
65  DataBlender& blender)
66  : m_topic()
67  , m_blender(blender)
68  , m_shm(std::move(shm_writer)) {
69  static_assert(std::is_invocable_r<std::error_code,
70  DataBlender,
72  UserTopicType&>::value,
73  "DataBlender must have the signature "
74  "`std::error_code(const rtctk::telSub::DataSamplesView&, "
75  "UserTopicType&)`");
76 }
77 
78 template <class UserTopicType, class DataBlender, class ShmWriter>
80  DataSamplesView const& samples) noexcept {
81  // Invoke blender and write to SHM queue.
82  if (auto err = m_blender(samples, m_topic); !err) /*[[likely]]*/ {
83  return m_shm.Write(m_topic, ipcq::Notify::All);
84  } else /*[[unlikely]]*/ {
85  return err;
86  }
87 }
88 
89 template <class UserTopicType, class DataBlender, class ShmWriter>
91  m_shm.Close();
92 }
93 
94 
95 } // namespace rtctk::telSub
96 #endif // #ifndef RTCKT_TELSUB_SHMPUBLISHER_HPP
rtctk::telSub::ShmPublisher
Definition: shmPublisher.hpp:31
rtctk::telSub::ShmPublisherIf
Simple interface to class that owns the shared memory queue.
Definition: shmPublisherIf.hpp:27
shmPublisherIf.hpp
Declares ShmPublisher.
rtctk::telSub::DataSamplesView
A set of correlated agnostic non-owning data samples references.
Definition: dataSampleView.hpp:46
rtctk::telSub::ShmPublisher::ShmPublisher
ShmPublisher(ShmWriter &&shm_writer, DataBlender &blender)
Definition: shmPublisher.hpp:64
rtctk::telSub::ShmPublisher::Publish
std::error_code Publish(DataSamplesView const &samples) noexcept override
Publish correlated DDS samples to shared memory, transforming it first using DataBlender to a UserTop...
Definition: shmPublisher.hpp:79
rtctk::telSub
Definition: main.cpp:6
std
Definition: mudpiProcessingError.hpp:119
rtctk::telSub::ShmPublisher::CloseQueue
void CloseQueue() noexcept override
Close shared memory queue.
Definition: shmPublisher.hpp:90
rtctk::telSub::MakeShmPublisher
auto MakeShmPublisher(ShmWriter &&shm_writer, DataBlender &blender) -> std::unique_ptr< ShmPublisher< UserTopicType, DataBlender, ShmWriter >>
Helper that can deduce DataBlender class template argument.
Definition: shmPublisher.hpp:57