#pragma once #include "DiosLock.h" #include //#include using namespace std; template class MsgMap { DiosLock m_Lock; HANDLE m_NotifyInMap; map m_DataMap; public: MsgMap(void) { m_NotifyInMap = CreateEvent(0, 1, 0, 0); }; ~MsgMap(void) { Clear(); CloseHandle(m_NotifyInMap); }; bool Clear() { ResetEvent(m_NotifyInMap); m_Lock.Thread_Lock(); m_DataMap.clear(); m_Lock.Thread_UnLock(); return true; }; bool Find(T rKey, Q &rVal) { bool ret = false; m_Lock.Thread_Lock(); //DebugPrint("before deQue size:%d\n",m_DataQueue.size()); if (m_DataMap.size() > 0) { map::iterator result1 = m_DataMap.find(rKey);// find(m_DataMap.begin(), m_DataMap.end(), rKey); if (result1 != m_DataMap.end()) { rVal = m_DataMap[rKey]; ret = true; } } //DebugPrint("after deQue size:%d\n",m_DataQueue.size()); m_Lock.Thread_UnLock(); return ret; }; bool FindAndClear(T &rKey) { bool ret = false; map::iterator result1 = m_DataMap.find(rKey);// find(m_DataMap.begin(), m_DataMap.end(), rKey); if (result1 != m_DataMap.end()) { m_DataMap.erase(result1); ret = true; } if (m_DataMap.size() == 0) { ResetEvent(m_NotifyInMap); } return ret; }; bool DeMap(T &rKey, Q &rVal) { bool ret = false; m_Lock.Thread_Lock(); //DebugPrint("before deQue size:%d\n",m_DataQueue.size()); if (m_DataMap.size() > 0) { map::iterator result1 = m_DataMap.find(rKey);//find(m_DataMap.begin(), m_DataMap.end(), rKey); if (result1 != m_DataMap.end()) { rVal = m_DataMap[rKey]; m_DataMap.erase(result1); ret = true; } } //DebugPrint("after deQue size:%d\n",m_DataQueue.size()); if (m_DataMap.size() == 0) { ResetEvent(m_NotifyInMap); } m_Lock.Thread_UnLock(); return ret; }; bool InMap(T &rKey,Q &rVal) { m_Lock.Thread_Lock(); m_DataMap[rKey] = rVal; m_Lock.Thread_UnLock(); SetEvent(m_NotifyInMap); return true; }; DWORD WaitForInMap(DWORD timeout) { DWORD ret = WAIT_TIMEOUT; m_Lock.Thread_Lock(); if (m_DataMap.size() > 0) { ret = WAIT_OBJECT_0; } m_Lock.Thread_UnLock(); if(ret == WAIT_TIMEOUT) { return WaitForSingleObject(m_NotifyInMap, timeout); } return ret; }; };