Using boost::bind() across C code, will it work?
Asked Answered
H

3

5

Can I use boost::bind(mycallback, this, _1, _2) across C code?

Update

The short answer is no, boost bind does not return a function pointer, which can be called in C code, but a functor (C++ object with overloaded () operator) see answer below.

Hector answered 27/11, 2011 at 0:3 Comment(3)
Please provide a code example of what you want to do. Until then I'd say the answer is "no".Brina
Please at the very least give us the declaration of invoke_callback!Avelin
void invoke_callback(void* funcptr) but I figured it out already, boost is not the best way to go in my case (using a C library), thanks.Hector
M
4

The best way to do what you want to do is to create a C callback that then calls the boost::function, which is stored in some sort of user memory with new.

Example:

void callFunction(void* data)
{

   boost::function<void(void)> *func = (boost::function<void(void)>* ) (data);
   (*func)();
   delete(func);
}

Then you simply pass this callback and set the user data(however it is specified in libev) to be a copy of your function allocated with new.

This is how you specify user data with libev: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#ASSOCIATING_CUSTOM_DATA_WITH_A_WATCH

Meill answered 27/11, 2011 at 0:15 Comment(3)
This would work if libev had a good place to store per descriptor user-data.Hector
@Hector You can store user data for libev quite easily. I added the link from the docs to my answer. You can either stuff it in the void*data member that is in each watcher or subclass the watcher and add a full boost::function member.Meill
Wow, thanks, I did not see that. I did see the per-loop which is Not what I wanted.Hector
L
4

No. boost::bind returns a Functor not a function pointer. The returned object is a C++ object which has an overloaded operator() which allows it to behave like a function pointer in C++ code. But it is not a function pointer which can be passed into C code.

Levona answered 27/11, 2011 at 0:7 Comment(0)
M
4

The best way to do what you want to do is to create a C callback that then calls the boost::function, which is stored in some sort of user memory with new.

Example:

void callFunction(void* data)
{

   boost::function<void(void)> *func = (boost::function<void(void)>* ) (data);
   (*func)();
   delete(func);
}

Then you simply pass this callback and set the user data(however it is specified in libev) to be a copy of your function allocated with new.

This is how you specify user data with libev: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#ASSOCIATING_CUSTOM_DATA_WITH_A_WATCH

Meill answered 27/11, 2011 at 0:15 Comment(3)
This would work if libev had a good place to store per descriptor user-data.Hector
@Hector You can store user data for libev quite easily. I added the link from the docs to my answer. You can either stuff it in the void*data member that is in each watcher or subclass the watcher and add a full boost::function member.Meill
Wow, thanks, I did not see that. I did see the per-loop which is Not what I wanted.Hector
B
2

I assume you want to use whatever boost::bind returns as a callback function for a C library?

If that's the case, then no, it won't work. It won't even build, as boost::bind does not return a function pointer.

Barsac answered 27/11, 2011 at 0:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.