classdef PortValue < modelpack.VariableValue
    %modelpack.PortValue class
    %   modelpack.PortValue extends modelpack.VariableValue.
    %

    %    modelpack.PortValue properties:
    %       Description - Property is of type 'ustring'
    %       Name - Property is of type 'string'  (read only)
    %       Value - Property is of type 'MATLAB array'
    %
    %    modelpack.PortValue methods:
    %       getID - Returns the handle of the identifier object associated with THIS.
    %       getName - Returns the name of the port.
    %       setID - Sets the identifier object associated with THIS.
    %       setName - Sets the name of the port.

    % Copyright 2000-2023 The MathWorks, Inc.
       
    properties (Access=protected, SetObservable)
        %ID Property is of type 'handle'
        ID = [];
    end

    properties (SetAccess=protected, SetObservable)
        %NAME Property is of type 'string'  (read only)
        Name = '';
    end

    properties (SetObservable)
        %VALUE Property is of type 'MATLAB array'
        Value = [];
    end


    methods  % constructor block
        function this = PortValue(portID, subsName)
            % PORTVALUE Constructor
            %
            % This constructor takes a port identifier object and an optional subscripted
            % name to create a PORTVALUE object.
            %
            % h = modelpack.PortValue(portID, [subsName]);
            %
            % PORTID   a PortID object.
            % SUBSNAME is the optional subscripted port name; otherwise set to empty.

            % Create object

            % No argument constructor call.
            ni = nargin;
            if (ni == 0)
                return
            end

            % Set the default value to empty.
            if (ni < 2 || isempty(subsName)), subsName = ''; end

            % Set properties.
            this.Version = 1.0;
            this.setID(portID);
            this.setName(subsName);

        end  % PortValue

    end  % constructor block

    methods
        function set.ID(obj,value)
            % DataType = 'handle'
            validateattributes(value,{'handle'}, {'scalar'},'','ID')
            obj.ID = value;
        end

        function set.Name(obj,value)
            % DataType = 'string'
            validateattributes(value,{'char'}, {'row'},'','Name')
            obj.Name = value;
        end

        function set.Value(obj,value)
            obj.Value = LocalSetValue(obj,value,'Value');
        end

    end   % set and get functions

    methods  % public methods
        function ID = getID(this)
            % GETID Returns the handle of the identifier object associated with THIS.

            ID = get(this, {'ID'});
            ID = cat(1, ID{:});

        end  % getID

        function name = getName(this)
            % GETNAME Returns the name of the port.

            name = get(this, 'Name');

        end  % getName

        function this = setID(this, ID)
            % SETID Sets the identifier object associated with THIS.

            % Type checking
            if ~isa(ID, 'modelpack.PortID')
                error(message('SLControllib:modelpack:errArgumentType', ...
                    'ID', 'modelpack.PortID' ));
            end

            % Handle vectorized call
            for ct = 1:numel(this)
                h  = this(ct);
                id = copy( ID(ct) );

                h.ID = id;
                h.setName( id.getName );
            end

        end  % setID

        function this = setName(this, name)
            % SETNAME Sets the name of the port.

            % Type checking
            if ~ischar(name)
                error(message('SLControllib:modelpack:InvalidStringArgument', ...
                    'NAME', 'setName' ));
            end

            this.Name  = modelpack.utCheckVariableSubsref(this.getID, name);
            this.Value = 0;

        end  % setName

    end  % public methods

end  % classdef

function value = LocalSetValue(this, value, pname)
if ~any( isnan(value(:)) )
    value = modelpack.utValidateValue(this, value, '(');
else
    error(message('SLControllib:general:RealDoubleArrayValue', pname ));
end
end  % LocalSetValue

function str = LocalMat2Str(value, N)
try
    str = mat2str(value, N);
catch
    s1  = regexprep( num2str(size(value)), ' *', 'x' );
    str = sprintf('[%s %s]', s1, class(value));
end
end  % LocalMat2Str

% LocalWords:  PORTID SUBSNAME
