Page 1 of 1

StructureOffset (or whatever sounds best)

Posted: Sat Jan 26, 2008 1:30 am
by Dare
One day when the team is idling along with little to do (:)) perhaps we could have a way to reposition fields within a structure without using StructureUnion.

For example:

Code: Select all

Structure one
  myLong.l
  myByte.b
  myWord.w
  StructureOffset = myByte    ; predefined field, optional for absolute, eg, 4
  aWord.w
  aByte.b
EndStructure

var.one
var\aWord = 1
Which would have myByte and aWord sharing the same start address, and everything after myByte increment offsets accordingly.

Currently the same thing can be achieved using StructureUnion but this requires (where more than one field variable is involved) additional structures.

Code: Select all

Structure bitA
  myByte.b
  myWord.w
EndStructure

Structure bitB
  aWord.w
  aByte.b
EndStructure

Structure one
  myLong.l
  StructureUnion
    partA.bitA
    partB.bitB
  EndStructureUnion
EndStructure

var.one
var\partB\aWord = 1
The advantages are:

1: Improved readability in code when using the structure.
2: Reduced typing when cutting code using "complex" structures.


Just a low priority wish. :)

Posted: Sat Jan 26, 2008 12:29 pm
by srod
Dare, I must admit that I do not understand how you would use this in a single structure - why overwrite fields which you have just declared?

Perhaps I'm having a blonde moment! :)

Posted: Sat Jan 26, 2008 4:38 pm
by Dare
To allow fields to share the same memory space, as per StructureUnion, but with a simplified approach.

In the example above, both bits of code would provide this:

Code: Select all

Offset
  0     myLong.l
  1       ..
  2       ..
  3       ..
  4     myByte.b    aWord.w
  5     myWord.w       ..
  6       ..        wByte.b
However with StructureUnion we need two other structures to get the same effect. So less source code, more simplicity, same end result.

We are not overwriting, just redefining.


Just another approach. :)

Re: StructureOffset (or whatever sounds best)

Posted: Sat Jan 26, 2008 6:11 pm
by tinman
Dare wrote:1: Improved readability in code when using the structure.
Seriously? That syntax would melt my head, at least with having to use two structures you can exactly what fields are shared.

With structures you can also stop the shared memory and have fields after the union. Unless I've missed something your suggestion doesn't?

Posted: Sat Jan 26, 2008 11:55 pm
by Dare
Sorry about your head! :)

* Finds a new bucket for tinman *

Think of it as multiple definitions of fields within a record. It can of course extend beyond the first StructureOffset:

Code: Select all

Structure one
  myLong.l
  myByte.b
  myWord.w
  moreA.b
  moreB.b
  moreC.w

  StructureOffset = myByte
  aWord.w
  aByte.b

  StructureOffset = 1
  AndSoOn.w
  Etc.w
EndStructure

Giving

Offset
  0   myLong.l
  1     ..                AndSoOn.w
  2     ..                   ..
  3     ..                Etc.w
  4   myByte.b    aWord      ..
  5   myWord.w      ..
  6     ..        aByte
  7   moreA.l
  8   moreB.l
  9   moreC.w
 10     ..
All we are doing assigning additional name/types within the structure at memory offsets of our choosing, which can then be used as desired.

No harder to understand (perhaps easier) than floating a structure over another:

Code: Select all

fixed.strucMain
*p.strucA
*p = @fixed + OffsetOf(someField) + 3    ; if that was indeed the offset
*p\aVar = 12
fixed\another = 11
.. save that this is a permanent configuration of fields within the memory allocated.


As I said earlier, perhaps when the team has an idle moment. In other words, never - unless it grabs someone's fancy. :)