Page 1 of 1

Access structure field names during run time

Posted: Sun Aug 23, 2015 12:48 am
by Xanos
Hi guys,
is there any possibility to access the names of a structure field during run time? I know, that the structure field are just used for specifying the offset at which the desired information is placed in the memory relative to the address of the structure variable. However, e.g. when using InsertJSONStructure(), the program obviously uses the structure field names for the corresponding JSON it generates. So I thought there has to be some way to implement this although I was not able to find anything here.

The specific case for which I want to use this is the following:

Code: Select all

Select name$
  Case "id"
    offset  = OffsetOf(struc\id)
  Case "name"
    offset  = OffsetOf(struc\name$)
  Case "value"
    offset  = OffsetOf(struc\value)
  Case "other_stuff"
    offset  = OffsetOf(struc\other_stuff$)
  Case "etc"
    offset  = OffsetOf(struc\etc$)
  ; ...
EndSelect
Which I obviously want to implement more efficiently by directly accessing the offset of the structure field by its name :)

Re: Access structure field names during run time

Posted: Sun Aug 23, 2015 5:22 am
by Little John
Hi!
netmaestro wrote:If you're willing to do a bit of work up front though, you can greatly simplify coding later by using offsets rather than names
You can also use a Map, or combine both approaches, see here.
Maybe that is helpful for you.

Re: Access structure field names during run time

Posted: Sun Aug 23, 2015 10:35 am
by Xanos
Little John wrote:
netmaestro wrote:If you're willing to do a bit of work up front though, you can greatly simplify coding later by using offsets rather than names
Which is what I do now :)

Right now my approach is to translate the user input string into an offset value plus the type of the field for the memory peeking function. (in order to decide when to use PeekI or PeekS etc...)
Of course I could replace the Select ... Case ... Endselect section with a map that takes the string as key and returns the offset.
However, as with my current approach I would have to manually write new code for referencing the offset when I add new field to the structure and my main question is if it is possible in any kind if way to automatize this :)
As mentioned, the JSON (and XML) libraries can do exactly this, though I wonder if they use compiler directives that are not public.

For the sake of completeness, my above code can be implemented with maps like this:

Code: Select all

Define NewMap off()

off("id")           = OffsetOf(struc\id)
off("name")         = OffsetOf(struc\name$)
off("value")        = OffsetOf(struc\value)
off("other_stuff")  = OffsetOf(struc\other_stuff$)
off("etc")          = OffsetOf(struc\etc$)
;...

offset = off(name$)

Re: Access structure field names during run time

Posted: Tue Nov 06, 2018 2:45 am
by Rinzwind
No one? One cannot 'loop' through any random structure at runtime? One cannot access a field in a structure dynamically?
pseudo code
fieldToAccess = "Name"
personStruct1\$fieldToAccess = "Full Name"

or get offset by field index OffsetOfIndex(personStruct1, 2)

Re: Access structure field names during run time

Posted: Tue Nov 06, 2018 8:19 am
by STARGĂ…TE
Rinzwind wrote:One cannot 'loop' through any random structure at runtime?
No, because there is no structure at runtime.
Rinzwind wrote:One cannot access a field in a structure dynamically?
You can. OffsetOf is also just a number. If you know your structure, you can choose a random offset.

Re: Access structure field names during run time

Posted: Tue Nov 06, 2018 1:36 pm
by Rinzwind
Yeah, but you can't ask 'give me the offset of the third field of structure x'.

Re: Access structure field names during run time

Posted: Tue Nov 06, 2018 1:57 pm
by Little John
Rinzwind wrote:Yeah, but you can't ask 'give me the offset of the third field of structure x'.
Just store the offsets beforehand in an array.