我如何将:: boost绑定到传递并返回std :: string的托管类的成员? [英] How can I boost::bind to a member of a managed class which passes and returns a std::string?

查看:127
本文介绍了我如何将:: boost绑定到传递并返回std :: string的托管类的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做与

I'm trying to do something very similar to this but I'm struggling to pass the string to/from the callback.

这是我尝试运行的代码的精简版本:

This is a pared down version of the code I'm trying to run:

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma unmanaged

// The unmanaged boost function prototype the native library wants to bind to
typedef boost::function<std::string(const std::string&)> MyNativeCallback;

// The unmanaged library I'm trying to wrap
class MyUnmanagedClass
{
    public:
        MyUnmanagedClass() {}

        void RegisterCallback(MyNativeCallback callback);
};

// Taken from here: https://stackoverflow.com/questions/163757/how-to-use-boostbind-in-c-cli-to-bind-a-member-of-a-managed-class
typedef std::string(__stdcall *ChangeCallback)(std::string);
public delegate std::string ChangeHandler(std::string);

#pragma managed

// The managed callback we'll eventually trigger if all this works.
public delegate String^ ManagedHandler(String^);

// A managed wrapper to bridge the gap and provide a managed call in response to the unmanaged callback
public ref class MyManagedClass
{
protected:

    MyUnmanagedClass* m_responder;

public:
    event ManagedHandler^ OnChange;

public:
    MyManagedClass():
        m_responder(new MyUnmanagedClass())
    {
        // Example code I'm trying to adapt from here: https://stackoverflow.com/questions/163757/how-to-use-boostbind-in-c-cli-to-bind-a-member-of-a-managed-class
        ChangeHandler^ handler = gcnew ChangeHandler(this, &MyManagedClass::HandleRequest);
        GCHandle gch = GCHandle::Alloc(handler);
        System::IntPtr ip = Marshal::GetFunctionPointerForDelegate(handler);
        ChangeCallback cbFunc = static_cast<ChangeCallback>(ip.ToPointer());

        MyNativeCallback handlerToBind = boost::bind(cbFunc, _1); // <-- This fails with 'error C2825'
        m_responder->RegisterCallback(handlerToBind);
    }

    std::string HandleRequest(std::string s)
    {
        OnChange(nullptr);
        return nullptr;
    }
};

...这给了我以下错误:

...which gives me the following error:

1>C:\boost_1_55_0\boost/bind/bind.hpp(69): error C2825: 'F': must be a class or namespace when followed by '::'
1>          C:\boost_1_55_0\boost/bind/bind_template.hpp(15) : see reference to class template instantiation 'boost::_bi::result_traits<R,F>' being compiled
1>          with
1>          [
1>              R=boost::_bi::unspecified,
1>              F=std::basic_string<char,std::char_traits<char>,std::allocator<char>> (__stdcall *)(std::string)
1>          ]
1>          c:\projects\zephir-git\zephirsoftware-gitlab\lcufirmware\managedzeromqdataforwarding\Responder.h(54) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1>          with
1>          [
1>              R=boost::_bi::unspecified,
1>              F=std::string (__stdcall *)(std::string),
1>              L=boost::_bi::list1<boost::arg<1>>
1>          ]
1>C:\boost_1_55_0\boost/bind/bind.hpp(69): error C2039: 'result_type' : is not a member of '`global namespace''
1>C:\boost_1_55_0\boost/bind/bind.hpp(69): error C2146: syntax error : missing ';' before identifier 'type'
1>C:\boost_1_55_0\boost/bind/bind.hpp(69): error C2208: 'boost::_bi::type' : no members defined using this type
1>C:\boost_1_55_0\boost/bind/bind.hpp(69): fatal error C1903: unable to recover from previous error(s); stopping compilation

我忍不住要快要拿到这个了,但是有什么我想念的?

I can't help feeling I've almost got this but there's something I'm missing?

推荐答案

通过一些实验和

Solved it with a bit of experimentation and some more detailed examination of the docs on different forms of bind.

这是按预期方式工作的结果代码:

This is the resulting code which works as expected:

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma unmanaged

// The unmanaged boost function prototype the native library wants to bind to
typedef boost::function<std::string(const std::string&)> MyNativeCallback;

// The unmanaged library I'm trying to wrap
class MyUnmanagedClass
{
    public:
        MyUnmanagedClass() {}

        void RegisterCallback(MyNativeCallback callback);
};

typedef std::string(__stdcall *ChangeCallback)(std::string);
public delegate std::string ChangeHandler(std::string);

#pragma managed

// The managed callback we'll eventually trigger if all this works.
public delegate std::string ManagedHandler(const std::string&);

// A managed wrapper to bridge the gap and provide a managed call in response to the unmanaged callback
public ref class MyManagedClass
{
protected:

    MyUnmanagedClass* m_responder;

public:
    event ManagedHandler^ OnChange;

public:
    MyManagedClass():
        m_responder(new MyUnmanagedClass())
    {
        // Example code I'm trying to adapt from here: http://stackoverflow.com/questions/163757/how-to-use-boostbind-in-c-cli-to-bind-a-member-of-a-managed-class
        ChangeHandler^ handler = gcnew ChangeHandler(this, &MyManagedClass::HandleRequest);
        GCHandle gch = GCHandle::Alloc(handler);
        System::IntPtr ip = Marshal::GetFunctionPointerForDelegate(handler);
        ChangeCallback cbFunc = static_cast<ChangeCallback>(ip.ToPointer());

        MyNativeCallback handlerToBind = boost::bind<std::string>(cbFunc, _1);
        m_responder->RegisterCallback(handlerToBind);
    }

    std::string HandleRequest(const std::string& s)
    {
        return OnChange(s);
    }
};

这篇关于我如何将:: boost绑定到传递并返回std :: string的托管类的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