You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AppFlow/FITK_Kernel/FITKCore/FITKOperatorRepo.h

126 lines
3.5 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
*
* @file FITKOperatorRepo.h
* @brief 操作器仓库声明
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-04
*
*/
#ifndef __FITK_OPERATOR_REPO_H__
#define __FITK_OPERATOR_REPO_H__
#include "FITKCoreAPI.h"
#include "FITKCoreMacros.h"
#include "FITKAbstractObject.hpp"
#include <QMutex>
#include <QHash>
#include <functional>
#include <QString>
#include <QDebug>
namespace Core {
class FITKAbstractOperator;
class FITKOperatorRepoPrivate;
class FITKOperatorDeletor;
/**
* @brief 操作器仓库声明
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-04
*/
class FITKCoreAPI FITKOperatorRepo : public FITKAbstractObject {
friend FITKOperatorDeletor;
//声明为单例
DeclSingleton(FITKOperatorRepo);
/**
* @brief 获取操作器,不存在的直接创建,存在的返回原有对象
* @param[i] key Operator注册的关键字
* @return FITKAbstractOperator*
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-04
*/
FITKAbstractOperator* getOperator(const QString& key);
template <class T>
/**
* @brief 获取操作器并转换为T类型不存在的直接创建存在的返回原有对象
* @param[i] key Operator注册的关键字
* @return FITKAbstractOperator*
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-04
*/
T* getOperatorT(const QString& key) {
return dynamic_cast<T*> (this->getOperator(key));
}
private:
/**
* @brief 初始化
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-04
*/
void initialize();
/**
* @brief 结束,析构函数调用
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-07-17
*/
void finalize();
private:
/**
* @brief 注册进来的创建operator函数
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-04
*/
QHash<QString, std::function<FITKAbstractOperator*()>> _createFuns{};
/**
* @brief 实际执行创建操作
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-04
*/
FITKOperatorRepoPrivate* _private{};
public:
template < typename T >
/**
* @brief 注册器
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
class FITKOperatorRepoRegHelper {
public:
FITKOperatorRepoRegHelper(QString key) {
auto ins = FITKOperatorRepo::getInstance();
qDebug() << key;
// 注册构造函数T类型必须有默认的无参构造函数
ins->_createFuns.insert( key, [&] { return new T; });
}
};
};
}
/**
* @brief 声明注册宏
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
#ifndef Register2FITKOPeratorRepo
#define Register2FITKOPeratorRepo(key,Class,...) \
static Core::FITKOperatorRepo::FITKOperatorRepoRegHelper< Class > \
_FITKDataIORegHelper_##key##_##Class##__VA_ARGS__(#key)
#endif
/**
* @brief 访问单例宏
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-04
*/
#ifndef FITKOPERREPO
#define FITKOPERREPO Core::FITKOperatorRepo::getInstance()
#endif // !FITKOPERREPO
#endif // !__FITK_OPERATOR_REPO_H__