matlab - Call subclass static method from inside superclass static method -


i have large set of small, related classes linked interface class. classes implement static method, retrieves , processes data specific class.

the output of static method needs formatted in @ least 2 ways. since transformation of 1 format other same , rather trivial (though long), thought i'd implement concrete, sealed, static method in superclass.

however, run following problem:

% (in superclass.m) classdef superclass < handle      methods (static, abstract)         [arg1, arg2] = subsstaticmethod;                 end      methods (sealed, static)          function [other_arg1, other_arg2] = supersstaticmethod              % data here             [arg1, arg2] = (???).subsstaticmethod              % transform data here             % ...          end      end  end  % (in subclass.m) classdef subclass < superclass       methods (static)          function [arg1, arg2] = subsstaticmethod             % class-specific data here             % ...         end      end  end 

as far can see, calling subclass.supersstaticmethod() impossible design, because static methods need called using class name explicitly. in other words, there no way insert subclass name instead of (???) in superclass.supersstaticmethod above.

things i've tried:

  • mfilename('class') doesn't work, because returns 'superclass'
  • dbstack not contain information method being called subclass

i know can work around problem making supersstaticmethod non-static, , calling method on temporary instance (like subclass().supersstaticmethod()). or create small wrapper method inside each subclass calls superclass method mfilename('class') argument. or of 100 other things seem equally clumsy.

but i'd know if there meta.class trickery or can cleanly solve problem. i've found this dated thread, processes matlab command line programmatically subclass name.

however, classes used inside scripts/functions, , command line use going debugging purposes only...

any ideas?

below's hacky proposal. idea store current calling class in "static variable" of superclass, query field , use in feval call correct subclass' method. several notes:

  • it work long you're not doing computations in parallel (i.e. calling subclass#.supersstaticmethod more 1 thread @ time under shared memory architecture - in case calling class field overwritten erratically).
  • superclass's supersstaticmethod cannot sealed, though subclasses' versions can.
  • the "static variable" cleared after subclass.supersstaticmethod ensure method ever called subclass.
  • i've added matlab.mixin.heterogeneous superclass of superclass purpose of demonstration.

classdef superclass < handle & matlab.mixin.heterogeneous    properties (abstract = true, access = private, constant = true)     subclass@meta.class scalar;   end    methods (static, abstract)     [arg1, arg2] = subsstaticmethod;   end    methods (sealed, static)     function out = currentclass(input) % < closest thing in matlab static variable       persistent currentclass;       if nargout == 0 && nargin == 1 % "setter" mode         currentclass = input;         out = [];       else % "getter" mode         out = currentclass;       end           end   end    methods (static)      function [other_arg1, other_arg2] = supersstaticmethod        % i?       whoscalling = superclass.currentclass();       if isempty(whoscalling) || ~isa(whoscalling,'meta.class')         [other_arg1,other_arg2] = deal(nan);         return       else         whoscalling = whoscalling.name;       end        fprintf(1,'\ncalled from: %s\n', whoscalling);       % data here       [arg1, arg2] = feval([whoscalling '.subsstaticmethod']);        % transform data here       other_arg1 = arg1+arg2; other_arg2=[arg1(:);arg2(:)];       fprintf(1,'other_arg1: %s, other_arg2: %s\n',...                 num2str(other_arg1), mat2str(other_arg2));        % clear current class       superclass.currentclass([]);     end   end  end 

classdef subclass1 < superclass    properties (constant)     subclass@meta.class scalar = ?subclass1;   end    methods (static)         function [other_arg1, other_arg2] = supersstaticmethod       subclass1.currentclass(subclass1.subclass);       [other_arg1, other_arg2] = supersstaticmethod@superclass;     end      function [arg1, arg2] = subsstaticmethod       arg1 = -1; arg2 = -2;     end                end % static methods  end % classdef 

classdef subclass2 < superclass    properties (constant)     subclass@meta.class scalar = ?subclass2;   end    methods (static)         function [other_arg1, other_arg2] = supersstaticmethod       subclass1.currentclass(subclass2.subclass);       [other_arg1, other_arg2] = supersstaticmethod@superclass;     end      function [arg1, arg2] = subsstaticmethod       arg1 = 1; arg2 = 2;     end                end % static methods  end % classdef 

then can test this:

function q31269260  arr = [subclass1, subclass2]; ind1 = 1:numel(arr)   arr(ind1).supersstaticmethod; end %  arr.supersstaticmethod not work because elements treated "instances" of %  superclass, supersstaticmethod should not called directly. 

the output is:

called from: subclass1 other_arg1: -3, other_arg2: [-1;-2]  called from: subclass2 other_arg1: 3, other_arg2: [1;2] 

Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -