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

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