Description
Bug imported from C2HS Trac
Trac ticket created: 2009-08-16T10:45:26-0700
I find it useful to be able to obtain the offset to a struct member within a struct, e.g. for constructing pointers to members. get and set implicitly do this when constructing their code.
I've implemented a working version of this in my local repository, and I was hoping it could be put in the upstream. The version I've implemented has similar syntax to get and set.
Consider:
typedef struct {
int field;
} substruct_t;
typedef struct {
char somefield[32];
substruct_t substruct;
substruct_t* substruct_p;
} struct_t;
with my patch you can write:
ptrToMember :: Ptr Struct -> IO (Ptr SubStruct)
ptrToMember p = p `plusPtr` {#offsetof struct_t->substruct#}
compared to get/set this emits the byte offset as an int instead of emitting peekByteOff/pokeByteOff code, e.g.:
ptrToMember :: Ptr Struct -> IO (Ptr SubStruct)
ptrToMember p = p `plusPtr` (32)
The results are confusing (and so my patch errors) if you try to get the offset of an indirect member, e.g.
{#offsetof struct_t->substruct_ptr->field#}
is computable (it's 0), but probably not what you want, so my patch forces you to instead use:
{#offsetof substruct_t->field#}
Note this patch is against 0.16, but there is no 0.16 in the version dropdown for trac, so I set no version.
Original Trac ticket had attachments: