function schema
%SCHEMA   Define the hdlmdlprops class.

%   Copyright 2005-2018 The MathWorks, Inc.
%     

mlock;

hPackage = findpackage('slprops'); 
hThisClass = schema.class(hPackage,'hdlmdlprops');

% Property definitions

% The model-saving code sets this property to "true" while saving any
% object that has it.
p = schema.prop(hThisClass, 'isSavingModel',  'bool');
p.Visible = 'Off';
p.AccessFlags.Serialize = 'off';

% The model-loading code sets this property to "true" while loading any
% object that has it.
p = schema.prop(hThisClass, 'isLoadingModel',  'bool');
p.Visible = 'Off';
p.AccessFlags.Serialize = 'off';

p = schema.prop(hThisClass, 'mdlProps',  'mxArray');
p.getFunction = @i_getMDLProps;
p.setFunction = @i_setMDLProps;

function val = i_getMDLProps(obj,val)
    if obj.isSavingModel && ~isempty(val)
        % Replace the model name with "$bdroot" so that it still
        % refers to the file's own model even if the file gets renamed.
        s = cellfun(@ischar,val);
        strs = val(s);
        strs = regexprep(strs,['^' bdroot '$'],'$bdroot');
        strs = regexprep(strs,['^' bdroot '\/'],'$bdroot/');
        val(s) = strs;
    end
end

function val = i_setMDLProps(obj,val)
    if obj.isLoadingModel && ~isempty(val)
        % The file may contain the placeholder "$bdroot" - replace that
        % with the model name if we find it.
        s = cellfun(@ischar,val);
        strs = val(s);
        strs = regexprep(strs,'^\$bdroot$',bdroot);
        strs = regexprep(strs,'^\$bdroot\/',[bdroot '/']);
        val(s) = strs;
    end
end

end
