How can I make such that every method call to a class is routed through a wrapper

7 views (last 30 days)
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:
  1. Print the inputs and the name of the method.
  2. Call the actual method (e.g., method1).
  3. 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?

Accepted Answer

Aquatris
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
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.
Alexander
Alexander on 9 Jul 2024
Sorry for the late answer, i did not get a notice.
I am implementing a wrapper to log every function call, including inputs and outputs, to a text file. The purpose is to keep track of each function call so that if an error occurs, I can review the log to identify exactly where it went wrong.
Currently, I have the wrapper in a separate helper class. Modifying each method individually to include logging would be time-consuming because there are many methods, and future users of my logging class would also need to understand and implement these changes themselves.
My current approach involves a wrapper that logs method calls by using the syntax wrapper("methodname", inputnames, outputnames, inputs). While this provides the desired functionality, it complicates the code readability for others and alters the method call format from methodname(...) to wrapper("methodname", inputnames, outputnames, ...).
The desired functionality would be a wrapper that does not change the method call, is easy to implement in new classes and saves every function call, including inputs and outputs to a text file.

Sign in to comment.

More Answers (1)

SACHIN KHANDELWAL
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!
  1 Comment
Alexander
Alexander on 28 Jun 2024
Thank you for you respsonse.
It feels like this solution has the same porblem as the first. I need that the call should stay the same. If i create a object from my class, test_object, i want to call the wrapped methods as test_object.method1. I dont want to have to call the wrapper function from outside the class.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!