ReneMiner
24-05-2013, 10:46
Topic sounds stark mad, but I'll try to explain:
I have some UDT as Gadgets (GUI-Controls). Since not every control has the same properties I want to make some pointer on my Gadget-Type that points to the data of the single Gadgets. Example:
Type t_Gadget
sName as String
'...the Main-Type
pData as Dword
'....
End Type
Dim Gadget() as t_Gadget
Type t_Timer
' example Sub-Type of some Gadget:
Interval As DWord
TimeOut As DWord
End Type
Now when it comes to some event, for example a timers time is up i want to call a user-sub as this
Call_IfExists Gadget(X).sName + "_TimeIsUp" ( Gadget(X).pData )
' now the sub should work as this:
Sub myTimer_TimeIsUp( ByPtr lTimer As t_Timer )
' now inside this sub I would like to have lTimer as virtual memory-overlay at byval Gadget(X).pData as t_Timer
End Sub
I know, I could just setup the sub as this:
Sub myTimer_TimeIsUp( Byval myPtr as Dword )
Local lTimer as t_Timer At myPtr
'....
but I would like to hide all that "complicated" stuff from the user on the one hand, on the other I don't like to create an unnecessary local variable first (byval myPtr) which will neutralize the speed-advantage of virtual overlay again.
Edit-Extended thoughts:
Type t_someFixedSizeType
A as Long
B as Byte
C as Double
End Type
Dim myHeap As Dword = Fill_In_Data(0, 1, 2, 3.5)
myHeap = Fill_In_Data(myHeap, 2, 3, 4.5)
myHeap = Fill_In_Data(myHeap, 3, 4, 5.6)
'...
ShowMyData(myHeap)
'...
Function Fill_In_Data(Byval oldPtr as Dword, Byval A as Long, byval B as Byte, byval C as Double) as Dword
Local lData as t_someFixedSizeType
lData.A = A
lData.B = B
lData.C = C
If oldPtr = 0 Then
Function = Heap_AllocByStr( Peek$(VarPtr(lData), SizeOf(t_someFixedSizeType)) )
Else
Local sData as String = Peek$(oldPtr, Heap_Size(oldPtr)) + Peek$(VarPtr(lData), SizeOf(t_someFixedSizeType))
Local newPtr = Heap_ReAlloc( oldPtr, len(sData) )
if newPtr Then Poke$(newPtr, sData)
Function = newPtr
Endif
End Function
Sub ShowMydata( ByHeap something() As t_someFixedSizeType )
' inside sub now have a virtual array of someFixedSizeType placed
' over passed Heap-Pointer with element-count of Heap_Size()/SizeOf(t_someFixedSizeType)
' if no parenthesis behind something means single element only - which is the same as ByPtr...
local i as Long
For i = 1 to UBound(something)
Print Something(i).A
Print Something(i).B
Print Something(i).C
Next
End Sub
I have some UDT as Gadgets (GUI-Controls). Since not every control has the same properties I want to make some pointer on my Gadget-Type that points to the data of the single Gadgets. Example:
Type t_Gadget
sName as String
'...the Main-Type
pData as Dword
'....
End Type
Dim Gadget() as t_Gadget
Type t_Timer
' example Sub-Type of some Gadget:
Interval As DWord
TimeOut As DWord
End Type
Now when it comes to some event, for example a timers time is up i want to call a user-sub as this
Call_IfExists Gadget(X).sName + "_TimeIsUp" ( Gadget(X).pData )
' now the sub should work as this:
Sub myTimer_TimeIsUp( ByPtr lTimer As t_Timer )
' now inside this sub I would like to have lTimer as virtual memory-overlay at byval Gadget(X).pData as t_Timer
End Sub
I know, I could just setup the sub as this:
Sub myTimer_TimeIsUp( Byval myPtr as Dword )
Local lTimer as t_Timer At myPtr
'....
but I would like to hide all that "complicated" stuff from the user on the one hand, on the other I don't like to create an unnecessary local variable first (byval myPtr) which will neutralize the speed-advantage of virtual overlay again.
Edit-Extended thoughts:
Type t_someFixedSizeType
A as Long
B as Byte
C as Double
End Type
Dim myHeap As Dword = Fill_In_Data(0, 1, 2, 3.5)
myHeap = Fill_In_Data(myHeap, 2, 3, 4.5)
myHeap = Fill_In_Data(myHeap, 3, 4, 5.6)
'...
ShowMyData(myHeap)
'...
Function Fill_In_Data(Byval oldPtr as Dword, Byval A as Long, byval B as Byte, byval C as Double) as Dword
Local lData as t_someFixedSizeType
lData.A = A
lData.B = B
lData.C = C
If oldPtr = 0 Then
Function = Heap_AllocByStr( Peek$(VarPtr(lData), SizeOf(t_someFixedSizeType)) )
Else
Local sData as String = Peek$(oldPtr, Heap_Size(oldPtr)) + Peek$(VarPtr(lData), SizeOf(t_someFixedSizeType))
Local newPtr = Heap_ReAlloc( oldPtr, len(sData) )
if newPtr Then Poke$(newPtr, sData)
Function = newPtr
Endif
End Function
Sub ShowMydata( ByHeap something() As t_someFixedSizeType )
' inside sub now have a virtual array of someFixedSizeType placed
' over passed Heap-Pointer with element-count of Heap_Size()/SizeOf(t_someFixedSizeType)
' if no parenthesis behind something means single element only - which is the same as ByPtr...
local i as Long
For i = 1 to UBound(something)
Print Something(i).A
Print Something(i).B
Print Something(i).C
Next
End Sub