119 lines
4.3 KiB
C++
119 lines
4.3 KiB
C++
#pragma once
|
||
|
||
#include "XNMonitorServer_global.h"
|
||
#include "DataMonitor.h"
|
||
|
||
//接口头文件
|
||
#include <C909_V1/ATA04/Aerodynamics_heartbeat.hpp>
|
||
#include <C909_V1/ATA04/Aerodynamics_input.hpp>
|
||
#include <C909_V1/ATA04/Aerodynamics_output.hpp>
|
||
#include <C909_V1/ATA04/GroundHandling_heartbeat.hpp>
|
||
#include <C909_V1/ATA04/GroundHandling_input.hpp>
|
||
#include <C909_V1/ATA04/GroundHandling_output.hpp>
|
||
#include <C909_V1/ATA04/WeightBalance_heartbeat.hpp>
|
||
#include <C909_V1/ATA04/WeightBalance_input.hpp>
|
||
#include <C909_V1/ATA04/WeightBalance_output.hpp>
|
||
|
||
/**
|
||
* @brief DataMonitor工厂类,用于创建不同类型的DataMonitor实例
|
||
*/
|
||
class XNMONITORSERVER_EXPORT DataMonitorFactory
|
||
{
|
||
public:
|
||
static DataMonitorBasePtr GetInstance(const std::string &interfaceName)
|
||
{
|
||
if (interfaceName == "Aerodynamics_heartbeat") {
|
||
return GetInstance<XNSim::C909::ATA04::Aerodynamics_heartbeat_Interface>();
|
||
} else if (interfaceName == "Aerodynamics_input") {
|
||
return GetInstance<XNSim::C909::ATA04::Aerodynamics_input_Interface>();
|
||
} else if (interfaceName == "Aerodynamics_output") {
|
||
return GetInstance<XNSim::C909::ATA04::Aerodynamics_output_Interface>();
|
||
} else if (interfaceName == "GroundHandling_heartbeat") {
|
||
return GetInstance<XNSim::C909::ATA04::GroundHandling_heartbeat_Interface>();
|
||
} else if (interfaceName == "GroundHandling_input") {
|
||
return GetInstance<XNSim::C909::ATA04::GroundHandling_input_Interface>();
|
||
} else if (interfaceName == "GroundHandling_output") {
|
||
return GetInstance<XNSim::C909::ATA04::GroundHandling_output_Interface>();
|
||
} else if (interfaceName == "WeightBalance_heartbeat") {
|
||
return GetInstance<XNSim::C909::ATA04::WeightBalance_heartbeat_Interface>();
|
||
} else if (interfaceName == "WeightBalance_input") {
|
||
return GetInstance<XNSim::C909::ATA04::WeightBalance_input_Interface>();
|
||
} else if (interfaceName == "WeightBalance_output") {
|
||
return GetInstance<XNSim::C909::ATA04::WeightBalance_output_Interface>();
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
static void ReleaseInstance(const std::string &interfaceName)
|
||
{
|
||
if (interfaceName == "Aerodynamics_heartbeat") {
|
||
ReleaseInstance<XNSim::C909::ATA04::Aerodynamics_heartbeat_Interface>();
|
||
} else if (interfaceName == "Aerodynamics_input") {
|
||
ReleaseInstance<XNSim::C909::ATA04::Aerodynamics_input_Interface>();
|
||
} else if (interfaceName == "Aerodynamics_output") {
|
||
ReleaseInstance<XNSim::C909::ATA04::Aerodynamics_output_Interface>();
|
||
} else if (interfaceName == "GroundHandling_heartbeat") {
|
||
ReleaseInstance<XNSim::C909::ATA04::GroundHandling_heartbeat_Interface>();
|
||
} else if (interfaceName == "GroundHandling_input") {
|
||
ReleaseInstance<XNSim::C909::ATA04::GroundHandling_input_Interface>();
|
||
} else if (interfaceName == "GroundHandling_output") {
|
||
ReleaseInstance<XNSim::C909::ATA04::GroundHandling_output_Interface>();
|
||
} else if (interfaceName == "WeightBalance_heartbeat") {
|
||
ReleaseInstance<XNSim::C909::ATA04::WeightBalance_heartbeat_Interface>();
|
||
} else if (interfaceName == "WeightBalance_input") {
|
||
ReleaseInstance<XNSim::C909::ATA04::WeightBalance_input_Interface>();
|
||
} else if (interfaceName == "WeightBalance_output") {
|
||
ReleaseInstance<XNSim::C909::ATA04::WeightBalance_output_Interface>();
|
||
}
|
||
}
|
||
|
||
private:
|
||
/**
|
||
* @brief 创建DataMonitor实例
|
||
* @param framework 框架指针
|
||
* @param modelId 模型ID
|
||
* @param DDS_type DDS类型
|
||
* @return 返回创建的DataMonitor实例的智能指针
|
||
*/
|
||
template <typename T>
|
||
static DataMonitorBasePtr GetInstance()
|
||
{
|
||
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
||
// 使用type_index作为键,更可靠的类型标识
|
||
std::type_index typeIndex(typeid(T));
|
||
|
||
// 检查是否已存在实例
|
||
auto it = instances_.find(typeIndex);
|
||
if (it != instances_.end()) {
|
||
return it->second;
|
||
}
|
||
|
||
// 创建新实例
|
||
auto monitor = std::make_shared<DataMonitorProduct<T>>();
|
||
instances_[typeIndex] = monitor;
|
||
return monitor;
|
||
}
|
||
|
||
/**
|
||
* @brief 释放指定类型的实例
|
||
*/
|
||
template <typename T>
|
||
static void ReleaseInstance()
|
||
{
|
||
std::lock_guard<std::mutex> lock(mutex_);
|
||
std::type_index typeIndex(typeid(T));
|
||
if (instances_.find(typeIndex) == instances_.end()) {
|
||
return;
|
||
}
|
||
instances_.erase(typeIndex);
|
||
}
|
||
|
||
private:
|
||
DataMonitorFactory() = delete;
|
||
~DataMonitorFactory() = delete;
|
||
|
||
static std::mutex mutex_;
|
||
static std::unordered_map<std::type_index, DataMonitorBasePtr> instances_;
|
||
};
|