Allow RegisterCallback with BoundFunc objects Topic is solved

Propose new features and changes
Elgin
Posts: 111
Joined: 30 Sep 2013, 09:19

Allow RegisterCallback with BoundFunc objects

24 Mar 2015, 06:19

I've noticed that its currently possible to register func-objects with RegisterCallback but not object methods bound with ObjBindMethod.

It would be kind of nice to be able to use methods directly for callbacks without having to code a dummy function outside the class (that calls the method...).
For completeness: What also doesn't work right now is to register a bound func object created with func().Bind(), but I can't really think of a practical case where I would want to override parameters of the callback function...

The code below illustrates the current situation.

Many thanks,

Elgin

Code: Select all

WT:=new WinTest()
WT.StartF() ; use the func object for EnumWindowsProc
WT.Show()		; works
WT.StartFB() ; use the boundfunc object for EnumWindowsProc
WT.Show()		; does not work
WT.StartO()	; use the method EnumWindowsProc
WT.Show()		; does not work
return
    
EnumWindowsProcE(hwnd, lParam)
{
	global EnumMethod
	return %EnumMethod%(hwnd, lParam)
}

class WinTest
{
	Output:=""
	
	StartF()
	{
		global EnumMethod 
		this.Output:=""	
		EnumMethod:=ObjBindMethod(this, "EnumWindowsProc")
		EnumAddress := RegisterCallback(Func("EnumWindowsProcE"), "Fast")
		DllCall("EnumWindows", Ptr, EnumAddress, Ptr, 0)
	}
	
	StartFB()
	{
		global EnumMethod 
		this.Output:=""	
		EnumMethod:=ObjBindMethod(this, "EnumWindowsProc")
		EnumAddress := RegisterCallback(Func("EnumWindowsProcE").Bind(WinExist("A")), "Fast")
		DllCall("EnumWindows", Ptr, EnumAddress, Ptr, 0)
	}
	
	StartO()
	{
		this.Output:=""
		EnumAddress := RegisterCallback(ObjBindMethod(this, "EnumWindowsProc"), "Fast")
		DllCall("EnumWindows", Ptr, EnumAddress, Ptr, 0)
	}
	
	EnumWindowsProc(hwnd, lParam)
	{
			WinGetTitle, title, ahk_id %hwnd%
			WinGetClass, class, ahk_id %hwnd%
			if title
					this.Output .= "HWND: " . hwnd . "`tTitle: " . title . "`tClass: " . class . "`n"
			return true  ; Tell EnumWindows() to continue until all windows have been enumerated.
	}
	
	Show()
	{
		msgbox, % this.output
	}

}
tmplinshi
Posts: 1549
Joined: 01 Oct 2013, 14:57

Re: Allow RegisterCallback with BoundFunc objects

27 Nov 2015, 07:56

Bump..

Wish there is a RegisterCallbackForObject() function.
idontevenseethecode
Posts: 4
Joined: 12 Feb 2017, 16:37

Re: Allow RegisterCallback with BoundFunc objects

23 Aug 2018, 16:51

Bump! This would be fantastic for encapsulation.
lexikos
Posts: 6668
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Allow RegisterCallback with BoundFunc objects  Topic is solved

23 Aug 2018, 22:45

This has already been implemented in v2 and will not be implemented in v1.
Helgef
Posts: 4031
Joined: 17 Jul 2016, 01:02
Contact:

Re: Allow RegisterCallback with BoundFunc objects

24 Aug 2018, 02:30

It would be kind of nice to be able to use methods directly for callbacks without having to code a dummy function outside the class (that calls the method...).
You do not need a dummy function,
registercallback wrote:[v1.1.06+]: A function reference can be passed instead of a function name.
You pass the reference to the object via a_eventinfo, the first callback parameter is passed to this, basic exapmle.

Code: Select all

cc := new c("hello")
cb := registercallback(c.f,,,&cc)	; in real use, call objaddref and objrelease appropriately.
msgbox % dllcall(cb, "int", 37, "str", "world")	; callback

class c {
	__new(name){
		this.name := name
	}
	f(param2){
		local
		param1 := this
		this := object(a_eventinfo)
		msgbox % a_thisfunc "`n" this.name " " strget(param2)
		return param1
	}
}
Using a variadic method is also possible and have some advantages.

Also, I've attempted to make the v2 version available for v1, see here (limited testing).

Cheers.
idontevenseethecode
Posts: 4
Joined: 12 Feb 2017, 16:37

Re: Allow RegisterCallback with BoundFunc objects

25 Aug 2018, 19:33

@lexikos Good to know. I'm really looking forward to the post-Alpha release.
@Helgef Thanks so much, its working great.
iseahound
Posts: 478
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: Allow RegisterCallback with BoundFunc objects

02 Aug 2019, 16:39

Helgef wrote:
24 Aug 2018, 02:30
in real use, call objaddref and objrelease appropriately.
Hey Helgef, what exactly is the 4th parameter of RegisterCallback() doing? If I pass a reference to this as in

Code: Select all

	pWndProc := RegisterCallback(this.callback, "F",,&this)
Do I need to do anything further to guard against memory leaks? It seems that when the address to this is lost at the same time as this.callback(). Sorry if this doesn't make any sense, your example code (workaround?) was extremely helpful.
User avatar
nnnik
Posts: 4328
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Allow RegisterCallback with BoundFunc objects

02 Aug 2019, 17:16

The & parameter returns the object pointer.

He uses that pointer is his code to get the object in the callback.

Getting the pointer using the & operator does not affect reference counting (Use ObjAddref and ObjRelease to modify reference counting)

If the last reference to the object is removed the object is deleted regardless of any pointer to the object exists.

Trying to access a pointer that is invalid could have serious consequences.
Recommends AHK Studio

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 21 guests