-
Notifications
You must be signed in to change notification settings - Fork 1
/
instantiationservice.cpp
41 lines (34 loc) · 1.18 KB
/
instantiationservice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "instantiationservice.h"
InstantiationService::InstantiationService(IInstantiationService *instantiationService, QObject *parent) :
IInstantiationService(parent),fParentInstantiationService(instantiationService)
{
}
InstantiationService::~InstantiationService()
{
qDeleteAll(fSingletons);
}
QSharedPointer<IInstantiationService> InstantiationService::inherited()
{
return QSharedPointer<IInstantiationService>(new InstantiationService(this));
}
bool InstantiationService::hasObject(const QString &className)
{
QPointer<QObject> obj = fSingletons.value(className);
return !obj.isNull();
}
void InstantiationService::registerObject(const QString &className, QObject *object)
{
fSingletons.insert(className,object);
}
QObject *InstantiationService::queryObject(const QString &className)
{
if(fSingletons.contains(className)){
QPointer<QObject> singleton = fSingletons.value(className);
return singleton.data();
}
//当本容器中找不到对应的实例时,如果存在父容器,则去父容器找
if(!fParentInstantiationService.isNull()){
return fParentInstantiationService->queryObject(className);
}
return nullptr;
}