How to access the n-th caller workspace?

63 vues (au cours des 30 derniers jours)
Jacorem
Jacorem le 22 Oct 2024
Commenté : Jacorem le 4 Nov 2024 à 10:41
I have a question concerning the problem represented by this diagram (black arrow represent function calls):
Where utilityClass.getOrigClasspath() should get access to the workspace of "mainClass.doStuffTroughtUtilityClass()" so to access the "obj" variable.
The code for "mainClass" is more or less:
classdef mainClass < handle
methods
function doStuffTroughtUtilityClass(obj)
utilityClass.doStuff1();
% and/or
utilityClass.doStuff2();
end
end
methods (Static)
function filePath=getObjPath()
mFullFile=mfilename("fullpath");
[filePath,~,~]=fileparts(mFullFile);
% + all the code to handle the different cases: deployed
% application, different OS, ...
end
end
end
and the one for the utilityClass:
classdef utilityClass
methods (Static)
function doStuff1()
origClassPath=utilityClass.getOrigClassPath();
% do some stuff
end
function doStuff2()
origClassPath=utilityClass.getOrigClassPath();
% do some other stuff
end
function pathOutMod=getOrigClassPath()
origClassPath=evalin("2-nd parent","obj.getObjPath()"); % this is the command I'm looking for
% and then I modify somehow the output
pathOutMod=sprintf("%s modified",origClassPath);
end
end
end
I would like to have the methods "doStuff1" and "doStuff2" in "utilityClass" because:
  1. I have several projects (that do different operations), each with the "getObjPath()" method (I like to use the same function name in case different functions of different classes should give similar results), and I would like to give all of them new functionalities (doStuff1 and doStuff2)
  2. it make sense that these new functionalities are managed by "utilityClass" (since they are related to such class)
So I would like to keep this structure.
The problem now is what I stated above: how to access the "mainClass.doStuffTroughtUtilityClass()" workspace from "utilityClass.getOrigClasspath()".
My idea was to use "evalin", but it is limited to the direct "parent" workspace (and the "base" one, but it does not help) and recursively calling it is discouraged (see evalin documentation) and it does not work in any case (I tried).
Do you have any idea how to dela with this problem?
The workaround I tought about is to pass "obj" at each function call, but I would like to avoid it for several reasons.
Thanks in advance,
Jacopo

Réponse acceptée

埃博拉酱
埃博拉酱 le 28 Oct 2024 à 11:47
  2 commentaires
Jacorem
Jacorem le 28 Oct 2024 à 12:54
Deart 埃博拉酱, actually dbstack may more or less do the trick. I will need some more time to testing it, especially in a deployed environment, but once done I will come back and, in case, accept the answer
Jacorem
Jacorem le 4 Nov 2024 à 10:41
Dear 埃博拉酱, I finally had time to test your solution and it seems to work. Thanks again for the suggestion!

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 27 Oct 2024 à 1:33
Since getObjPath() is a Static method, why not just do,
function pathOutMod=getOrigClassPath()
origClassPath=mainClass.getObjPath();
pathOutMod=sprintf("%s modified",origClassPath);
end
  1 commentaire
Jacorem
Jacorem le 28 Oct 2024 à 8:04
Dear Matt, the fact is that once I want to get the path of "mainClass", sometimes "mainClass2", other "mainClass3",... (all that has the same "getObjPath()" static method) so hard-coding it does not solve my problem

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by