PDA

View Full Version : enumerating-thoughts



ReneMiner
22-09-2014, 14:11
there's often in a project a way needed to enumerate certain things globally which are all somehow unique properties or settings.

For example in my current GUI-project i save loads of ini-settings classified to be either measurements or colors (see GUI.tBasicU if nosy) but to make it short i like the way to abuse an UDT for enumerations

-just to give an example of what i talk about- i do this:


' "abuse" this udt a little bit

Type t_Defaultcolorscheme

DisabledBack As TBGL_TRGBA
DisabledFront As TBGL_TRGBA
DisabledText As TBGL_TRGBA
'... lots more...
ListHeaders As TBGL_TRGBA
ListHeaderFont As TBGL_TRGBA

End Type

Global Colors As t_Defaultcolorscheme

'access like
Colors.DisabledBack.R = 123

' another type...

Type t_Measurements
' these are mostly for setting up sizes as
MenubarHeight As Long
ScrollsClient As Long ' these is thickness of scrolls at t_Client-structure
ScrollsThick As Long ' default-scroll-thickness for controls
BorderWidth As Long ' border-width for controls - boxes have always 4!
' other user-defineables
KeyRepeatTime As Long ' default timeout for a key to repeat .OnKeyDown() when hold
TabKeySpaces As Long
TooltipDistance As Long
TooltipTime As Long
CursorBlinkTime As Long
CheckBoxOffset As Long ' add to text-width of checkeables

End Type

Global Measurements As t_Measurements
' access like:
Measurements.Menubarheight = 42 '...


but i don't like that it's possible to create another variable of that UDT - since it's supposed to be unique. But in general this way is much faster when coding in thinAir since one just have to look for the udt in codebrowser to get the full list of properties

There's another way too, pretty simple, just use a global array



' alternative:

Begin Const
%MenubarHeight = 1
%ScrollsClient
%ScrollsThick
%BorderWidth
' other user-defineables
%KeyRepeatTime
%TabKeySpaces
%TooltipDistance
%TooltipTime
%CursorBlinkTime
%CheckBoxOffset

%Last
End Const

Global Measurements(%Last-1) As Long

' access like
Measurements(%Menubarheight) = 42

but i don't like the parenthesis-stuff and neither the idea that each single const is a global variable on its own which not does even store the value but just is an index that makes it necessary for the interpreter to jump around just to get the needed values together...

now i dreamt of another way. Syntax just an idea... could name it Enumerate/End Enumerate instead of Begin Enum/End Enum too



Begin Enum Measurements As Long
MenubarHeight
ScrollsClient
ScrollsThick
BorderWidth
' other user-defineables
KeyRepeatTime
TabKeySpaces
TooltipDistance
TooltipTime
CursorBlinkTime
CheckBoxOffset
End Enum

Begin Enum Colors As TBGL_TRGBA
DisabledBack
'...
End Enum

to access as if it were an udt in the first example but of course an "Enumeration" can not have functions and they are somewhat like a fixed array (size) where each member has a name.


if thinAir get's some makeover some day the enumerations could be listed in codebrowser too.

I give that request a very low priority by not posting it in support yet - just to get the thought rollin'...

ReneMiner
23-09-2014, 21:13
none replies yet :( ?
none any time to discuss this - better ideas - it's good as it is?
but no denials neither :)
- however, it would make the coding a lot more fun for people that like pretty ordered structures (Like Me) and probably the code could be interpreted a lil' bit faster than using constants as indexes.

But i think the fun-factor ( fun during coding ) weighs much heavier since it's all passion-powered. The end-user of the scripted code probably won't have any additional fun nor ease on use...

maybe we could prefix €numerated "type-arrays" where each member has a name instead of an index alike



Enumerate €Colors As TBGL_TRGBA
Foreground
Background
'...
End Enumerate

€Colors.Foreground.R = 123 ' invalid op !!!

'...but as the current tB-"constants" we can assign this:
Memory_Copy( VarPtr(€Colors.Background.R), VarPtr(%RGB_MidnightBlue), 4 )
' to set the backcolor midnight-blue...
Memory_Set(VarPtr(€Colors.Foreground.R), MkByt$(255, 255, 255, 0))
' set foreground-color white...
'...
' use the foreground-color:
With €Colors.ForeGround : TBGL_Color .R, .G, .B : End With
'...


... it would allow to have "kind'a udt-constant-arrays" (tB-constants are no real constants as you surely know - you can poke new value at their varptr) ...

however i hope some of you think about it and tell some thoughts...

John Spikowski
24-09-2014, 04:28
Rene,

Is it possible to create a wrapper library of these low level hacks and still provide the functionality your looking for? This way you don't have to wait for Eros to catch up with you.

John

ReneMiner
24-09-2014, 10:12
as said, i abuse common udts - that works fine, but somehow does not feel right.
it disturbes me that one has to dim a variable of the type & the possibility to dimension more of that type isn't right neither. Of course one could simply use static udt-members, these are for sure the same values to all variables of that type but... i experienced static udt-members cannot be udts themselves...

ReneMiner
09-10-2014, 10:39
what I would like to do is this:



