Add listener programmatically for each object method
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Steven Lord
2024-5-2
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.)
采纳的回答
Matt J
2024-5-2
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')
for i=1:numel(S)
if startsWith(S(i),whitespacePattern+"function")
S(i)=S(i)+"; notify(stuff);";
end
end
S
3 个评论
Matt J
2024-5-2
You're welcome, but if this is the solution you end up using, please Accept-click the answer.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Events 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!