time | Calls | line |
---|
< 0.001 | 47 | 1 | classdef (HandleCompatible,Abstract) HasPropertiesAsNVPairs
|
| | 2 | %
|
| | 3 |
|
| | 4 | % Copyright 2016-2018 The MathWorks, Inc.
|
| | 5 | methods (Access = protected)
|
| | 6 | function [obj, otherArgs] = parseInputs(obj,argsIn,priority)
|
| | 7 | p = getParserByClass(class(obj));
|
| | 8 | if iscell(argsIn)
|
| | 9 | p.parse(argsIn{:});
|
| | 10 | else % must be a struct.
|
| | 11 | p.parse(argsIn);
|
| | 12 | end
|
| | 13 |
|
| | 14 | % Deal the results to the converter.
|
| | 15 | results = p.Results;
|
| | 16 | defaults = string(p.UsingDefaults);
|
| | 17 | if exist('priority','var')
|
| | 18 | % Inputs are assigned in the order they appear in the argument list.
|
| | 19 | % To change this order, move any proprties which may have dependencies to
|
| | 20 | % the beginning of the list.
|
| | 21 | names = p.Parameters(:)';
|
| | 22 | names(any(names(:)==defaults(:)',2)) = [];
|
| | 23 | for i = numel(priority):-1:1
|
| | 24 | tomove = strcmpi(names,priority(i));
|
| | 25 | names = [names(tomove) names(~tomove)];
|
| | 26 | end
|
| | 27 | else
|
| | 28 | names = p.Parameters;
|
| | 29 | end
|
| | 30 |
|
| | 31 | for param = names
|
| | 32 | % only set the ones given by the user.
|
| | 33 | if ~any(param==defaults)
|
| | 34 | try
|
| | 35 | obj.(param{:}) = results.(param{:});
|
| | 36 | catch ME
|
| | 37 | throwAsCaller(ME)
|
| | 38 | end
|
| | 39 | end
|
| | 40 | end
|
| | 41 | otherArgs = p.Unmatched;
|
| | 42 | end
|
| | 43 |
|
| | 44 | function assertNoAdditionalParameters(~,fieldNames,name)
|
| | 45 | if ~isempty(fieldNames)
|
| | 46 | error(message('MATLAB:InputParser:UnmatchedParameter',...
|
| | 47 | fieldNames{1},...
|
| | 48 | getString(message('MATLAB:InputParser:ValidParameters',name))));
|
| | 49 | end
|
| | 50 | end
|
| | 51 |
|
| | 52 | end
|
| | 53 |
|
| | 54 | end
|
| | 55 |
|
| | 56 | % This creates a collection of parsers so the inputParser is
|
| | 57 | % generated only once per class
|
| | 58 | function p = getParserByClass(classname)
|
| | 59 | persistent map baseParser
|
| | 60 |
|
| | 61 | if isempty(map)
|
| | 62 | map = containers.Map('KeyType','char','ValueType','any');
|
| | 63 | baseParser = inputParser;
|
| | 64 | baseParser.PartialMatching = false;
|
| | 65 | baseParser.CaseSensitive = false;
|
| | 66 | baseParser.KeepUnmatched = true;
|
| | 67 | baseParser.StructExpand = true;
|
| | 68 | end
|
| | 69 |
|
| | 70 | if map.isKey(classname)
|
| | 71 | p = map(classname);
|
| | 72 | else
|
| | 73 | % create custom parser for this class
|
| | 74 | newParser = baseParser.copy();
|
| | 75 | newParser.FunctionName = classname;
|
| | 76 | % get public properties
|
| | 77 | props = unique(properties(classname))';
|
| | 78 |
|
| | 79 | % Add accessable properties given access by this mixin class
|
| | 80 | % This treats properties as NV pairs that might otherwise not be settable
|
| | 81 | % through the public interface.
|
| | 82 | me = meta.class.fromName(classname);
|
| | 83 | access = {me.PropertyList(:).GetAccess};
|
| | 84 | for i = 1:numel(access)
|
| | 85 | accessList = [access{i}];
|
| | 86 | if iscell(accessList)
|
| | 87 | accessors = [accessList{:}];
|
| | 88 | if any(strcmp('matlab.io.internal.mixin.HasPropertiesAsNVPairs',{accessors.Name}))
|
| | 89 | props{end+1} = me.PropertyList(i).Name; %#ok<AGROW>
|
| | 90 | end
|
| | 91 | end
|
| | 92 | end
|
| | 93 |
|
| | 94 | for p = props
|
| | 95 | newParser.addParameter(p{:},[]);
|
| | 96 | end
|
| | 97 | map(classname) = newParser;
|
| | 98 | p = newParser;
|
| | 99 | end
|
| | 100 |
|
| | 101 | end
|
| | 102 |
|