Borrar filtros
Borrar filtros

Add listener programmatically for each object method

4 visualizaciones (últimos 30 días)
Thomas Ewald
Thomas Ewald el 30 de Abr. de 2024
Comentada: Thomas Ewald el 3 de Mayo de 2024
Hello there!
I have the following situation: Let's say I have a class:
classdef Memoizer < handle & matlab.mixin.Heterogeneous
methods
function obj = Memoizer()
... constructor code
end
function val = veryExpensiveMethod(obj, argsAsObj)
... some very time consuming code
end
function val = calledByListener(obj, methodName, argsAsObj)
... some code to check if method was called before and there are results cached
fcn_handle = str2func(methodName);
val = fcn_handle(argsAsObj);
end
end
end
Is it possible to add a listener to the specific method veryExpensiveMethod, where the listener will have access to the input argument argsAsObj of the method and is called automatically, without using notify()? The callback should be calledByListener. Essentially, the idea would be, that I can implement the method veryExpensiveMethod without thinking about firing events, but each call to the method will automatically fire such an event, and consequently call calledByListener.
I saw that you can add listeners to object properties (preGet, postGet, preSet, postSet) - which is essentially what I need, but I need it for a method call - e.g., preCalled.
In the example it is important that the class inherits from Heterogeneous. This is crucial, thus I cannot use RedefineDot operations in order to trigger events, which is not compatible with Heterogeneous. Anyways, dumping the Heterogeneous from the class definition would not solve the problem entirely, since RedefineDot is only triggered when object methods are called publicly (from the outside), but not if one method calls another method inside the object. In addition, RedefineDot does not have access to the method call's input arguments.
So, is it possible to have automatic/programmatic listener creation based on a method name, that creates a preCalled listener for a method? I can use the call stack, if that helps.
EDIT:
I forgot to add the object's reference as first input to all the methods. This, unfortunately, changes the requirements. The input argument argsAsObj is supposed to reflect the fact, that veryExpensiveMethod can be considered as a method whos entirety of input values can be passed as an object.
Best regards and thanks a lot in advance!
TE
  8 comentarios
Steven Lord
Steven Lord el 2 de Mayo de 2024
From this statement on the documentation page, I believe it will call isequaln to determine if the inputs are the same as a cached set of inputs but I'm not 100% certain.
"The input arguments are numerically equal to cached inputs. When comparing input values, MATLAB treats NaNs as equal."
I recommend contacting Technical Support to ask this question (and asking that they file an enhancement request for the documentation to describe how determining when to use the cache is handled when one of the inputs is an object.)
Thomas Ewald
Thomas Ewald el 2 de Mayo de 2024
Thanks for the comment, I will do that.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 2 de Mayo de 2024
In the project I am working on, many people contribute, and all would have to remember and implement this line.
It wouldn't be so hard for the project leader to write a code-modifying tool that inserts the notify() commands automatically, e.g.,
S=readlines('myclass.m')
S = 16x1 string array
"classdef myclass" "" " properties" " a" " end" " " " methods " " function func1(obj,argsObj)" "" " end" " function func2(obj,argsObj)" "" " end" "" " end" "end"
for i=1:numel(S)
if startsWith(S(i),whitespacePattern+"function")
S(i)=S(i)+"; notify(stuff);";
end
end
S
S = 16x1 string array
"classdef myclass" "" " properties" " a" " end" " " " methods " " function func1(obj,argsObj); notify(stuff);" "" " end" " function func2(obj,argsObj); notify(stuff);" "" " end" "" " end" "end"
  3 comentarios
Matt J
Matt J el 2 de Mayo de 2024
You're welcome, but if this is the solution you end up using, please Accept-click the answer.
Thomas Ewald
Thomas Ewald el 3 de Mayo de 2024
I will go for a slightly different implementation (instead of listener and notify, I go for direct method call to a method all childs inherit).
However, the answer solves the problem as requested, so I mark it as accepted answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Events en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by