PDA

View Full Version : A way to get datatype-pattern?



ReneMiner
13-06-2013, 08:57
To make it short- is there a way to store/retrieve the pattern/structure of a datatype?

example:


' have two different types:
Type t_A
X as Byte
Y as Long
End Type

Type t_B
E as Ext
D as Double
End Type

' have some other type which can be a pointer to stored data of either type t_A or t_B
Type t_foo
theSubtype as ' unknown Type-Info
ptrSubType as Dword
End Type

Dim foo(2) as t_foo

foo(1).theSubtype = t_A
foo(1).ptrSubtype = Heap_Alloc(SizeOf(t_A)
foo(2).theSubtype = t_B
foo(2).ptrSubtype = Heap_Alloc(SizeOf(t_B)

'...call a sub/function which is part of some other, multiple usable .tBasicU
' to not too complicate it, this sub "knows" expression "foo" in this example

Sub InsideSub(Byval Index as Long)
' where index is foo's(Index)

Local data as foo(Index).theSubtype at foo(Index).ptrSubtype


End Sub


Now how can I replace/fill ".theSubtype" ? Is there a pointer or whatever to the type-defines/structures?

Can I use


thinBasic_VariableGetInfo()
'or
thinBasic_VariableGetInfoPtr()
'or
thinBasic_VariableGetInfoEx()

onto some dummy-variable of t_A or t_B
and how would that work to Dim As ... ?

Edit: other thought


' have two different types:
Type t_A
X As Byte
Y As Long
End Type

Type t_B
E As Ext
D As Double
End Type

' have some other type which can be a pointer to stored data of either type t_A or t_B
Type t_foo
theSubtype As Long
ptrSubType As DWord
End Type

Dim foo(2) As t_foo

' some empty dummy-array just borrow the name:
Dim bType(2) As Byte

Alias t_A As bType(1)
Alias t_B As bType(2)

' ---------------------------------------------------
' but no work: Alias only works on keywords...
' ---------------------------------------------------

foo(1).theSubtype = 1
foo(1).ptrSubtype = HEAP_Alloc(SizeOf(bType(foo(1).theSubtype)))
' ...

Sub InsideSub(ByVal Index As Long)
' where index is foo's(Index)

Local data As bType(foo(Index).theSubtype) At foo(Index).ptrSubtype

End Sub

ErosOlmi
16-06-2013, 11:37
Quite complex.
Let me some time to understand it better about current possibilities and future development.

If you were dealing with base data type like LONG, STRING, DWORD, EXT, ... not complex to develop something.
But in this examples you are dealing with user data types so all info must be catched at runtime.
... thinking.

ReneMiner
16-06-2013, 15:05
maybe is possible to think into a direction if there would be some TypeOf()-Function if there were a pointer to the storage-space (like SizeOf) of the UDT-pattern




Type t_A
Fish as Fin ' don't care what's in here
Bread as Dough
End Type

Type t_Foo
P_Type as Dword
P_Ptr as Dword
End Type

Dim foo() as t_Foo
'...
foo(1).P_Type = TypeOf(t_A)

' if this not possible, make some local dummy as UDT_ElementByte would need
' to read TypeOf from a variable:

Local dummy as t_A

foo(1).P_Type = TypeOf(dummy)

'...
Local data As foo(1).P_Type At foo(1).P_Ptr


but would that work already or need an additional clause as


Local data As UDT_Signature(foo(1).P_Type) At foo(1).P_Ptr

ErosOlmi
16-06-2013, 22:58
TypeOf is something I like

ReneMiner
07-07-2013, 15:23
Dig it out again because of "new idea"


Since you already accepted UDT_ListElements-function (http://www.thinbasic.com/community/project.php?issueid=401) as realizeable - we won't need a TypeOf()-function for UDTs now - maybe later.
So TypeOf should just return for any variable the usual vartypes or simple "it's Union/Udt" so one knows there are still elements to seperate.
It would be a great start and if this works the other stuff results from that base.



Type t_type1
A as Long
End Type

Type t_type2
T1 as t_Type1
B as Double
End Type
Dim foo as t_type2

so if TypeOf(foo.T1) returns "Is_UDT", I could seperate the elements using UDT_ListElements and thereafter check typeOf for each single element until I reach the "usual" vartypes. I think this would not be too much effort - in comparison how often this will be used - this method will be enough for the start. If we got the skeleton once the built-on-top-of-this stuff will follow through simple evolution :yes:

Edit: but has to "understand strings" - because thinBasic_VariableGetInfoEx does not respond to the subsets- so i could write


string x() = UDT_ListElements(T1)
'or
Dim x() as String
Long numElements = UDT_ListElements(T1, X)
...TypeOf("foo."+ x(1)) ...