RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
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 <string>
19#include <system_error>
20#include <unordered_map>
21#include <vector>
22
24
25namespace rtctk::telSub {
26
27// Alias of the DDS topic type used throughout telSub
28using DdsSample = rtctk::componentFramework::AgnosticTopic;
29using DdsSampleSeq = rtctk::componentFramework::AgnosticTopicSeq;
30
31using DdsInfoSeq = rtctk::componentFramework::SampleInfoSeq;
32
40public:
52 virtual std::error_code
53 ReturnLoan(const std::string& topic, DdsSampleSeq& samples, DdsInfoSeq& infos) noexcept = 0;
54};
55
76private:
80 const size_t m_expected_topic_count;
84 std::pmr::map<std::string, std::pair<DdsSampleSeq, DdsInfoSeq>> m_samples;
85 SeqLoanerIf& m_loaner;
86
87public:
88 using Alloc = std::pmr::polymorphic_allocator<std::byte>;
89
97 AgnosticDataSamples(SeqLoanerIf& loaner, size_t topic_count, const Alloc& alloc = {})
98 : m_expected_topic_count(topic_count), m_samples(alloc), m_loaner(loaner) {
99 }
101 Clear();
102 }
103
106
109
112 inline std::uint8_t Size() const noexcept {
113 return m_samples.size();
114 }
115
124 inline DdsSampleSeq& operator[](const std::string& topic) noexcept {
125 return m_samples[topic].first;
126 }
127
135 inline const DdsSampleSeq& At(const std::string& topic) const {
136 return m_samples.at(topic).first;
137 }
138
142 inline bool Empty() const noexcept;
143
147 inline bool Full() const noexcept;
149
152
160 inline std::tuple<DdsSampleSeq&, DdsInfoSeq&> GetUpdateRefs(const std::string& topic) noexcept {
161 return {m_samples[topic].first, m_samples[topic].second};
162 }
163
170 inline void Clear(const std::string& topic) noexcept;
171
177 inline void Clear() noexcept;
178
179 using iterator = decltype(m_samples)::iterator;
180 using const_iterator = decltype(m_samples)::const_iterator;
181
182 iterator begin() { // NOLINT(readability-identifier-naming)
183 return m_samples.begin();
184 };
185
186 const_iterator cbegin() { // NOLINT(readability-identifier-naming)
187 return m_samples.cbegin();
188 };
189
190 iterator end() { // NOLINT(readability-identifier-naming)
191 return m_samples.end();
192 };
193
194 const_iterator cend() { // NOLINT(readability-identifier-naming)
195 return m_samples.cend();
196 };
197
199};
200
205 for (const auto& entry : m_samples) {
206 Clear(entry.first);
207 }
208 assert(Empty());
209}
210void AgnosticDataSamples::Clear(const std::string& topic) noexcept {
211 auto& seq = m_samples.at(topic).first;
212 if (seq.has_ownership()) {
213 // if sequence owns its own memory we simply release the elements
214 (void)seq.length(0);
215 } else {
216 // Otherwise we return the loaned sequence
217 (void)m_loaner.ReturnLoan(topic, seq, m_samples[topic].second);
218 }
219 assert(not Full());
220}
221
222bool AgnosticDataSamples::Empty() const noexcept {
223 for (const auto& [topic, sample] : m_samples) {
224 if (sample.first.length() > 0) {
225 return false;
226 }
227 }
228 return true;
229}
230
231bool AgnosticDataSamples::Full() const noexcept {
232 if (m_samples.size() != m_expected_topic_count) {
233 return false;
234 }
235 for (const auto& [topic, sample] : m_samples) {
236 if (sample.first.length() == 0) {
237 return false;
238 }
239 }
240 return true;
241}
242
243} // namespace rtctk::telSub
244
245#endif // RTCTK_TELSUB_AGNOSTICDATASAMPLES_HPP
const_iterator cend()
Definition agnosticDataSamples.hpp:194
std::uint8_t Size() const noexcept
Definition agnosticDataSamples.hpp:112
bool Empty() const noexcept
Definition agnosticDataSamples.hpp:222
~AgnosticDataSamples() noexcept
Definition agnosticDataSamples.hpp:100
std::pmr::polymorphic_allocator< std::byte > Alloc
Definition agnosticDataSamples.hpp:88
iterator begin()
Definition agnosticDataSamples.hpp:182
AgnosticDataSamples(const AgnosticDataSamples &)=delete
bool Full() const noexcept
Definition agnosticDataSamples.hpp:231
iterator end()
Definition agnosticDataSamples.hpp:190
AgnosticDataSamples(SeqLoanerIf &loaner, size_t topic_count, const Alloc &alloc={})
Construct instance with the specified number of topics.
Definition agnosticDataSamples.hpp:97
std::tuple< DdsSampleSeq &, DdsInfoSeq & > GetUpdateRefs(const std::string &topic) noexcept
Get references to objects to be updated with new values.
Definition agnosticDataSamples.hpp:160
AgnosticDataSamples & operator=(const AgnosticDataSamples &)=delete
const DdsSampleSeq & At(const std::string &topic) const
Access sample sequence for topic.
Definition agnosticDataSamples.hpp:135
DdsSampleSeq & operator[](const std::string &topic) noexcept
Access sample sequence for topic topic, will create it if it does not exist.
Definition agnosticDataSamples.hpp:124
void Clear() noexcept
Return samples if not owned and clear all sequences.
Definition agnosticDataSamples.hpp:204
const_iterator cbegin()
Definition agnosticDataSamples.hpp:186
decltype(m_samples)::const_iterator const_iterator
Definition agnosticDataSamples.hpp:180
decltype(m_samples)::iterator iterator
Definition agnosticDataSamples.hpp:179
Loaner interface.
Definition agnosticDataSamples.hpp:39
virtual std::error_code ReturnLoan(const std::string &topic, DdsSampleSeq &samples, DdsInfoSeq &infos) noexcept=0
Returns loaned sample sequence.
Project-wide configuration header.
Declares common DDS class.
Definition main.cpp:23
rtctk::componentFramework::AgnosticTopicSeq DdsSampleSeq
Definition agnosticDataSamples.hpp:29
rtctk::componentFramework::SampleInfoSeq DdsInfoSeq
Definition agnosticDataSamples.hpp:31
rtctk::componentFramework::AgnosticTopic DdsSample
Definition agnosticDataSamples.hpp:28
Definition ddsSub.hpp:155