Nested structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Nested structures

Post by Hroudtwolf »

Hi Team,

My little wish is, to have the possibility of nested structures.
So, that we can put sub datasets in our structures.

Example C:

Code: Select all

typedef struct TEST {
   dword dwTest1;
   union {
      dword dwTest2;
      struct {
         word wTest3;
         word wTest4;
      }
   }

} TEST;
My recommendation for PB:

Code: Select all

Structure TEST
   lTest1.l
   StructureUnion
      lTest2.l
      StructureNested
         wTest3.w
         wTest4.w
      EndStructureNested
   EndStructureUnion
EndStructure
Best regards

Wolf
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

How about SubStructure rather than StructureNested?

Would this only be used for anonymous structures (since we could easily define another external structure in the case where it is not anonymous)?
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Sub datasets does only make sense for structureunion datasets.

Scenario:

You want to set a union dataset for "one long" and alternative for "2 word" for the same memoryblock.
So you can't set the 2 words into another structure for embedding it, because the new dataset datafield points to another memoryblock.

Sorry for my english. I hope another one can explain it better than myself.

Regards

Wolf
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

that way it already works:

Code: Select all

Structure Nested
         wTest3.w
         wTest4.w
EndStructure

Structure TEST
   lTest1.l
   StructureUnion
      lTest2.l
      Test34.Nested
   EndStructureUnion
EndStructure 
> Sub datasets does only make sense for structureunion datasets.
could you pse post an example for a nested struct without a union?
oh... and have a nice day.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

@Kaeru

If I want to share the memory of lTest2 with to WORDs, it isn't the adequate way to point with Test34 to another memoryblock.

> Sub datasets does only make sense for structureunion datasets.
could you pse post an example for a nested struct without a union?
That wouldn't make sence in my opinion ^^

Please read an english documentation to nested structures in C.
The english in such docs should be better than mine.

Regards

Wolf
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

If I want to share the memory of lTest2 with to WORDs, it isn't the adequate way to point with Test34 to another memoryblock.
you get this wrong.

a Structure is just a label how to adress the memory, not memory itself.

Define Carrier.Test in my example, and you can
use Carrier\Test34\wTest3 to access LoWord
and Carrier\Test34\wTest4 to access HiWord
of the Long in Carrier\lTest2


I often use

Code: Select all

Structure PointContainer
  StructureUnion
    L.l
    C.COORD
  EndStructureUnion
EndStructure
to Return both an x and y value from a Procedure though the returning Long.
oh... and have a nice day.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

Ok. Your right. My mistake.
I had a datasetpointer in my mind.

But, its a fact, that defining of subdatasets would be more easier with nested structures.

Regards

Wolf
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

you mean you would not need two levels of \ to adress the nested element....?

would not be bad, but is also a matter of taste.
perhaps one wants to see how deep the nesting level is....
oh... and have a nice day.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Needed for DirectDraw DDPIXELFORMAT Structure:

Code: Select all

Structure DDPIXELFORMAT
  dwSize.l
  dwFlags.l
  dwFourCC.l
  StructureUnion
    dwRGBBitCount.l
    dwYUVBitCount.l
    dwZBufferBitDepth.l
    dwAlphaBitDepth.l
    dwLuminanceBitCount.l
    dwBumpBitCount.l
    dwPrivateFormatBitCount.l
  EndStructureUnion
  StructureUnion
    dwRBitMask.l
    dwYBitMask.l
    dwStencilBitDepth.l
    dwLuminanceBitMask.l
    dwBumpDuBitMask.l
    dwOperations.l
  EndStructureUnion
  StructureUnion
    dwGBitMask.l
    dwUBitMask.l
    dwZBitMask.l
    dwBumpDvBitMask.l
    Structure MultiSampleCaps
      wFlipMSTypes.w
      wBltMSTypes.w
    EndStructure
  EndStructureUnion
  StructureUnion
    dwBBitMask.l
    dwVBitMask.l
    dwStencilBitMask.l
    dwBumpLuminanceBitMask.l
  EndStructureUnion
  StructureUnion
    dwRGBAlphaBitMask.l
    dwYUVAlphaBitMask.l
    dwLuminanceAlphaBitMask.l
    dwRGBZBitMask.l
    dwYUVZBitMask.l
  EndStructureUnion
EndStructure
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

@Psychophanta
Wouldn't this be valid?

Code: Select all

Structure MultiSampleCaps
  wFlipMSTypes.w
  wBltMSTypes.w
EndStructure
Structure DDPIXELFORMAT
  dwSize.l
  dwFlags.l
  dwFourCC.l
  StructureUnion
    dwRGBBitCount.l
    dwYUVBitCount.l
    dwZBufferBitDepth.l
    dwAlphaBitDepth.l
    dwLuminanceBitCount.l
    dwBumpBitCount.l
    dwPrivateFormatBitCount.l
  EndStructureUnion
  StructureUnion
    dwRBitMask.l
    dwYBitMask.l
    dwStencilBitDepth.l
    dwLuminanceBitMask.l
    dwBumpDuBitMask.l
    dwOperations.l
  EndStructureUnion
  StructureUnion
    dwGBitMask.l
    dwUBitMask.l
    dwZBitMask.l
    dwBumpDvBitMask.l
    MultiSampleCaps.MultiSampleCaps
  EndStructureUnion
  StructureUnion
    dwBBitMask.l
    dwVBitMask.l
    dwStencilBitMask.l
    dwBumpLuminanceBitMask.l
  EndStructureUnion
  StructureUnion
    dwRGBAlphaBitMask.l
    dwYUVAlphaBitMask.l
    dwLuminanceAlphaBitMask.l
    dwRGBZBitMask.l
    dwYUVZBitMask.l
  EndStructureUnion
EndStructure
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Well done Rescator, that's ok!
who said this request was necessary?
:P
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply