添加一个单例的模板

Signed-off-by: dmy@lab <dmy@lab.lab>
This commit is contained in:
dmy@lab 2015-01-15 22:06:19 +08:00
parent 230aef6995
commit 51eed6e99e
5 changed files with 46 additions and 5 deletions

View File

@ -16,13 +16,13 @@ LoadMapping::~LoadMapping()
}
bool LoadMapping::load(const QString &loadDir,const QString &matchdDir)
bool LoadMapping::load(const QString &loadDir,const QString &matchdDir,const QString &exceptionFile)
{
if(LoadMapping::ht->keys().length()>0)
{
return true;//只执行一次
}
if(!QFileInfo::exists(loadDir)|!QFileInfo::exists(matchdDir))
if(!QFileInfo::exists(loadDir)|!QFileInfo::exists(matchdDir)|!QFileInfo::exists(exceptionFile))
{
return false;
}

View File

@ -20,7 +20,7 @@ class LoadMapping
public:
LoadMapping();
~LoadMapping();
bool load(const QString& loadDir, const QString &matchdDir);
bool load(const QString& loadDir, const QString &matchdDir, const QString &exceptionFile);
bool readLoads(const QString& dir);
bool readMatch(const QString& dir);
private:

View File

@ -8,7 +8,7 @@ int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
LoadMapping lm;
lm.load("D:/Project/佛山项目/数据/搭网架参数文件/泰安/泰安负荷","D:/Project/佛山项目/数据/匹配的数据");
lm.load("D:/Project/佛山项目/数据/搭网架参数文件/泰安/泰安负荷","D:/Project/佛山项目/数据/匹配的数据","");
// ReadWrite aa;
// RegexExtract re;

View File

@ -0,0 +1,40 @@
#ifndef SINGLETONBASE_H
#define SINGLETONBASE_H
//做一个单例
#include <QHash>
template<typename KeyType,typename ValueType>
class SingletonBase
{
public:
SingletonBase();
~SingletonBase();
void add(const KeyType& key,const ValueType& val)
{
this->ht[key]=val;
}
bool contains(const KeyType& key)
{
return this->ht->contains(key);
}
ValueType get(const KeyType& key)
{
return this->ht[key];
}
protected:
class CG // 它的唯一工作就是在析构函数中删除CSingleton的实例
{
public:
~CG()
{
if (SingletonBase::ht)
delete SingletonBase::ht;
}
};
static CG Garbo; // 定义一个静态成员,在程序结束时,系统会调用它的析构函数
static QHash<KeyType,ValueType> *ht; *ht;
};
#endif // SINGLETONBASE_H

View File

@ -66,7 +66,8 @@ HEADERS += \
task.h \
loadmapping.h \
recursedir.h \
loadinfo.h
loadinfo.h \
singletonbase.h
#release{
DEFINES += QT_NO_DEBUG_OUTPUT