digitalmars.D - Some guidance on writing a Deimos C library interface
- Jens Mueller (24/24) Jul 13 2012 Hi,
- dnewbie (7/13) Jul 13 2012 Yes.
- Jens Mueller (5/15) Jul 14 2012 Why is this? What is the rational behind it?
- Jonathan M Davis (6/13) Jul 15 2012 It thought that it was decided that that it was _bad_ idea to have any k...
- Jens Mueller (5/18) Jul 15 2012 I found
- Johannes Pfau (12/18) Jul 13 2012 When it's a function parameter you could even make it const(T*):
- Jens Mueller (5/22) Jul 15 2012 Very true. But just to be on the safe side I think replacing it with
- Jacob Carlborg (11/34) Jul 14 2012 I would say that when a C typedef has a corresponding type in D, use
- Jens Mueller (7/50) Jul 15 2012 By using core.stdc.stdint I'm safe, right? I mean int8_t is just an
- Jacob Carlborg (4/7) Jul 16 2012 Sure it's safe. Fair enough but I think it's pointless.
Hi, there is some documentation on writing a Deimos interface. E.g. http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP12 http://dlang.org/interfaceToC.html I'd like to get a list of rules that one has to follow. First you have to convert the name of the header file to a valid D module name. Therefore, I found/came up with the following rules: * a slash (/) has to be replaced by a dot (.) * a dash (-) has to be replaced by a dot (.) * a D keyword gets a underscore (_) appended Maybe someone can commented on them. I suppose there are even more. These are just the ones I encountered. Some questions on module names I could not find a definite answer for: 1. The D page says they should be lower case. Should Deimos module names also be made lower case? 2. Should all interfaces be put in a common package, say deimos? Translating the header file itself is pretty straightforward following http://dlang.org/interfaceToC.html. Though some cases are not covered: * E.g. use core.stdc.stdint for standard C types. * const T* should be replaced with const(T)* * How to deal with macros? Can we write a recipe like guide for writing a Deimos interface? Jens
Jul 13 2012
Hi Jens.1. The D page says they should be lower case. Should Deimos module names also be made lower case?Yes.2. Should all interfaces be put in a common package, say deimos?Yes.* const T* should be replaced with const(T)*Yes.* How to deal with macros?http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=146873 Also please check this guide http://www.gamedev.net/blog/1140/entry-2254003-binding-d-to-c/
Jul 13 2012
dnewbie wrote:Why is this? What is the rational behind it? Because some Deimos projects seem not to follow it.2. Should all interfaces be put in a common package, say deimos?Yes.Most of this is covered by http://dlang.org/interfaceToC.html, isn't it? Jens* How to deal with macros?http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=146873 Also please check this guide http://www.gamedev.net/blog/1140/entry-2254003-binding-d-to-c/
Jul 14 2012
On Sunday, July 15, 2012 08:14:22 Jens Mueller wrote:dnewbie wrote:It thought that it was decided that that it was _bad_ idea to have any kind of deimos package when it was discussed previously, but I don't remember what all was discussed about it or when. Presumably, it's in the newsgroup archives somewhere though. - jonathan M DavisWhy is this? What is the rational behind it? Because some Deimos projects seem not to follow it.2. Should all interfaces be put in a common package, say deimos?Yes.
Jul 15 2012
Jonathan M Davis wrote:On Sunday, July 15, 2012 08:14:22 Jens Mueller wrote:I found http://www.digitalmars.com/d/archives/digitalmars/D/Deimos_Consistent_structure_149602.html#N149602 But there is no consensus. Jensdnewbie wrote:It thought that it was decided that that it was _bad_ idea to have any kind of deimos package when it was discussed previously, but I don't remember what all was discussed about it or when. Presumably, it's in the newsgroup archives somewhere though.Why is this? What is the rational behind it? Because some Deimos projects seem not to follow it.2. Should all interfaces be put in a common package, say deimos?Yes.
Jul 15 2012
Am Sat, 14 Jul 2012 05:24:50 +0200 schrieb Jens Mueller <jens.k.mueller gmx.de>:* const T* should be replaced with const(T)*When it's a function parameter you could even make it const(T*): void hello(const char *name); --> extern(C) void hello(const(char*) name); as the pointer itself is passed by value, there's no way it could be modified by hello. DMD should be able to figure that out and treat const(char)* exactly the same, but last time I checked it didn't.* How to deal with macros?There are different solutions. Some people prefer to make those templates, so the binding can be used as a .di file and doesn't need to be compiled. Others just use functions and hope the optimizer will inline it ;-)Can we write a recipe like guide for writing a Deimos interface? Jens
Jul 13 2012
Johannes Pfau wrote:Am Sat, 14 Jul 2012 05:24:50 +0200 schrieb Jens Mueller <jens.k.mueller gmx.de>:Very true. But just to be on the safe side I think replacing it with const(T)* is better.* const T* should be replaced with const(T)*When it's a function parameter you could even make it const(T*): void hello(const char *name); --> extern(C) void hello(const(char*) name); as the pointer itself is passed by value, there's no way it could be modified by hello. DMD should be able to figure that out and treat const(char)* exactly the same, but last time I checked it didn't.I see. Jens* How to deal with macros?There are different solutions. Some people prefer to make those templates, so the binding can be used as a .di file and doesn't need to be compiled. Others just use functions and hope the optimizer will inline it ;-)
Jul 15 2012
On 2012-07-14 05:24, Jens Mueller wrote:Hi, there is some documentation on writing a Deimos interface. E.g. http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP12 http://dlang.org/interfaceToC.html I'd like to get a list of rules that one has to follow. First you have to convert the name of the header file to a valid D module name. Therefore, I found/came up with the following rules: * a slash (/) has to be replaced by a dot (.) * a dash (-) has to be replaced by a dot (.) * a D keyword gets a underscore (_) appended Maybe someone can commented on them. I suppose there are even more. These are just the ones I encountered. Some questions on module names I could not find a definite answer for: 1. The D page says they should be lower case. Should Deimos module names also be made lower case? 2. Should all interfaces be put in a common package, say deimos? Translating the header file itself is pretty straightforward following http://dlang.org/interfaceToC.html. Though some cases are not covered: * E.g. use core.stdc.stdint for standard C types.I would say that when a C typedef has a corresponding type in D, use that instead of an alias. For example: int8_t -> use byte int32_t -> use int But one need to make sure that the typedef resolves to the same D type on all platforms.* const T* should be replaced with const(T)* * How to deal with macros? Can we write a recipe like guide for writing a Deimos interface?You can have a look at dstep for automatically translate C/Objective-C headers: https://github.com/jacob-carlborg/dstep -- /Jacob Carlborg
Jul 14 2012
Jacob Carlborg wrote:On 2012-07-14 05:24, Jens Mueller wrote:By using core.stdc.stdint I'm safe, right? I mean int8_t is just an alias in core.stdc.stdint. I'd like to change the header file as little as possible.Hi, there is some documentation on writing a Deimos interface. E.g. http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP12 http://dlang.org/interfaceToC.html I'd like to get a list of rules that one has to follow. First you have to convert the name of the header file to a valid D module name. Therefore, I found/came up with the following rules: * a slash (/) has to be replaced by a dot (.) * a dash (-) has to be replaced by a dot (.) * a D keyword gets a underscore (_) appended Maybe someone can commented on them. I suppose there are even more. These are just the ones I encountered. Some questions on module names I could not find a definite answer for: 1. The D page says they should be lower case. Should Deimos module names also be made lower case? 2. Should all interfaces be put in a common package, say deimos? Translating the header file itself is pretty straightforward following http://dlang.org/interfaceToC.html. Though some cases are not covered: * E.g. use core.stdc.stdint for standard C types.I would say that when a C typedef has a corresponding type in D, use that instead of an alias. For example: int8_t -> use byte int32_t -> use int But one need to make sure that the typedef resolves to the same D type on all platforms.Usually, the changes are straightforward. It's just these little things where is no clear rule that cost time. Jens* const T* should be replaced with const(T)* * How to deal with macros? Can we write a recipe like guide for writing a Deimos interface?You can have a look at dstep for automatically translate C/Objective-C headers: https://github.com/jacob-carlborg/dstep
Jul 15 2012
On 2012-07-16 06:10, Jens Mueller wrote:By using core.stdc.stdint I'm safe, right? I mean int8_t is just an alias in core.stdc.stdint. I'd like to change the header file as little as possible.Sure it's safe. Fair enough but I think it's pointless. -- /Jacob Carlborg
Jul 16 2012