Re: A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
Oh I'm not trying to get this into Phobos, I just needed the function so I wrote it and sharing it here. Maybe it should throw. For my purposes I don't need it to throw. :)

Re: A use case for fromStringz

2011-03-31 Thread Jesse Phillips
Andrej Mitrovic Wrote: > Actually, this still suffers from the problem when the returned char* > doesn't have a null terminator. It really sucks when C code does that, > and I've just experienced that. There is a solution though: > > Since we can detect the length of the D array passed into > `fr

Re: A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
On 3/31/11, Jesse Phillips wrote: > Why not: > > string getNameOld() > { > static char[256] name; > cDispatch(name.ptr, kGetProductName); > return to!string(name.ptr); > } > Nice catch! But see my second reply. If a null terminator is missing and we know we're operating on a D a

Re: A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
Actually, this still suffers from the problem when the returned char* doesn't have a null terminator. It really sucks when C code does that, and I've just experienced that. There is a solution though: Since we can detect the length of the D array passed into `fromStringz`, we can do the job of to!

Re: A use case for fromStringz

2011-03-31 Thread Jesse Phillips
Why not: string getNameOld() { static char[256] name; cDispatch(name.ptr, kGetProductName); return to!string(name.ptr); }

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Steven Schveighoffer
On Thu, 31 Mar 2011 14:02:43 -0400, Kagamin wrote: Steven Schveighoffer Wrote: The issue is that you can't have two templates with the same exact template parameters, even if they have different function parameters. This is because the compiler first instantiates the template, then calls the

A use case for fromStringz

2011-03-31 Thread Andrej Mitrovic
There are situations where you have to call a C dispatch function, and pass it a void* and a selector. The selector lets you choose what the C function does, for example an enum constant selector `kGetProductName` could ask the C function to fill a null-terminated string at the location of the v

Re: template template parameter

2011-03-31 Thread Ali Çehreli
On 03/30/2011 04:32 PM, Caligo wrote: > I have a struct that looks something like this: > > struct Box(T, size_t width, size_t height){ > > alias width width_; > alias height height_; > > //do something with a Box of different size >void Fun( B )(B b){ > // using width and height of b >

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Kagamin
Steven Schveighoffer Wrote: > The issue is that you can't have two templates with the same exact > template parameters, even if they have different function parameters. > This is because the compiler first instantiates the template, then calls > the function. If two template instances have

Re: Contracts or Exceptions?

2011-03-31 Thread Kagamin
Kai Meyer Wrote: > On 03/30/2011 08:25 AM, Kagamin wrote: > > Kai Meyer Wrote: > > > >> do all the checking before hand. Arrays are a good example. When not in > >> -release mode, array boundaries are checked upon every access to the > >> array, and an exception is thrown if access goes out of bou

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Aleksandar Ružičić
On Thu, Mar 31, 2011 at 1:30 PM, Steven Schveighoffer wrote: > > or you can change the template parameters in the opDispatch setter: > > @property ref ConfigSection opDispatch(string sectionName, T : string)(T > arg) > > -Steve > Thanks, that's much more readable I now have these templates: // g

Re: template template parameter

2011-03-31 Thread Caligo
I should have been more clear, but my actual question is how do I access the parameters of a template parameter. My example works, but I wanted to know if there is a different and perhaps a better of doing it. In your example it would look something like this: struct SomeContainer(T, int x, int

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Steven Schveighoffer
On Wed, 30 Mar 2011 21:50:43 -0400, spir wrote: On 03/31/2011 02:40 AM, Aleksandar Ružičić wrote: 2011/3/31 Aleksandar Ružičić: Or maybe there is some other way to achive what I want and I'm not aware of it? :-) I know I could have used opIndex and opIndexAssign but I really want config.s

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Steven Schveighoffer
On Thu, 31 Mar 2011 02:52:15 -0400, Jacob Carlborg wrote: On 3/31/11 2:32 AM, Aleksandar Ružičić wrote: Is it possible to use opDispatch as generic getter and setter at the same time? Something like __get() and __set() in PHP.. this is what I've tried: https://gist.github.com/895571 and I ge

Re: Using opDispatch as *magic* getter/setter. Possible or not?

2011-03-31 Thread Aleksandar Ružičić
On Thu, Mar 31, 2011 at 8:52 AM, Jacob Carlborg wrote: > > Or, I think this will work as well: > > @property ref ConfigSection opDispatch(string sectionName, Args ...)(Args > args) if (Args.length == 0) > { > // getter > } > > > @property ref ConfigSection opDispatch(string sectionName, Args ...)(