www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - X header help

reply Lucas Goss <lgoss007 gmail.com> writes:
Has anyone translated X C headers to D? I have X.h done and Xlib.h is 
partly done.

Anyways, I've run into some code that I'm not sure how to translate it to D.

//----------
typedef void (*XIMProc)(
     XIM,
     XPointer,
     XPointer
);

// then later a struct which uses the above function pointer

typedef struct {
     XPointer client_data;
     XIMProc callback;
} XIMCallback;
//----------

I translated the it as:
//----------
void function(XIM, XPointer, XPointer) XIMProc;

struct XIMCallback
{
	XPointer client_data;
	// XIMProc callback; ??? nope.
	// void* callback = XIMProc(); ??? nope.
}
//----------

Obviously it failed to compile, but I'm not sure how to translate it.
Mar 11 2006
next sibling parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
Actually, you want it to be a typedef or alias.  You could just use its:

typedef void (*XIMProc)(XIM, XPointer, XPointer);

Which should be equivalent to:

typedef void function(XIM, XPointer, XPointer) XIMProc;

The important thing here is the typedef; the way you tried doesn't 
specify this is a type, but is more like defining a variable.

Then you would use:

struct XIMCallback
{
	XPointer client_data;
	XIMProc callback;
}

Hope that helps.

Thanks,
-[Unknown]


 Has anyone translated X C headers to D? I have X.h done and Xlib.h is 
 partly done.
 
 Anyways, I've run into some code that I'm not sure how to translate it 
 to D.
 
 //----------
 typedef void (*XIMProc)(
     XIM,
     XPointer,
     XPointer
 );
 
 // then later a struct which uses the above function pointer
 
 typedef struct {
     XPointer client_data;
     XIMProc callback;
 } XIMCallback;
 //----------
 
 I translated the it as:
 //----------
 void function(XIM, XPointer, XPointer) XIMProc;
 
 struct XIMCallback
 {
     XPointer client_data;
     // XIMProc callback; ??? nope.
     // void* callback = XIMProc(); ??? nope.
 }
 //----------
 
 Obviously it failed to compile, but I'm not sure how to translate it.

Mar 11 2006
parent reply Lucas Goss <lgoss007 gmail.com> writes:
Unknown W. Brackets wrote:
 The important thing here is the typedef; the way you tried doesn't 
 specify this is a type, but is more like defining a variable.

Ah, right. Thanks, that helped.
Mar 11 2006
parent reply Kyle Furlong <kylefurlong gmail.com> writes:
Lucas Goss wrote:
 Unknown W. Brackets wrote:
 The important thing here is the typedef; the way you tried doesn't 
 specify this is a type, but is more like defining a variable.

Ah, right. Thanks, that helped.

Actually the C typedef I believe is more like the D alias.
Mar 11 2006
parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
IMHO, in the case of function pointers typedefs usually make sense.

-[Unknown]


 Lucas Goss wrote:
 Unknown W. Brackets wrote:
 The important thing here is the typedef; the way you tried doesn't 
 specify this is a type, but is more like defining a variable.

Ah, right. Thanks, that helped.

Actually the C typedef I believe is more like the D alias.

Mar 11 2006
parent reply John Reimer <terminal.node gmail.com> writes:
Unknown W. Brackets wrote:
 IMHO, in the case of function pointers typedefs usually make sense.
 
 -[Unknown]
 
 
 Lucas Goss wrote:
 Unknown W. Brackets wrote:
 The important thing here is the typedef; the way you tried doesn't 
 specify this is a type, but is more like defining a variable.

Ah, right. Thanks, that helped.

Actually the C typedef I believe is more like the D alias.


It's safer/easier to use "alias" when converting from C typedef. typedef are so type safe that they cause tons of problems when creating a D interface to C code. For strict D use, of course, typedef's are great. -JJR
Mar 11 2006
parent Lucas Goss <lgoss007 gmail.com> writes:
John Reimer wrote:
 It's safer/easier to use "alias" when converting from C typedef. typedef 
 are so type safe that they cause tons of problems when creating a D 
 interface to C code.  For strict D use, of course, typedef's are great.

Yeah, I use "alias" when converting any C typedef just to be safe. Lucas
Mar 12 2006
prev sibling next sibling parent reply John Reimer <terminal.node gmail.com> writes:
Lucas Goss wrote:
 Has anyone translated X C headers to D? I have X.h done and Xlib.h is 
 partly done.
 
 Anyways, I've run into some code that I'm not sure how to translate it 
 to D.
 
 //----------
 typedef void (*XIMProc)(
     XIM,
     XPointer,
     XPointer
 );
 
 // then later a struct which uses the above function pointer
 
 typedef struct {
     XPointer client_data;
     XIMProc callback;
 } XIMCallback;
 //----------
 
 I translated the it as:
 //----------
 void function(XIM, XPointer, XPointer) XIMProc;
 
 struct XIMCallback
 {
     XPointer client_data;
     // XIMProc callback; ??? nope.
     // void* callback = XIMProc(); ??? nope.
 }
 //----------
 
 Obviously it failed to compile, but I'm not sure how to translate it.

These headers could be quite useful! Please submit them to the D bindings project at dsource.org when you are done? I could use them in a project or two in the future. :) -JJR
Mar 11 2006
parent Lucas Goss <lgoss007 gmail.com> writes:
John Reimer wrote:
 These headers could be quite useful!  Please submit them to the D 
 bindings project at dsource.org when you are done?  I could use them in 
 a project or two in the future. :)
 

Yes, I think the headers are useful too and I plan on submitting them to the D bindings when done. Lucas
Mar 12 2006
prev sibling parent Dejan Lekic <dejan nu6.org> writes:
One guy (essiene) and I are working on it. Snapshot is available at:
http://gnu.nu6.org:8000/files/dxlib.tar.bz2 .

It would be nice if you join our D IRC channel: irc://irc.freenode.org/D and
discuss the code further with us (my nick there is Dejan) .

Kind regards

Dejan Lekic
  http://dejan.lekic.org
Mar 14 2006