Add listener programmatically for each object method
Hello there!
I have the following situation: Let’s say I have a class:
classdef Calculator < handle & matlab.mixin.Heterogeneous
methods
function obj = Calculator()
… constructor code
end
function val = veryExpensiveMethod(argsAsObj)
… some very time consuming code
end
function val = calledByListener(listenerArgs, methodName, argsAsObj)
… some code to check stuff
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.
Best regards and thanks a lot in advance!
TEHello there!
I have the following situation: Let’s say I have a class:
classdef Calculator < handle & matlab.mixin.Heterogeneous
methods
function obj = Calculator()
… constructor code
end
function val = veryExpensiveMethod(argsAsObj)
… some very time consuming code
end
function val = calledByListener(listenerArgs, methodName, argsAsObj)
… some code to check stuff
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.
Best regards and thanks a lot in advance!
TE Hello there!
I have the following situation: Let’s say I have a class:
classdef Calculator < handle & matlab.mixin.Heterogeneous
methods
function obj = Calculator()
… constructor code
end
function val = veryExpensiveMethod(argsAsObj)
… some very time consuming code
end
function val = calledByListener(listenerArgs, methodName, argsAsObj)
… some code to check stuff
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.
Best regards and thanks a lot in advance!
TE addlistener, listener, method call, programmatically create listener MATLAB Answers — New Questions