digitalmars.D.learn - C callbacks?
- Chris Andrews (7/7) Mar 18 2009 I've hit another snag on my C library interfacing. The .h defines a fun...
- BCS (8/26) Mar 18 2009 In C read this as: pointer to function taking ... (stuff) and returning ...
-
Stewart Gordon
(6/9)
Mar 19 2009
- Chris Andrews (3/5) Mar 19 2009 I believe it was a typedef in this case. There were other points where ...
- Chris Andrews (3/7) Mar 19 2009 I must have been misreading that bit then, thanks for clarifying. Liste...
- BCS (6/22) Mar 19 2009 The under-the-hood for delegate is that they are a function-pointer/cont...
I've hit another snag on my C library interfacing. The .h defines a function: //typedef bool (*TCOD_bsp_callback_t)(TCOD_bsp_t *node, void *userData); bool* bsp_callback_t(bsp_t* node, void* userData); //bsp_t is a struct defining the bsp tree Sidenote: Did I translate that right? Anyway, this function is later passed into various functions, like: bool bsp_traverse_pre_order(bsp_t *node, bsp_callback_t listener, void *userData); I've tried doing some reading on this board regarding c callbacks and delegates, but it feels a bit over my head. Can anyone assist me in figuring out how to D-ify this bit of code so I can interact with the C dll?
Mar 18 2009
Reply to Chris,I've hit another snag on my C library interfacing. The .h defines a function: //typedef bool (*TCOD_bsp_callback_t)(TCOD_bsp_t *node, void *userData);In C read this as: pointer to function taking ... (stuff) and returning bool in D the more normal way to write that would be: typedef bool function(bsp_t* node, void* userData) TCOD_bsp_callback_t; if you are passing this to C code you will need an exter(C) in there somewhere.bool* bsp_callback_t(bsp_t* node, void* userData); //bsp_t is a structdefining the bsp treeSidenote: Did I translate that right?Try it, test it, beat the snot out of it. If it works it's likely correct.Anyway, this function is later passed into various functions, like: bool bsp_traverse_pre_order(bsp_t *node, bsp_callback_t listener, void *userData); I've tried doing some reading on this board regarding c callbacks and delegates, but it feels a bit over my head. Can anyone assist me in figuring out how to D-ify this bit of code so I can interact with the C dll?C code and delegates are incomparable but C and function pointers are not.
Mar 18 2009
BCS wrote: <snip>in D the more normal way to write that would be: typedef bool function(bsp_t* node, void* userData) TCOD_bsp_callback_t;<snip> But do check whether you really want an alias (the D equivalent of typedef in C) or a typedef (an actual new type in D). Stewart.
Mar 19 2009
Stewart Gordon Wrote:But do check whether you really want an alias (the D equivalent of typedef in C) or a typedef (an actual new type in D).I believe it was a typedef in this case. There were other points where I used alias to get it all fixed up. Thankfully, my conversion seems to compile just fine, so now I'm writing the wrappers on that to make sure it all works correctly! Thank you for the heads up.
Mar 19 2009
BCS Wrote:in D the more normal way to write that would be: typedef bool function(bsp_t* node, void* userData) TCOD_bsp_callback_t;Ahh! Thanks a ton, that seems to work like I needed.C code and delegates are incomparable but C and function pointers are not.I must have been misreading that bit then, thanks for clarifying. Listeners/Delegates have always confused me, I really need to do some tutorials and try to understand them better.
Mar 19 2009
Reply to Chris,BCS Wrote:The under-the-hood for delegate is that they are a function-pointer/context-pointer pair where the context pointer is passed like in the this point in methods. In fact, for delegates based on objects, the context is the this pointer. For local functions and delegate literals, it is a pointer into the stack or some such.in D the more normal way to write that would be: typedef bool function(bsp_t* node, void* userData) TCOD_bsp_callback_t;Ahh! Thanks a ton, that seems to work like I needed.C code and delegates are incomparable but C and function pointers are not.I must have been misreading that bit then, thanks for clarifying. Listeners/Delegates have always confused me, I really need to do some tutorials and try to understand them better.
Mar 19 2009