Operator like AddressOf?

Post

Posted
Rating:
#1 (In Topic #1992)
Avatar
Trainee
WillGhost is in the usergroup ‘unknown’

get Callback function's pointer

describled as /lang/extdecl - Gambas Documentation, about the "Function Callbacks" part. My question is:

for some reason If I have to declare "qsort" in a module (such as "mylib.module") :
Public Extern qsort(base As Pointer, nmemb As Pointer, size As Pointer, compar As Pointer) In "libc:6"
and call it on the FMain side, so, shall I put the callback function "Compare" in mylib.module or in FMain.class?
If put it in mylib.module, how do I get the "Compare" function's pointer from FMain like the VB6's AddressOf operator?
If put it in FMain.class it compiled success but gbr throw "Stack overflow" at run time.

Gambas version: 3.21.0
Operating system and version: Fedora 42
QT or GTK: GTK
 
Online now: No Back to the top

Post

Posted
Rating:
#2
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Trainee
WillGhost is in the usergroup ‘unknown’
Thank u   Gianluigi

    I saw the examples on the wiki page,  Take the second example, if Funzione_di_Callback() is declared in a module, then:

   if  the riceve(rit As Integer) is defined in the module, then from the Sub Main() how to get receive's point and pass it to Funzione_di_Callback() ?

   2.  if  the riceve(rit As Integer) is defined in Sub Main(), how can the Funzione_di_Callback()  access the Sub Main's pointer ?
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Enthusiast
Gianluigi is in the usergroup ‘Enthusiast’
Gianluigi is in the usergroup ‘GambOS Contributor’
Hi WillGhost

Let me start by saying I'm talking about things I don't know and don't use. You can find an explanation here:
Extern: richiamare funzioni esterne a Gambas - Gambas-it.org - Wikipedia

Since we're talking about things you know, and the code you're referring to works.
Also, since there's an explanation (albeit in Italian), you should have all your questions answered.

Otherwise, you'll need to better explain what you want to achieve, submitting some Gambas code and asking (those more expert than me) if they can help.

 :goodbye:
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
In gambas, just entering the call back function name from the module or class the conversion to pointer is handled by gambas.
The call back function must be declared public if I remember correctly and must be in the same module class that the call back is being set in.
Hope this helps a little.

I will try to make example with qsort in morning as an template.

before going any farther, gambas objects have a special function that lets you define the sort order called _compare, doing this is safer than trying to call external c functions as the data structures used in gambas my not match the type used in c.
Here is a script with an example using the gambas call back for sorting
You can cut between the lines and paste to a linux text file and make it executable to see the example. I will follow up with the qsort, but it is tricky
#!/usr/bin/env gbs3
' Gambas Script File Created 01/01/2026 00:43:05
'' how to implement custom sort in gambas
class myitem
property firstname as string use $firstname
property lastname as string use $lastname
public sub _new(firstn as string, lastn as string)
    $firstname = firstn
    $lastname = lastn
end

' return 0 if = 1 if > -1 if <
' we will sort by last name
public sub _compare(value as myitem) as integer
    if value.lastname = $lastname then return 0
    if $lastname > value.lastname then return 1
    return -1
end

public sub _formatted() as string
    dim buffer as string = left($lastname & space(35),35) & ", " & left($firstname&space(35),35)
    return buffer
end

end class

public sub main()
dim myarray as new myitem[]
for i as integer = 0 to 100
dim newitem as new myitem("bonney",format(str(rnd(999)),"00000")&"bunny")
myarray.add(newitem)
next
print "Before Sorting"
for each s as myitem in myarray
  print s._formatted()
next
dim mysorted as new myitem[]

mysorted = myarray.sort()
print "After Sorting"
for each s as myitem in mysorted
  print s._formatted()
next

'place code here
Quit 0
Catch
error "Script Error >";;error.text & "\n" & error.where
end

Last edit: by ercoupeflyer

Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
ercoupeflyer is in the usergroup ‘Regular’
ercoupeflyer is in the usergroup ‘Blogger’
Ok here is a script that calls the qsort, but I don't think it is a good idea as you must understand the internals of gambas and know how to access the array elements correctly
again, cut between the lines and paste into a linux text file, make it executable, you can run it.
But I must stress that doing this requires a good understanding of gambas architecture, I have no idea how well this would work with strings etc.
It really would be best to use the gambas _compare and define a class containing your data to sort.
#!/usr/bin/env gbs3
' Gambas Script File Created 01/01/2026 00:43:05

Extern qsort(base As Pointer, count As Integer, element_size As Integer, compfunc As Pointer) In "libc:6"

Public mylist As New Integer[]
For i As Integer = 0 To 100
  mylist.add(Rnd(999))
Next
printlist(mylist)

Sub printlist(thelist As Integer[])
For Each i As Integer In mylist
  Print i; ",";
Next
Print "end"
End

Public Sub compit(value1 As Pointer, value2 As Pointer) As Integer
   If Integer@(value1) > Integer@(value2) Then Return 1
   If Integer@(value1) < Integer@(value2) Then Return -1
   Return 0
End

Print "Starting the sort"
qsort(mylist.data, mylist.count, SizeOf(gb.integer), compit)
Print "ending the sort"
printlist(mylist)

Quit 0
Catch
Error "Script Error >";; error.text & "\n" & error.where

 
Online now: No Back to the top
1 guest and 0 members have just viewed this.