c++ - 传递包装器使用的非托管成员函数指针,以将其连接到 c++/cli 中的信号

标签 c++ c++-cli native function-pointers boost-signals

我已经在我的托管 C++ 文件中使用 Boost::Signal 和 Boost::Bind 实现了事件处理。 引用链接:Boost::bind

此外,我还在我的 native C++ 文件中创建了函数指针,该文件作为托管代码中的 EventHandler 传递到我的 boost::Signal.Connect()。 在我的 native C++ 中作为函数指针传递的函数代码

std::string NativeCplus::AddFunctionPointer( std::string message )  
{
return message;
}

和上面的函数在另一个函数 NameChangeEvent() 中作为 boost::function 对象传递,如下所示:

void NativeCplus::NameChangeEvent()
{
UnmanagedNameEvent* unmanagedNameEvent=new UnmanagedNameEvent();
boost::function<std::string (std::string)> f;
f=std::bind1st(std::mem_fun(&AlgebraAPI::AddFunctionPointer),this);//FunctionPointer

std::string name="abcd";
unmanagedNameEvent->AddEvent(f,name);
}

在上面的代码中,我使用了 boost::function 并将函数指针转换为那个 boost::function (f)。(我这样说对吗?)。然后是 unmanagedNameEvent->AddEvent( f,name) 其中 boost::function(f) 传递给 AddEvent(f,name) 而这个 AddEvent(f,name)在我的托管 C++ 代码文件中实现。下面是我在 native c++ 项目中引用的托管 C++ 代码:

//在我的c++/CLI Wrapper.cpp中

    declspec(dllexport) void UnmanagedNameEvent::AddEvent(boost::function<std::string (std::string)> f,std::string name)
        {

                UnmanagedNameEvent* unmanagedNameEvent=new UnmanagedNameEvent();
            unmanagedNameEvent->signalEventMessage.connect(f);
//which should be like this.. unmanagedNameEvent->signalEventMessage.connect(bind(NativeCplus::f));

        }

问题是我不能使用 NativeCplus 类来引用它的非托管函数(即 f),因为这将创建 dll 文件的循环依赖性。对此有任何解决方法吗?所有更短的解决方案都在耳边!!

最佳答案

根据我的理解,这是您想要的示例:

您的C++/CLI“包装器”:

市场.h:

#include <boost/function.hpp>
#include <boost/signals2.hpp>

class __declspec(dllexport) Market
{
    private: boost::signals2::signal<void(double)> signalEvent;
    public: void AddHandler(boost::function<void(double)> handler);
    public: void Move(double value);
};

市场.cpp:

#include "Market.h"

#include <boost/function.hpp>

void Market::AddHandler(boost::function<void(double)> handler)
{
    signalEvent.connect(handler);
}

void Market::Move(double value)
{
    signalEvent(value);
}

还有你的原生应用,test.cpp:

#include <iostream>

#include <boost/function.hpp>
#include <boost/bind.hpp>

#include "Market.h"

class Investor
{
    public: void move_handler(double value)
    {
        std::cout << (value >= 0 ? "Hey! I'm the best!" : "Wat! I'm losing my shirt! ") << std::endl;
    }
};

int main()
{
    Investor investor;

    Market market;

    boost::function<void(double)> move = boost::bind(&Investor::move_handler, &investor, _1);

    market.AddHandler(move);

    market.Move(+0.10);
    market.Move(-0.10);
}

结果:

Hey! I'm the best!
Wat! I'm losing my shirt!

希望这有助于...

关于c++ - 传递包装器使用的非托管成员函数指针,以将其连接到 c++/cli 中的信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17248341/

相关文章:

c++ - For 循环 : LED's Will Not Display Correctly on Arduino

c++ - 从具有相同虚函数的父派生时如何调用子函数

c++ - 使用共享内存减少 CUDA 内核计算的计算时间

c++ - 转发初始化列表表达式

mysql - VC++ 将数组<unsigned char>^存储在MySql数据库中

c++ - 加载库中的对象实例应该被库删除还是从客户端代码中删除?

php - 将现有的 PHP/MYSQL/网站转换为原生 IOS/Android 应用程序

winforms - 在 C++/CLI 中将命令行参数传递给 Windows 表单文件

c# - 与标准 C++ 或 C# 相比,使用 C++/CLI 有什么优势吗?

java.lang.UnsatisfiedLinkError - JNI