Post by (X) on Jun 4, 2023 14:35:21 GMT 1
Can you save a variable of your own User Defined Type to a Hash Table?
Apparently not. Well, not directly.
Here is a work around...
'#################################################################################
'
' DISCLAIMER: THIS CODE IS OFFERED AS IS FOR EDUCATIONAL PURPOSES.
' YOU ARE FREE TO USE, COPY AND DISTRIBUTE.
' THE USE OF THIS CODE IS AT YOUR OWN RISK.
' I ACCEPT NO LIABILITY NOR MAKE ANY CLAIM OF
' CORRECTNESS OR SUITABILITY IN PART OR IN WHOLE.
'
'#################################################################################
'
' FILENAME Demo UDT Hash.G32
'
' DESCRIPTION How can you assign a UDT record in a Hash that won't accept UDTs?
' Here is a proof of concept that could be further streamlined for
' practical use.
'
' AUTHOR (X)
' EMAIL xman.gb32@gmail.com
' WEBSITE https://gfabasic32.blogspot.com/
' FORUM https://gb32.proboards.com/
' STARTED 2023-06-04
' UPDATED <ISO date>
'
'
$Library "gfawinx"
$Library "UpdateRT"
UpdateRuntime
Mode Date "-"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' How to store any data type in a Hash?
'
Type T_x
x As Long
EndType
Trace Len(T_x)
Dim p As T_x
p.x = 65
Trace p.x
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Create an instance of a Hash Table of type string: hx
'
Dim hx As Hash String
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The string will store the data as a byte array.
'
Dim s$ = Space(Len(T_x))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Move the record data of T_x typed variable: p, to a string: s$
' (A string is essentially a char or byte array)
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Before the data transfer
'
Debug "s dst", HexDump(V:s, 16)
Debug "p src", HexDump(V:p, 16)
//
// N.B.: In this version of GFA (2.6.2.2626), the MemMove help popup indicates
// srcAddr, dstAddr, Count in the wrong order. Info sent to GFA 2023-06-04
// In the Help file the actual order of parameters: dstAddr, srcAddr is correct.
//
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' dst, src, cnt
MemMove V:s, V:p, Len(T_x)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' After the data transfer
'
Debug "s dst", HexDump(V:s, 16)
Debug "p src", HexDump(V:p, 16)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Set the string to a Hash Table
'
Hash Add hx["test"], s$
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Get the string from the Hash Table
'
Dim s2$ = hx["test"]
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Declare a new T_x typed variable...
'
Dim p2 As T_x
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Transfer the byte data to a new T_x typed variable: p2.
'
MemMove V:p2, V:s2, Len(T_x)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' And, voilĂ !
' A variable of User Defined Type was stored and retrieved from a Hash Table!
'
Trace p2.x
Debug Output...
TRACE:(1):Len(T_x) = 4
TRACE:(2):p.x = 65
s dst 07BACC44: 20 20 20 20 00 65 52 75 6E 74 69 6D 65 00 20 20 .eRu ntime.
p src 06321AC0: 41 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 A....... ........
s dst 07BACC44: 41 00 00 00 00 65 52 75 6E 74 69 6D 65 00 20 20 A....eRu ntime.
p src 06321AC0: 41 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 A....... ........
TRACE:(3):p2.x = 65
TRACE:(2):p.x = 65
s dst 07BACC44: 20 20 20 20 00 65 52 75 6E 74 69 6D 65 00 20 20 .eRu ntime.
p src 06321AC0: 41 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 A....... ........
s dst 07BACC44: 41 00 00 00 00 65 52 75 6E 74 69 6D 65 00 20 20 A....eRu ntime.
p src 06321AC0: 41 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 A....... ........
TRACE:(3):p2.x = 65