RTC Toolkit 5.0.0
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 <string>
20#include <system_error>
21#include <unordered_map>
22#include <vector>
23
25
26namespace rtctk::telSub {
27
28// Alias of the DDS topic type used throughout telSub
29using DdsSample = rtctk::componentFramework::AgnosticTopic;
31
33
41public:
53 virtual std::error_code ReturnLoan(const std::string& topic,
54 DdsSampleSeq& samples,
56};
57
78private:
82 const size_t m_expected_topic_count;
86 std::pmr::map<std::string, std::pair<DdsSampleSeq, DdsInfoSeq>> m_samples;
87 SeqLoanerIf& m_loaner;
88
89public:
90 using Alloc = std::pmr::polymorphic_allocator<std::byte>;
91
100 : m_expected_topic_count(topic_count), m_samples(alloc), m_loaner(loaner) {
101 }
105
108
111
114 inline std::uint8_t Size() const noexcept {
115 return m_samples.size();
116 }
117
126 inline DdsSampleSeq& operator[](const std::string& topic) noexcept {
127 return m_samples[topic].first;
128 }
129
137 inline const DdsSampleSeq& At(const std::string& topic) const {
138 return m_samples.at(topic).first;
139 }
140
144 inline bool Empty() const noexcept;
145
149 inline bool Full() const noexcept;
151
154
163 return {m_samples[topic].first, m_samples[topic].second};
164 }
165
172 inline void Clear(const std::string& topic) noexcept;
173
179 inline void Clear() noexcept;
180
183
184 iterator begin() { // NOLINT(readability-identifier-naming)
185 return m_samples.begin();
186 };
187
188 const_iterator cbegin() { // NOLINT(readability-identifier-naming)
189 return m_samples.cbegin();
190 };
191
192 iterator end() { // NOLINT(readability-identifier-naming)
193 return m_samples.end();
194 };
195
196 const_iterator cend() { // NOLINT(readability-identifier-naming)
197 return m_samples.cend();
198 };
199
201};
202
207 for (const auto& entry : m_samples) {
208 Clear(entry.first);
209 }
210 assert(Empty());
211}
212void AgnosticDataSamples::Clear(const std::string& topic) noexcept {
213 auto& seq = m_samples.at(topic).first;
214 if (seq.has_ownership()) {
215 // if sequence owns its own memory we simply release the elements
216 (void)seq.length(0);
217 } else {
218 // Otherwise we return the loaned sequence
219 (void)m_loaner.ReturnLoan(topic, seq, m_samples[topic].second);
220 }
221 assert(not Full());
222}
223
225 for (const auto& [topic, sample] : m_samples) {
226 if (sample.first.length() > 0) {
227 return false;
228 }
229 }
230 return true;
231}
232
234 if (m_samples.size() != m_expected_topic_count) {
235 return false;
236 }
237 for (const auto& [topic, sample] : m_samples) {
238 if (sample.first.length() == 0) {
239 return false;
240 }
241 }
242 return true;
243}
244
245} // namespace rtctk::telSub
246
247#endif // RTCTK_TELSUB_AGNOSTICDATASAMPLES_HPP
Container of DDS samples and associated sample information.
Definition agnosticDataSamples.hpp:77
const_iterator cend()
Definition agnosticDataSamples.hpp:196
std::uint8_t Size() const noexcept
Definition agnosticDataSamples.hpp:114
bool Empty() const noexcept
Definition agnosticDataSamples.hpp:224
~AgnosticDataSamples() noexcept
Definition agnosticDataSamples.hpp:102
std::pmr::polymorphic_allocator< std::byte > Alloc
Definition agnosticDataSamples.hpp:90
iterator begin()
Definition agnosticDataSamples.hpp:184
AgnosticDataSamples(const AgnosticDataSamples &)=delete
bool Full() const noexcept
Definition agnosticDataSamples.hpp:233
iterator end()
Definition agnosticDataSamples.hpp:192
AgnosticDataSamples(SeqLoanerIf &loaner, size_t topic_count, const Alloc &alloc={})
Construct instance with the specified number of topics.
Definition agnosticDataSamples.hpp:99
std::tuple< DdsSampleSeq &, DdsInfoSeq & > GetUpdateRefs(const std::string &topic) noexcept
Get references to objects to be updated with new values.
Definition agnosticDataSamples.hpp:162
AgnosticDataSamples & operator=(const AgnosticDataSamples &)=delete
const DdsSampleSeq & At(const std::string &topic) const
Access sample sequence for topic.
Definition agnosticDataSamples.hpp:137
DdsSampleSeq & operator[](const std::string &topic) noexcept
Access sample sequence for topic topic, will create it if it does not exist.
Definition agnosticDataSamples.hpp:126
void Clear() noexcept
Return samples if not owned and clear all sequences.
Definition agnosticDataSamples.hpp:206
const_iterator cbegin()
Definition agnosticDataSamples.hpp:188
decltype(m_samples)::const_iterator const_iterator
Definition agnosticDataSamples.hpp:182
decltype(m_samples)::iterator iterator
Definition agnosticDataSamples.hpp:181
Loaner interface.
Definition agnosticDataSamples.hpp:40
virtual std::error_code ReturnLoan(const std::string &topic, 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.
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
Definition main.cpp:24
rtctk::componentFramework::AgnosticTopicSeq DdsSampleSeq
Definition agnosticDataSamples.hpp:30
rtctk::componentFramework::SampleInfoSeq DdsInfoSeq
Definition agnosticDataSamples.hpp:32
rtctk::componentFramework::AgnosticTopic DdsSample
Definition agnosticDataSamples.hpp:29