51 lines
978 B
C++
Executable File
51 lines
978 B
C++
Executable File
/**
|
|
* @file DataReaderListenerImpl.h
|
|
* @author jinchao
|
|
* @brief 数据读取器监听器模板类
|
|
* @version 1.0
|
|
* @date 2025-03-10
|
|
*
|
|
* @copyright Copyright (c) 2025 COMAC
|
|
*
|
|
*/
|
|
#pragma once
|
|
|
|
#include "TypeDefine.h"
|
|
|
|
/**
|
|
* @brief 数据读取器监听器模板类
|
|
* @tparam T 数据类型
|
|
*/
|
|
template <typename T>
|
|
class DataReaderListenerImpl : public XNDataReaderListener
|
|
{
|
|
public:
|
|
/**
|
|
* @brief 构造函数
|
|
* @param callback 回调函数
|
|
*/
|
|
DataReaderListenerImpl(std::function<void(const T &)> callback) : callback_(callback) {}
|
|
|
|
/**
|
|
* @brief 数据可用回调函数
|
|
* @param reader 数据读取器
|
|
*/
|
|
void on_data_available(XNDataReader *reader) override
|
|
{
|
|
XNSampleInfo info;
|
|
if (reader->take_next_sample(&data_, &info) == eprosima::fastdds::dds::RETCODE_OK
|
|
&& info.valid_data) {
|
|
callback_(data_);
|
|
}
|
|
}
|
|
|
|
private:
|
|
/**
|
|
* @brief 数据
|
|
*/
|
|
T data_;
|
|
/**
|
|
* @brief 回调函数
|
|
*/
|
|
std::function<void(const T &)> callback_;
|
|
}; |