Re: Using D libs in C
AFAIK 'int' in D is always a 32-bit value. But in C, 'int' could be 64bit on 64bit platforms. You could try printing sizeof(int) in C and compare that to int.sizeof in D and see if they match. You probably know this, but make sure your exported D function is annotated with extern(C).
TickDuration.ticksPerSec why would it be zero?
The documentation[1] states one should check if the value is zero to see if they can use it. For me this value is 0, Windows XP. So why would it be 0 and what do I use when it is? On a related note, anyone know of a UUID generator? I'm using a possible incorrect translation of the example in RFC 4122[2]. 1. http://digitalmars.com/d/2.0/phobos/core_time.html#TickDuration 2. http://www.ietf.org/rfc/rfc4122.txt
Re: Using D libs in C
They both return 4, and both short and int16_t return 2. Also, I've noticed that if I use a struct (of four ints) instead, the same thing happens, the parameters are not correct. And yes, of course they are extern already.
Re: TickDuration.ticksPerSec why would it be zero?
The documentation[1] states one should check if the value is zero to see if they can use it. For me this value is 0, Windows XP. So why would it be 0 and what do I use when it is? It means that QueryPerformanceFrequency failed: http://msdn.microsoft.com/en-us/library/ms644905(VS.85).aspx Looking at the doc page for it, it looks like it means that your hardware doesn't support a high-resolution performance counter. I should probably improve TickDuration's docs on that. On a related note, anyone know of a UUID generator? I'm using a possible incorrect translation of the example in RFC 4122[2]. 1. http://digitalmars.com/d/2.0/phobos/core_time.html#TickDuration 2. http://www.ietf.org/rfc/rfc4122.txt If all you need to do is generate one, and you're on Linux, then uuidgen would do it, and I guess that you'd use libuuid if you wanted to do it programatically. But I have noe idea what you'd do on Windows. - Jonathan M Davis
question about AutoImplement_Helper (and a couple of others)
I'm a bit new with the D programming language, so I figured this would be the right place to ask a few questions that have been piling up. So let's start. First, I recently found the AutoImplement class while I was trying to build a Proxy object. It seemed like an interesting thing to try, though it may not be a good way to manage asynchronously created resources (which is what I was planning to use it for). At any rate, since AutoImplement is a class already, and I have this thing about not deriving from concrete classes, I decided use AutoImplement_Helper instead. I needed a proxied object, after all, so I needed to be able to manipulate the class definition. It was only after doing so that I noticed it was marked private, and thus presumably not intended for public consumption (and I have no idea why it even compiles). private shared class Proxy(InterfaceType) if (is (InterfaceType == interface)) : public InterfaceType { private alias AutoImplement_Helper!(autoImplement_helper_, InterfaceType, InterfaceType, GeneratePassthroughMethod, isAbstractFunction) autoImplement_helper_; public mixin(autoImplement_helper_.code); public shared static this() { s_proxy = new BlackHole!(InterfaceType); } public this() { m_instance = s_proxy; } public void setProxiedInstance(shared(InterfaceType) instance) { m_instance = instance; } private static shared(InterfaceType) s_proxy; private InterfaceType m_instance; } private template GeneratePassthroughMethod(InterfaceType, method...) { public const(string) GeneratePassthroughMethod = __traits(getMember, this.m_instance, __traits(identifier, self)) (args);; } Is there another way (that I just haven't seen) to do what I'm trying to do? I could just bite the bullet and derive my proxy from AutoImplement, but I thought I'd ask first. Next, you may have noticed that it's a shared class. I wanted to make sure that the assignment I'm doing in the setProxiedInstance() method will be atomic, and that reading the variable will also be atomic. My third question is about attributes. As far as I can tell, D has no user defined attributes, correct? I was making myself a unit test framework (with simple reporting, encapsulation of unit tests as methods, and assertion tools). I was hoping to perform automatic registration of the individual unit tests, but the best I could manage was compile-time detection of methods starting with test, and this feels like a hack. I would prefer to mark the methods explicitly in some way. Does anyone know of a way to do this? Thanks for your time.
Re: question about AutoImplement_Helper (and a couple of others)
Read Bixby: (and I have no idea why it even compiles). If it's in the same module then it compiles because everything in a module is public to each other. If it's in another module then it's a compiler bug. My third question is about attributes. As far as I can tell, D has no user defined attributes, correct? Right. But I think they will be added someday. We have discussed this many times, despite I think there is no concrete proposal yet. I was making myself a unit test framework (with simple reporting, encapsulation of unit tests as methods, and assertion tools). I was hoping to perform automatic registration of the individual unit tests, but the best I could manage was compile-time detection of methods starting with test, and this feels like a hack. I would prefer to mark the methods explicitly in some way. Does anyone know of a way to do this? A unit test module will be soon added to Phobos. Currently the unittest hooks and D introspection capabilities aren't so strong. More people need to ask for this basic capabilities in the main D newsgroup to eventually let the D devs know that here there is a need. Bye, bearophile
Re: A use case for fromStringz
Hmm.. now I need a function that converts a wchar* to a wchar[] or wstring. There doesn't seem to be anything in Phobos for this type of conversion. Or maybe I haven't looked hard enough? I don't know whether this is safe since I'm not sure how the null terminator is represented in utf16, but it does seem to work ok from a few test cases: wstring fromWStringz(wchar* value) { if (value is null) return ; auto oldPos = value; uint nullPos; while (*value++ != '\0') { nullPos++; } if (nullPos == 0) return ; return to!wstring(oldPos[0..nullPos]); } I thought we would pay more attention to interfacing with C code. Since D is supposed to work side-by-side with C, we should have more functions that convert common data types between the two languages.
Re: A use case for fromStringz
Microsoft has some of the most ridiculous functions. This one (GetEnvironmentStrings) returns a pointer to a block of null-terminated strings, with no information on the count of strings returned. Each string ends with a null-terminator, standard stuff. But only when you find two null terminators in succession you'll know that you've reached the end of the entire block of strings. So from some example code I've seen, people usually create a count variable and increment it for every null terminator in the block until they find a double null terminator. And then they have to loop all over again when constructing a list of strings. Talk about inefficient designs.. There's also a wchar* edition of this function, I don't want to even touch it. Here's what the example code looks like: char *l_EnvStr; l_EnvStr = GetEnvironmentStrings(); LPTSTR l_str = l_EnvStr; int count = 0; while (true) { if (*l_str == 0) break; while (*l_str != 0) l_str++; l_str++; count++; } for (int i = 0; i count; i++) { printf(%s\n, l_EnvStr); while(*l_EnvStr != '\0') l_EnvStr++; l_EnvStr++; } FreeEnvironmentStrings(l_EnvStr); I wonder.. in all these years.. have they ever thought about using a convention in C where the length is embedded as a 32/64bit value at the pointed location of a pointer, followed by the array contents? I mean something like the following (I'm pseudocoding here, this is not valid C code, and it's 7 AM.): // allocate memory for the length field + character count char* mystring = malloc(sizeof(size_t) + sizeof(char)*length); *(cast(size_t*)mystring) = length; // embed the length // call a function expecting a char* printString(mystring); // void printString(char* string) { size_t length = *(cast(size_t*)string); (cast(size_t*)string)++; // skip count to reach first char // now print all chars one by one for (size_t i; i length; i++) { printChar(*string++); } } Well, they can always use an extra parameter in a function that has the length, but it seems many people are too lazy to even do that. I guess C programmers just *love* their nulls. :p