Results 1 to 6 of 6

Thread: enumerating-thoughts

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171

    enumerating-thoughts

    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'...
    Last edited by ReneMiner; 22-09-2014 at 14:12.
    I think there are missing some Forum-sections as beta-testing and support

  2. #2
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171
    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...
    Last edited by ReneMiner; 23-09-2014 at 22:25.
    I think there are missing some Forum-sections as beta-testing and support

  3. #3
    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
    ScriptBasic Project Manager
    Project Site
    support@scriptbasic.org

  4. #4
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171
    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...
    I think there are missing some Forum-sections as beta-testing and support

  5. #5
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171
    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)
    
    Last edited by ReneMiner; 09-10-2014 at 11:15.
    I think there are missing some Forum-sections as beta-testing and support

  6. #6
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171
    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
    
    Last edited by ReneMiner; 25-01-2015 at 23:45.
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. Few thoughts on OpenCL, first custom problem solved
    By Petr Schreiber in forum OpenCL
    Replies: 1
    Last Post: 22-04-2010, 19:10
  2. Replies: 0
    Last Post: 08-08-2008, 04:36

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •