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

📄 base_classobject_impl.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.

*///==============================================================================// base_ClassObject_Impl.cpp//       Implementation of base class  ClassObjectImpl//==============================================================================#include "base_ClassObject_Impl.h"             #include "base_Exception.h"#include "IntelMobile/Ipc_Support.h"//==============================================================================// C_Tor()//==============================================================================ClassObjectImpl::ClassObjectImpl(  Intel::Mobile::BaseAPI::Object *pOwner, IntelMobileChar* type, IntelMobileChar* version)              : EventClientImpl       ( pOwner, IntelMobileText("ClassObject") ){	EnterCriticalSection(&m_csIpc);	if ( type )    {        m_Type = ::strcpy( new IntelMobileChar[::strlen(type)+1], type );     }	if ( version )    {        m_Version = ::strcpy( new IntelMobileChar[::strlen(version)+1], version );     }	try    {		//m_pSvrClassObj = &GetClassObject(type) ;			//IPC support		m_sName = type;		m_nDataType = ID_TYPE_CLASSOBJECT;	    vector<string> vectParam, vectRtn;	    vectParam.push_back(CStringHelper::Param2Str(m_sName));	    //printf("ClassObjectImpl C_Tor(): want to send %s\n",vectParam[0].c_str());		HRESULT hr = GetIpcClient()->IpcCall(m_nDataType, /*GetSvrObj()*/NULL, vectParam, vectRtn); //now GetSvrObj() should be null	    CStringHelper::Str2Param(vectRtn[0], (HANDLE &)m_pSvrClassObj);		/*		//if((int) m_pSvrClassObj & 0xf0000000 == 0x80000000)		{			printf("ClassObjectImpl: get %x\n", m_pSvrClassObj);			//::abort();		}		*///	    printf("ClassObjectImpl C_Tor(): getted  %x\n",m_pSvrClassObj);    }    catch(...)     {		IntelMobileString strClass = m_Type;		strClass += IntelMobileText("Class");        IntelMobileException ex( IntelMobileText("Operation Error"), strClass.c_str(), IntelMobileText("Constructor") );	 	LeaveCriticalSection(&m_csIpc);        throw( ex );     }	LeaveCriticalSection(&m_csIpc);}    ClassObjectImpl::~ClassObjectImpl(){	EnterCriticalSection(&m_csIpc);	RELEASE(m_pSvrClassObj);	LeaveCriticalSection(&m_csIpc);	//printf("There is a bug when relase svrclassobj in ClassObjectImpl, please notify  to fix it\n");}//==============================================================================// operator==()//==============================================================================bool ClassObjectImpl::operator==( const ObjectImpl& rhs ) const{	IntelMobileChar *pwcThis = GetType();	IntelMobileChar *pwcRhs = reinterpret_cast<const ClassObjectImpl&>(rhs).GetType();    bool bRtn = ( !strcmp(pwcThis ,  pwcRhs));	delete []pwcThis;	delete []pwcRhs;	return bRtn;} // operator==()//==============================================================================// GetType()//==============================================================================IntelMobileChar* ClassObjectImpl::GetType() const{     IntelMobileChar *pszType = ::strcpy( new IntelMobileChar[::strlen(m_Type)+1], m_Type );     return pszType;} // GetType()//==============================================================================// GetVersion()//==============================================================================IntelMobileChar* ClassObjectImpl::GetVersion() const{     IntelMobileChar* pszVersion = NULL;    pszVersion = ::strcpy( new IntelMobileChar[::strlen(m_Version)+1], m_Version );     return pszVersion;} // GetVersion()//==============================================================================HRESULT ClassObjectImpl::DoSubscribe(/*HANDLE hEvent,*/ Event::EventType eType, void *pCliObj){		EnterCriticalSection(&m_csIpc);	//return m_pSvrClassObj->Subscribe(/*(EventServer*)*/hEvent, eType); //	//IPC support	//HRESULT S_OK;	vector<string> vectParam, vectRtn;	vectParam.push_back(IntelMobileText("Subscribe"));	//vectParam.push_back(CStringHelper::Param2Str(hEvent));	vectParam.push_back(CStringHelper::Param2Str(eType));	vectParam.push_back(CStringHelper::Param2Str(pCliObj));	HRESULT hr = GetIpcClient()->IpcCall(m_nDataType, GetSvrObj(), vectParam, vectRtn);	if (FAILED(hr))	{		IntelMobileException ex( IntelMobileText("Failure DoSubscribe"),                         IntelMobileText("ClassObjectImpl"),                         IntelMobileText("DoSubscribe"), hr );		LeaveCriticalSection(&m_csIpc);		throw( ex ); 	}	else	{		//CStringHelper::Str2Param(vectRtn[0], (HANDLE &)subClass);		LeaveCriticalSection(&m_csIpc);		return S_OK;	}	LeaveCriticalSection(&m_csIpc);}HRESULT ClassObjectImpl::UnDoSubscribe(/*HANDLE hEvent,*/ Event::EventType eType, void *pCliObj){		EnterCriticalSection(&m_csIpc);	vector<string> vectParam, vectRtn;	vectParam.push_back(IntelMobileText("Unsubscribe"));	//vectParam.push_back(CStringHelper::Param2Str(hEvent));	vectParam.push_back(CStringHelper::Param2Str(eType));	vectParam.push_back(CStringHelper::Param2Str(pCliObj));	HRESULT hr = GetIpcClient()->IpcCall(m_nDataType, GetSvrObj(), vectParam, vectRtn);	if (FAILED(hr))	{		IntelMobileException ex( IntelMobileText("Failure UnDoSubscribe"),                         IntelMobileText("ClassObjectImpl"),                         IntelMobileText("UnDoSubscribe"), hr );		LeaveCriticalSection(&m_csIpc);		throw( ex ); 	}	else	{		//CStringHelper::Str2Param(vectRtn[0], (HANDLE &)subClass);		LeaveCriticalSection(&m_csIpc);		return S_OK;	}	LeaveCriticalSection(&m_csIpc);}Event *ClassObjectImpl::CreateClassEvent(Event::EventType eType){	IntelMobileChar* pszKey = NULL;	Event *pTheEvent = NULL;	try	{		pszKey = GetType();		void *pReciever = (void *)(new ClassObjectImplPtr( this));		pTheEvent = new Event( eType, pReciever, IntelMobileText("ClassObject"), pszKey, pszKey );	}	catch(...)	{			delete[] pszKey;		delete pTheEvent;		IntelMobileException ex( IntelMobileText("Operation Error"), IntelMobileText("ClassObjectImpl"), IntelMobileText("CreateClassEvent") );        throw( ex ); 	}	delete[] pszKey;	return pTheEvent;}void* ClassObjectImpl::GetSvrObj(){	return m_pSvrClassObj;}//==============================================================================// GetInstances() : Get the collection interface and return it in a wrapper.//==============================================================================InstanceCollectionImplPtr ClassObjectImpl::GetInstances( const IntelMobileChar* sType ){	EnterCriticalSection(&m_csIpc);    InstanceCollectionImplPtr              spCollectionWrapper = NULL;    	if ( m_pSvrClassObj != NULL )	{				vector<string> vectParam, vectRtn;        	vectParam.push_back(IntelMobileText("GetInstances"));		if(!strcmp(sType, ""))			vectParam.push_back(CStringHelper::Param2Str(IntelMobileText("")));		else			vectParam.push_back(CStringHelper::Param2Str(sType));		HRESULT hr = GetIpcClient()->IpcCall(m_nDataType, GetSvrObj(), vectParam, vectRtn);		if (FAILED(hr))		{				IntelMobileException ex( IntelMobileText("Error in GetInstances of Class"), IntelMobileText("ClassObject"), IntelMobileText("GetInstances"), hr );	 			LeaveCriticalSection(&m_csIpc);				throw( ex ); 		}		else		{			void *pSvrCol = NULL;			CStringHelper::Str2Param(vectRtn[0], (int &)pSvrCol);    		spCollectionWrapper = new InstanceCollectionImpl(pSvrCol);		}	}	LeaveCriticalSection(&m_csIpc);    return spCollectionWrapper;} // GetInstances()InstanceObjectImplPtr ClassObjectImpl::GetInstance( const IntelMobileChar* sKey ) { 	EnterCriticalSection(&m_csIpc);    InstanceObjectImplPtr spInstanceWrapper = NULL;    	if ( m_pSvrClassObj != NULL )	{			//BSTR pszKey = ::SysAllocString( sKey );		IntelMobileString pszKey = sKey;		//ServerInstanceObject* pSvrInstObj = m_pSvrClassObj->GetInstance(pszKey);		//::SysFreeString( pszKey );				//IPC support		void* pSvrInstObj;		vector<string> vectParam, vectRtn;	    vectParam.push_back(IntelMobileText("GetInstance"));	    vectParam.push_back(pszKey);		HRESULT hr = GetIpcClient()->IpcCall(m_nDataType, GetSvrObj(), vectParam, vectRtn);		if (FAILED(hr))		{			IntelMobileException ex( IntelMobileText("Failure GetInstances"),                             IntelMobileText("ClassObjectImpl"),                             IntelMobileText("GetInstance"), hr );	 		LeaveCriticalSection(&m_csIpc);			throw( ex ); 		}		else		{						CStringHelper::Str2Param(vectRtn[0], (HANDLE &)pSvrInstObj); 			spInstanceWrapper = new InstanceObjectImpl( pSvrInstObj);		}	}	LeaveCriticalSection(&m_csIpc);    return spInstanceWrapper;} // GetInstance() //==============================================================================// IsEventAvailable() //==============================================================================bool ClassObjectImpl::IsEventAvailable( Event::EventType eType ){	EnterCriticalSection(&m_csIpc);    bool bAvailable = false;    if ( m_pSvrClassObj )    {        //bAvailable = m_pSvrClassObj->IsEventAvailable( eType) ;		//IPC support		vector<string> vectParam, vectRtn;		IntelMobileChar buf[256];	    vectParam.push_back(IntelMobileText("IsEventAvailable"));		vectParam.push_back(CStringHelper::Param2Str(eType));		HRESULT hr = GetIpcClient()->IpcCall(m_nDataType, GetSvrObj(), vectParam, vectRtn);		if (FAILED(hr))		{			IntelMobileException ex( IntelMobileText("Failure IsEventAvailable"),                             IntelMobileText("ClassObjectImpl"),                             IntelMobileText("IsEventAvailable"), hr );	 		LeaveCriticalSection(&m_csIpc);			throw( ex ); 		}		else		{	            CStringHelper::Str2Param(vectRtn[0], (bool &)bAvailable);		}    }    else    {        InvalidInterfaceException ex( IntelMobileText("ClassObject"), IntelMobileText("IsEventAvailable") );	 	LeaveCriticalSection(&m_csIpc);        throw( ex );     }	LeaveCriticalSection(&m_csIpc);    return bAvailable;} // IsEventAvailable() bool ClassObjectImpl::AddObserver   ( Intel::Mobile::BaseAPI::Event::EventType eType, ObserverImpl& observer ){	HRESULT hr = E_POINTER;	//use the this pointer as the Client Event Subject	hr = DoSubscribe(eType, reinterpret_cast<void *>(this));	if(SUCCEEDED(hr))	{		observer.AddEventClient(this);		m_vObservers.push_back(ObserverMap(&observer, eType));	}	else	{		IntelMobileException ex( IntelMobileText("Failure Add Observer for Class Object"), IntelMobileText("ClassObject"), IntelMobileText("AddObserver"), hr );		throw( ex );	}	return SUCCEEDED(hr);}bool ClassObjectImpl::RemoveObserver( Intel::Mobile::BaseAPI::Event::EventType eType, ObserverImpl& observer ){    bool bRemoved = false;    EventClientImplPtr spRemoved = observer.RemoveEventClient( this );    if ( spRemoved != NULL )    {        ClassObjectImplPtr spClass( *reinterpret_cast<ClassObjectImpl**>(&spRemoved) );								                spClass->m_vObservers.remove( ObserverMap(&observer,eType) );        HRESULT hr = UnDoSubscribe( eType, reinterpret_cast<void *>(this) );        if (FAILED(hr))        {           IntelMobileException ex( IntelMobileText("Call UnsubscribeEvent error"), IntelMobileText("ClassObject"), IntelMobileText("RemoveObserver"), hr );           throw( ex );         }       /* else        {            IntelMobileException ex( IntelMobileText("Failure RemoveObserver for Class Object"), IntelMobileText("RemoveObserver"), IntelMobileText("AddObserver"), 0x80047002 );            throw( ex );         }*/        bRemoved = true;    }    return bRemoved;}bool ClassObjectImpl::EventFired( LONG lType, time_t lTimeStamp ){    // Create an event object passing in the type of event and the     // address of the public object that ownes this impl object.    Event::EventType eType = static_cast<Event::EventType>(lType);    //============================================================    //		  The retreival of the identifying data is called in the     //        context of the server thread. This can throw an exception     //        which would cause problems with the interface.    //            IntelMobileString strKey;    try    {        IntelMobileChar* pszKey = GetType();        strKey = pszKey;        delete[] pszKey;    }    catch ( IntelMobileException ex )    {        strKey = IntelMobileText("Unknown");    }    //============================================================    //	Duplicated the use of pszKey as the class key IS the class type.    //            Event theEvent( eType, new ClassObjectImplPtr( this),                     IntelMobileText("ClassObject"), strKey.c_str(), strKey.c_str() );    theEvent.SetTimestamp( lTimeStamp );  // Copy in the server timestamp.    _Log( LOG_DEBUG_FRAMEWORK, IntelMobileText("Class event of type %s fired, timestamp = %s"),                                theEvent.GetTypeName(), theEvent.GetTSstring() );    // Now notify all the local observers    ObserverCollection::const_iterator ittr;    m_vObservers.Lock();     try    {        for ( ittr = m_vObservers.begin(); ittr < m_vObservers.end(); ++ittr )        {            if ( ittr->second == eType )            {                ittr->first->Notify( theEvent );            }        }    }    catch ( ... )     {        _Log( LOG_SOFT_ERROR, IntelMobileText("EventFired: Exception calling Observer's Notify function"));    }    m_vObservers.Unlock();        return true;} // EventFired() 

⌨️ 快捷键说明

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