StructureOffset (or whatever sounds best)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

StructureOffset (or whatever sounds best)

Post 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. :)
Dare2 cut down to size
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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! :)
I may look like a mule, but I'm not a complete ass.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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. :)
Dare2 cut down to size
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: StructureOffset (or whatever sounds best)

Post 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?
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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. :)
Dare2 cut down to size
Post Reply