How can I make such that every method call to a class is routed through a wrapper
9 views (last 30 days)
Show older comments
Is there a way to automatically wrap every method call to a class in a wrapper function in MATLAB? For example, suppose I have a class with methods method1 and method2 and a wrapper function wrapper_func. The wrapper_func should:
- Print the inputs and the name of the method.
- Call the actual method (e.g., method1).
- Print the outputs of the method.
When I create an object obj of this class and call obj.method1, it should implicitly call wrapper_func with method1 so that the wrapper function handles the input/output printing and method execution. How can this be achieved in MATLAB?
0 Comments
Accepted Answer
Aquatris
on 28 Jun 2024
Any reason why you are not using wrapper_func with an input argument that determines which method it should call? With this you can make method1 and method2 private, so noone but the class can call them and create a wraper_func public.
You might wanna switch to a switch case type of selection within the wrapper_func instead of dynamically defining the method.
classdef myClass < handle
methods (Access = private)
function y = method1(obj,a)
y = 13*a;
end
function y = method2(obj,a)
y = 25+a;
end
end
methods (Access = public)
function wrapper_func(obj,mtd,a)
fprintf('\t\t Calling method %s with input %d \n ',mtd,a)
y = obj.(mtd)(a);
fprintf('\t\t\t Output is %d \n ',y)
end
end
end
5 Comments
Steven Lord
on 2 Jul 2024
I am making a wrapper that should log all functions in an already existing class which is in use. The real class have around 30 methods which needs to be logged.
How are you hoping to make use of this log? Are you trying to determine how often functions are called in a particular piece of code for purposes of determining what to optimize? The MATLAB Profile may be able to help with this.
Alternately if you need this information in a run of some code that cannot (because it's not part of your normal workflow or because the profiling overhead is not acceptable for your workflow) run in the Profiler, the easiest approach would be to modify the methods that you want to log in the class to include a call to a helper function (which could be a local function in the class file, a private function, a function in a namespace, or a carefully named function on the MATLAB path; in that last case you shouldn't call it log) that performs the logging.
More Answers (1)
SACHIN KHANDELWAL
on 28 Jun 2024
You can achieve this in MATLAB by using a combination of class inheritance and method overloading. You'll need to create a base class that defines a generic method call handler, and then use this base class in your actual class to wrap me
Let's say we have created a superclass with two methods: "method1" and "method2".
classdef mySuperClass
methods
function output = method1(obj, input)
output = input * 2;
end
function output = method2(obj, input)
output = input + 10;
end
end
end
We have now created another class that inherits from the superclass. This means that the new class has access to the properties and methods of the superclass.
classdef myBaseClass < mySuperClass
methods
function wrapper_fcn(obj)
% apply your logic to handle method name etc.
output1 = obj.method1(3)
output2 = obj.method2(4)
end
end
end
I hope this is helpful.
Thanks!
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!