簡體   English   中英

使用boost :: bind替換使用成員函數的自由函數本機回調

[英]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::functionboost::bind 這在本機類之間很有效,但是我想說我想改變我的registerEventCallback函數,以便它接收一個boost::function<void()> 我如何更改addremove方法?

我想到了這一點,但它迫使我為每個事件編寫另一個成員函數,我不確定它是否會構建,因為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM