libcurl callbacks w/c++ class members

后端 未结 2 1794
一个人的身影
一个人的身影 2021-01-25 10:44

Taken from the libcurl programming tutorial on the libcurl site:

libcurl with C++

There\'s basically only one thing to keep in mind when using

相关标签:
2条回答
  • 2021-01-25 11:30

    To call an instance method, you need an instance to call it on. The libcurl functions that take function pointers don't also take object pointers (rather, can't, since they're C functions), so there's no way of passing the requisite data to call an instance method.

    You can pass an instance pointer via the userdata argument, then use that to invoke an instance method:

    class AClass {
    public:
        static size_t invoke_write_data
            (void *data, size_t size, size_t nmemb, void* pInstance)       
        {
            return ((AClass*)pInstance)->write_data(ptr, size, nmemb);
        }
    
        size_t write_data(void *data, size_t size, size_t nmemb) {
            /* ... */
        }
    };
    
    ...
        extern AClass instance;
    
        curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, AClass::invoke_write_data);
        curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, &instance);
    

    If you need to pass more data via the userdata argument, use a struct that includes the instance pointer as a member. Be careful about memory management: the argument struct could end up leaking or being reclaimed early.

    When C++0x becomes standard and supported by compilers, you should be able to make use of closures to bind an instance and instance method, passing an anonymous function as the callback.

    See also: "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc?" from the C++ FAQ.

    0 讨论(0)
  • 2021-01-25 11:43

    A non-static member function is normally not allowed as a C callback because it needs also the instance pointer as a hidden parameter, which is not provided by the calling C code (and this hidden parameter, depending from the compiler, may not be passed according to the "regular" calling convention, but in a particular register, ...).

    Static member functions, instead, are "normal" functions with a particular scope (which is not relevant as far as the ABI is concerned).

    0 讨论(0)
提交回复
热议问题