RTC Toolkit 4.0.2
Loading...
Searching...
No Matches
agnosticDataSamples.hpp
Go to the documentation of this file.
1
12#ifndef RTCTK_TELSUB_AGNOSTICDATASAMPLES_HPP
13#define RTCTK_TELSUB_AGNOSTICDATASAMPLES_HPP
14#include <rtctk/config.hpp>
15
16#include <cassert>
17#include <iostream>
18#include <memory_resource>
19#include <system_error>
20#include <vector>
21
23
24namespace rtctk::telSub {
25
26// Alias of the DDS topic type used throughout telSub
27using DdsSample = rtctk::componentFramework::AgnosticTopic;
28using DdsSampleSeq = rtctk::componentFramework::AgnosticTopicSeq;
29
30using DdsInfoSeq = rtctk::componentFramework::SampleInfoSeq;
31
39public:
51 virtual std::error_code
52 ReturnLoan(std::uint8_t idx, DdsSampleSeq& samples, DdsInfoSeq& infos) RTCTK_NOEXCEPT = 0;
53};
54
75public:
76 using Alloc = std::pmr::polymorphic_allocator<std::byte>;
77
85 AgnosticDataSamples(std::uint8_t size, SeqLoanerIf& loaner, Alloc const& alloc = {});
86 ~AgnosticDataSamples() noexcept;
87
89 AgnosticDataSamples& operator=(AgnosticDataSamples const&) = delete;
90
93
96 inline std::uint8_t Size() const noexcept {
97 return m_samples.size();
98 }
99
109 inline DdsSampleSeq const& operator[](std::uint8_t idx) const noexcept {
110 assert(idx < Size());
111 return m_samples[idx];
112 }
113
123 inline DdsSampleSeq& operator[](std::uint8_t idx) noexcept {
124 assert(idx < Size());
125 return m_samples[idx];
126 }
127
135 inline DdsSampleSeq const& At(std::uint8_t idx) const {
136 if (idx < Size()) {
137 return this->operator[](idx);
138 }
139 throw std::out_of_range("idx is out of range");
140 }
141
145 inline bool Empty() const noexcept;
146
150 inline bool Full() const noexcept;
152
155
164 inline std::tuple<DdsSampleSeq&, DdsInfoSeq&> GetUpdateRefs(std::uint8_t idx) noexcept {
165 assert(idx < Size());
166 return {m_samples[idx], m_infos[idx]};
167 }
168
175 inline void Clear(std::uint8_t idx) noexcept;
176
182 inline void Clear() noexcept;
184private:
186 /* @{ */
190 std::pmr::vector<DdsSampleSeq> m_samples;
194 std::pmr::vector<DdsInfoSeq> m_infos;
195 /* @} */
196
198 SeqLoanerIf& m_loaner;
199};
200
201void AgnosticDataSamples::Clear() noexcept {
202 for (std::uint8_t idx = 0u; idx < m_samples.size(); ++idx) {
203 Clear(idx);
204 }
205}
206void AgnosticDataSamples::Clear(std::uint8_t idx) noexcept {
207 assert(idx < Size());
208 auto& seq = m_samples[idx];
209 if (seq.has_ownership()) {
210 // if sequence owns its own memory we simply release the elements
211 (void)seq.length(0);
212 } else {
213 // Otherwise we return the loaned sequence
214 (void)m_loaner.ReturnLoan(idx, seq, m_infos[idx]);
215 }
216}
217
218bool AgnosticDataSamples::Empty() const noexcept {
219 for (std::uint8_t idx = 0u; idx < Size(); ++idx) {
220 if (m_samples[idx].length() > 0) {
221 return false;
222 }
223 }
224 return true;
225}
226
227bool AgnosticDataSamples::Full() const noexcept {
228 for (std::uint8_t idx = 0u; idx < Size(); ++idx) {
229 if (m_samples[idx].length() == 0) {
230 return false;
231 }
232 }
233 return true;
234}
235
236} // namespace rtctk::telSub
237
238#endif // RTCTK_TELSUB_AGNOSTICDATASAMPLES_HPP
Container of DDS samples and associated sample information.
Definition: agnosticDataSamples.hpp:74
std::uint8_t Size() const noexcept
Definition: agnosticDataSamples.hpp:96
bool Empty() const noexcept
Definition: agnosticDataSamples.hpp:218
DdsSampleSeq const & At(std::uint8_t idx) const
Access sample sequence idx with bounds checking.
Definition: agnosticDataSamples.hpp:135
~AgnosticDataSamples() noexcept
Definition: agnosticDataSamples.cpp:21
std::pmr::polymorphic_allocator< std::byte > Alloc
Definition: agnosticDataSamples.hpp:76
bool Full() const noexcept
Definition: agnosticDataSamples.hpp:227
DdsSampleSeq & operator[](std::uint8_t idx) noexcept
Access sample sequence at index idx without bounds checking.
Definition: agnosticDataSamples.hpp:123
DdsSampleSeq const & operator[](std::uint8_t idx) const noexcept
Access sample sequence at index idx without bounds checking.
Definition: agnosticDataSamples.hpp:109
void Clear() noexcept
Return samples if not owned and clear all sequences.
Definition: agnosticDataSamples.hpp:201
std::tuple< DdsSampleSeq &, DdsInfoSeq & > GetUpdateRefs(std::uint8_t idx) noexcept
Get references to objects to be updated with new values.
Definition: agnosticDataSamples.hpp:164
Loaner interface.
Definition: agnosticDataSamples.hpp:38
virtual std::error_code ReturnLoan(std::uint8_t idx, DdsSampleSeq &samples, DdsInfoSeq &infos) RTCTK_NOEXCEPT=0
Returns loaned sample sequence.
Project-wide configuration header.
#define RTCTK_NOEXCEPT
Definition: config.hpp:63
Declares common DDS class.
Definition: main.cpp:18
rtctk::componentFramework::AgnosticTopic DdsSample
Definition: agnosticDataSamples.hpp:27
rtctk::componentFramework::SampleInfoSeq DdsInfoSeq
Definition: agnosticDataSamples.hpp:30
rtctk::componentFramework::AgnosticTopicSeq DdsSampleSeq
Definition: agnosticDataSamples.hpp:28