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/FITKAdaptor/FITKIOAdaptorFactory.h

160 lines
4.4 KiB
C

/**
*
* @file FITKIOAdaptorFactory.h
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*
*/
#ifndef __FITK_IO_FACTORY_H___
#define __FITK_IO_FACTORY_H___
#include "FITK_Kernel/FITKCore/FITKAbstractObject.hpp"
#include "FITK_Kernel/FITKCore/FITKCoreMacros.h"
#include "FITKAdaptorAPI.h"
#include <QMutex>
#include <QHash>
#include <functional>
namespace Core
{
class FITKAbstractObject;
}
namespace Adaptor
{
class FITKAbstractIOAdaptor;
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
#ifndef FILETypeFuns
#define FILETypeFuns QHash<QString,std::function<FITKAbstractIOAdaptor*()> >
#endif
class FITKIOAdaptorFactoryDeleator;
class FITKAdaptorAPI FITKIOAdaptorFactory : public Core::FITKAbstractObject
{
friend FITKIOAdaptorFactoryDeleator;
DeclSingleton(FITKIOAdaptorFactory);
public:
/**
* @brief
* @param[i] fileType
* @param[i] className
* @return FITKAbstractIOAdaptor*
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
FITKAbstractIOAdaptor* create(const QString& fileType, const QString & className );
/**
* @brief
* @param[i] fileType
* @param[i] obj
* @return FITKAbstractIOAdaptor*
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
FITKAbstractIOAdaptor* create(const QString& fileType, Core::FITKAbstractObject* obj);
template<class T>
/**
* @brief
* @param[i] fileType
* @param[i] className
* @return T*
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
T* createT(const QString& fileType, const QString & className)
{
return dynamic_cast<T*>(create(fileType, className));
}
template<class T>
/**
* @brief
* @param[i] fileType
* @param[i] obj
* @return T*
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
T* createT(const QString& fileType, Core::FITKAbstractObject* obj)
{
return dynamic_cast<T*>(create(fileType, obj));
}
private:
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
void initialize();
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-07-17
*/
void finalize();
private:
/**
* @brief Adaptor
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
QHash<QString, FILETypeFuns> _createFuns{};
public:
template < typename T >
/**
* @brief
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
class FITKIOAdaFacRegHelper
{
public:
FITKIOAdaFacRegHelper(QString file, QString className)
{
auto ins = FITKIOAdaptorFactory::getInstance();
// 注册构造函数T类型必须有默认的无参构造函数
ins->_createFuns[file][className] = ([&] { return new T; });
}
};
};
class FITKIOAdaptorFactoryDeleator
{
public:
explicit FITKIOAdaptorFactoryDeleator() = default;
virtual ~FITKIOAdaptorFactoryDeleator();
};
}
/**
* @brief Adaptor
* @author LiBaojun (libaojunqd@foxmail.com)
* @date 2024-03-03
*/
#ifndef Register2FITKIOAdaptorFactory
#define Register2FITKIOAdaptorFactory( File,DataClass, T, ...) \
static Adaptor::FITKIOAdaptorFactory::FITKIOAdaFacRegHelper< T > \
_FITKDataIORegHelper_##T##_##File_##__VA_ARGS__(#File, #DataClass);
#endif
#ifndef FITKIOADAPTORFACTORY
#define FITKIOADAPTORFACTORY ( Adaptor::FITKIOAdaptorFactory::getInstance() )
#endif
#endif