Author Topic: Unions  (Read 1698 times)

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Guest
Unions
« on: December 25, 2018, 12:34:05 AM »
Would this:

Code: [Select]
UNION RECT
    nLeft   AS LONG
    nTop    AS LONG
    nRight  AS LONG
    nBottom AS LONG
    =
    Left   AS LONG
    Top    AS LONG
    Right  AS LONG
    Bottom AS LONG
END UNION

Be how this code:

Code: [Select]
TYPE RECT_old
    nLeft   AS LONG
    nTop    AS LONG
    nRight  AS LONG
    nBottom AS LONG
END TYPE

TYPE RECT_new
    Left   AS LONG
    Top    AS LONG
    Right  AS LONG
    Bottom AS LONG
END TYPE

UNION RECT
    RECT_old
    RECT_new
END UNION

Should be converted?

Charles Pegge

  • Guest
Re: Unions
« Reply #1 on: December 25, 2018, 02:59:46 AM »
Yes, Brian

'=' resets the offset tally, and can be used more than once.

Brian Alvarez

  • Guest
Re: Unions
« Reply #2 on: December 25, 2018, 12:28:24 PM »
I understand.

 Now i needed to add a union to a TYPE (Windows API required it), the offset should not be reset to zero, it is supposed to only reset back to the previous member offset.
I got this already working with low level methods, handling the offsets myself, but is this done already in oxygen? Maybe you could add something like:

Code: [Select]
TYPE RECT
    LONG A
    LONG B
  *             ' Save the offset
    LONG C
    LONG D
  <            ' Go back to saved offset
    LONG E
    LONG F
END TYPE

This way A and B are part of the UDT while E and F overlaps C and D. This would allow to add UNIONS to TYPES.

The number of nested UNIONS in TYPES are undetermined, so, maybe if you decide to do this, the * can also have an ID for example:


Code: [Select]
TYPE RECT
    LONG A
    LONG B
  * 1            ' Save the offset 1
    LONG C
    LONG D
  < 1           ' Go back to saved offset 1
    LONG E
    LONG F
  * 2            ' Save the offset 2
    LONG G
    LONG H
  < 2           ' Go back to saved offset 2
    LONG I
    LONG J
  < 1           ' Go back to saved offset 1
END TYPE

No offset ID would default to 1.
« Last Edit: December 25, 2018, 02:17:59 PM by Brian Alvarez »

Charles Pegge

  • Guest
Re: Unions
« Reply #3 on: December 25, 2018, 08:11:59 PM »
How about using '+'  to fix the base offset for the next bunch of unions:

Code: [Select]
type omni
  int attrib        'offset 0
  +
  float fx,fy,fz    'offsets 4 8 12
  =
  double dx,dy,dz   'offsets 8 16 24
  =
  int ix,iy,iz      'offsets 4 8 12
  +
  int flags         'offset 32
end type


I can  implement this very easily

Brian Alvarez

  • Guest
Re: Unions
« Reply #4 on: December 25, 2018, 08:27:21 PM »
Im not sure i follow you... take a look at this (correct) example i have right now:

Code: [Select]
UNION UNION1
    C AS QUAD ' Offset 8
    D AS QUAD ' Offset 8
END UNION

UNION UNION2
    E AS QUAD ' Offset 16
    F AS QUAD ' Offset 16
END UNION

TYPE UDT1
    A AS LONG  ' Offset 0
    B AS LONG  ' offset 4
    UNION1
    UNION2
    G AS LONG  ' Offset 24
    H AS LONG  ' Offset 28
END TYPE   

LOCAL U AS UDT1

These are the offsets for all the members of U.




Charles Pegge

  • Guest
Re: Unions
« Reply #5 on: December 25, 2018, 08:41:23 PM »
These structures could be merged into one, by using '+' and '=':

Code: [Select]
TYPE UDT1
    A AS LONG  ' Offset 0
    B AS LONG  ' offset 4
    +
    C AS QUAD ' Offset 8
    =
    D AS QUAD ' Offset 8
    +
    E AS QUAD ' Offset 16
    =
    F AS QUAD ' Offset 16
    +
    G AS LONG  ' Offset 24
    H AS LONG  ' Offset 28
END TYPE   

Brian Alvarez

  • Guest
Re: Unions
« Reply #6 on: December 25, 2018, 08:46:06 PM »
Perfect. Let's do it! :)

jack

  • Guest
Re: Unions
« Reply #7 on: December 26, 2018, 05:28:59 AM »
hello Brian Alvarez, Charles Pegge
would you be willing to make a mini-tutorial on unions for dummies?
the concepts presented here are way over my head, using + and = in a union are foreign to me and I got lost trying to keep track of the offsets

Charles Pegge

  • Guest
Re: Unions
« Reply #8 on: December 26, 2018, 10:08:11 AM »
Hello Jack,

Taking a variant as one of the more extreme examples:

