I'm using std::bind to provide a callback while abstracting some logic by binding some parameters first. i.e.
void start() {
int secret_id = 43534;
//Bind the secret_id to the callback function object
std::function<void(std::string)> cb = std::bind(&callback, secret_id, std::placeholders::_1);
do_action(cb);
}
void do_action(std::function<void(std::string)> cb) {
std::string result = "hello world";
//Do some things...
//Call the callback
cb(result);
}
void callback(int secret_id, std::string result) {
//Callback can now do something with the result and secret_id
}
So in the above example, the do_action does not need to know about the secret_id and other functions can reuse it without having a secret_id of their own. This is especially useful when do_action is some kind of asynchronous operation.
My question is, is there a way to bind parameter values to function pointers using only C?
If not by emulating std::bind then is there another way to pass data from first() to callback() without complicating the neutral do_action()?
An opaque type and keeping secret in a source should do it:
(The above does not address registering callback functions)
No. C doesn't allow you to do that directly.
In C the standard way to handle callbacks is using context pointers:
this means that you will pass a function that will accept a
void *
in addition to the normal parameters that the callback should handle (in the above case an integer) and you will also pass avoid *
that you want to be passed back.This
void *
normally points to astruct
that will contain all the extra parameters or data you need in the callback and using this approach the library doesn't depend on what this context is. If the callback doesn't need any context you just pass a NULL pointer ascontext
and ignore the first parameter when being called from the library.Something that is kind of hackish and formally unsafe but it's sometimes done is that if the context is a simple data that fits the size of a
void *
(e.g. an integer) and if your environment is not going to have problems with it you can trick the library by passing a fakevoid *
that is just an integer and you convert it back to an integer when being called from the library (this saves the caller from allocating the context and managing its lifetime).On how to how to trick the language to avoid this limitation (still remaining in the land of portable C) I can think some hack:
First we allocate a pool of two-arguments callbacks and context data
then we write (or macro-generate) functions that we wish to register and that will call the two-arguments versions.
We also store them in a pool where they're allocated and deallocated:
Then to bind the first parameter we can do something like
but bound functions must also be explicitly deallocated
The short answer is no.
The only thing you can do is declare another function that has the
secret_id
built into it. If you're using C99 or newer you can make it an inline function to at least limit the function call overhead, although a newer compiler may do that by itself anyway.To be frank though, that is all std::bind is doing, as it is returning a templated struct, std::bind simply declares a new functor that has secret_id built into it.
As 6502 explained, it is not possible to do this in portable C without some kind of context argument being passed to the callback, even if it doesn't name
secret_id
directly. However, there are libraries such as Bruno Haible's trampoline that enable creation of C functions with additional information (closures) through non-portable means. These libraries do their magic by invoking assembly or compiler extensions, but they are ported to many popular platforms; if they support architectures you care about, they work fine.Taken from the web, here is an example of code that trampoline enables is this higher-order function that takes parameters
a
,b
, andc
(analogous to yoursecret_id
, and returns a function of exactly one parameterx
that calculatesa*x^2 + b*x + c
: