Files
2026-10-23 16:13:31 +00:00

78 lines
2.5 KiB
C++

#pragma once
#include "ddkit/access/TopicPool.hpp"
#include "ddkit/access/Topic.hpp"
#include "ddkit/access/Dispatcher.hpp"
#include "ddkit/access/Reader.hpp"
#include "ddkit/utils/string.hpp"
#include <utility>
namespace ddkit {
namespace access {
class TopicPoolRead final {
public:
TopicPoolRead(
Participant& participant,
const IQosStrategyPtr& qosStrategy,
const ITopicStrategyPtr& topicStrategy,
std::shared_ptr<Dispatcher> dispatcher);
~TopicPoolRead();
// create Topic and Reader and register function for beeing callbacked by given dispatcher
// (a second callback may be given for reading disposes as well)
// todo name is historical, could be createReader
template <typename T>
void createTopicRead(std::function<void(T const&)> func, std::function<void(T const&)> funcDispose = nullptr) {
DDKIT_LOG_TRACE;
std::unique_lock<std::recursive_mutex> lock(m_mutex);
std::string topicName = m_topicPool.getTopicName<T>();
std::shared_ptr<Topic<T>> topic = m_topicPool.topic<T>();
if(!topic) {
ddkit::log::error() << "TopicPoolRead::createTopicRead: no topic in topic pool.";
return;
}
if(topic->hasReader()) {
ddkit::log::error() << "TopicPoolRead::createTopicRead: Reader (" << topicName << ") already exists.";
return;
}
QosPattern qosPattern = m_topicPool.qosPattern(topic->typeName());
dds::sub::qos::DataReaderQos dataReaderQos = qosPattern.qosRead();
dds::sub::qos::SubscriberQos subQos = qosPattern.qosSub();
auto reader = std::make_shared<Reader<T>>(m_dispatcher, topic, dataReaderQos, subQos, func, funcDispose);
topic->setReader(reader);
}
// to be able to call with member-function-callbacks as well - transform to std::function
// - this is used in ldm/test-context so far, no dispose-callback used here so far ...
template <typename T, typename U>
void createTopicRead(void (U::*funcPtr)(T const&), U& u) {
DDKIT_LOG_TRACE;
createTopicRead<T>(std::bind(funcPtr, &u, std::placeholders::_1));
}
bool hasReaders();
void closeReaders();
void unloadReaders();
private:
std::recursive_mutex m_mutex;
TopicPool m_topicPool;
std::shared_ptr<Dispatcher> m_dispatcher;
// no copying
TopicPoolRead(TopicPoolRead const&) = delete;
TopicPoolRead(TopicPoolRead&&) = delete;
void operator=(TopicPoolRead const&) = delete;
};
} // namespace access
} // namespace ddkit