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/FITKOperatorRepoPrivate.cpp

56 lines
1.8 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.

#include "FITKOperatorRepoPrivate.h"
#include "FITKAbstractOperator.h"
namespace Core
{
FITKOperatorRepoPrivate::FITKOperatorRepoPrivate(QHash<QString, std::function<FITKAbstractOperator*()>>& funs)
:_createFuns(funs)
{
}
FITKOperatorRepoPrivate::~FITKOperatorRepoPrivate()
{
//遍历释放全部存在的operator
QList<FITKAbstractOperator*> opers = _existOperators.values();
for (auto oper : opers)
if(oper != nullptr)
delete oper;
//容器清空
_existOperators.clear();
}
Core::FITKAbstractOperator* FITKOperatorRepoPrivate::getOperator(const QString & key)
{
//如果已经存在则返回原来的operator
if (_existOperators.contains(key))
return _existOperators.value(key);
//不存在则直接创建
return this->createOperator(key);
}
Core::FITKAbstractOperator* FITKOperatorRepoPrivate::createOperator(const QString & key)
{
//查找创建函数
auto fun = _createFuns.value(key);
if (!fun) return nullptr;
//执行创建操作
FITKAbstractOperator* op = fun();
if (op == nullptr) return nullptr;
//纳入到容器管理,并关联槽函数
_existOperators.insert(key, op);
connect(op, &FITKAbstractOperator::operatorDestoryedSig,
this, &FITKOperatorRepoPrivate::operatorDestoryedSlot);
return op;
}
void FITKOperatorRepoPrivate::operatorDestoryedSlot(FITKAbstractOperator* oper)
{
//operator被释放 delete
const QString name = _existOperators.key(oper);
if (!_existOperators.contains(name)) return;
//直接从管理器移除不要重复delete
_existOperators.remove(name);
}
}