Allow RegisterCallback with BoundFunc objects

Propose new features and changes
Elgin
Posts: 106
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: 1280
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: 6207
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Allow RegisterCallback with BoundFunc objects

23 Aug 2018, 22:45

This has already been implemented in v2 and will not be implemented in v1.
Helgef
Posts: 3295
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.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 7 guests