![]() |
![]()
| ![]() |
![]()
NAMEdyncallback —
callback interface of dyncall
SYNOPSIS#include <dyncall_callback.h>
DCCallback *
void
void
void
DESCRIPTIONThedyncallback dyncall library has an interface to
create callback objects, that can be passed to functions as callback
arguments. In other words, a pointer to the callback object can be
"called", directly. The callback handler then allows iterating
dynamically over the arguments once called back.
Declaration of a dyncallback handler (following function pointer definition in dyncallback/dyncall_callback.h): char cbHandler(DCCallback* cb, DCArgs* args, DCValue* result, void* userdata); cb is a pointer to the DCCallback object in
use EXAMPLELet's say, we want to create a callback object and call it. For simplicity, this example will omit passing it as a function pointer to a function (e.g. compar in qsort(), etc.) and demonstrate calling it, directly. First, we need to define our callback handler - the following handler illustrates how to access the passed- in arguments:char cbHandler(DCCallback* cb, DCArgs* args, DCValue* result, void* userdata) { int* ud = (int*)userdata; int arg1 = dcbArgInt (args); float arg2 = dcbArgFloat (args); short arg3 = dcbArgShort (args); double arg4 = dcbArgDouble (args); long long arg5 = dcbArgLongLong(args); // .. do something .. result->s = 1244; return 's'; } Note that the return value of the handler is a signature character, not the actual return value, itself. Now, let's call it through a DCCallback object: DCCallback* cb; short result = 0; int userdata = 1337; cb = dcbNewCallback("ifsdl)s", &cbHandler, &userdata); result = ((short(*)(int, float, short, double, long long))cb) (123, 23.f, 3, 1.82, 9909ll); dcbFreeCallback(cb); CONFORMING TOThe dyncallback library needs at least a c99 compiler with additional support for anonymous structs/unions (which were introduced officially in c11). Given that those are generally supported by pretty much all major c99 conforming compilers (as default extension), it should build fine with a c99 toolchain. Strictly speaking, dyncall conforms to c11, though.SEE ALSOdyncall(3), dynload(3) and the dyncall manual (available in HTML and PDF format) for more information.AUTHORSDaniel Adler ⟨dadler@uni-goettingen.de⟩Tassilo Philipp ⟨tphilipp@potion-studios.com⟩
|