boost::bind for C function

Hello, I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K); So I would like to use boost::bind / boost::function to create the binding to a class method: boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) ) so that I can use the class method with the CFunction signature. I would like to call a class method with the object context like a C function. Can anybody help me to create a correct binding? Thanks Phil

[Please do not mail me a copy of your followup] boost-users@lists.boost.org spake the secret code <911E9779-6F81-4420-AC3D-BF023FDBB358@flashpixx.de> thusly:
Hello,
I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K);
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
What happens if you try:
boost::function

On Fri, 21 Dec 2012, Philipp Kraus wrote:
Hello,
I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K);
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
so that I can use the class method with the CFunction signature. I would like to call a class method with the object context like a C function.
Can anybody help me to create a correct binding?
Assuming State* is void* or something that you can control the definition
of, what about this?
extern "C"
void call_function_object(void* obj) {
(*static_cast

Am 21.12.2012 um 00:59 schrieb Jeremiah Willcock:
On Fri, 21 Dec 2012, Philipp Kraus wrote:
Hello,
I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K);
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
so that I can use the class method with the CFunction signature. I would like to call a class method with the object context like a C function.
Can anybody help me to create a correct binding?
Assuming State* is void* or something that you can control the definition of, what about this?
Sorry, I can't do this, the typedef is fixed, I must use it in exactly this case.
extern "C" void call_function_object(void* obj) { (*static_cast
(obj))(); } where function_obj_type is something that you create that acts like the boost::bind result (or is just boost::function
). Note that you are responsible for managing the lifetime of the function object you use in this way, since you pass a non-owning pointer to it into the C code.
Can I cast the boost::bind result to the typedef definition? I need something like my_c_call( static_cast<CFunction>( boost::bind(&MyClass::myMethod, this) ) ) Thanks Phil

On Fri, 21 Dec 2012, Philipp Kraus wrote:
Am 21.12.2012 um 00:59 schrieb Jeremiah Willcock:
On Fri, 21 Dec 2012, Philipp Kraus wrote:
Hello,
I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K);
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
so that I can use the class method with the CFunction signature. I would like to call a class method with the object context like a C function.
Can anybody help me to create a correct binding?
Assuming State* is void* or something that you can control the definition of, what about this?
Sorry, I can't do this, the typedef is fixed, I must use it in exactly this case.
Does it have certain members that some other part of the code uses?
extern "C" void call_function_object(void* obj) { (*static_cast
(obj))(); } where function_obj_type is something that you create that acts like the boost::bind result (or is just boost::function
). Note that you are responsible for managing the lifetime of the function object you use in this way, since you pass a non-owning pointer to it into the C code. Can I cast the boost::bind result to the typedef definition? I need something like
my_c_call( static_cast<CFunction>( boost::bind(&MyClass::myMethod, this) ) )
I don't think boost::bind result types are documented, so you would need C++11's decltype/auto to get its type. You will still need to either put the function object on the heap or make sure it is alive during the time that it might be called. -- Jeremiah Willcock

Am 21.12.2012 um 01:57 schrieb Jeremiah Willcock:
On Fri, 21 Dec 2012, Philipp Kraus wrote:
Am 21.12.2012 um 00:59 schrieb Jeremiah Willcock:
On Fri, 21 Dec 2012, Philipp Kraus wrote:
Hello,
I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K);
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
so that I can use the class method with the CFunction signature. I would like to call a class method with the object context like a C function.
Can anybody help me to create a correct binding?
Assuming State* is void* or something that you can control the definition of, what about this?
Sorry, I can't do this, the typedef is fixed, I must use it in exactly this case.
Does it have certain members that some other part of the code uses?
Yes, the typdef comes from a header file of a library, so all library calls uses the typedef.
extern "C" void call_function_object(void* obj) { (*static_cast
(obj))(); } where function_obj_type is something that you create that acts like the boost::bind result (or is just boost::function
). Note that you are responsible for managing the lifetime of the function object you use in this way, since you pass a non-owning pointer to it into the C code. Can I cast the boost::bind result to the typedef definition? I need something like
my_c_call( static_cast<CFunction>( boost::bind(&MyClass::myMethod, this) ) )
I don't think boost::bind result types are documented, so you would need C++11's decltype/auto to get its type. You will still need to either put the function object on the heap or make sure it is alive during the time that it might be called.
Do you know a way to bind a class member function like a C function without C++11(I can not use the C++11 definition at the moment) ? The function exists during execution, because if my object is destroyed, the C bindings are also destroyed. My first idea is / was, that I create a boost::function object and get its function pointer and cast the pointer to my typedef definition, so that I can push the casted pointer to my C function. Phil

