Results 1 to 5 of 5

Thread: passing virtual type ByPtr?

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

    passing virtual type ByPtr?

    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
    
    Last edited by ReneMiner; 24-05-2013 at 21:28.
    I think there are missing some Forum-sections as beta-testing and support

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Maybe this solves the issue of passing multiple ~types using single generic parameter.

    How does it work?
    • You have specific types for specific controls, GadgetButton, GadgetLabel, ...
    • You have one master type, GadgetMultiUDT, which could contain the type of gadget + data, which have all gadgets in common + Union, which allows to access store/retrieve data for any type


    Little example here:
    ' -- Gadget types
    Begin Const
      %Gadget_TypeButton
      %Gadget_TypeLabel
    End Const
    
    ' -- Gadget specific data
    Type GadgetButton
      OnClickFunction As String
    End Type
    
    Type GadgetLabel
      IsMultiline     As Long
    End Type
    
    ' -- Union to collect all possible gadgets
    Union GadgetsAll
      gButton As GadgetButton
      gLabel  As GadgetLabel
    End Union
    
    ' -- UDT with type identifier
    Type GadgetMultiUDT
      gType   As Long   
      
      caption As String
      x       As Long
      y       As Long
      width   As Long
      height  As Long
       
      gData   As GadgetsAll
    End Type            
    
    Function TBMain()
      Dim myButton As GadgetMultiUDT      
      myButton.gType = %Gadget_TypeButton
      myButton.caption = "Click me"             ' -- Shared data
      myButton.x       = 5                        
      myButton.y       = 5
      myButton.width   = 100
      myButton.height  = 20
      myButton.gData.gButton.OnClickFunction = "myButton_OnClick" ' -- Control specific data
      
      Dim myLabel As GadgetMultiUDT      
      myLabel.gType   = %Gadget_TypeLabel       ' -- Shared data
      myLabel.caption = "Some text" + $CRLF + "And more of it"
      myLabel.x       = 5
      myLabel.y       = 5
      myLabel.width   = 100
      myLabel.height  = 20 
      myLabel.gData.gLabel.IsMultiline       = TRUE    ' -- Control specific data
      
      Gadget_DoSomething(myButton)
      Gadget_DoSomething(myLabel)
    End Function
    
    Function Gadget_DoSomething( g As GadgetMultiUDT ) ' -- You can pass any control here
    
      Select Case g.gType
        Case %Gadget_TypeButton   
          MsgBox 0, "It's a button: data = " + g.gData.gButton.OnClickFunction
          
        Case %Gadget_TypeLabel
          MsgBox 0, "It's a label: data = " + Format$(g.gData.gLabel.IsMultiline)
      End Select
    
    End Function
    

    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    So Unions can have different length - this not a problem nor waste of memory?
    I think there are missing some Forum-sections as beta-testing and support

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Quote Originally Posted by ReneMiner View Post
    So Unions can have different length - this not a problem nor waste of memory?
    Hi Rene,

    it is not exact to say Unions have different length but instead it is exactly the opposite:
    • an Union size is the size of the maximum element included inside the Union so all Unions of the same type have the same length
    • all elements inside an Union share the same memory area


    If inside an union you have a byte and a long, the union is 4 bytes (long)
    Union MyUnion
    b As Byte
    l as Long
    End Union
    The difference is how you interpret that memory:
    • when you access b (the single byte) you are changing a byte
    • when you access l (the single long, 4 bytes) you are changing all 4 bytes including b byte because the memory area is the same, you are just interpreting it in different ways.


    Yes, there is a little waste of memory when there are big differences of types inside the same Union but unless you have many many thousands of them, it is not something you have to consider.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    OK, I see now that united subsets don't need all the same length -there can be some leftover.
    Still I would like to hide all my variables and use heap instead of some global array for controls. I know it works with Dim At myPtr and it would be great if the function call could already do the "dim at" so the later user won't/can't fool around with my passed pointer to heap and gets some object-like variable presented
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. interesting demo of a 3D virtual world
    By zak in forum Shout Box Area
    Replies: 7
    Last Post: 15-07-2011, 13:57
  2. Here's a working virtual listview script
    By martin in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 12
    Last Post: 09-09-2009, 19:12
  3. Amazing video of Virtual City soon coming to the web
    By kryton9 in forum Shout Box Area
    Replies: 3
    Last Post: 14-08-2008, 11:25
  4. Microsoft Virtual PC 2007 SP1
    By ErosOlmi in forum Technology
    Replies: 3
    Last Post: 22-05-2008, 00:02
  5. Need to develop in Linux, need virtual stuff
    By Michael Hartlef in forum Shout Box Area
    Replies: 6
    Last Post: 12-03-2008, 00:47

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
  •