c++
Code: C++
  1. typedef struct tagVARIANT {
  2.   union {
  3.     struct {
  4.       VARTYPE vt;
  5.       WORD    wReserved1;
  6.       WORD    wReserved2;
  7.       WORD    wReserved3;
  8.       union {
  9.         LONGLONG     llVal;
  10.         LONG         lVal;
  11.         BYTE         bVal;
  12.         SHORT        iVal;
  13.         FLOAT        fltVal;
  14.         DOUBLE       dblVal;
  15.         VARIANT_BOOL boolVal;
  16.         SCODE        scode;
  17.         CY           cyVal;
  18.         DATE         date;
  19.         BSTR         bstrVal;
  20.         IUnknown     *punkVal;
  21.         IDispatch    *pdispVal;
  22.         SAFEARRAY    *parray;
  23.         BYTE         *pbVal;
  24.         SHORT        *piVal;
  25.         LONG         *plVal;
  26.         LONGLONG     *pllVal;
  27.         FLOAT        *pfltVal;
  28.         DOUBLE       *pdblVal;
  29.         VARIANT_BOOL *pboolVal;
  30.         SCODE        *pscode;
  31.         CY           *pcyVal;
  32.         DATE         *pdate;
  33.         BSTR         *pbstrVal;
  34.         IUnknown     **ppunkVal;
  35.         IDispatch    **ppdispVal;
  36.         SAFEARRAY    **pparray;
  37.         VARIANT      *pvarVal;
  38.         PVOID        byref;
  39.         CHAR         cVal;
  40.         USHORT       uiVal;
  41.         ULONG        ulVal;
  42.         ULONGLONG    ullVal;
  43.         INT          intVal;
  44.         UINT         uintVal;
  45.         DECIMAL      *pdecVal;
  46.         CHAR         *pcVal;
  47.         USHORT       *puiVal;
  48.         ULONG        *pulVal;
  49.         ULONGLONG    *pullVal;
  50.         INT          *pintVal;
  51.         UINT         *puintVal;
  52.         struct {
  53.           PVOID       pvRecord;
  54.           IRecordInfo *pRecInfo;
  55.         } __VARIANT_NAME_4;
  56.       } __VARIANT_NAME_3;
  57.     } __VARIANT_NAME_2;
  58.     DECIMAL decVal;
  59.   } __VARIANT_NAME_1;
  60. } VARIANT;
  61.  

With '=' and '+' notation, and some reorganising, the variant struct can be simplified to something more comprehensible:

Code: OxygenBasic
  1. typedef struct tagVARIANT {
  2.   DECIMAL decVal
  3.   =
  4.   VARTYPE vt;
  5.   WORD    wReserved1;
  6.   WORD    wReserved2;
  7.   WORD    wReserved3;
  8.   +
  9.   PVOID       pvRecord
  10.   IRecordInfo *pRecInfo
  11.   =
  12.   LONGLONG     llVal
  13.   =
  14.   LONG         lVal
  15.   =
  16.   BYTE         bVal
  17.   =
  18.   SHORT        iVal
  19.   =
  20.   FLOAT        fltVal
  21.   =
  22.   DOUBLE       dblVal
  23.   =
  24.   VARIANT_BOOL boolVal
  25.   =
  26.   SCODE        scode
  27.   =
  28.   CY           cyVal
  29.   =
  30.   DATE         date
  31.   =
  32.   BSTR         bstrVal
  33.   =
  34.   IUnknown     *punkVal
  35.   =
  36.   IDispatch    *pdispVal
  37.   =
  38.   SAFEARRAY    *parray
  39.   =
  40.   BYTE         *pbVal
  41.   =
  42.   SHORT        *piVal
  43.   =
  44.   LONG         *plVal
  45.   =
  46.   LONGLONG     *pllVal
  47.   =
  48.   FLOAT        *pfltVal
  49.   =
  50.   DOUBLE       *pdblVal
  51.   =
  52.   VARIANT_BOOL *pboolVal
  53.   =
  54.   SCODE        *pscode
  55.   =
  56.   CY           *pcyVal
  57.   =
  58.   DATE         *pdate
  59.   =
  60.   BSTR         *pbstrVal
  61.   =
  62.   IUnknown     **ppunkVal
  63.   =
  64.   IDispatch    **ppdispVal
  65.   =
  66.   SAFEARRAY    **pparray
  67.   =
  68.   VARIANT      *pvarVal
  69.   =
  70.   PVOID        byref
  71.   =
  72.   CHAR         cVal
  73.   =
  74.   USHORT       uiVal
  75.   =
  76.   ULONG        ulVal
  77.   =
  78.   ULONGLONG    ullVal
  79.   =
  80.   INT          intVal
  81.   =
  82.   UINT         uintVal
  83.   =
  84.   DECIMAL      *pdecVal
  85.   =
  86.   CHAR         *pcVal
  87.   =
  88.   USHORT       *puiVal
  89.   =
  90.   ULONG        *pulVal
  91.   =
  92.   ULONGLONG    *pullVal
  93.   =
  94.   INT          *pintVal
  95.   =
  96.   UINT         *puintVal
  97. end type
  98.  
« Last Edit: December 26, 2018, 12:24:47 PM by o2admin »

jack

  • Guest
Re: Unions
« Reply #9 on: December 26, 2018, 01:28:12 PM »
thank you Charles Pegge :)
it will take some time study this nice example.

Brian Alvarez

  • Guest
Re: Unions
« Reply #10 on: December 26, 2018, 03:05:00 PM »
Charles said it all, and i think a VARIANT is a perfect example of use for unions. :)