Note that there are some explanatory texts on larger screens.

plurals
  1. POSimulate C++ template in MATLAB
    text
    copied!<p>I am trying to figure out the best way to create an alternative to the C++ template or the Java generic objects. I have wanted to do this several times in the past for several different reasons, however right now what I want to do is related to creating the saveobj and loadobj functions for several related classes. The idea is that I want to have a generic set of routines to create a default struct and then manipulate that a little bit farther to get the structs the way I want them.</p> <p>I cannot simply use an external function because I need access to all the public (not a problem) and protected (problem) non-transient properties of an object in order to create the loadobj and saveobj.</p> <p>Then I considered using an abstract interface. However, using an abstract interface leaves me with the same problem; identical, copy an pasted code floating around in all of my object files. So then I thought of using some sort of full blown object combined with multiple inheritance (Most of my objects are already inheriting from a basic concretion of interfaces). I thought using a superclass would allow me to expose the subclass protected properties, but it doesn't seem to work that way. Any suggestions?</p> <p>Here is a sample of the multiple inheritance approach (the closest thing I have so far) for the save obj approach.</p> <p>Serializer.m</p> <pre><code>% Serializer.m classdef Serializer methods function [saveObj] = saveobj( obj ) % Get metadata about the Object meta = metaclass( obj ); meta = meta.PropertyList; for p = meta' if p.Transient | p.Dependent continue; % Only serialize the correct fields end saveobj.(p.Name) = { obj.(p.Name) }; % Serialize end % for ( property ) end % function ( saveobj ) end % methods end % classdef ( Serializer ) </code></pre> <p>TestSerializerA.m</p> <pre><code>% TestSerializerA.m classdef TestSerializerA &lt; Serializer properties PropA = 'a'; end % properties ( public ) properties( Access = protected ) HiddenA = 'ha' end % properties ( protected ) end % classdef ( TestSerializerA ) </code></pre> <p>TestSerializerB.m</p> <pre><code>% TestSerializerB.m classdef TestSerializerB &lt; TestSerializerA &amp; Serializer properties PropB = 'b' end properties( Access = protected ) HiddenB = 'hb'; end % properties ( protected ) end % classdef ( TestSerializerB ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload