📄 eventmodel.h
字号:
/*
* name: EventModel.h
*
* desc: 实现文件
*
*/
#pragma once
namespace shaker_utility
{
class CEvent;
/*
================
CFunImpl
================
*/
class CFunImpl
{
public:
CFunImpl() {}
virtual ~CFunImpl() {}
virtual void operator()(CEvent& e) = 0;
virtual bool operator== (const CFunImpl& fun) = 0;
virtual CFunImpl* Clone() = 0;
};
/*
================
CMemberFunc
================
*/
template <typename ClassName>
class CMemberFunc : public CFunImpl
{
public:
typedef CMemberFunc<ClassName> this_type;
typedef void (ClassName::*fEventHandler) ( CEvent & e );
CMemberFunc( ClassName& obj, fEventHandler impl ) : m_Object(obj), m_pImpl(impl) {}
void operator ()( CEvent & e )
{
if ( m_pImpl!=NULL ) (m_Object.*(m_pImpl))( e );
}
CFunImpl* Clone()
{
return new this_type(m_Object, m_pImpl);
}
bool operator== (const CFunImpl& fun)
{
if ( typeid(*this) == typeid(fun) )
{
const this_type& rFun = dynamic_cast<const this_type& >(fun);
return (&m_Object == &rFun.m_Object && m_pImpl == rFun.m_pImpl);
}
return false;
}
virtual ~CMemberFunc()
{
}
protected:
ClassName& m_Object;
fEventHandler m_pImpl;
};
/*
================
CStaticFunc
================
*/
class CStaticFunc : public CFunImpl
{
public:
typedef void (*fEventHandler) ( CEvent & e );
CStaticFunc( fEventHandler impl ) : m_pImpl(impl) {}
void operator ()( CEvent & e )
{
if ( m_pImpl!=NULL ) m_pImpl( e );
}
CFunImpl* Clone()
{
return new CStaticFunc(m_pImpl);
}
bool operator== (const CFunImpl& fun)
{
if ( typeid(*this) == typeid(fun) )
{
const CStaticFunc& rFun = dynamic_cast<const CStaticFunc& >(fun);
return (m_pImpl == rFun.m_pImpl);
}
return false;
}
virtual ~CStaticFunc()
{
}
protected:
fEventHandler m_pImpl;
};
/*
================
CEventHandler
================
*/
class CEventHandler
{
private:
void Clear(){if(m_pImpl){delete m_pImpl ;m_pImpl = NULL ;}}
CFunImpl* m_pImpl;
public:
~CEventHandler()
{
Clear();
}
template<typename ClassName>
CEventHandler( ClassName& obj, void (ClassName::*impl)(CEvent&) ) : m_pImpl( new CMemberFunc<ClassName>(obj,impl) ) {}
CEventHandler( void(*impl)(CEvent&) ) : m_pImpl( new CStaticFunc(impl) ) {}
CEventHandler( const CEventHandler&fun ) : m_pImpl( NULL ) { *this=fun; }
void operator() ( CEvent & e )
{
(*m_pImpl)(e);
}
CEventHandler& operator= ( const CEventHandler& fun )
{
Clear();
if (fun.m_pImpl) m_pImpl = fun.m_pImpl->Clone();
return *this;
}
bool operator== ( const CEventHandler & handler )
{
if ( m_pImpl==NULL || handler.m_pImpl==NULL ) return true ;
if ( typeid(*m_pImpl) == typeid(*(handler.m_pImpl)) )
{
return (*m_pImpl) == (*(handler.m_pImpl));
}
return false ;
}
};
/*
================
CEvent
================
*/
class CEvent
{
private:
std::list<CEventHandler>m_Funcs;
void Register( CEventHandler handle )
{
m_Funcs.push_back(handle);
}
void UnRegister( const CEventHandler & handler )
{
m_Funcs.remove(handler);
}
void* lpData;
public:
void* GetPointer() { return lpData; }
CEvent & operator<< ( const CEventHandler & handler )
{
Register ( handler );
return * this ;
}
CEvent & operator>> ( const CEventHandler & handler )
{
UnRegister ( handler );
return * this ;
}
void operator ( )( void* pData=NULL )
{
lpData=pData;
for(std::list<CEventHandler>::iterator pos=m_Funcs.begin(); pos!=m_Funcs.end(); ++pos )
(*pos)(*this);
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -