Page 1 of 1

StructureOffset

Posted: Mon Sep 13, 2010 3:49 am
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?

Re: StructureOffset

Posted: Mon Sep 13, 2010 4:10 am
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

Re: StructureOffset

Posted: Mon Sep 13, 2010 11:03 am
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

Re: StructureOffset

Posted: Mon Jan 24, 2011 1:57 am
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