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.

109 lines
2.6 KiB
C

/**
*
* @file FITKDataFactory.h
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-07
*
*/
#ifndef _FITKDATAFACTORY_H__
#define _FITKDATAFACTORY_H__
#include "FITKAbstractFactory.h"
#include "FITKCoreMacros.h"
#include <QMutex>
#include <QHash>
#include <functional>
namespace Core
{
class FITKAbstractDataObject;
class FITKDataFactoryDeleator;
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-07
*/
class FITKCoreAPI FITKDataFactory : public FITKAbstractFactory
{
friend FITKDataFactoryDeleator;
//声明为单例
DeclSingleton(FITKDataFactory);
public:
/**
* @brief
* @param[i] key
* @return FITKAbstractObject*
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-07
*/
FITKAbstractObject* create(const QString& key) override;
private:
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-07
*/
void initialize();
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-07-17
*/
void finalize();
private:
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2023-07-24
*/
QHash< QString, std::function< FITKAbstractDataObject*() > > _itemDataFuns{ };
public:
template < typename T >
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-07
*/
class FITKDataFacRegHelper
{
public:
FITKDataFacRegHelper(QString type)
{
auto ins = FITKDataFactory::getInstance();
// 注册构造函数T类型必须有默认的无参构造函数
if (!((ins->_itemDataFuns).contains(type)))
(ins->_itemDataFuns).insert(type, [&] { return new T; });
}
};
};
class FITKDataFactoryDeleator
{
public:
explicit FITKDataFactoryDeleator() = default;
virtual ~FITKDataFactoryDeleator();
};
}
/**
* @brief
*/
#ifndef Register2FITKDataFactory
#define Register2FITKDataFactory( key, T, ... ) \
static Core::FITKDataFactory::FITKDataFacRegHelper< T > \
_FITKDataRegHelper_##T##_##__VA_ARGS__( #key );
#endif
#endif