2017-02-17 10 views
0

this과 매우 유사한 작업을 수행하려고하지만 콜백에서 /로 문자열을 전달하는 데 어려움을 겪고 있습니다. 나는이 느낌을 도울 수어떻게하면 boost :: :: bind를 전달하고 std :: string을 반환하는 관리되는 클래스의 멤버에 바인딩 할 수 있습니까?

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 

: 나에게 다음과 같은 오류를 제공하는 ...

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; 
    } 
}; 

을 :

코드의 아래로 껍질을 벗겼다 버전은 내가 실행하는 데 노력하고있다 나는 거의 가지고 있지만 실종 된 것이있다?

답변

1

약간의 실험과 좀 더 자세한 조사를 통해 the docs on different forms of bind을 해결했습니다.

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); 
    } 
}; 
+0

같은 접근 방식은 표준 : 기능과 표준 : : 바인드와 함께 작동 :

는 예상대로 작동 결과 코드입니다. – user3717478