PracticeDev/study_clang/Mimic/test_judge/include/CMessageQueue.h

246 lines
6.6 KiB
C++

/*************************************************************************
> File Name : ../include/CMessageQueue.h
> Author : FengChao
> EMail : smile.fengchao@gmail.com
> Created Time : Thu 16 Jul 2020 11:35:11 AM CST
************************************************************************/
#ifndef CMESSAGEQUEUE_H
#define CMESSAGEQUEUE_H
#include "CTypedef.h"
#include "CJudgeUtils.h"
template <typename T>
class CMessageQueue : public CSingleton<CMessageQueue<T> >
{
public:
CMessageQueue();
virtual ~CMessageQueue();
public:
void push(T stData,Message_QueueType_E MessageType = m_MessageTypeE);
void pop(Message_QueueType_E MessageType = m_MessageTypeE);
T& front(Message_QueueType_E MessageType = m_MessageTypeE);
T& back(Message_QueueType_E MessageType = m_MessageTypeE);
int empty(Message_QueueType_E MessageType = m_MessageTypeE);
uint size(Message_QueueType_E MessageType = m_MessageTypeE);
void remove(T stData, Message_QueueType_E MessageType = m_MessageTypeE);
private:
private:
static const Message_QueueType_E m_MessageTypeE = MQT_CONFIG_E;
pthread_mutex_t m_QueueMutex;
std::list<T> m_queue;
pthread_mutex_t ss_QueueMutex;
std::list<T> q_SysState;
pthread_mutex_t ps_QueueMutex;
std::list<T> q_ProcessState;
pthread_mutex_t fs_QueueMutex;
std::list<T> q_FileState;
pthread_mutex_t fn_QueueMutex;
std::list<T> q_FileNum;
pthread_mutex_t sl_QueueMutex;
std::list<T> q_SysLog;
};
template <typename T>
CMessageQueue<T>::CMessageQueue()
{
pthread_mutex_init(&m_QueueMutex, NULL);
pthread_mutex_init(&ss_QueueMutex, NULL);
pthread_mutex_init(&ps_QueueMutex, NULL);
pthread_mutex_init(&fs_QueueMutex, NULL);
pthread_mutex_init(&fn_QueueMutex, NULL);
pthread_mutex_init(&sl_QueueMutex, NULL);
}
template <typename T>
CMessageQueue<T>::~CMessageQueue()
{
pthread_mutex_destroy(&m_QueueMutex);
pthread_mutex_destroy(&ss_QueueMutex);
pthread_mutex_destroy(&ps_QueueMutex);
pthread_mutex_destroy(&fs_QueueMutex);
pthread_mutex_destroy(&fn_QueueMutex);
pthread_mutex_destroy(&sl_QueueMutex);
}
template <typename T>
void CMessageQueue<T>::push(T stData,Message_QueueType_E MessageType)
{
switch(MessageType)
{
#define _MQT_PUSH(type, q, lock) \
case MQT_##type##_E: \
{ \
CMutexLockGuard MutexLock(lock##_QueueMutex); \
q.push_back(stData); \
break;\
}
_MQT_PUSH(CONFIG, m_queue, m);
_MQT_PUSH(SYS_STATE, q_SysState, ss);
_MQT_PUSH(PROCESS_STATE, q_ProcessState, ps);
_MQT_PUSH(FILE_STATE, q_FileState, fs);
_MQT_PUSH(FILE_NUM, q_FileNum, fn);
_MQT_PUSH(SYS_LOG, q_SysLog, sl);
#undef _MQT_PUSH
default:
break;
}
}
template <typename T>
void CMessageQueue<T>::pop(Message_QueueType_E MessageType)
{
switch(MessageType)
{
#define _MQT_POP(type, q, lock ) \
case MQT_##type##_E: \
{ \
CMutexLockGuard MutexLock(lock##_QueueMutex); \
q.pop_front(); \
break; \
}
_MQT_POP(CONFIG, m_queue, m);
_MQT_POP(SYS_STATE, q_SysState, ss);
_MQT_POP(PROCESS_STATE, q_ProcessState, ps);
_MQT_POP(FILE_STATE, q_FileState, fs);
_MQT_POP(FILE_NUM, q_FileNum, fn);
_MQT_POP(SYS_LOG, q_SysLog, sl);
#undef _MQT_POP
default:
break;
}
}
template <typename T>
T& CMessageQueue<T>::front(Message_QueueType_E MessageType)
{
switch(MessageType)
{
#define _MQT_FRONT(type, q, lock) \
case MQT_##type##_E: \
{ \
CMutexLockGuard MutexLock(lock##_QueueMutex); \
return q.front(); \
}
_MQT_FRONT(CONFIG, m_queue, m);
_MQT_FRONT(SYS_STATE, q_SysState, ss);
_MQT_FRONT(PROCESS_STATE, q_ProcessState, ps);
_MQT_FRONT(FILE_STATE, q_FileState, fs);
_MQT_FRONT(FILE_NUM, q_FileNum, fn);
_MQT_FRONT(SYS_LOG, q_SysLog, sl);
#undef _MQT_FRONT
default:
return m_queue.front();
}
}
template <typename T>
T& CMessageQueue<T>::back(Message_QueueType_E MessageType)
{
switch(MessageType)
{
#define _MQT_BACK(type, q, lock) \
case MQT_##type##_E: \
{ \
CMutexLockGuard MutexLock(lock##_QueueMutex); \
return q.back(); \
}
_MQT_BACK(CONFIG, m_queue, m);
_MQT_BACK(SYS_STATE, q_SysState, ss);
_MQT_BACK(PROCESS_STATE, q_ProcessState, ps);
_MQT_BACK(FILE_STATE, q_FileState, fs);
_MQT_BACK(FILE_NUM, q_FileNum, fn);
_MQT_BACK(SYS_LOG, q_SysLog, sl);
#undef _MQT_BACK
default:
return m_queue.back();
}
}
template <typename T>
int CMessageQueue<T>::empty(Message_QueueType_E MessageType)
{
switch(MessageType)
{
#define _MQT_EMPTY(type, q, lock) \
case MQT_##type##_E: \
{ \
CMutexLockGuard MutexLock(lock##_QueueMutex); \
return q.empty(); \
}
_MQT_EMPTY(CONFIG, m_queue, m);
_MQT_EMPTY(SYS_STATE, q_SysState, ss);
_MQT_EMPTY(PROCESS_STATE, q_ProcessState, ps);
_MQT_EMPTY(FILE_STATE, q_FileState, fs);
_MQT_EMPTY(FILE_NUM, q_FileNum, fn);
_MQT_EMPTY(SYS_LOG, q_SysLog, sl);
#undef _MQT_EMPTY
default:
return 0;
}
return 0;
}
template <typename T>
uint CMessageQueue<T>::size(Message_QueueType_E MessageType)
{
switch(MessageType)
{
#define _MQT_SIZE(type, q, lock) \
case MQT_##type##_E: \
{ \
CMutexLockGuard MutexLock(lock##_QueueMutex); \
return q.size(); \
}
_MQT_SIZE(CONFIG, m_queue, m);
_MQT_SIZE(SYS_STATE, q_SysState, ss);
_MQT_SIZE(PROCESS_STATE, q_ProcessState, ps);
_MQT_SIZE(FILE_STATE, q_FileState, fs);
_MQT_SIZE(FILE_NUM, q_FileNum, fn);
_MQT_SIZE(SYS_LOG, q_SysLog, sl);
#undef _MQT_SIZE
default:
return 0;
}
}
template <typename T>
void CMessageQueue<T>::remove(T stData, Message_QueueType_E MessageType)
{
switch(MessageType)
{
#define _MQT_REMOVE(type, q, lock) \
case MQT_##type##_E: \
{ \
CMutexLockGuard MutexLock(lock##_QueueMutex); \
return q.remove(stData); \
}
_MQT_REMOVE(CONFIG, m_queue, m);
_MQT_REMOVE(SYS_STATE, q_SysState, ss);
_MQT_REMOVE(PROCESS_STATE, q_ProcessState, ps);
_MQT_REMOVE(FILE_STATE, q_FileState, fs);
_MQT_REMOVE(FILE_NUM, q_FileNum, fn);
_MQT_REMOVE(SYS_LOG, q_SysLog, sl);
#undef _MQT_REMOVE
default:
break;
}
}
#endif