修改以后得到一个可以用的模板类单例。

Signed-off-by: dmy@lab <dmy@lab.lab>
This commit is contained in:
dmy@lab 2015-01-16 15:55:07 +08:00
parent 29b219fbe9
commit 9faa1cce49
1 changed files with 16 additions and 7 deletions

View File

@ -4,8 +4,7 @@
//做一个单例
#include <QHash>
template<typename KeyType,typename ValueType>
template<typename KeyType,typename ValueType,typename ChildType>
class SingletonBase
{
public:
@ -13,20 +12,26 @@ public:
// ~SingletonBase();
void add(const KeyType& key,const ValueType& val)
{
this->ht[key]=val;
QHash<KeyType,ValueType> *t=SingletonBase<KeyType,ValueType,ChildType>::ht;
(*t)[key]=val;
}
bool contains(const KeyType& key)
{
return this->ht->contains(key);
return SingletonBase<KeyType,ValueType,ChildType>::ht->contains(key);
}
ValueType get(const KeyType& key)
{
return this->ht[key];
QHash<KeyType,ValueType> *t=SingletonBase<KeyType,ValueType,ChildType>::ht;
return (*t)[key];
}
void initInstance()
{
if()
if(SingletonBase<KeyType,ValueType,ChildType>::ht)
{
delete SingletonBase<KeyType,ValueType,ChildType>::ht;
SingletonBase<KeyType,ValueType,ChildType>::ht==NULL;
}
SingletonBase<KeyType,ValueType,ChildType>::ht=new QHash<KeyType,ValueType>;
}
protected:
@ -43,4 +48,8 @@ protected:
static QHash<KeyType,ValueType> *ht;
};
template<typename KT,typename VT,typename CT>
QHash<KT,VT> *SingletonBase<KT,VT,CT>::ht=NULL;
#endif // SINGLETONBASE_H