Thread overview
C callbacks?
Mar 18, 2009
Chris Andrews
Mar 18, 2009
BCS
Mar 19, 2009
Stewart Gordon
Mar 19, 2009
Chris Andrews
Mar 19, 2009
Chris Andrews
Mar 19, 2009
BCS
March 18, 2009
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?
March 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 struct 
defining the bsp tree
> 
> Sidenote: 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.


March 19, 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.
March 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.


March 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.
March 19, 2009
Reply to Chris,

> 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.
> 

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.