structs, templates, alias
Robert Clipsham
robert at octarineparrot.com
Sun Apr 25 13:35:52 PDT 2010
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;
More information about the Digitalmars-d-learn
mailing list