It is currently Thu Dec 13, 2018 7:24 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Access structure field names during run time
PostPosted: Sun Aug 23, 2015 12:48 am 
Offline
User
User
User avatar

Joined: Sat Feb 28, 2015 1:20 pm
Posts: 41
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 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 :)

_________________
PB 5.62 x64 - Windows 7 Enterprise - Windows 10 Pro - Linux Mint 18.x


Last edited by Xanos on Sun Aug 23, 2015 10:36 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Access structure field names during run time
PostPosted: Sun Aug 23, 2015 5:22 am 
Online
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3304
Location: Berlin, Germany
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.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Access structure field names during run time
PostPosted: Sun Aug 23, 2015 10:35 am 
Offline
User
User
User avatar

Joined: Sat Feb 28, 2015 1:20 pm
Posts: 41
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:
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$)

_________________
PB 5.62 x64 - Windows 7 Enterprise - Windows 10 Pro - Linux Mint 18.x


Top
 Profile  
Reply with quote  
 Post subject: Re: Access structure field names during run time
PostPosted: Tue Nov 06, 2018 2:45 am 
Offline
User
User

Joined: Wed Mar 11, 2009 4:06 pm
Posts: 72
Location: NL
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: Access structure field names during run time
PostPosted: Tue Nov 06, 2018 8:19 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1169
Location: Germany, Glienicke
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.

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: Access structure field names during run time
PostPosted: Tue Nov 06, 2018 1:36 pm 
Offline
User
User

Joined: Wed Mar 11, 2009 4:06 pm
Posts: 72
Location: NL
Yeah, but you can't ask 'give me the offset of the third field of structure x'.


Top
 Profile  
Reply with quote  
 Post subject: Re: Access structure field names during run time
PostPosted: Tue Nov 06, 2018 1:57 pm 
Online
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3304
Location: Berlin, Germany
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.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Little John and 16 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye