95 lines
2.8 KiB
C++
95 lines
2.8 KiB
C++
|
/**
|
|||
|
* @file TopicMonitorFactory.cpp
|
|||
|
* @author jinchao
|
|||
|
* @brief 主题监控工厂类实现
|
|||
|
* @version 1.0
|
|||
|
* @date 2025-03-10
|
|||
|
*
|
|||
|
* @copyright Copyright (c) 2025 COMAC
|
|||
|
*
|
|||
|
*/
|
|||
|
#include "TopicMonitorFactory.h"
|
|||
|
#include "../ModelTopicMonitor/AerodynamicsMonitor.h"
|
|||
|
#include "../ModelTopicMonitor/WeightBalanceMonitor.h"
|
|||
|
#include "../ModelTopicMonitor/GroundHandlingMonitor.h"
|
|||
|
#include <QDebug>
|
|||
|
|
|||
|
TopicMonitorFactory &TopicMonitorFactory::getInstance()
|
|||
|
{
|
|||
|
// 单例模式
|
|||
|
static TopicMonitorFactory instance;
|
|||
|
|
|||
|
// 在首次调用时初始化默认的监控类型
|
|||
|
static bool initialized = false;
|
|||
|
if (!initialized) {
|
|||
|
// 注册气动模型主题监控器
|
|||
|
instance.registerMonitorType(
|
|||
|
"XNAerodynamics", [](QObject *parent) { return new AerodynamicsMonitor(parent); });
|
|||
|
|
|||
|
// 注册质量模型主题监控器
|
|||
|
instance.registerMonitorType(
|
|||
|
"XNWeightBalance", [](QObject *parent) { return new WeightBalanceMonitor(parent); });
|
|||
|
|
|||
|
// 注册地操模型主题监控器
|
|||
|
instance.registerMonitorType(
|
|||
|
"XNGroundHandling", [](QObject *parent) { return new GroundHandlingMonitor(parent); });
|
|||
|
|
|||
|
initialized = true;
|
|||
|
}
|
|||
|
|
|||
|
return instance;
|
|||
|
}
|
|||
|
|
|||
|
QSharedPointer<TopicMonitor> TopicMonitorFactory::createMonitor(const QString &modelName,
|
|||
|
QObject *parent)
|
|||
|
{
|
|||
|
// 首先找到匹配的类型前缀
|
|||
|
QString matchedPrefix;
|
|||
|
for (auto it = m_creators.begin(); it != m_creators.end(); ++it) {
|
|||
|
if (modelName.contains(it.key(), Qt::CaseInsensitive)) {
|
|||
|
matchedPrefix = it.key();
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 如果没有匹配的类型,返回nullptr
|
|||
|
if (matchedPrefix.isEmpty()) {
|
|||
|
qWarning() << "无法为主题" << modelName << "创建监控器,未找到匹配的类型";
|
|||
|
return nullptr;
|
|||
|
}
|
|||
|
|
|||
|
// 检查是否已经存在该类型的实例
|
|||
|
if (m_monitorInstances.contains(matchedPrefix)) {
|
|||
|
qDebug() << "返回已存在的" << matchedPrefix << "监控器实例";
|
|||
|
return m_monitorInstances[matchedPrefix];
|
|||
|
}
|
|||
|
|
|||
|
// 创建新的监控器实例
|
|||
|
TopicMonitor *monitor = m_creators[matchedPrefix](parent);
|
|||
|
if (!monitor) {
|
|||
|
qWarning() << "创建" << matchedPrefix << "类型的监控器失败";
|
|||
|
return nullptr;
|
|||
|
}
|
|||
|
|
|||
|
// 缓存并返回新创建的实例
|
|||
|
QSharedPointer<TopicMonitor> monitorPtr(monitor);
|
|||
|
m_monitorInstances[matchedPrefix] = monitorPtr;
|
|||
|
qDebug() << "已创建并缓存" << matchedPrefix << "类型的监控器实例";
|
|||
|
|
|||
|
return monitorPtr;
|
|||
|
}
|
|||
|
|
|||
|
void TopicMonitorFactory::registerMonitorType(const QString &modelName,
|
|||
|
std::function<TopicMonitor *(QObject *)> creator)
|
|||
|
{
|
|||
|
// 注册模型主题监控器创建函数
|
|||
|
m_creators[modelName] = creator;
|
|||
|
qDebug() << "已注册模型" << modelName << "的主题监控器创建函数";
|
|||
|
}
|
|||
|
|
|||
|
void TopicMonitorFactory::clearMonitors()
|
|||
|
{
|
|||
|
// 清除所有缓存的监控器实例
|
|||
|
m_monitorInstances.clear();
|
|||
|
qDebug() << "已清除所有缓存的监控器实例";
|
|||
|
}
|