Access structure field names during run time

Just starting out? Need help? Post your questions and find answers here.
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Access structure field names during run time

Post 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 :)
Last edited by Xanos on Sun Aug 23, 2015 10:36 am, edited 1 time in total.
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Access structure field names during run time

Post 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.
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Re: Access structure field names during run time

Post 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$)
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Access structure field names during run time

Post 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)
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Access structure field names during run time

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Access structure field names during run time

Post by Rinzwind »

Yeah, but you can't ask 'give me the offset of the third field of structure x'.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Access structure field names during run time

Post 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.
Post Reply