![](/img/trans.png)
[英]Class member function as callback using boost::bind and boost::function
[英]Replace free function native callback with member function using boost::bind
我有C ++ / CLI類包裝的本機C ++類,因此C#類可以使用它們。 討厭,但有效。 到目前為止,為了將本機回調映射到.NET事件,我在我的包裝類中做了類似的事情:
void Wrapper::ManagedEvent::add( Action^ managedEventHandler ){
m_dManagedEvent += managedEventHandler;
m_pNativeInstance->registerEventCallback( static_cast<INativeInterface::NativeCallback*>(
Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate( managedEventHandler ).ToPointer() ) );
}
void Wrapper::ManagedEvent::remove( Action^ managedEventHandler ){
m_dManagedEvent -= managedEventHandler;
m_pNativeInstance->registerEventCallback( NULL );
}
m_dManagedEvent
是一個System::Action^
typedef void __stdcall NativeCallback();
,在INativeInterface
。 這很好用,但現在我愛上了Boost,這意味着使用boost::function
和boost::bind
。 這在本機類之間很有效,但是我想說我想改變我的registerEventCallback
函數,以便它接收一個boost::function<void()>
。 我如何更改add
和remove
方法?
我想到了這一點,但它迫使我為每個事件編寫另一個成員函數,我不確定它是否會構建,因為this
是一個跟蹤句柄:
void Wrapper::FireManagedEvent(){
m_dManagedEvent();
}
void Wrapper::ManagedEvent::add( Action^ managedEventHandler ){
m_dManagedEvent += managedEventHandler;
m_pNativeInstance->registerEventCallback( boost::bind( &Wrapper::FireManagedEvent, this ) );
}
有更好的解決方案嗎?
更新 :根據@Ben Voigt的回答,我嘗試了以下方法:
void Wrapper::ManagedEvent::add( Action^ managedEventHandler ){
m_dManagedEvent += managedEventHandler;
m_pNativeInstance->registerEventCallback( static_cast< boost::function< void() > >(
Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate( managedEventHandler ).ToPointer() ) );
}
但它給出了編譯器錯誤:
2>D:\SVN.DRA.WorkingCopy\DRALibraries\Boost_1_48_0\boost/function/function_template.hpp(112): error C2064: term does not evaluate to a function taking 0 arguments
2> D:\SVN.DRA.WorkingCopy\DRALibraries\Boost_1_48_0\boost/function/function_template.hpp(110) : while compiling class template member function 'void boost::detail::function::void_function_invoker0<FunctionPtr,R>::invoke(boost::detail::function::function_buffer &)'
2> with
2> [
2> FunctionPtr=void *,
2> R=void
2> ]
2> D:\SVN.DRA.WorkingCopy\DRALibraries\Boost_1_48_0\boost/function/function_template.hpp(907) : see reference to class template instantiation 'boost::detail::function::void_function_invoker0<FunctionPtr,R>' being compiled
2> with
2> [
2> FunctionPtr=void *,
2> R=void
2> ]
2> D:\SVN.DRA.WorkingCopy\DRALibraries\Boost_1_48_0\boost/function/function_template.hpp(722) : see reference to function template instantiation 'void boost::function0<R>::assign_to<Functor>(Functor)' being compiled
2> with
2> [
2> R=void,
2> Functor=void *
2> ]
2> D:\SVN.DRA.WorkingCopy\DRALibraries\Boost_1_48_0\boost/function/function_template.hpp(1043) : see reference to function template instantiation 'boost::function0<R>::function0<void*>(Functor,int)' being compiled
2> with
2> [
2> R=void,
2> Functor=void *
2> ]
2> Test.cpp(61) : see reference to function template instantiation 'boost::function<Signature>::function<void*>(Functor,int)' being compiled
2> with
2> [
2> Signature=void (void),
2> Functor=void *
2> ]
2>
2>Build FAILED.
(Test.cpp的第61行是add
方法的最后一行)
更新2 :執行此操作,它構建並運行正常:
void Wrapper::ManagedEvent::add( Action^ managedEventHandler ){
m_dManagedEvent += managedEventHandler;
void(__stdcall*pTrampoline)() = static_cast<void(__stdcall*)()>( Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate( managedEventHandler ).ToPointer() );
m_pNativeInstance->registerEventCallback( boost::function<void()>(pTrampoline) );
}
是。 你已經擁有的。
GetFunctionPointerForDelegate
創建一個包含this
指針的trampoline,因此不需要boost::bind
。
唯一會改變的是傳遞一個普通的函數指針,你將傳遞一個boost::function
仿函數。 轉換應該是隱式的,您的C ++ / CLI代碼不需要更改。
此外,在你愛上Boost之前,看看std::function
類,它有很多C ++ 11的新功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.