struct / cast / ? design problem

Charles Hixson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 15 16:34:14 PDT 2015


I've got, say, a file header in a routine that looks like:
struct    BlockHead
{  uint    magic    =    20150312;        //block magic
     uint    magic2;                                //magic for use by 
the using routine
     uint    blockSize;
     uint    unused1;
     ulong    unused2;
     ulong    spare[61];
}

The "unused" vars are intended to be reserved for future use.  The 
"spare" vars are intended for use by another routine that uses the 
routines of the owner of BlockHead to deal with basic things.  So it 
needs to define the data in spare.

How?
The only almost nice way I've thought of is with a struct pointer to 
spare, and calling that nice is a statement about how bad the other ways 
I've thought of are.  (E.g., redefining spare as a ubyte vector and then 
casting the struct of the using program as a ubyte vector and copying it 
over.)

I'm sure that there must be a better way, but I can't think of what it 
is.  BlockHead needs to be a struct to allow the data to be written with 
a writeBlock.  I don't want to use a buffered file because this needs 
random access.  Perhaps I just shouldn't define spare, but then I 
wouldn't retrieve the data with the readBlock.  So I'd double the number 
of disk accesses (to the BlockHead).

One alternative is to have each using routine define it's own header 
struct, and to cast each BlockHead to
the appropriate routine's own header type, but that begs for errors to 
creep in if any of the structs isn't the right length, of ever becomes 
modified to not be the correct length.

Every way I've thought of looks like something that's just begging for 
errors to creep into the code, if not now then later...so *is* there a 
good way?


More information about the Digitalmars-d-learn mailing list