libcurl callbacks w/c++ class members

后端 未结 2 1796
一个人的身影
一个人的身影 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.

提交回复
热议问题