Author Topic: Self-Referencing Customtype Library  (Read 3378 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Self-Referencing Customtype Library
« on: August 06, 2019, 05:16:09 am »
Just something rather neat that I thought I'd share, even if I haven't honestly sorted out an use for it (yet)...

Code: QB64: [Select]
  1. DECLARE CUSTOMTYPE LIBRARY 'Use Customtype for self-referencing a sub written inside your program
  2.     SUB SUB_EXAMPLE (BYVAL passed AS _OFFSET) 'this points to SUB EXAMPLE below, but uses an OFFSET to point to its parameter.
  3.     'NOTE:  The sub/function name *MUST* be the same as QB64 translates it as, for us.
  4.     'General rule of thumb is to make the subname ALL CAPS, preceeded by SUB_ or FUNCTION_ as dictated.
  5.  
  6.     SUB SUB_EXAMPLE2 (BYVAL passed AS _OFFSET)
  7.  
  8. TYPE DataType 'A datatype to use as an example
  9.     x AS STRING * 12
  10.     y AS LONG
  11.     z AS LONG
  12.  
  13. TYPE DataType2 'a second datatype
  14.     byte1 AS _UNSIGNED _BYTE
  15.     byte2 AS _UNSIGNED _BYTE
  16.     byte3 AS _UNSIGNED _BYTE
  17.     byte4 AS _UNSIGNED _BYTE
  18.     byte5 AS _UNSIGNED _BYTE
  19.     byte6 AS _UNSIGNED _BYTE
  20.     byte7 AS _UNSIGNED _BYTE
  21.     byte8 AS _UNSIGNED _BYTE
  22.     byte9 AS _UNSIGNED _BYTE
  23.     byte10 AS _UNSIGNED _BYTE
  24.     byte11 AS _UNSIGNED _BYTE
  25.     byte12 AS _UNSIGNED _BYTE
  26.     byte13 AS _UNSIGNED _BYTE
  27.     byte14 AS _UNSIGNED _BYTE
  28.     byte15 AS _UNSIGNED _BYTE
  29.     byte16 AS _UNSIGNED _BYTE
  30.     byte17 AS _UNSIGNED _BYTE
  31.     byte18 AS _UNSIGNED _BYTE
  32.     byte19 AS _UNSIGNED _BYTE
  33.     byte20 AS _UNSIGNED _BYTE
  34.  
  35.  
  36.  
  37. DIM m AS _MEM 'A memblock to store some information
  38. m = _MEMNEW(20) 'The proper size to fill the data type that we're interested in passing back to our program.
  39. _MEMPUT m, m.OFFSET, "Hello World" '12 bytes
  40. _MEMPUT m, m.OFFSET + 12, -2 AS LONG '4 more
  41. _MEMPUT m, m.OFFSET + 16, 3 AS LONG '4 more to make all 20
  42.  
  43. SUB_EXAMPLE m.OFFSET 'Call the sub with the offset to these 20 bytes of memory
  44. SUB_EXAMPLE2 m.OFFSET 'Notice, we passed the same block of memory, but are handling it differently here,
  45. '                            according to the paramters set in the second sub
  46.  
  47.  
  48.  
  49.  
  50.  
  51. SUB Example (t AS DataType) 'And here, we want to set up the actual sub to work with our example datatype.
  52.     PRINT t.x 'print the values of that memblock
  53.     PRINT t.y
  54.     PRINT t.z
  55.  
  56. SUB Example2 (x AS DataType2)
  57.     COLOR 12
  58.     PRINT x.byte1
  59.     PRINT x.byte2
  60.     PRINT x.byte3
  61.     PRINT x.byte4
  62.     PRINT x.byte5
  63.     PRINT x.byte6
  64.     PRINT x.byte7
  65.     PRINT x.byte8
  66.     PRINT x.byte9
  67.     PRINT x.byte10
  68.     PRINT x.byte11
  69.     PRINT x.byte12
  70.     PRINT x.byte13
  71.     PRINT x.byte14
  72.     PRINT x.byte15
  73.     PRINT x.byte16
  74.     PRINT x.byte17
  75.     PRINT x.byte18
  76.     PRINT x.byte19
  77.     PRINT x.byte20
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!