Overloading struct members (or cast?)

2014-11-18 Thread Wsdes via Digitalmars-d-learn

Hello again and welcome to another tale of mystery :)

I, again, am facing problems connecting my D program to the C API 
as seen in the callback thread before.


I want to write a callback function that is assigned to a struct 
member. The function should write data from members of another 
struct to the command line. Please consider my code:



   void MyCB(void* pVoid) {
 DataInfo_t* userData; //DataInfo_t is a struct
 userData = cast(DataInfo_t*) pVoid;

 writeln(Value: , pMsgUserData.Values[i].value.string);
   }

Here the DataInfo_t struct has a member Values which itself is a 
struct. The programmer of the API called the members of the union 
inside the Values struct after simple data types or pointers to 
them, specifically


 union value {
  const char   *string;
 }

When I try to compile I get the following error:

   Error: need 'this' for 'string' of type 'const(char*)'

I'm not quite sure what the error wants to tell me. The way I see 
it is that there is some kind of problem with the instance of the 
struct and hence the reference to that specific member value does 
not work.


Any ideas?



Callbacks in D as void functions

2014-11-13 Thread Wsdes via Digitalmars-d-learn

Hi all,

I already posted this to Code Project, but maybe you have a
solution for my problem.

I am trying to write a wrapper for a C API in D. In C I have the
following definition of a callback type:

typedef void (*Callback)(void*);

In a struct all members are of the type Callback. In the main
function each member is assigned a function like this:

Events.OnData = MyDtaCB;

Here the MyDtaCB function has no prototype but is defined as
follows:

void MyDtaCB(void* pVoid){
// Do stuff
}

Now I tried to port this to D. The wiki says that callback
functions should be declared as integer functions, so my D code
looks like this:

extern (C) alias CEventCallback_t = int function(int, int);
extern (C) CEventCallback_t MyDtaCB(void*);

Here's the catch: when I try to assign the MyDtaCB function to a
member of the Events struct, I get a compilation error because
MyDtaCB is a void function and therefore returns no value. Seems
to work in C, though, but I don't know how to handle this in D.

The D tutorial has a function called getCallback() which returns
the void function but itself has the Callback type as return
value. However, in D this would lead to the same error as before
since the void function has no non-empty return value.

Any suggestions how to accomplish this? My thought now was to
screw the function definitions of the C API and implement them in
D. But what's the point in having the API then?

Thanks in advance.


Re: Callbacks in D as void functions

2014-11-13 Thread Wsdes via Digitalmars-d-learn
On Thursday, 13 November 2014 at 15:17:45 UTC, Adam D. Ruppe 
wrote:

On Thursday, 13 November 2014 at 14:50:00 UTC, Wsdes wrote:
I am trying to write a wrapper for a C API in D. In C I have 
the

following definition of a callback type:

typedef void (*Callback)(void*);


I would translate this directly to D:

extern(C) alias Callback = void function(void*);



Here the MyDtaCB function has no prototype but is defined as
follows:

void MyDtaCB(void* pVoid){
// Do stuff
}


And don't forget extern(C) on this too:

extern(C) void MyDtaCB(void* pVoid) {

}


And assign it to the struct:

Events.OnData = MyDtaCB;



Unless you have a link to the wiki that talks about ints, maybe 
that says something different, but I like to keep my C and D 
code that calls it looking pretty much the same when I can.


Hi,

thank you everybody for your replies.

First of all, the link to the wiki that has an example of 
callbacks in C and D:

http://dlang.org/interfaceToC.html

Secondly, I tried your code and that was exactly what I was 
thinking and what I tried before. Then I thought I'd turn to the 
wiki example, so that's where the int function came from. In the 
meantime I changed that to return void so I gave you my old code 
:(


Anyway, I think I got the problem solved. Well, there seems to 
never have been any problem as I am taught now. I asked the 
developer of the C API this morning if I should try to implement 
the callback functions redundantly in D and he said he will have 
a look with me later. So now it turns out that I cannot call the 
extern callback function because it's not provided within the 
library O.O I was already wondering why there are no prototypes 
to these callback functions but I assumed they are provided from 
another library that I don't have direct access to...


So the solution to my problem finally is to define the function 
in my D file and then cast the function pointer to the Callback 
type like this:


void MyDtaCB(void* v){
// Do stuff
}

Events.OnData = cast(Callback) MyDtaCB;

At least this one compiles. Now I'm facing other problems with 
struct members but I guess I'll figure this out. If not, I'll ask 
again so stay tuned for some more unsolved mysteries ;)


Still, thank you all for your effort and time. Keep it up! :)