www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/+mbcutils/@propertycollection/propertycollection.m

    classdef (Hidden) propertycollection < mbcutils.abstractdataobject 
%PROPERTYCOLLECTION base property collection object for MCOS

    %  Copyright 2007-2013 The MathWorks, Inc.

    
    properties (SetAccess=protected,GetAccess=protected,Transient)
        ObjectProperties = struct;
    end
    properties (SetAccess=protected,GetAccess=protected)
        Converter
        Alias
    end
    
    properties (Hidden,GetAccess=protected)
        ExtraArguments = {};
    end
    
    methods (Hidden)
        function obj = propertycollection
        end
    end
    
    methods
        function [proplist,Options] = properties(OPT,writeable)
            %PROPERTIES list of object properties
            %
            % properties(P)   displays information about properties at the command-line
            % PropList = properties(P); 
            
            % [PropList,Options] = properties(P); 
            %     PropList:   a cell array of property names
            %     Options:    Options for properties

            if ~isscalar(OPT)
                error(message('mbc:doe:InvalidState27', class( OPT )))
            end

            if nargin<2
                writeable= false;
            end
            S = OPT.ObjectProperties;
            if ~isempty(S)

                f = fieldnames(S);
                Options = cell(size(f));
                r = false(size(f));
                for i=1:length(f)
                    prop = f{i};
                    p = S.(prop);
                    r(i)= ~p.isreadonly;
                    Options{i} = p.Options;
                end
                if writeable
                    f = f(r);
                end
            else
                f = {};
            end

            if nargout == 0
                disp(OPT)
                for i=1:length(f)
                    prop = f{i};
                    % display each property in turn
                    disp(S.(prop))
                end
            else
                proplist = f;
            end
        end
        
    end
    
    methods (Hidden)
        
        function varargout = subsref(obj,S)
            %SUBSREF subs reference for MBCUTILS.PROPERTIESCOLLECTION objects
            %
            %  Value = OPT.Property



            if strcmp(S(1).type,'{}')
                ME = MException('mbc:propertycollection:InvalidReference',...
                    '{} referencing is not defined for %s objects',class(obj));
                throwAsCaller(ME)
            end
            try
                % builtin subsref
                [varargout{1:nargout}] = builtin('subsref',obj,S);
            catch ME
                try
                    switch S(1).type
                        case '.'
                            if strcmp(ME.identifier,'MATLAB:noSuchMethodOrField')
                                % part of the object collection
                                prop = S(1).subs;
                                if isscalar(obj)
                                    v = mdotref(obj,prop);
                                    if length(S)>1
                                        [varargout{1:nargout}] = subsref(v,S(2:end));
                                    else
                                        varargout{1} = v;
                                    end
                                else
                                    % reference into object array
                                    [varargout{1:numel(obj)}] = mdotref(obj,prop);
                                    if length(S)>1
                                        error(message('mbc:propertycollection:extraneousStrucRefBlocks'))
                                    end
                                end 
                            else
                                rethrow(ME)
                            end
                        otherwise
                            % this handles res = obj(ind).collectionprop
                            v = builtin('subsref',obj,S(1));
                            [varargout{1:nargout}] = subsref(v,S(2:end));
                    end
                catch ME
                    throwAsCaller(ME)
                end
            end
        end

        
        function obj = subsasgn(obj,S,v)
            %SUBSASGN assignment for MBCUTILS.PROPERTIESCOLLECTION objects
            %
            %  OPT.Property = NewValue

            if strcmp(S(1).type,'{}')
                ME = MException('mbc:propertycollection:InvalidReference',...
                    '{} assignment is not defined for %s objects',class(obj));
                throwAsCaller(ME)
            end
            
            try 
                obj = builtin('subsasgn',obj,S,v);
            catch ME
                try
                    if any(strcmp(ME.identifier,{'MATLAB:noPublicFieldForClass','MATLAB:noSuchField','MATLAB:noSuchMethodOrField'}))
                        switch S(1).type
                            case '.'
                                % part of the object collection
                                prop = S(1).subs;
                                if length(S)>1
                                    res = subsref(obj,S(1));
                                    v = subsasgn(res,S(2:end),v);
                                end
                                obj = mdotasgn(obj,prop,v);
                            otherwise
                                % this handles obj(ind).collectionprop = val
                                res = subsref(obj,S(1));
                                v = subsasgn(res,S(2:end),v);
                                obj = builtin('subsasgn',obj,S(1),v);
                        end
                    else
                        rethrow(ME)
                    end
                catch ME
                    throwAsCaller(ME)
                end
            end
        end
        
        
        function P = addprop(P,Name,varargin)
            %ADDPROP add property to property collection
            %
            % P = addprop(P,Name,fGet,fSet,Type,Options,Description)

            S = P.ObjectProperties;
            if isfield(S,Name)
                % remove property if it exists so property appears at end of list
                S = rmfield(S,Name);
            end

            % make propertyinterface object and store it in ObjectProperties structure
            S.(Name) = mbcutils.propertyinterface(Name,varargin{:});
            P.ObjectProperties = S;
        end
        
        function [m,Changed] = copyprops(h,m)
            %COPYPROPS internal function
            %
            % m = copyprops(h,m)

            Changed = false;
            pmdl = h.Object;
            props = h.ObjectProperties;
            if ~isempty(props)
                f = fieldnames(props);
                for i=1:length(f)
                    p = props.(f{i});
                    [m,Ci] = p.copyprop(pmdl,m,h.ExtraArguments);
                    Changed = Changed || Ci;
                end
            end
        end
        
        function f = fieldnames(obj)
            %FIELDNAMES
            %
            % f = fieldnames(obj);

            % this causes properties to show up in tab completion
            f = properties(obj);
            f = [extendedfieldnames(obj);f];
        end
        
        function fn = extendedfieldnames(obj)
            %EXTENDEDFIELDNAMES private method
            %
            % fn = extendedfieldnames(obj)
            fn = properties(class(obj));
        end

        function OK = isequal(P1,P2)
            %ISEQUAL True if all the properties of P1 and P2 are equal
            %
            % OK = isequal(P1,P2)

            OK = strcmp(class(P1),class(P2));
            m1 = P1.Object;
            m2 = P2.Object;

            if OK && ~isempty(m1) && ~isempty(m2)
                pnames = properties(P1);
                OK = isequal(pnames,properties(P2));
                S = P1.ObjectProperties;
                for i=1:length(pnames)
                    p = S.(pnames{i});
                    OK = OK && p.isequalprops(m1,m2,P1.Converter,P1.ExtraArguments);
                end
            end
        end
            
        function OK = isprop(p,prop)
            %ISPROP remove property from propertycollection
            %
            % p= isprop(p,prop)

            OK = isfield(p.ObjectProperties,prop);
        end
        
        function P = redirect(P,fInternalGet,fInternalSet,props)
            %REDIRECT - redirect the get and set functions for the object properties
            %
            % P = redirect(P,fInternalGet,fInternalSet,props)
            %
            %     @(m) fInternalGet( fGet, m)
            %     @(m,v) fInternalSet( fSet, m, v );

            if nargin<4
                props = properties(P);
            end
            S = P.ObjectProperties;
            for i=1:length(props)
                S.(props{i}) = redirect(S.(props{i}),fInternalGet,fInternalSet);
            end
            P.ObjectProperties = S;
        end
        
        function p= rmprop(p,prop)
            %RMPROP remove property from propertycollection
            %
            % p= rmprop(p,prop)

            if isfield(p.ObjectProperties,prop)
                p.ObjectProperties = rmfield(p.ObjectProperties,prop);
            else
                error(message('mbc:mbcutils:propertycollection:InvalidProperty', prop));
            end
        end

        function s = structprops(p)
            %STRUCTPROPS puts all values into a structure
            %
            % s = structprops(p)

            s = [];
            f = extendedfieldnames(p);
            for i=1:length(f)
                prop = f{i};
                s.(prop) = p.(prop);
            end

            PROPS = p.ObjectProperties;
            if ~isempty(PROPS)
                f = fieldnames(PROPS);
                m = p.Object;
                for i=1:length(f)
                    prop = f{i};
                    s.(prop) = PROPS.(prop).getprop(m,p.Converter,p.ExtraArguments);
                end
            end
        end
        
        function display(p)
            %DISPLAY command window display for MODELPROPERTIES object

            isLoose = strcmp(get(0,'FormatSpacing'),'loose');
            if isLoose
                disp(' ')
            end
            if ~isempty(inputname(1))
                disp([inputname(1), ' = '])
            else
                disp('ans = ')
            end
            % get structure and display it

            if isscalar(p)
                disp(p)
                if ~isempty(p)
                    ps= structprops(p);
                    if ~isempty(ps)
                        disp(ps);
                    end
                else
                    disp(class(p))
                end
            else
                s = sprintf('%dx',size(p));
                fprintf('%s array of %s\n',s(1:end-1),class(p))
                disp(p)
            end
            if isLoose
                disp(' ')
            end
        end
        
        
        function [p,OK] = PersistentStore(p,ID)

            persistent PROPS DOSTORE

            ID = strrep([class(p),'_',ID],'.','');
            if nargout == 0 && DOSTORE
                PROPS.(ID) = p;
            elseif isfield(PROPS,ID)
                obj = PROPS.(ID);
                DOSTORE = isempty(p.ObjectProperties);
                OK = DOSTORE && IsReusable(p,obj);
                if OK
                    obj.Object = p.Object;
                    obj.ExtraArguments = p.ExtraArguments;
                    p = obj;
                end
            else
                DOSTORE = isempty(p.ObjectProperties);
                OK = false;
            end
        end
        
    end
        
    methods (Access= protected)

        function varargout = mdotref(obj,prop)
           
            if isscalar(obj)
                p = obj.ObjectProperties.(prop);
                % get property from propertyinterface
                varargout{1} = p.getprop( obj.Object , obj.Converter, obj.ExtraArguments); 
            else
                for i=1:numel(obj)
                    p = obj(i).ObjectProperties.(prop);
                    % get property from propertyinterface
                    varargout{i} = p.getprop( obj(i).Object , obj(i).Converter,obj(i).ExtraArguments);
                end
            end
        end
        
        function obj = mdotasgn(obj,prop,v)
            
            p = obj.ObjectProperties.(prop);
            % set main property
            obj.Object = p.setprop(obj.Object,v,obj.ExtraArguments);

        end
        
        
        function obj = pInitialise(obj,varargin)
            %PINITIALISE internal function to initialise fitalgorithm
            %
            % obj = pInitialise(obj,varargin);

        end
        
        function OK = IsReusable(p1,p2)
            OK = false;
        end
        
        function p = pGetInternalProp(obj,prop)
            %PGETINTERNALPROP private method
            %
            % p = pGetInternalProp(obj,prop)

            try
                p = obj.ObjectProperties.(prop);
            catch
                if ~isempty(obj.Alias)
                    % look in alias structure
                    p = obj.ObjectProperties.(obj.Alias.(prop));
                    % issue warning that an obsolete property name is being used.
                    warning(message('mbc:mbcmodel:Obsolete7a', prop, obj.Alias.(prop)));
                else
                    error(message('mbc:propertycollection:InvalidProperty', class( obj )));
                end
            end
        end
        
    end
    methods (Hidden,Static)
        function obj = loadobj(obj) 
            % reinitialise ObjectProperties
            obj = pInitialise(obj,obj.Object,obj.ExtraArguments{:});
        end
    end
end