函数指针实现回掉函数

 用法:
头文件中声明:
DECL_LINK( PaintingFirstLineHdl, PaintFirstLineInfo* );

宏:#define DECL_LINK( Method, ArgType ) \
long Method( ArgType ); \
static long LinkStub##Method( void* pThis, void* )
展开:
long PaintingFirstLineHdl(PaintFirstLineInfo*);
static long LinkStubPaintingFirstLineHdl(void*pThis,void*);
等于声明了两个方法。

CXX文件: IMPL_LINK(OutlineView, PaintingFirstLineHdl, PaintFirstLineInfo*, pInfo)
宏:
#define IMPL_LINK( Class, Method, ArgType, ArgName ) \
IMPL_STUB( Class, Method, ArgType ) \
long Class::Method( ArgType ArgName )

#define IMPL_STUB(Class, Method, ArgType) \
long __EXPORT Class::LinkStub##Method( void* pThis, void* pCaller) \
{ \
return ((Class*)pThis )->Method( (ArgType)pCaller ); \
}
展开:
long_EXPORT OutlineView::LinkStubPaintingFirstLineHdl(void *pThis,void *pCaller)
{
return ((Outlineview*)pThis)->PaintingFirstLineHdl((PaintFirstLineInfo*)pCaller)
}
long OutlineView::PaintingFirstLineHdl(PaintFirstLineInfo* plnfo)

传递函数指针:
Link alink (LINK(this,OutlineView,PaintingFirstLineHdl))
宏:
typedef long (*PSTUB)( void*, void* );
#define LINK( Inst, Class, Member ) Link( (Class*)Inst, (PSTUB)&Class::LinkStub##Member )
展开:
Link((OutlineView*)this,(PSTUB)&OutlineView::LinkStubPaintingFirstLineHdl)
Link类的构造函数: Link( void* pLinkHdl, PSTUB pMemFunc );
pLinkHdl: 对象的地址。
pMemFunc: 指向所要调用的函数指针。

这样把alink传递到任何地方,只要对象还存在就能调用PaintingFirstLineHdl这个方法。

调用:
inline long Link::Call(void *pCaller) const
{
return pFunc ? (*pFunc)(pInst, pCaller) : 0;
}

================
补充:
 IMPL_LINK(OutlineView, PaintingFirstLineHdl, PaintFirstLineInfo*, pInfo)
void ImpEditEngine::Paint( OutputDevice* pOutDev, Rectangle aClipRec, Point aStartPos, sal_Bool bStripOnly, short nOrientation )
mpOutliner->SetPaintFirstLineHdl(LINK(this,OutlineView,PaintingFirstLineHdl));

Link aLink( LINK(this,OutlineView,EventMultiplexerListener) );
mpOutlineViewShell->GetViewShellBase().GetEventMultiplexer()->AddEventListener(
aLink,
tools::EventMultiplexerEvent::EID_CURRENT_PAGE
| tools::EventMultiplexerEvent::EID_PAGE_ORDER);
/
赋值:
mpOutliner->SetPaintFirstLineHdl(LINK(this,OutlineView,PaintingFirstLineHdl));
/
调用:
inline long Link::Call(void *pCaller) const
{
return pFunc ? (*pFunc)(pInst, pCaller) : 0;
}
构造函数: Link( void* pLinkHdl, PSTUB pMemFunc );
pLinkHdl: 对象的地址。
pMemFunc: 指向所要调用的函数指针。
宏:
typedef long (*PSTUB)( void*, void* );
#define LINK( Inst, Class, Member ) Link( (Class*)Inst, (PSTUB)&Class::LinkStub##Member )
mpOutliner->SetPaintFirstLineHdl(LINK(this,OutlineView,PaintingFirstLineHdl));


Using ## concatenates what's before the ## with what's after it.

展开:Link((OutlineView*)this,(PSTUB)&OutlineView::LinkStub##PaintingFirstLineHdl)


LinkStubPaintingFirstLineHdl


IMPL_LINK(OutlineView, PaintingFirstLineHdl, PaintFirstLineInfo*, pInfo)
按照下面的宏展开:
IMPL_STUB(OutlineView,PaintingFirstLineHdl,PaintFirstLINEInfo*) \
long OutlineView::PaintingFirstLineHdl(PaintFirstLineInfo* plnfo)


long_EXPORT OutlineView::LinkStubPaintingFirstLineHdl(void *pThis,void *pCaller){
return ((Outlineview*)pThis)->Method((argType)pCaller) }\
long OutlineView::PaintingFirstLineHdl(PaintFirstLineInfo* plnfo)



#define IMPL_LINK( Class, Method, ArgType, ArgName ) \
IMPL_STUB( Class, Method, ArgType ) \
long Class::Method( ArgType ArgName )


#define IMPL_STUB(Class, Method, ArgType) \
long __EXPORT Class::LinkStub##Method( void* pThis, void* pCaller) \
{ \
return ((Class*)pThis )->Method( (ArgType)pCaller ); \
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值