Enumerate myFlags As Long ' this is a unique udt
'...all members are same type
accept_Numeral Value &H00000001 ' accepts numbers
accept_Signed Value &H00000002 ' accepts one leading minus
accept_Decimal Value &H00000004 ' accepts . and , as a decimal-point
accept_Filename Value &H00000008 ' won't accept * :\/<|>?"
'...
End Enumerate

' not allowed/necessary to "Dim Flags As myFlags"
' since its unique use it directly like

If Me.Flags And myFlags.accept_Numeral Then...

Enumerate DefaultColors As TBGL_tRGBA
Forecolor Value Ini_GetKey(...)
BackColor Value MKByt$(128,0,255,0)
'...no changes outside this enumeration allowed
'... no functions and no statics allowed
End Enumerate

' no Dim X as DefaultColors needed/possible


not very spectacular on first sight and could use constants here also... but i guess if this gets managed through a new derivative of UDT, possibly just consisting of Statics ( as soon as they can hold UDTs- not fixed yet) it would be a step into the direction for the next idea i had & a good brain-training...

That were another kind of UDT that consists of one built-in thinBasic-variable -StrPtr for a string probably- but one numeral mostly.
It should only be allowed to have statics & functions and those statics shall be read-only (protected) outside a function of this UDT.



Unit myUnit As Dword ' - the udt is the size of 1 Dword here, this value is still public

Static [Protected by Default] X as something ' unchangeable but by Me
Init as Function
' only Statics & Functions allowed here...

Manipulate As Private Function ' future idea if until here works
End Unit

Dim abc as myUnit ' ... SizeOf(Dword)

ReneMiner
25-01-2015, 23:13
enumerating any names



#MINVERSION 1.9.15.0

' this to enumerate names - each name will get a unique identity = pointer
' at that pointer the name can be read out using heap_get()

' what do we need this for?

' imagine controls in a gui wearing names and all events for those controls
' will call a function composed of the controls name and the event...
' imagine you need to store type-names for dynamic dimensioning using "Like"
' and there's certainly a lot more names that can be enumerated.
' since each "name" only has the size of a pointer this allows uniform arrays
' things that wear the same name also have the same pointer if part of same enumeration
' = can simply compare pointer,
' don't need string comparison to see if two things have the same name




Type t_Enumeration
CaseSensitive As Boolean ' PUBLIC, set True in advance if needed

ptrs As DWord ' PRIVATE
Enumerate As Function
End Type

Function t_Enumeration.Enumerate(ByVal s As String, _
Optional ByVal TestExist As Boolean _
) As DWord

' If s does Not exist In This enumeration then
' If TestExist then
' Function will Return 0
' Else
' Function stores s
'

' returns pointer where s can be read out using Heap_Get()

Local i As Long

If StrPtrLen(StrPtr(s)) < 1 Then Return 0 ' minimum 1 char required

Local vPtr() As DWord At 0

If HEAP_Size(Me.ptrs) Then

ReDim vPtr(HEAP_Size(Me.ptrs)/4) At Me.ptrs

If Me.CaseSensitive Then
i = Array Scan vPtr Ptr, = s
Else
i = Array Scan vPtr Ptr, Collate Ucase, = Ucase$(s)
EndIf

If i Then
Return vPtr(i)
Else
If TestExist Then Return 0
EndIf
Else
If TestExist Then Return 0
EndIf

Me.ptrs = HEAP_ReAllocByStr(Me.ptrs, HEAP_Get(Me.ptrs) & MKDWD$(HEAP_AllocByStr(s)) )

Function = Peek( DWord, HEAP_End(Me.ptrs) - 3 )

End Function


' ------------------------------
' test

Uses "console"


Function TBMain()

Dim TypeName As t_Enumeration
Dim ControlName As t_Enumeration
Dim password As t_Enumeration

ftest TypeName.Enumerate("By" & "te")
ftest TypeName.Enumerate("Long")
fTest typeName.Enumerate("Double")
fTest typeName.Enumerate("bYTE") ' "Byte" has been enumerated already...


PrintL


Local radiobutton(7) As DWord Value ControlName.Enumerate("radiobutton") ' just example. usually store this in some udt-element...
Local exitbutton As DWord Value controlname.Enumerate("btn_Exit")
Local i As Long
For i = 1 To UBound(radiobutton)
PrintL "controls name: " & HEAP_Get(radiobutton(i)) & ", controls Index " & i & $TAB & "Ptr=" & radiobutton(i)
Next

PrintL

PrintL HEAP_Get(exitbutton) & $TAB & "Ptr = " & $CRLF & exitbutton
PrintL "request name again..."
PrintL controlname.Enumerate "btn_exit"


PrintL
' not case sensitive by default
password.CaseSensitive = TRUE

password.Enumerate("ZYX1234abc")

If Not password.Enumerate("zyx1234ABC", TRUE) Then
PrintL "password does not match"
EndIf

WaitKey
End Function

Function fTest(ByVal pTypename As DWord)

PrintL "Dim something Like " & $dq & HEAP_Get(pTypename) & $dq

End Function