Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: ThinBASIC OOPification

  1. #11
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    vb uses Me for a form - so inside a vb-code-module "Me" refers to the "current window-code"-class. At least it was that way til vb 6.0 - what they do now? I don't know...
    I think there are missing some Forum-sections as beta-testing and support

  2. #12
    Compatibility with OxygenBasic Objects

    Oxygen can read and write thinBasic Object members, using Oxygen methods

    this. prefixes (optional) instead of me.
    bstring should be used for compatibility with thinBasic string

    This tests member alignment
    uses "oxygen"
    
    type vector3f
    x as single
    y as single
    z as single
    end type
    
    type color4f
    r as single
    g as single
    b as single
    a as single
    end type
    
    type ObjectClass
    pos as vector3f
    ang as vector3f
    col as color4f
    init as function
    act  as function
    end type
    
    function ObjectClass.init(v as single) as long
    me.ang.y=v
    end function
    
    function ObjectClass.act() as long
    msgbox 0,"Q.ang.y: " & me.ang.y
    end function
    
    dim Q(10) as ObjectClass
    Q(2).init(1.5)
    Q(2).act '1.5
    dim as long pt
    pt=varptr(Q(1))
    
    dim as string src
    
    src="
    type vector3f
      single x,y,z
    end type
    type color4f
      single r,g,b,a
    end type
    class ObjectClass
      pos as vector3f
      ang as vector3f
      col as color4f
      method init(single v)
      ang.y=v
      end method
      method act()
        print "Q.Ang.y: " ang.y '1.5
      end method
      method mulangy(single f)
        ang.y*=f
      end method
    end class
    '
    ObjectClass Q at *#pt
    Q(2).act
    Q(2).mulangy 10
    "
    
    o2_basic src
    if o2_errno then
      msgbox 0,o2_error
      stop
    else
      o2_exec
    end if
    
    Q(2).act '15
    
    PS: I'm using thinBasic in Ubuntu 12

  3. #13
    That's great, Charles & Eros.
    Works well here :

    (In some languages inside Object EndObject With Endwith -- you don't need the "dotting" , but that's only a "habit" imo)
    I like the idea of inheritance .. tried something and that copying and pasting is somewhat boring + (as Charles mentions) it are the methods determening the system-logic.


    thank you Rob
    Attached Files Attached Files

  4. #14
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    playing around for some sniff of "privacy" - very basic, incomplete, just a direction...
    #MINVERSION 1.9.12.0.
    
    Uses "console"
    
    Alias DWord As Heap 
    
    ' virtual object at heap...
    
    Type t_virtual_object
      hPtr As Heap
      
      Create As Function    
      Destroy As Function
      
    End Type
    
    Function t_virtual_object.Create() As Heap
    
      ' allocate default-space for any child-type
      Me.hPtr = HEAP_Alloc(SizeOf(Me))
      ' Memory_Set( Me.hPtr, VarPtr(Me), SizeOf(Me) ) ...not yet... need to store type of Me somehow...
      
      Return Me.hPtr
      
    End Function
    
    Function t_virtual_object.Destroy() As Heap
      
      If HEAP_Size(Me.hPtr) Then HEAP_Free(Me.hPtr) 
      Me.hPtr = 0
      Return 0
      
    End Function
    ' - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    Type t_hString Extends t_virtual_object
      Create  As Function
      SetText As Function        
      GetText As Function
      StrLen  As Function
    End Type
    
    Function t_hString.Create(Optional ByVal s As String ) As Heap
    
      If s <> "" Then Me.hPtr = HEAP_AllocByStr(s)
      Return Me.hPtr
    End Function
    
    Function t_hString.SetText(ByVal s As String) As Heap
      
      If HEAP_Size(Me.hPtr) Then 
        HEAP_Free(Me.hPtr)
        Me.hPtr = 0
      EndIf
      
      If s <> "" Then Me.hPtr = HEAP_AllocByStr(s)
      Return Me.hPtr
      
    End Function   
    
    Function t_hString.StrLen() As Long
      
      Return HEAP_Size(Me.hPtr)
    
    End Function
    
    Function t_hString.GetText() As String
      Return HEAP_Get(Me.hPtr)
      
    End Function
      
    Dim foo As t_hString
    If foo.Create("this is very important") Then
    
      PrintL foo.GetText()        
      PrintL "Length is " + foo.StrLen()
      PrintL
    
      foo.SetText("this is more funny")
      PrintL foo.GetText()
      PrintL "Length is " + foo.StrLen()
      PrintL
    
      foo.Destroy()
      PrintL "- check:"
      PrintL "Length is " + foo.StrLen()
      PrintL
    Else
      PrintL "should not happen"
    EndIf
    
    
    PrintL "----------------------------"
    PrintL "All done, press a key to end"
    WaitKey
    
    btw. I think it should be mentioned in the help file, section "thinBasic language\Data types and variables\Type" that functions can be part of a type too now.
    And maybe some remark in "thinBasic Modules\Core (thinBasic Core Engine)\UDT Functions" that these are not about user-defined functions of UDTs - or maybe add a clue/branch here.
    Last edited by ReneMiner; 16-03-2014 at 09:36.
    I think there are missing some Forum-sections as beta-testing and support

  5. #15
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    just for the idea: the "inheritance" has more ways of usage than just "oop".

    Imagine some GUI, controls as buttons, textboxes and more which now all can have a type of the actually needed size for that control. Previously there was only the Union-way - which is an enormous waste of memory if you consider a timer-control needs the same memory as some listview
    Now one can have base-controls requiring a minimum of memory and extend any of them with the needed control-specific properties and proceed them altogether in the base-types functions - that's really cool
    Last edited by ReneMiner; 17-03-2014 at 17:02.
    I think there are missing some Forum-sections as beta-testing and support

  6. #16
    Ah, thanks ReneMiner ..

    Can you explain about the union way ? (you mean they all have their private memory allocation ?).
    I'm a little bit familiar with FOOP (Lisp) -- you can copy objects and change the "bits" needed , but those not changed are linked by reference to the original. (not sure about the mechanism behind subclasses ... it's somewhat complicated (for me))

    best Rob

  7. #17
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    ok, imagine to have a type that shall serve to hold the data for all controls, might be dimensioned in an array. Somewhat like

    Type '...nay
    
    too cumbersome, just check out the attached unit file which is kind of small, incomplete TBGL-GUI-engine limited to one monospaced font
    To test-run try one of the templates then - has to be saved into the same folder. I did not finish because around that time Types in tB became functional so I wanted to redo it from scratch anyway and now having the posibillity to have different sized "childs" due to Extends there are totally new ways possible to handle this

    The things I mean you can see at TYPE t_CTRL_OWT which contains the "base-type" and check in codebrowser for a note "UNION" - very lowest -
    all controls finally have the same size but their individual data gets interpreted different.
    The point is, I had to use the same memory for any control and not to waste any space I had to use free/unused properties like .Checked sometimes in a control-specific different exceptional way, so xyz.Checked not always meant "xyz.Checked"... Now using Extends makes it possible each control to have exactly the data it needs = Much more overview & clean, readeable script-structure
    Attached Files Attached Files
    Last edited by ReneMiner; 18-03-2014 at 08:12.
    I think there are missing some Forum-sections as beta-testing and support

  8. #18
    Thanks Reneminer, I'll study it to catch up with all this !

    (vielen Dank) Rob

  9. #19
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    Using the new possibilities extensively I've experienced that there are often somewhere stored Indexes of array-elements are not accessable from within the type-function - so it's not possible to just retrieve some index of the current into some pattern/condition fitting element from a type-function and to compare this Index later within another function of that type.

    Anyway a variable could have some property which tells who in the world current Me exactly is. Now if there's no additional waste of memory for such identifier wanted or needed for each element - there is a simple way if the type we talk about is used for one array-variable only:

    Type t_Type
      Static current    As DWord  ' Static is same memory to all elements of that type
      someFunctionCheck As Function  
      otherFunctionDo   As Function
    End Type
    
    Function t_Type.someFunctionCheck( something As Any )
      If something And Me Then 
    
        Me.current = VarPtr(Me)
      EndIf
      
    End Function
    
    Function t_Type.otherFunctionDo()
    
      If Me.current = VarPtr(Me) Then
        ' Me gets a pretty pink hairbow...
      Else
        ' Me gets same old grey hat as all the others  
      EndIf
    
    End Function
    
    Dim foo(123)    As t_Type 
    
    '...maybe:
    Long i
    
    foo(1).Current = 0 ' delete old information
    While i < UBound(foo)
      i += 1
      foo(i).someFunctionCheck(whatever)
      If  foo(1).Current Then Exit While  ' found out the current matching one, so exit
    Wend
    
     '...to find out current foo,
    '...different ways thinkable
    
    For i = 1 to Ubound(foo)
    
      If Varptr(foo(i)) = foo(1).Current Then  ' could call a type-function instead
        ' we got a match...
      Else
       ' no match
      EndIf
    
    Next
    '...
    
    of course one can store the "current elements pointer" to some globals as well if the type is used for more than one array.
    So omit the static type member then and
    Dim current_foo as DWord
    Dim current_moo ...
    
    If use this way on dynamic used arrays make sure to do the checking after redim and before calling the function that uses the information about "current element" since varptrs could have changed.
    Anyway can make some local overlay to access current element instantly like
    Sub SomeSub()
    Local x as t_myType at current_foo
      With X 
       '...
      End With
    End Sub
    
    Last edited by ReneMiner; 03-04-2014 at 15:44.
    I think there are missing some Forum-sections as beta-testing and support

Page 2 of 2 FirstFirst 12

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
  •