You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.2 KiB
C

5 years ago
/******************************************************************************
Copyright(C):2015~2018 hzleaper
FileName:QZkMutexList.h
Author:zhikun wu
Email:zk.wu@hzleaper.com
Tools:vs2010 pc on company
Created:2015/04/15
History:15:4:2015 16:44
*******************************************************************************/
#ifndef QZKMUTEXLIST_H
#define QZKMUTEXLIST_H
#include <QtCore/QList>
#include <QtCore/QMutexLocker>
template<typename T>
class QZkMutexList : public QList<T>
{
public:
typedef void(*cbfunc_list_param)(T& value, void* pData);
QZkMutexList(bool bUseMutex = true)
: m_mutex(QMutex::Recursive) {
m_bUseMutex = bUseMutex;
}
~QZkMutexList() {}
void clear() {
QList::clear();
}
void clear(cbfunc_list_param pfb, void* pData = NULL) {
QMutexLocker locker(&m_mutex);
if( NULL != pfb ) {
for(Iterator it = begin(); it != end(); ++it) {
pfb(*it, pData);
}
}
QList::clear();
}
void append(const T &t) {
QMutexLocker locker(&m_mutex);
QList::append(t);
}
void appendOnce(const T & value) {
if( !QList::contains(value) ) {
QList::append(value);
}
}
T takeFirst(const T & defaultV = NULL) {
QMutexLocker locker(&m_mutex);
if( QList::isEmpty() ) {
return defaultV;
} else {
return QList::takeFirst();
}
}
T takeLast(const T & defaultV = NULL) {
QMutexLocker locker(&m_mutex);
if( QList::isEmpty() ) {
return defaultV;
} else {
return QList::takeLast();
}
}
int removeAll(const T &t) {
QMutexLocker locker(&m_mutex);
return QList::removeAll(t);
}
bool removeOne(const T &t) {
QMutexLocker locker(&m_mutex);
return QList::removeOne(t);
}
void iterateCall(cbfunc_list_param pfb, void* pData) {
if( NULL == pfb ) {
return;
}
QMutexLocker locker(&m_mutex);
for(Iterator it = begin(); it != end(); ++it) {
pfb(*it, pData);
}
}
private:
QMutex m_mutex;
bool m_bUseMutex;
};
#endif // QZKMUTEXLIST_H