2013年9月3日 星期二

Cross platform inter thread communication

//////////////function pointer//////////////
template class IEventFunctorBase
{
public:
virtual void Call(ParamType& event) = 0;
virtual void* GetThis() = 0;
};

template class iEventFunctor : public IEventFunctorBase
{
public:
typedef void (ClassType::*Member)(ParamType&);

iEventFunctor(ClassType* this_, Member member):m_pThis(this_), m_pMember(member)
{
}

virtual void* GetThis()
{
return m_pThis;
}

virtual void Call(ParamType& event)
{
if(m_pThis)
(m_pThis->*m_pMember)(event);
}

private:
ClassType* m_pThis;
Member m_pMember;
}

////////////////EventManager class header
//IEventFunctorBase<PLCStatusEvent>* pointer
typedef std::vector<IEventFunctorBase<PLCStatusEvent>*>    PLCStatusEventSlot_Vector;
//<message ID, PLCStatusEvent Vector>
typedef std::map<int, PLCStatusEventSlot_Vector>        PLCStatusEventSlot_Map;

template<class ClassName, class ParamInfo>
void ConnectEventSlot(int messageID, iEventFunctor<ClassName, ParamInfo>* functor)
{
    //iEventFunctor<ClassName, ParamInfo>*    data_slot    = new iEventFunctor<ClassName, ParamInfo>((ClassName*)functor->GetThis(), *(iEventFunctor<ClassName, ParamInfo>::CallMember*)functor->GetMember());
        iEventFunctor<ClassName, ParamInfo>*    data_slot    = new iEventFunctor<ClassName, ParamInfo>((ClassName*)functor->GetThis(), functor->GetMember());
        IEventFunctorBase<ParamInfo>*            data_parent    = (IEventFunctorBase<ParamInfo>*) data_slot;  

m_PLCStatusEventSlotMap[messageID].push_back(data_parent);
}

//////////////////Registe message destination//////////////////////////////
DvrStreamPlugin::DvrStreamPlugin()
{
EventManager::Get()->RegisterEventSink(iEVT_STATUS_CONNECTED, &iEventFunctor (this,&DvrStreamPlugin::OnStatusConnect));

EventManager::Get()->RegisterEventSink(iEVT_STATUS_DISCONNECTED, &iEventFunctor (this,&DvrStreamPlugin::OnStatusDisconnect));
}

//////////////////send message//////////////////////////////
void DvrConnector::ConnectEvent()
{
DvrStatusEvent evtA(iEVT_STATUS_CONNECTED);
StatusData data;

data.channels = 1;
data.fileid = 2;
data.port = 30;

evtA.SetStatus( 0 );
evtA.SetData(data);

EventManager::Get()->ProcessEvent(evtA);
}

Here is files:
https://github.com/fatalfeel/inter_thread_message

沒有留言:

張貼留言