cURL / Mailing Lists / curl-library / Single Mail

curl-library

How to retrieve "context" information in write/read callbacks?

From: Andrea Funtò <straight.to.trash_at_gmail.com>
Date: Wed, 6 Feb 2008 18:00:01 +0100

Hallo everybody,
I am writing a brand new C++ wrapper for libcurl, based on Trolltech's Qt. I
would like to use Qt's signals and slots instead of callbacks for read and
write events management. Inorder to abstract away from callbacks, I need to
wrap them and emit Qt signals instead.
I have often seen a "signature pattern" in C libraries that are "C++
friendly": they expose a way to retrieve context information in the
callbacks, in order to let C++ class developers grab the "this" pointer and
switch back to the OO programming style. You can see this behaviour in eXpat
as well as many other libraries, it's usual in Windows API, it's in pthreads
etc. A callback registering function looks like:
     void set_my_callback(callback_function_ptr_t fx_ptr, void *
callback_data);
and when you actually use it in a C++ class, you can do something like:
     set_my_callback(<static method of my class>, (void *)this);
so the "stub" callback you provide in the <static member of the class> can
go back to object orientation by grabbing and casting as appropriate the
callback_data.
Unfortunately I cannot see this feature in libcurl: if I register a write
callback with curl_easy_setopt(CURLOPT_WRITEFUNCTION), I can only provide a
pointer to the memory area to be filled by libcurl before invoking my
callback (through CURLOPT_WRITEDATA). There is no CURL handle available to
the callback either... Am I missing some important point, or misintepreting
the docs?
What I would suggest, if you agree with this potential need, is an
additional CURLOPT_CTX_DATA (void *) option, and a slight change to the
existing callbacks to add an additional void * parameter, passed in and
untouched by libcurl.

Thank you all in advance for your support!

Andrea
Received on 2008-02-06