On Fri, 21 Dec 2012, Philipp Kraus wrote:
Am 21.12.2012 um 01:57 schrieb Jeremiah Willcock:
On Fri, 21 Dec 2012, Philipp Kraus wrote:
Am 21.12.2012 um 00:59 schrieb Jeremiah Willcock:
On Fri, 21 Dec 2012, Philipp Kraus wrote:
Hello,
I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K);
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
so that I can use the class method with the CFunction signature. I would like to call a class method with the object context like a C function.
Can anybody help me to create a correct binding?
Assuming State* is void* or something that you can control the definition of, what about this?
Sorry, I can't do this, the typedef is fixed, I must use it in exactly this case.
Does it have certain members that some other part of the code uses?
Yes, the typdef comes from a header file of a library, so all library calls uses the typedef.
Can you add some member that is a void* or such that you can use to point to your function object? Is there one already?
extern "C" void call_function_object(void* obj) { (*static_cast
(obj))(); } where function_obj_type is something that you create that acts like the boost::bind result (or is just boost::function
). Note that you are responsible for managing the lifetime of the function object you use in this way, since you pass a non-owning pointer to it into the C code. Can I cast the boost::bind result to the typedef definition? I need something like
my_c_call( static_cast<CFunction>( boost::bind(&MyClass::myMethod, this) ) )
I don't think boost::bind result types are documented, so you would need C++11's decltype/auto to get its type. You will still need to either put the function object on the heap or make sure it is alive during the time that it might be called.
Do you know a way to bind a class member function like a C function without C++11(I can not use the C++11 definition at the moment) ? The function exists during execution, because if my object is destroyed, the C bindings are also destroyed. My first idea is / was, that I create a boost::function object and get its function pointer and cast the pointer to my typedef definition, so that I can push the casted pointer to my C function.
It's easier than that:
extern "C" call_boost_function(void* f) {
(*static_cast

On Fri, Dec 21, 2012 at 12:30:20AM +0100, Philipp Kraus wrote:
Hello,
I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K);
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
so that I can use the class method with the CFunction signature. I would like to call a class method with the object context like a C function.
Can anybody help me to create a correct binding?
Within the standard language, no, there is no way to get a free function pointer to an arbitrary callable. The result of a bind expression is not a free function pointer nor convertible to one. It's an object of an unspecified class type with a suitable operator (). You have several horrible choices for getting a free function pointer from something like this. Stateless C++11 lambdas can be coerced into a free function pointer, but as they're stateless, they will not fly here. The common within-the-language solution is to make a free "trampoline" function that forwards calls to your object, which it retrieves from some secret hiding place. In some APIs, you can pass in a "context" or "state" object at the same place you provide the free function pointer to the callback: R trampoline(State* s, A0 a0, A1 a1) { return get_my_callable(s)(a0, a1); } register_callback_with_api(&trampoline, &my_callable); Another choice that is not within the standard is to generate machine code for a trampoline-like function that directly forwards your call, either through some code generation library like Xbyak or by taking a suitable function, copying the generated code and patch the parts where the call happens. The above is often called "thunking". In summary, the ways in order of unhorribleness are: 1) squirrel the actual callable away in some context/state provided to the trampoline; 2) squirrel the actual callable away in some global or static variable, which the trampoline uses; 3) dive into the abyss of code/thunk generation. -- Lars Viklund | zao@acc.umu.se

Am 21.12.2012 um 02:32 schrieb Lars Viklund:
On Fri, Dec 21, 2012 at 12:30:20AM +0100, Philipp Kraus wrote:
Hello,
I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K);
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
so that I can use the class method with the CFunction signature. I would like to call a class method with the object context like a C function.
Can anybody help me to create a correct binding?
You have several horrible choices for getting a free function pointer from something like this.
Stateless C++11 lambdas can be coerced into a free function pointer, but as they're stateless, they will not fly here.
Thanks for this long description. I think my option without C++11 is to define a global pointer to "this" and define a static function, that uses this stored pointer. Phil

