Referencing variables via pointers
#3
(10-21-2022, 03:58 PM)Pete Wrote: What's the point of using pointers when QB64 points the variables to C/C++, whch does the pointer work for you?

Point... I mean Pete

If you look at the first code, you'll see that I'm self-referencing my variables -- regardless of their type.  (As long as they're integers here, which is all that really makes sense to NOT between TRUE and FALSE states.)  For this simple demo, to showcase the proof-of-concept, it's not all that impressive.  I can hear you now, just muttering, "Why not just write a FUNCTION:"

Function Toggle&& (x as _INTEGER64)
    Toggle&& = NOT x
end Function

All well and good, but let's expand this concept to the next step.  Imagine creating a button on the screen which the user can use to toggle some options ON or OFF.  Easy enough, right?  I'm certain you've did it a dozen times in the past...

Now imagine that you need to toggle 10 different variables, and they're not the sort that fit nice in an array.  How do you work those up?  10 different SUBs, with one to manage each toggle??

How about one SUB that looks like the following:

Code: (Select All)
Sub ToggleVar (variable_offset As _Offset, variable_size As _Byte)
    $Checking:Off
    Static m As _MEM
    m = _Mem(variable_offset, variable_size)
    Select Case variable_size
        Case 1: _MemPut m, m.OFFSET, Not _MemGet(m, m.OFFSET, _Byte) As _BYTE
        Case 2: _MemPut m, m.OFFSET, Not _MemGet(m, m.OFFSET, Integer) As INTEGER
        Case 4: _MemPut m, m.OFFSET, Not _MemGet(m, m.OFFSET, Long) As LONG
        Case 8: _MemPut m, m.OFFSET, Not _MemGet(m, m.OFFSET, _Integer64) As _INTEGER64
    End Select
    $Checking:On
End Sub

Then you set up your Togglebox routine to look like:

Code: (Select All)
SUB ToggleBox (x, y, wide, high, var_offset as _Offset, var_size as _BYTE)
    If _mousebutton(1) and _mousex >= x and _mousex <= x + wide and _mousey >= y and _mousey <= y + high then
       'mouse button is down in the designated area
       Togglevar var_offset, var_size
    END IF
END SUB

So now you draw and caption your box as usual, but now you can have it reference an internal variable and NOT the value of that variable, without having to worry about types matching so you're passing via reference and not by value.

ToggleBox 100, 100, 200, 50, _Offset(foo), LEN(foo)   <-- that's all it'd take to turn box from 100,100 to 300,150, into a toggle for the variable foo.
ToggleBox 100, 100, 200, 50, _Offset(foo2), LEN(foo2)   <-- that's all it'd take to turn box from 100,100 to 300,150, into a toggle for the variable foo2.

You can use it to be certain to pass a variable by reference, regardless of type, rather than just passing by value.  Smile
Reply


Messages In This Thread
Referencing variables via pointers - by SMcNeill - 10-21-2022, 03:49 PM
RE: Referencing variables via pointers - by Pete - 10-21-2022, 03:58 PM
RE: Referencing variables via pointers - by SMcNeill - 10-21-2022, 09:28 PM
RE: Referencing variables via pointers - by Pete - 10-22-2022, 01:25 AM
RE: Referencing variables via pointers - by Pete - 10-23-2022, 03:03 PM



Users browsing this thread: 1 Guest(s)