#pragma once #include "CcosLock.h" #include "LinuxEvent.h" #include //#include using namespace std; template class MsgMap { CcosLock m_Lock; std::shared_ptr m_NotifyInMap; map m_DataMap; public: MsgMap(void) { m_NotifyInMap = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET,false); }; ~MsgMap(void) { Clear(); }; bool Clear() { m_NotifyInMap->ResetEvent(); 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) { typename 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; typename 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) { m_NotifyInMap->ResetEvent(); } 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) { typename 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) { m_NotifyInMap->ResetEvent(); } m_Lock.Thread_UnLock(); return ret; }; bool InMap(T &rKey,Q &rVal) { m_Lock.Thread_Lock(); m_DataMap[rKey] = rVal; m_Lock.Thread_UnLock(); m_NotifyInMap->SetEvent(); 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 m_NotifyInMap->Wait(timeout); } return ret; }; };