On Friday, December 21, 2012 09:32 AM, Lars Viklund wrote:
Hello,
I have got a a C library function call, which defines a function: typedef int (*CFunction) (State* K);
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
so that I can use the class method with the CFunction signature. I would like to call a class method with the object context like a C function.
Can anybody help me to create a correct binding? Within the standard language, no, there is no way to get a free function
On Fri, Dec 21, 2012 at 12:30:20AM +0100, Philipp Kraus wrote: pointer to an arbitrary callable.
The result of a bind expression is not a free function pointer nor convertible to one. It's an object of an unspecified class type with a suitable operator ().
You have several horrible choices for getting a free function pointer from something like this.
Stateless C++11 lambdas can be coerced into a free function pointer, but as they're stateless, they will not fly here.
The common within-the-language solution is to make a free "trampoline" function that forwards calls to your object, which it retrieves from some secret hiding place. In some APIs, you can pass in a "context" or "state" object at the same place you provide the free function pointer to the callback:
R trampoline(State* s, A0 a0, A1 a1) { return get_my_callable(s)(a0, a1); }
register_callback_with_api(&trampoline, &my_callable);
Another choice that is not within the standard is to generate machine code for a trampoline-like function that directly forwards your call, either through some code generation library like Xbyak or by taking a suitable function, copying the generated code and patch the parts where the call happens.
The above is often called "thunking".
If the State structure has a void *user_data field, then you can use the
following:
template
In summary, the ways in order of unhorribleness are:
1) squirrel the actual callable away in some context/state provided to the trampoline;
2) squirrel the actual callable away in some global or static variable, which the trampoline uses;
3) dive into the abyss of code/thunk generation.
-- Nick Jones nick.jones@network-box.com Senior Manager Core Development Team Network Box Corporation Ltd 16th Floor, Metro Loft, 38 Kwai Hei Street, Tel: +852 2736 2083 Kwai Chung, Kowloon, Fax: +852 2736 2778 Hong Kong S.A.R. www.network-box.com

On 20 Dec 2012, at 23:30, Philipp Kraus wrote:
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
As Lars Viklund said, boost::bind returns an object, whereas a c-function is just a pointer - it is not possible to convert an object into a pointer. Assuming this is Lua (based on your posts of lua-l and the typedef). I think you have two choices. If this is about another API then ignore the rest of the message! 1) If you really need to use boost::bind then you need to have some way to box the object in lua. This is relatively complicated, and completely unnecessary to simply call a class method. However, I can provide an example if necessary. 2) Create a closure with the object's address stored as a lightuserdata. int callobjectmethod(lua_State *l) { Object *x = (Object *)lua_tolightuserdata(l, lua_upvalueindex(1)); return x->method(l); //Make sure this doesn't throw, or enclose in try/catch } Instead of using lua_pushcfunction, you should use lua_pushcclosure as follows Object o; lua_pushlightuserdata(l, &o); lua_pushcclosure(l, callobjectmethod, 1); You must remember you you are responsible for the lifetime of the object and must keep it in scope while the closure still exists. There are many ways to achieve what you're trying to do, and the most appropriate depends on many things. Lua has a beautifully simplistic API, that doesn't take long to learn with the right materials. The PiL3 book comes out soon, I suggest you spend some time reading it, as it will help you enormously with these kind of problems. Thanks, Kevin

On 2012-12-21 10:21:09 +0100, Kevin Martin said:
On 20 Dec 2012, at 23:30, Philipp Kraus wrote:
So I would like to use boost::bind / boost::function to create the binding to a class method:
boost::function<CFunction>( boost::bind(&MyClass::myMethod, this) )
As Lars Viklund said, boost::bind returns an object, whereas a c-function is just a pointer - it is not possible to convert an object into a pointer.
Assuming this is Lua (based on your posts of lua-l and the typedef). I think you have two choices. If this is about another API then ignore the rest of the message!
1) If you really need to use boost::bind then you need to have some way to box the object in lua. This is relatively complicated, and completely unnecessary to simply call a class method. However, I can provide an example if necessary.
2) Create a closure with the object's address stored as a lightuserdata.
int callobjectmethod(lua_State *l) { Object *x = (Object *)lua_tolightuserdata(l, lua_upvalueindex(1)); return x->method(l); //Make sure this doesn't throw, or enclose in try/catch }
Instead of using lua_pushcfunction, you should use lua_pushcclosure as follows
Object o; lua_pushlightuserdata(l, &o); lua_pushcclosure(l, callobjectmethod, 1);
You must remember you you are responsible for the lifetime of the object and must keep it in scope while the closure still exists.
There are many ways to achieve what you're trying to do, and the most appropriate depends on many things. Lua has a beautifully simplistic API, that doesn't take long to learn with the right materials. The PiL3 book comes out soon, I suggest you spend some time reading it, as it will help you enormously with these kind of problems.
Thanks, I use exactly this structure and I will add a seperate definition for C++11. It is the best way and in my case the closure exists during the lifetime of my object in the ctor the this pointer will be used and in the dtor the closure will be removed. Thanks Phil
participants (6)
-
Jeremiah Willcock
-
Kevin Martin
-
Lars Viklund
-
legalize+jeeves@mail.xmission.com
-
Nick Jones
-
Philipp Kraus