Results 1 to 1 of 1

Thread: idea for stack-variables

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

    idea for stack-variables

    stack-variables could be very useful if they were built-in. of course one could use an array, but it has to be dynamic then, so for the meantime a stack on some udt-element would be difficult to realize, except one uses some dynamic string or heap memory.

    This example uses dynamic String, the only thing that i don't like about it that one has to pass a VarPtr on Stack.PushData() - but i could not find any other dynamic solution without having to pass something like MKx$() nor Memory_Get to be able to Push any type of data onto a stack.

    Uses "console"
    
    
    Type Stack
      sData As String
      sType As String
      lSize As Long
      
      As_Type  As Function
      
      PushData As Function
      PopData  As Function
      GetData  As Function
    
    End Type
    ' ---------------------------------------
    Function Stack.As_Type(ByVal sType As String) As String
      
      Me.sType = Ucase$(sType)  ' save type-names uniform, might be faster to compare
      
      Local data Like sType At 0  ' determine element-size
      Me.lSize = SizeOf(data)
      
      Function = Me.sType
      
    End Function
    
    ' ---------------------------------------
    
    Function Stack.PushData(ByVal pData As DWord) As Long   
                         ' "ByRef data" without any Type sadly not working here
      
      If Me.lSize = 0 Then Exit Function
      
      Me.sData += Memory_Get(pData, Me.lSize)
      
      ' returns number of elements stored on stack now
      
      Function = StrPtrLen(StrPtr(Me.sData))/Me.lSize
      
    End Function
    ' ---------------------------------------
    
    Function Stack.PopData() As Long
      ' returns number of elements left on stack
      
      
      If Not Me.lSize Then Return 0
      If StrPtrLen(StrPtr(Me.sData)) < Me.lSize Then Return 0
    
      If StrPtrLen(StrPtr(Me.sData)) = Me.lSize Then
        Me.sData = ""
      Else
        Me.sData = LEFT$(Me.sData, StrPtrLen(StrPtr(Me.sData)) - Me.lSize )    
      EndIf                                          
      
      Function = StrPtrLen(StrPtr(Me.sData))/Me.lSize
      
    End Function
    ' ---------------------------------------
    
    Function Stack.GetData() As String
      
      If Not Me.lSize Then Exit Function
      If StrPtrLen(StrPtr(Me.sData)) < Me.lSize Then Exit Function 
      
      Local data Like Me.sType At StrPtr(Me.sData) + StrPtrLen(StrPtr(Me.sData)) - Me.lSize
      Function = data
      
    End Function   
    ' ---------------------------------------------------------------------
    
    
    Dim foo As Stack
    Dim i   Like foo.As_Type "Long"
    
    For i = 1 To 50 Step 7
      foo.PushData(VarPtr i)   ' ... still not satisfied because have to pass pointer...
                        ' neither can be an expression here
    Next
    
    Do
      PrintL foo.GetData  
    Loop While foo.PopData
    
    PrintL
    
    Dim bac As Stack
    Dim d   Like bac.As_Type "Double"
    
    For d = 0.25 To 2.0 Step 0.25
      bac.PushData(VarPtr d)   ' but at least it allows any type this way
    Next 
    
    Do
      PrintL bac.GetData  
    Loop While bac.PopData
    
    WaitKey
    
    in the end i would like to type in just as simple as this:

    Stack foo As Long | Dim foo As Long Stack  
    
    Push foo 12345
    PrintL foo
    Pop foo
    
    '...or like a module:
    
    Dim bac As Long = Stack_Create [As |( ] Double | "Double" [)]
    Stack_Push( bac, 123.45 )
    Do
      PrintL Stack_Get( bac )
    Loop While Stack_Pop( bac )
    
    bac = Stack_Destroy( bac ) ' <<< assigns 0
    
    Last edited by ReneMiner; 17-11-2014 at 13:05.
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. A Stack of Quads
    By danbaron in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 4
    Last Post: 20-05-2010, 07:13
  2. Do you know: STATIC variables
    By ErosOlmi in forum Do you know ...
    Replies: 5
    Last Post: 05-08-2008, 11:03
  3. Allocating Local Workspace on the Stack
    By Charles Pegge in forum Machine Code: script sources and examples
    Replies: 0
    Last Post: 08-03-2008, 21:37

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
  •