RTC Toolkit  2.0.0
agnosticDataSamples.hpp
Go to the documentation of this file.
1 
11 #ifndef RTCTK_TELSUB_AGNOSTICDATASAMPLES_HPP
12 #define RTCTK_TELSUB_AGNOSTICDATASAMPLES_HPP
13 #include <rtctk/config.hpp>
14 
15 #include <cassert>
16 #include <iostream>
17 #include <memory_resource>
18 #include <system_error>
19 #include <vector>
20 
22 
23 namespace rtctk::telSub {
24 
25 // Alias of the DDS topic type used throughout telSub
26 using DdsSample = rtctk::componentFramework::AgnosticTopic;
27 using DdsSampleSeq = rtctk::componentFramework::AgnosticTopicSeq;
28 
29 using DdsInfoSeq = rtctk::componentFramework::SampleInfoSeq;
30 
36 class SeqLoanerIf {
37 public:
49  virtual std::error_code
50  ReturnLoan(std::uint8_t idx, DdsSampleSeq& samples, DdsInfoSeq& infos) RTCTK_NOEXCEPT = 0;
51 };
52 
72 public:
73  using Alloc = std::pmr::polymorphic_allocator<std::byte>;
74 
82  AgnosticDataSamples(std::uint8_t size, SeqLoanerIf& loaner, Alloc const& alloc = {});
83  ~AgnosticDataSamples() noexcept;
84 
87 
89 
93  inline std::uint8_t Size() const noexcept {
94  return m_samples.size();
95  }
96 
106  inline DdsSampleSeq const& operator[](std::uint8_t idx) const noexcept {
107  assert(idx < Size());
108  return m_samples[idx];
109  }
110 
120  inline DdsSampleSeq& operator[](std::uint8_t idx) noexcept {
121  assert(idx < Size());
122  return m_samples[idx];
123  }
124 
132  inline DdsSampleSeq const& At(std::uint8_t idx) const {
133  if (idx < Size()) {
134  return this->operator[](idx);
135  }
136  throw std::out_of_range("idx is out of range");
137  }
138 
142  inline bool Empty() const noexcept;
143 
147  inline bool Full() const noexcept;
149 
151 
161  inline std::tuple<DdsSampleSeq&, DdsInfoSeq&> GetUpdateRefs(std::uint8_t idx) noexcept {
162  assert(idx < Size());
163  return {m_samples[idx], m_infos[idx]};
164  }
165 
172  inline void Clear(std::uint8_t idx) noexcept;
173 
179  inline void Clear() noexcept;
181 private:
183  /* @{ */
187  std::pmr::vector<DdsSampleSeq> m_samples;
191  std::pmr::vector<DdsInfoSeq> m_infos;
192  /* @} */
193 
195  SeqLoanerIf& m_loaner;
196 };
197 
198 void AgnosticDataSamples::Clear() noexcept {
199  for (std::uint8_t idx = 0u; idx < m_samples.size(); ++idx) {
200  Clear(idx);
201  }
202 }
203 void AgnosticDataSamples::Clear(std::uint8_t idx) noexcept {
204  assert(idx < Size());
205  auto& seq = m_samples[idx];
206  if (seq.has_ownership()) {
207  // if sequence owns its own memory we simply release the elements
208  (void)seq.length(0);
209  } else {
210  // Otherwise we return the loaned sequence
211  (void)m_loaner.ReturnLoan(idx, seq, m_infos[idx]);
212  }
213 }
214 
215 bool AgnosticDataSamples::Empty() const noexcept {
216  for (std::uint8_t idx = 0u; idx < Size(); ++idx) {
217  if (m_samples[idx].length() > 0) {
218  return false;
219  }
220  }
221  return true;
222 }
223 
224 bool AgnosticDataSamples::Full() const noexcept {
225  for (std::uint8_t idx = 0u; idx < Size(); ++idx) {
226  if (m_samples[idx].length() == 0) {
227  return false;
228  }
229  }
230  return true;
231 }
232 
233 } // namespace rtctk::telSub
234 #endif // #ifndef RTCTK_TELSUB_AGNOSTICDATASAMPLES_HPP
rtctk::telSub::DdsSampleSeq
rtctk::componentFramework::AgnosticTopicSeq DdsSampleSeq
Definition: agnosticDataSamples.hpp:27
rtctk::telSub::AgnosticDataSamples::Size
std::uint8_t Size() const noexcept
Definition: agnosticDataSamples.hpp:93
rtctk::telSub::AgnosticDataSamples::GetUpdateRefs
std::tuple< DdsSampleSeq &, DdsInfoSeq & > GetUpdateRefs(std::uint8_t idx) noexcept
Get references to objects to be updated with new values.
Definition: agnosticDataSamples.hpp:161
config.hpp
Project-wide configuration header.
rtctk::telSub::AgnosticDataSamples::At
DdsSampleSeq const & At(std::uint8_t idx) const
Access sample sequence idx with bounds checking.
Definition: agnosticDataSamples.hpp:132
rtctk::telSub::AgnosticDataSamples::Clear
void Clear() noexcept
Return samples if not owned and clear all sequences.
Definition: agnosticDataSamples.hpp:198
rtctk::telSub::AgnosticDataSamples::operator[]
DdsSampleSeq & operator[](std::uint8_t idx) noexcept
Access sample sequence at index idx without bounds checking.
Definition: agnosticDataSamples.hpp:120
rtctk::telSub::SeqLoanerIf
Loaner interface.
Definition: agnosticDataSamples.hpp:36
rtctk::telSub::AgnosticDataSamples::AgnosticDataSamples
AgnosticDataSamples(std::uint8_t size, SeqLoanerIf &loaner, Alloc const &alloc={})
Construct instance with the specified number of topics.
Definition: agnosticDataSamples.cpp:15
rtctk::telSub::AgnosticDataSamples::Empty
bool Empty() const noexcept
Definition: agnosticDataSamples.hpp:215
rtctk::telSub::AgnosticDataSamples::Full
bool Full() const noexcept
Definition: agnosticDataSamples.hpp:224
rtctk::telSub::AgnosticDataSamples::~AgnosticDataSamples
~AgnosticDataSamples() noexcept
Definition: agnosticDataSamples.cpp:22
rtctk::telSub
Definition: main.cpp:6
rtctk_config_tool.delete
def delete(repo, path, recursive)
Definition: rtctk_config_tool.py:216
rtctk::telSub::DdsSample
rtctk::componentFramework::AgnosticTopic DdsSample
Definition: agnosticDataSamples.hpp:26
rtctk::telSub::SeqLoanerIf::ReturnLoan
virtual std::error_code ReturnLoan(std::uint8_t idx, DdsSampleSeq &samples, DdsInfoSeq &infos) RTCTK_NOEXCEPT=0
Returns loaned sample sequence.
std
Definition: mudpiProcessingError.hpp:119
rtctk::telSub::AgnosticDataSamples::operator[]
DdsSampleSeq const & operator[](std::uint8_t idx) const noexcept
Access sample sequence at index idx without bounds checking.
Definition: agnosticDataSamples.hpp:106
RTCTK_NOEXCEPT
#define RTCTK_NOEXCEPT
Definition: config.hpp:15
rtctk::telSub::DdsInfoSeq
rtctk::componentFramework::SampleInfoSeq DdsInfoSeq
Definition: agnosticDataSamples.hpp:29
dds.hpp
Declares common DDS class.
rtctk::telSub::AgnosticDataSamples
Container of DDS samples and associated sample information.
Definition: agnosticDataSamples.hpp:71
rtctk::telSub::AgnosticDataSamples::Alloc
std::pmr::polymorphic_allocator< std::byte > Alloc
Definition: agnosticDataSamples.hpp:73