ReneMiner
03-10-2013, 15:23
suggest MKUDT$ As t_Type
The new type-functions brought me to some point where I always have to setup some constructor-functions to a type which not necessary needs to be a function of this type since no element of that type exists before one has been created.
I have some idea to skip writing those always in the same manner appearing functions. I use mostly heap to organize dynamic sub-arrays to the type or to extend types with different other types or to store strings within types - so not to have troubles with theirs pointers.
Now to have some multiple useable constructor I thought of something like this to get the first steps into the direction:
#MinVersion 1.9.9.0.
Uses "console"
' have some arbitrary, different types
Type t_Type1
A As Long ' whatever...
B As Byte
Out As Function
End Type
' --- this the "constructor-function" t_Type1 ---
Function MKUDT_As_t_Type1( Optional A As Long, _
B As Byte _
) As String
' simplest way:
Function = MKL$(A)+ MKBYT$(B)
End Function
' this is just a regular type-function to print values out
Function t_Type1.Out() As Long
PrintL "Me.A =" + Str$(Me.A)
PrintL "Me.B =" + Str$(Me.B)
PrintL
End Function
' other type
Type t_Type2
C As Double
D As DWord
E As Ext
Out As Function
End Type
' --- this the "constructor-function" t_Type2 ---
Function MKUDT_As_t_Type2( Optional C As Double, _
D As DWord, _
E As Ext _
) As String
' use another approach here
Local locData As t_Type2
locData.C = C
locData.D = D
locData.E = E
'same result:
Function = Memory_Get( VarPtr locData, SizeOf t_Type2 )
End Function
Function t_Type2.Out() As Long
PrintL "Me.C =" + Str$(Me.C)
PrintL "Me.D =" + Str$(Me.D)
PrintL "Me.E =" + Str$(Me.E)
PrintL
End Function
' using string-way:
String sData = MKUDT_As_t_Type1( -123, 255 )
Dim check1 As t_Type1 At StrPtr(sData)
check1.Out
sData = MKUDT_As_t_Type2( 1.23, ,4.5678 )
Dim check2 As t_Type2 At StrPtr(sData)
check2.Out
' using heap-way:
DWord hData = HEAP_AllocByStr(MKUDT_As_t_Type1(-246,128))
SetAt( check1, hData )
check1.Out
HEAP_Free hData
hData = HEAP_AllocByStr(MKUDT_As_t_Type2(2.46,&HFFFFFFFF, 9.8765))
SetAt( check2, hData )
check2.Out
PrintL $CRLF + Repeat$(50,"-")
PrintL "key to end" + $CRLF
WaitKey
further thinking would be to create another function which will put the data directly to heap (HEAP_AllocByType) and return the pointer - but currently MKUDT$ would be just the functionality it needs...
The new type-functions brought me to some point where I always have to setup some constructor-functions to a type which not necessary needs to be a function of this type since no element of that type exists before one has been created.
I have some idea to skip writing those always in the same manner appearing functions. I use mostly heap to organize dynamic sub-arrays to the type or to extend types with different other types or to store strings within types - so not to have troubles with theirs pointers.
Now to have some multiple useable constructor I thought of something like this to get the first steps into the direction:
#MinVersion 1.9.9.0.
Uses "console"
' have some arbitrary, different types
Type t_Type1
A As Long ' whatever...
B As Byte
Out As Function
End Type
' --- this the "constructor-function" t_Type1 ---
Function MKUDT_As_t_Type1( Optional A As Long, _
B As Byte _
) As String
' simplest way:
Function = MKL$(A)+ MKBYT$(B)
End Function
' this is just a regular type-function to print values out
Function t_Type1.Out() As Long
PrintL "Me.A =" + Str$(Me.A)
PrintL "Me.B =" + Str$(Me.B)
PrintL
End Function
' other type
Type t_Type2
C As Double
D As DWord
E As Ext
Out As Function
End Type
' --- this the "constructor-function" t_Type2 ---
Function MKUDT_As_t_Type2( Optional C As Double, _
D As DWord, _
E As Ext _
) As String
' use another approach here
Local locData As t_Type2
locData.C = C
locData.D = D
locData.E = E
'same result:
Function = Memory_Get( VarPtr locData, SizeOf t_Type2 )
End Function
Function t_Type2.Out() As Long
PrintL "Me.C =" + Str$(Me.C)
PrintL "Me.D =" + Str$(Me.D)
PrintL "Me.E =" + Str$(Me.E)
PrintL
End Function
' using string-way:
String sData = MKUDT_As_t_Type1( -123, 255 )
Dim check1 As t_Type1 At StrPtr(sData)
check1.Out
sData = MKUDT_As_t_Type2( 1.23, ,4.5678 )
Dim check2 As t_Type2 At StrPtr(sData)
check2.Out
' using heap-way:
DWord hData = HEAP_AllocByStr(MKUDT_As_t_Type1(-246,128))
SetAt( check1, hData )
check1.Out
HEAP_Free hData
hData = HEAP_AllocByStr(MKUDT_As_t_Type2(2.46,&HFFFFFFFF, 9.8765))
SetAt( check2, hData )
check2.Out
PrintL $CRLF + Repeat$(50,"-")
PrintL "key to end" + $CRLF
WaitKey
further thinking would be to create another function which will put the data directly to heap (HEAP_AllocByType) and return the pointer - but currently MKUDT$ would be just the functionality it needs...