classdef (Hidden = true) IntermediateTarget < handle
% Class that represents an intermediate target.
    
% Copyright 2012-2020 The MathWorks, Inc.
    
    properties
        TargetType
        
        % <PATH>/<NAME>/<EXT> : <DEP_PATH>/<DEP_NAME>/<DEP_EXT>
        %   <PREMAIN RULES>
        %   <MAIN RULE> <MODULES>
        %   <POSTMAIN RULES>
        
        TargetPath
        TargetName
        TargetExtension
        
        DependencyPaths
        DependencyNames
        DependencyExtensions
        
        Rules
    end
    
    methods
        % -----------------------------------------------------------------
        % Constructor
        % -----------------------------------------------------------------
        function h = IntermediateTarget(arg)
            cloning = (nargin>0 && isa(arg, 'coder.make.IntermediateTarget'));
            if cloning
                % Initialize
                h.Rules = coder.make.util.OrderedList();
                % Copy constructor
                h.clone(arg);
            else
                % Initialize
                if nargin>0
                    h.TargetType = arg;
                end
                h.Rules  = coder.make.util.OrderedList();
            end
        end
        
        % -----------------------------------------------------------------
        % GET/SET METHODS
        % -----------------------------------------------------------------
        function set.TargetType(h, value)
            if ~isempty(value) && ~ischar(value)
                error(message('CoderFoundation:utility:InvalidProperty', ...
                    'TargetType', class(h), 'string'))
            end
            h.TargetType = value;
        end
        
        function set.TargetPath(h, value)
            if ~isempty(value) && ~ischar(value)
                error(message('CoderFoundation:utility:InvalidProperty', ...
                    'TargetPath', class(h), 'string'))
            end
            h.TargetPath = value;
        end
        
        function set.TargetName(h, value)
            if ~isempty(value) && ~ischar(value)
                error(message('CoderFoundation:utility:InvalidProperty', ...
                    'TargetName', class(h), 'string'))
            end
            h.TargetName = value;
        end
        
        function set.TargetExtension(h, value)
            if ~isempty(value) && ~ischar(value)
                error(message('CoderFoundation:utility:InvalidProperty', ...
                    'TargetExtension', class(h), 'string'))
            end
            h.TargetExtension = value;
        end
        
        function set.DependencyPaths(h, value)
            if ~isempty(value) && ~ischar(value) && ~iscell(value) %% ~iscellstr(value)
                error(message('CoderFoundation:utility:InvalidProperty', ...
                    'DependencyPaths', class(h), 'string'))
            end
            h.DependencyPaths = value;
        end
        
        function set.DependencyNames(h, value)
            if ~isempty(value) && ~ischar(value) && ~iscell(value) %% ~iscellstr(value)
                error(message('CoderFoundation:utility:InvalidProperty', ...
                    'DependencyNames', class(h), 'string'))
            end
            h.DependencyNames = value;
        end
        
        function set.DependencyExtensions(h, value)
            if ~isempty(value) && ~ischar(value) && ~iscell(value) %% ~iscellstr(value)
                error(message('CoderFoundation:utility:InvalidProperty', ...
                    'DependencyExtensions', class(h), 'string'))
            end
            h.DependencyExtensions = value;
        end
        
        function set.Rules(h, value)
            validType = 'coder.make.util.OrderedList';
            if ~isempty(value) && ~isa(value, validType)
                error(message('CoderFoundation:utility:InvalidProperty', ...
                    'Rules', class(h), validType))
            end
            h.Rules = value;
        end
    end
    
    methods(Hidden)
        
        % -----------------------------------------------------------------
        % Clone contents of another object (copy values only, not handles)
        % -----------------------------------------------------------------
        function clone(h, src)
            h.copyValues(src);
        end
        
        % -----------------------------------------------------------------
        % Check if the contents are equal
        % -----------------------------------------------------------------
        function out = isEqualTo(h, src)
            out = false;
            if ~isequal(h, src)
                return
            end
            % Double check the handle properties
            if ~isequal(h.Rules, src.Rules)
                return
            end
            out = true;
        end
        
        % -----------------------------------------------------------------
        % Add an entry to the dependency list
        % -----------------------------------------------------------------
        function addDependency(h, list)
            if ischar(list)
                h.DependencyNames{end+1} = list;
            elseif iscell(list) %% iscellstr(list)
                h.DependencyNames = horzcat(h.DependencyNames, list);
            end
        end
        
        % -----------------------------------------------------------------
        % Internal-only access to private/public properties
        % -----------------------------------------------------------------
        function out = getProp(h, prop)
            out = h.(prop);
        end
        
    end % Hidden
    
    methods (Access=protected)
        
        function copyValues(h, src)
            if ~strcmp(class(h), class(src))
                error(message('coder_compile:toolchain:CannotCloneClassMismatch', ...
                    class(src), class(h)));
            end
            h.TargetType            = src.TargetType;
            h.TargetPath            = src.TargetPath;
            h.TargetName            = src.TargetName;
            h.TargetExtension       = src.TargetExtension;
            h.DependencyPaths       = src.DependencyPaths;
            h.DependencyNames       = src.DependencyNames;
            h.DependencyExtensions  = src.DependencyExtensions;
            coder.make.internal.copyListProperty('Rules', src, h);
        end
        
    end
end
