gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:41:27 25 4
gpt4 key购买 nike

我有由 C++/CLI 类包装的 native C++ 类,以便 C# 类可以使用它们。讨厌,但有效。到目前为止,为了将 native 回调映射到 .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_dManagedEventSystem::Action^
  • native 回调被定义为自由函数;在这种情况下,typedef void __stdcall NativeCallback(); , 里面 INativeInterface .

这很好用,但现在我爱上了 Boost,这意味着使用 boost::functionboost::bind .这在 native 类之间非常有效,但假设我想改变我的 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 指针的蹦床,因此不需要 boost::bind

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

此外,在你太爱上 Boost 之前,看看 std::function 类,它有许多 C++11 的新功能。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com