⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventmanager.cpp

📁 270的linux说明
💻 CPP
字号:
/*

Copyright (c) 2008, Intel Corporation. 

All rights reserved.

 

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:


    * Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation and/or 
other materials provided with the distribution.

    * Neither the name of Intel Corporation nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.

 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.

*/#include "base_EventClient_Impl.h"#include "base_Observer_Impl.h"#include "EventManager.h"#include "base_Exception.h"#include <algorithm>//CClientImplEventManager CClientImplEventManager::s_ClientImplEventManagerInst;CClientImplEventManager s_ClientImplEventManagerInst;//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CClientImplEventManager::CClientImplEventManager(){	InitializeCriticalSection(&m_cs);}CClientImplEventManager::~CClientImplEventManager(){	DeleteCriticalSection(&m_cs);}bool CClientImplEventManager::FireEvent(Intel::Mobile::BaseAPI::Event::EventType eType, void *pSvrObj){	bool retVal = true;	EnterCriticalSection(&m_cs);	CObserversOnSameEvent* pList;	MapSvrObj2CliEvents::iterator it = m_mapSvrObj2CliEvents.find(pSvrObj);	if(it != m_mapSvrObj2CliEvents.end())	{		MapEventType2Observers* pMap = it->second;				MapEventType2Observers::iterator it2 = pMap->find(eType);		if(it2 != pMap->end())		{			pList = it2->second;			//Fire all observers			retVal = pList->NotifyAllObservers();		}		else		{			retVal = false;		}	}	else	{		retVal = false;	}	LeaveCriticalSection(&m_cs);	return retVal;}bool CClientImplEventManager::AddObserver(Intel::Mobile::BaseAPI::Event::EventType eType, ObserverImpl *pObserver, EventClientImpl *pCltImpl){	bool retVal = true;	EnterCriticalSection(&m_cs);	//void* pSvrObj = pCltImpl->GetSvrObj();	//use the client ptr	void* pSvrObj = pCltImpl;	CObserversOnSameEvent* pList;	MapEventType2Observers* pMap;	MapSvrObj2CliEvents::iterator it = m_mapSvrObj2CliEvents.find(pSvrObj);	if(it != m_mapSvrObj2CliEvents.end())	{		pMap = it->second;	}	else	{		pMap = new MapEventType2Observers;		m_mapSvrObj2CliEvents.insert(pair<void *, MapEventType2Observers *>(pSvrObj, pMap));	}	MapEventType2Observers::iterator it2 = pMap->find(eType);	if(it2 != pMap->end())	{		pList = it2->second;	}	else	{		pList = new CObserversOnSameEvent(pCltImpl, eType);		pMap->insert(pair<Event::EventType, CObserversOnSameEvent*>(eType, pList));	}	cout << "CClientImplEventManager::AddObserver" << " pSvrObj " << pCltImpl->GetSvrObj() << " pCliObj " << pSvrObj <<" eType " << eType << endl;	pList->AddObserver(pObserver);	LeaveCriticalSection(&m_cs);	return retVal;}bool CClientImplEventManager::RemoveObserver(Intel::Mobile::BaseAPI::Event::EventType eType, ObserverImpl *pObserver, EventClientImpl *pCltImpl, BOOL &bRemoveEvent){		bool retVal = true;	EnterCriticalSection(&m_cs);	//void* pSvrObj = pCltImpl->GetSvrObj();	void* pSvrObj = pCltImpl;	CObserversOnSameEvent* pList;	MapEventType2Observers* pMap;	MapSvrObj2CliEvents::iterator it = m_mapSvrObj2CliEvents.find(pSvrObj);	if(it != m_mapSvrObj2CliEvents.end())	{		pMap = it->second;				MapEventType2Observers::iterator it2 = pMap->find(eType);		if(it2 != pMap->end())		{			pList = it2->second;		}		else		{			retVal = false;		}		pList->RemoveObserver(pObserver);		if(pList->GetObserverCount() == 0)		{			delete pList;			pMap->erase(it2);			bRemoveEvent = true;			if(pMap->empty())			{				delete pMap;				m_mapSvrObj2CliEvents.erase(it);			}		}	}	else	{		retVal = false;	}	LeaveCriticalSection(&m_cs);}bool CClientImplEventManager::HasAddedObserver(Event::EventType eType, EventClientImpl *pCltImpl){	bool bRtn = false;	EnterCriticalSection(&m_cs);	try	{		//void* pSvrObj = pCltImpl->GetSvrObj();		void* pSvrObj = pCltImpl;		MapSvrObj2CliEvents::iterator it = m_mapSvrObj2CliEvents.find(pSvrObj);		if(it != m_mapSvrObj2CliEvents.end())		{			MapEventType2Observers* pMap = it->second;					MapEventType2Observers::iterator it2 = pMap->find(eType);			if(it2 != pMap->end())			{				CObserversOnSameEvent* pList = it2->second;				bRtn = true;			}		}		}	catch(...)	{		LeaveCriticalSection(&m_cs);		IntelMobileException ex( IntelMobileText("Operation Error"), IntelMobileText("CClientImplEventManager"), IntelMobileText("HasAddedObserver") );        throw( ex ); 	}	LeaveCriticalSection(&m_cs);	return bRtn;}bool CClientImplEventManager::UpdateEvent(Event::EventType eType, Event &pTheEvent, EventClientImpl *pCltImpl){	bool bRtn = true;	EnterCriticalSection(&m_cs);	try	{		//void* pSvrObj = pCltImpl->GetSvrObj();		void* pSvrObj = pCltImpl;		MapSvrObj2CliEvents::iterator it = m_mapSvrObj2CliEvents.find(pSvrObj);		if(it != m_mapSvrObj2CliEvents.end())		{			MapEventType2Observers* pMap = it->second;					MapEventType2Observers::iterator it2 = pMap->find(eType);			if(it2 != pMap->end())			{				CObserversOnSameEvent* pList = it2->second;				pList->SetTheEvent(pTheEvent);			}			else			{				bRtn = false;			}		}		else		{			bRtn = false;		}	}	catch(...)	{		LeaveCriticalSection(&m_cs);		IntelMobileException ex( IntelMobileText("Operation Error"), IntelMobileText("CClientImplEventManager"), IntelMobileText("HasAddedObserver") );        throw( ex ); 	}	LeaveCriticalSection(&m_cs);	return bRtn;}unsigned int CClientImplEventManager::GetObserverCount(Intel::Mobile::BaseAPI::Event::EventType eType, EventClientImpl *pCltImpl){	unsigned int iRtn = 0;	EnterCriticalSection(&m_cs);	try	{		//void* pSvrObj = pCltImpl->GetSvrObj();		void* pSvrObj = pCltImpl;		MapSvrObj2CliEvents::iterator it = m_mapSvrObj2CliEvents.find(pSvrObj);		if(it != m_mapSvrObj2CliEvents.end())		{			MapEventType2Observers* pMap = it->second;					MapEventType2Observers::iterator it2 = pMap->find(eType);			if(it2 != pMap->end())			{				CObserversOnSameEvent* pList = it2->second;				iRtn = pList->GetObserverCount();			}		}	}	catch(...)	{		LeaveCriticalSection(&m_cs);		IntelMobileException ex( IntelMobileText("Operation Error"), IntelMobileText("CClientImplEventManager"), IntelMobileText("HasAddedObserver") );        throw( ex ); 	}	LeaveCriticalSection(&m_cs);	return iRtn;}ObserverImpl* CClientImplEventManager::GetObserver(Intel::Mobile::BaseAPI::Event::EventType eType, unsigned int nIndex, EventClientImpl *pCltImpl){	ObserverImpl* pRtn = NULL;	EnterCriticalSection(&m_cs);	try	{		//void* pSvrObj = pCltImpl->GetSvrObj();		void* pSvrObj = pCltImpl;		MapSvrObj2CliEvents::iterator it = m_mapSvrObj2CliEvents.find(pSvrObj);		if(it != m_mapSvrObj2CliEvents.end())		{			MapEventType2Observers* pMap = it->second;					MapEventType2Observers::iterator it2 = pMap->find(eType);			if(it2 != pMap->end())			{				CObserversOnSameEvent* pList = it2->second;				pRtn = pList->GetObserver(nIndex);			}		}	}	catch(...)	{		LeaveCriticalSection(&m_cs);		IntelMobileException ex( IntelMobileText("Operation Error"), IntelMobileText("CClientImplEventManager"), IntelMobileText("HasAddedObserver") );        throw( ex ); 	}	LeaveCriticalSection(&m_cs);	return pRtn;}bool CClientImplEventManager::RemoveObservers( Intel::Mobile::BaseAPI::Event::EventType eType, EventClientImpl *pCltImpl){	bool bRtn = true;	EnterCriticalSection(&m_cs);	try	{		//void* pSvrObj = pCltImpl->GetSvrObj();		void* pSvrObj = pCltImpl;		MapSvrObj2CliEvents::iterator it = m_mapSvrObj2CliEvents.find(pSvrObj);		if(it != m_mapSvrObj2CliEvents.end())		{			MapEventType2Observers* pMap = it->second;					MapEventType2Observers::iterator it2 = pMap->find(eType);			if(it2 != pMap->end())			{				CObserversOnSameEvent* pList = it2->second;				pList->RemoveObservers();				if(pList->GetObserverCount() == 0)				{					delete pList;					pMap->erase(it2);					//bRemoveEvent = true;					if(pMap->empty())					{						delete pMap;						m_mapSvrObj2CliEvents.erase(it);					}				}			}			else			{				bRtn = false;			}		}		else		{			bRtn = false;		}	}	catch(...)	{		LeaveCriticalSection(&m_cs);		IntelMobileException ex( IntelMobileText("Operation Error"), IntelMobileText("CClientImplEventManager"), IntelMobileText("HasAddedObserver") );        throw( ex ); 	}	LeaveCriticalSection(&m_cs);	return bRtn;}CClientImplEventManager & CClientImplEventManager::GetClientImplEventManagerInst(){	return s_ClientImplEventManagerInst;}//---------------------------------------------------------------------------------// CObserversOnSameEvent////---------------------------------------------------------------------------------CObserversOnSameEvent::CObserversOnSameEvent(EventClientImpl *pCltImpl, Intel::Mobile::BaseAPI::Event::EventType eType){	InitializeCriticalSection(&m_csProtectList);	//Create new Event	m_pTheEvent = pCltImpl->CreateClassEvent(eType);}CObserversOnSameEvent::~CObserversOnSameEvent(){	DeleteCriticalSection(&m_csProtectList);}void CObserversOnSameEvent::AddObserver(ObserverImpl *pObserver){	EnterCriticalSection(&m_csProtectList);	m_lstObservers.push_back(pObserver);	LeaveCriticalSection(&m_csProtectList);}void CObserversOnSameEvent::RemoveObserver(ObserverImpl *pObserver, 										   BOOL bOnce /*= TRUE*/){	EnterCriticalSection(&m_csProtectList);	if(bOnce)	{		//for(ListObservers::iterator itr = m_lstObservers.begin(); itr != m_lstObservers.end(); itr++)		ListObservers::iterator itr = find(m_lstObservers.begin(), m_lstObservers.end(), pObserver);		if(itr != m_lstObservers.end())			m_lstObservers.erase(itr);	}	else		m_lstObservers.remove(pObserver);	LeaveCriticalSection(&m_csProtectList);}void CObserversOnSameEvent::RemoveObservers(){	EnterCriticalSection(&m_csProtectList);	m_lstObservers.clear();	LeaveCriticalSection(&m_csProtectList);}int CObserversOnSameEvent::GetObserverCount(){	int nCnt = 0;	EnterCriticalSection(&m_csProtectList);	nCnt = m_lstObservers.size();	LeaveCriticalSection(&m_csProtectList);	return nCnt;}const ObserverImpl* CObserversOnSameEvent::GetObserver(unsigned int nIndex){	unsigned int iCount;	EnterCriticalSection(&m_csProtectList);	ListObservers::iterator it;	for(it = m_lstObservers.begin(),iCount=0; it != m_lstObservers.end(), iCount<nIndex; it++, iCount++) ;		if (it!=m_lstObservers.end())	{		LeaveCriticalSection(&m_csProtectList);		return *it;	}	LeaveCriticalSection(&m_csProtectList);	return NULL;	}bool   CObserversOnSameEvent::SetTheEvent(Event & event){	try	{		m_pTheEvent->SetObjectType(event.GetObjectType());		m_pTheEvent->SetObjectKey(event.GetObjectKey());		m_pTheEvent->SetClassType(event.GetClassType());		m_pTheEvent->SetParentKey(event.GetParentKey());	}	catch (...)	{		return false;	}	return true;}bool CObserversOnSameEvent::NotifyAllObservers(){	bool bRtn = true;	EnterCriticalSection(&m_csProtectList);			try	{		time_t t = ::time(NULL);		m_pTheEvent->SetTimestamp( t );		ListObservers::iterator it;		for(it = m_lstObservers.begin(); it != m_lstObservers.end(); it++)		{				(*it)->Notify(*m_pTheEvent);					}	}	catch(...)	{		LeaveCriticalSection(&m_csProtectList);		bRtn = false;  		return bRtn;		//IntelMobileException ex( IntelMobileText("Operation Error"), IntelMobileText("CObserversOnSameEvent"), IntelMobileText("NotifyAllObservers") );        //throw( ex ); 	}	LeaveCriticalSection(&m_csProtectList);	return bRtn;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -