StructureOffset

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

StructureOffset

Post by Joakim Christiansen »

First I'll just visualize how this could be used:

Code: Select all

Structure test
  long.l
  StructureOffset 0
    firstByteOfLong.b
  EndStructureOffset
  StructureOffset 1
    secondByteOfLong.b
  EndStructureOffset
  StructureOffset 2
    theTwoLatestBytesOfLong.w
  EndStructureOffset
EndStructure
Many people might have a difficult understanding the need for this but it would be extremely handy in many cases!
I also want a StructureOffsetR for any relative position (read below).

I especially needed this when I wrote a server to handle remote control events from some iPhone application.
The data sent to my server was always 16 bytes and for neatness and ease of access I wanted to put this into a structure... But depending on what was sent the data could contain different values, most of the time the value I needed could be read as a word but sometimes I needed to read it as two bytes, below I will demonstrate what I mean.

Lets say that when command = 0 a relative x position is filled into the word relativePos, then I just read that value! But when command = 2 this means that a button has been pressed and the data at the position of relativePos now contains two values to be read as isPressed and buttonId. Get it? A StructureUnion wont help me with this...

Code: Select all

Structure data
  command.b
  relativePos.w
  StructureOffsetR 0
    isPressed.b
  EndStructureOffset
  StructureOffsetR 1
    buttonId.b
  EndStructureOffset
EndStructure
And here I introduced the use of StructureOffsetR, R for a relative offset (relative to the start position of the latest value defined which is not inside a StructureOffset).

Did I finish my server without this YES, but it would be very sexy to be able to write structures like this!
Anyone understands and agree?
I like logic, hence I dislike humans but love computers.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: StructureOffset

Post by Joakim Christiansen »

Actually this could replace StructureUnion, let me show this, first the StructureUnion example in the help file:

Code: Select all

Structure Type
  Name$         ; This is a pointer to the string, 4 or 8 bytes (x86,x64)
  StructureUnion
    Long.l      ; Each field (Long, Float and Byte) resides at the
    Float.f     ; same address in memory.
    Byte.b      ;
  EndStructureUnion
EndStructure

Debug SizeOf(Type) ;This is the size of the Name$ pointer + Long
                    ;So 8 on x86 and 12 on x64
Then the same using StructureOffset:

Code: Select all

Structure Type
  Name$         ; This is a pointer to the string, 4 or 8 bytes (x86,x64)
  StructureOffsetR 4 ; 4 if compiled as x86
    Long.l      ; Each field (Long, Float and Byte) resides at the
    Float.f     ; same address in memory.
    Byte.b      ;
  EndStructureOffset
EndStructure
I like logic, hence I dislike humans but love computers.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: StructureOffset

Post by STARGÅTE »

+1

now i use this one:

Code: Select all

Structure Test
  StructureUnion
    Long.l
    Byte.b[3]
    Word.w[1]
  EndStructureUnion
EndStructure
to get any Byte of the long, or one word ...

but if i only need the last Byte of the long, your idea is nice, or like this:

Code: Select all

Structure Test
  StructureUnion
    Long.l
    LastByte.b  Offset 3
    Word.w  Offset 2
  EndStructureUnion
EndStructure

Code: Select all

0 |- Long
1 |  
2 |   |- Word
3 |   |   |- LastByte
4
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
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: StructureOffset

Post by Joakim Christiansen »

Here is another example to show how it would make coding easier!

Old way:

Code: Select all

Structure RGB
  r.b
  g.b
  b.b
EndStructure
Structure color
  StructureUnion
    c.RGB
    long.l
  EndStructureUnion
EndStructure

Define color.color
color\long = RGB(10,20,40)
Debug color\c\b
New way:

Code: Select all

Structure color
  r.b
  g.b
  b.b
  StructureOffset 0
    long.l
  EndStructureOffset
EndStructure

Define color.color
color\long = RGB(10,20,40)
Debug color\b
I like logic, hence I dislike humans but love computers.
Post Reply