[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to get the offset of the current struct
From: |
Jose E. Marchesi |
Subject: |
Re: how to get the offset of the current struct |
Date: |
Mon, 12 Feb 2024 00:38:57 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
>> Hi,
>>
>> In some poke struct, for the _print function, I needed to get the offset of
>> the current struct:
>>
>> type ABXML_string_pool =
>> struct
>> {
>> // String pool header:
>> uint<16> string_pool_type;
>> ...
>>
>> method _print = void:
>> {
>> var my_offset = /*** HOW ***/;
>> ...
>> }
>> }
>>
>> What did not work:
>> this'offset because this is not known
>> string_pool_type'offset because string_pool_type is an integer
>>
>> What did work: Adding a zero-sized array the beginning:
>>
>> type ABXML_string_pool =
>> struct
>> {
>> uint<8>[0] struct_start;
>> // String pool header:
>> uint<16> string_pool_type;
>> ...
>>
>> method _print = void:
>> {
>> var my_offset = struct_start'offset;
>> ...
>> }
>> }
>>
>> But this feels like a bad workaround. There should be a better way in the
>> Poke
>> language. Is there?
>>
>> Bruno
>
> method _print = void:
> {
> var my_offset = SELF'offset;
> var my_ios = SELF'ios;
> var name_of_first_field = SELF'ename (1);
>
> [...]
> }
>
> Any attribute that works for values of type `any' can be applied to
> SELF.
Some other useful attributes:
SELF'mapped -> Tells whether the struct is mapped or not.
SELF'length -> Gives the number of fields in the struct.
SELF'eoffset(N) -> Gives the offset of the Nth field withing the struct.
SELF'ename(N) -> Gives the name of the Nth field.
SELF'elem(N) -> Gives the value of the Nth field as an `any' value.
The manual has this nice example of using these:
For example, this is how we could print the name of the current
alternative in a pretty-printer for an union:
type Foo =
union int<32>
{
int<32> One == 1;
int<32> Two == 2;
method _print = void:
{
printf ("%s (%v)", SELF'ename (0), SELF'elem (0));
}
};