parent
29b219fbe9
commit
9faa1cce49
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue