cuốn sách gpt4 ai đã làm

c++ - 使用 boost::bind 将自由函数 native 回调替换为成员函数

In lại Tác giả: Walker 123 更新时间:2023-11-28 03:41:27 25 4
mua khóa gpt4 Nike

我有由 C++/CLI 类包装的 native C++ 类,以便 C# 类可以使用它们。讨厌,但有效。到目前为止,为了将 native 回调映射到 .NET 事件,我在包装类中做了类似的事情:

void Wrapper::ManagedEvent::add( Action^ managedEventHandler ){
m_dManagedEvent += managedEventHandler;
m_pNativeInstance->registerEventCallback( static_cast(
Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate( managedEventHandler ).ToPointer() ) );
}

void Wrapper::ManagedEvent::remove( Action^ managedEventHandler ){
m_dManagedEvent -= managedEventHandler;
m_pNativeInstance->registerEventCallback( NULL );
}
  • m_dManagedEventĐúng System::Action^
  • native 回调被定义为自由函数;在这种情况下,typedef void __stdcall NativeCallback(); , 里面 INativeInterface .

这很好用,但现在我爱上了 Boost,这意味着使用 boost::functiontăng cường::liên kết .这在 native 类之间非常有效,但假设我想改变我的 registerEventCallback函数,以便它接收 boost::function .我将如何更改 thêm vàoremove方法?

我想到了这一点,但它迫使我为每个事件编写另一个成员函数,而且我不确定它是否会构建,因为 cái này是一个跟踪句柄:

void Wrapper::FireManagedEvent(){
m_dManagedEvent();
}

void Wrapper::ManagedEvent::add( Action^ managedEventHandler ){
m_dManagedEvent += managedEventHandler;
m_pNativeInstance->registerEventCallback( boost::bind( &Wrapper::FireManagedEvent, this ) );
}

有没有更好的解决方案?

gia hạn:根据@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::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' 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::assign_to(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::function0(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::function(Functor,int)' being compiled
2> with
2> [
2> Signature=void (void),
2> Functor=void *
2> ]
2>
2>Build FAILED.

(Test.cpp的第61行是thêm vào方法的最后一个)

更新 2:这样做,构建并运行正常:

void Wrapper::ManagedEvent::add( Action^ managedEventHandler ){
m_dManagedEvent += managedEventHandler;
void(__stdcall*pTrampoline)() = static_cast( Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate( managedEventHandler ).ToPointer() );
m_pNativeInstance->registerEventCallback( boost::function(pTrampoline) );
}

câu trả lời hay nhất

是的。你已经拥有的。

GetFunctionPointerForDelegate 创建一个包含 cái này 指针的蹦床,因此不需要 tăng cường::liên kết.

唯一会改变的是,您将传递一个 boost::function 仿函数,而不是传递一个普通的函数指针。转换应该是隐式的,您的 C++/CLI 代码不需要更改。

此外,在你太爱上 Boost 之前,看看 std::chức năng 类,它有许多 C++11 的新功能。

关于c++ - 使用 boost::bind 将自由函数 native 回调替换为成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9082249/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress