Re: structs, templates, alias
On 25/04/10 19:15, Ellery Newcomer wrote: Yeah, that's about what I do. The trouble is getting blit to know which field is the length field. I suppose you could pass an index into the tuple to the substructure. That still wouldn't fix the substructure being able to modify the field length. I don't know how the input data is formatted, but an array is always in the form: struct { size_t length; void* ptr; } So it shouldn't be a problem knowing what offset to write the length/elements at.
Re: structs, templates, alias
On 04/25/2010 02:10 PM, Robert Clipsham wrote: On 25/04/10 19:15, Ellery Newcomer wrote: Yeah, that's about what I do. The trouble is getting blit to know which field is the length field. I suppose you could pass an index into the tuple to the substructure. That still wouldn't fix the substructure being able to modify the field length. I don't know how the input data is formatted, but an array is always in the form: struct { size_t length; void* ptr; } So it shouldn't be a problem knowing what offset to write the length/elements at. Hmm. Either I'm not understanding you or I didn't explain something clearly. something like this struct Rec2{ ushort index; ushort nparams; ushort options; ushort[] params; // this has nparams elements } Rec2 rec2 = {index:1, nparams:2, options:~0, params:[4,5]}; would translate to something like x01 00 02 00 FF FF 04 00 05 00 only the elements part of params gets written out, the length part doesn't.
Re: structs, templates, alias
On 25/04/10 20:32, Ellery Newcomer wrote: Hmm. Either I'm not understanding you or I didn't explain something clearly. something like this struct Rec2{ ushort index; ushort nparams; ushort options; ushort[] params; // this has nparams elements } Rec2 rec2 = {index:1, nparams:2, options:~0, params:[4,5]}; would translate to something like x01 00 02 00 FF FF 04 00 05 00 only the elements part of params gets written out, the length part doesn't. Ah, I didn't know this. In which case you should be able to store nparams for use later when the array is encountered, then parse as appropriate? static nparams = 0; foreach( m; MyStruct.tupleof ) { static if( is( typeof(m) x : x[] ) ) { parseParams(nparams); } else { static if( MyStruct.tupleof[i].stringof[$-7 $] == nparams ) nparams = m; } }
Re: structs, templates, alias
On 04/25/2010 02:50 PM, Robert Clipsham wrote: On 25/04/10 20:32, Ellery Newcomer wrote: Hmm. Either I'm not understanding you or I didn't explain something clearly. something like this struct Rec2{ ushort index; ushort nparams; ushort options; ushort[] params; // this has nparams elements } Rec2 rec2 = {index:1, nparams:2, options:~0, params:[4,5]}; would translate to something like x01 00 02 00 FF FF 04 00 05 00 only the elements part of params gets written out, the length part doesn't. Ah, I didn't know this. In which case you should be able to store nparams for use later when the array is encountered, then parse as appropriate? Yeah. The trick is generalizing it. I have about a bazillion different record types, and I want my read/write routines to operate only on the type information provided by tupleof as much as possible, so the structure of each struct documents what it looks like in a binary form. Something like the following could work struct Rec2{ ushort index; ushort nparams; ushort options; NoLength!(Parameter[], nparams) params; } ... foreach(i,m; rec2.tupleof){ static if(isNoLength!(typeof(m))){ auto len = lenField!(rec2, typeof(m)); ... }else{ ... } } The other thing is, once I've read something in, I'm probably going to want to manipulate it. As above, to maintain consistency, whenever I do rec2.params ~= p; I'd have to make sure to do rec2.nparams ++; I'd like to have the second statement come for free (although I suppose I should just ignore nparams for anything but reading in and use params.length instead), but I haven't come up with any way to get NoLength to see nparams. I bet this would be easier in D2.
Re: structs, templates, alias
On 25/04/10 21:21, Ellery Newcomer wrote: struct Rec2{ ushort index; ushort nparams; ushort options; NoLength!(Parameter[], nparams) params; } ... foreach(i,m; rec2.tupleof){ static if(isNoLength!(typeof(m))){ auto len = lenField!(rec2, typeof(m)); ... }else{ ... } } The other thing is, once I've read something in, I'm probably going to want to manipulate it. As above, to maintain consistency, whenever I do rec2.params ~= p; I'd have to make sure to do rec2.nparams ++; I'd like to have the second statement come for free (although I suppose I should just ignore nparams for anything but reading in and use params.length instead), but I haven't come up with any way to get NoLength to see nparams. I bet this would be easier in D2. Seems to me it'd be easier to change how the records are laid out so you don't have to specify this, I'm guessing you already have the data so you can't do this though. If that is the case, perhaps you could have something like: struct Rec2 { ushort index; ushort nparams; ushort options; Parameter[] params; Meta[] meta; } Then ignore the meta field when reading/writing, but use it to store extra info such as which field holds the offset for nparams? Another possibility would be: struct Rec2 { ushort index; ushort nparams; ushort options; Parameter[] params; static size_t offset() { return nparams.offsetof; } } You could then call this function to get the offset, and read the length from there. As for updating nparams, all you need to do is make sure that when you write the struct you make sure you do: Rec2 rec; ... // Append to params ... // Set nparams to the actual length of params. *(rec + Rec.offset) = rec.params.length;