classdef BuildItem < handle
% BUILDITEM Class representation of a build item.

% Copyright 2011-2020 The MathWorks, Inc.
    
    properties(Access=private)
        Value
        Macro
    end
    
    properties(Dependent, Hidden, GetAccess=public, SetAccess=private)
        Token
    end
    
    methods
        % -----------------------------------------------------------------
        % Constructor
        % -----------------------------------------------------------------
        function h = BuildItem(varargin)
            if nargin==0
                h.Value = '';
                h.Macro  = '';
            elseif nargin==1
                h.Macro  = '';
                h.Value = varargin{1};
            elseif nargin==2
                h.Macro = varargin{1};
                h.Value = varargin{2};
            else
                error(message('coder_compile:toolchain:BuildItemConstructorInputs'));
            end
        end
        
        % -----------------------------------------------------------------
        % Value
        % -----------------------------------------------------------------
        function value = getValue(h)
            value = h.Value;
        end
        
        function setValue(h, value)
            h.Value = value;
        end
        
        function set.Value(h, value)
            value = convertStringsToChars(value);
            if ~(ischar(value) || iscellstr(value))
                error(message('coder_compile:toolchain:BuildItemValueMustBeString', class(h)));
            end
            h.Value = value;
        end
        
        % -----------------------------------------------------------------
        % Macro
        % -----------------------------------------------------------------
        function value = getMacro(h)
            value = h.Macro;
        end
        
        function setMacro(h, value)
            h.Macro = value;
        end
        
        function set.Macro(h, value)
            value = convertStringsToChars(value);
            if ~isempty(value) && ~coder.make.internal.iscvar(value)
                error(message('coder_compile:toolchain:BuildItemMacroMustBeCVar', class(h)));
            end
            h.Macro = value;
        end
        
        % -----------------------------------------------------------------
        %
        % -----------------------------------------------------------------
        function resp = isempty(h)
            resp = (isempty(h.Value) && isempty(h.Macro));
        end
        
        function str = getToken(h)
            str = h.Token;
        end
        
        function str = get.Token(h)
            str = '';
            if ~isempty(h.Macro)
                str = coder.make.internal.tokenize(h.Macro);
            end
        end
        
    end % Public
    
    methods(Hidden)
        
        % -----------------------------------------------------------------
        %
        % -----------------------------------------------------------------
        function buffer = info(h)
            macro = h.getMacro();
            value = h.getValue();
            if isempty(macro)
                macro = '<unspecified macro>';
            end
            if isempty(value)
                if ischar(value)
                    value = '(empty)';
                else
                    value = '<unspecified value>';
                end
            end
            buffer = sprintf('%s = %s', macro, value);
        end
        
        % -----------------------------------------------------------------
        %
        % -----------------------------------------------------------------
        function str = getRef(h)
            str = '';
            referencePattern = '\$\($1\)';
            if ~isempty(h.Macro)
                str = regexprep(h.Macro, '(\w*)', referencePattern);
            end
        end
        
        % -----------------------------------------------------------------
        %
        % -----------------------------------------------------------------
        function disp(h)
            variable = h.Macro;
            value    = h.Value;
            
            if isempty(h.Macro)
                variable = '(empty)';
            end
            if isempty(h.Value)
                value    = '(empty)';
            end
            
            fprintf('BuildItem: \n');
            fprintf('\tMacro : %s\n', variable);
            if ischar(value)
                fprintf('\tValue : %s\n', value);
            else
                fprintf('\tValue : %s ', value{1});
                for i=2:numel(value)
                    fprintf('%s ', value{i});
                end
                fprintf('\n');
            end
        end
        
        % -----------------------------------------------------------------
        % Copy the properties
        % -----------------------------------------------------------------
        function clone(h, src)
            if ~strcmp(class(h), class(src))
                error(message('coder_compile:toolchain:CannotCloneClassMismatch', ...
                    class(src), class(h)));
            end
            h.Value = src.Value;
            h.Macro = src.Macro;
        end
        
        % -----------------------------------------------------------------
        % Check if the contents are equal
        % -----------------------------------------------------------------
        function out = isEqualTo(h, src)
            out = isequal(h, src);
        end
        
        % -----------------------------------------------------------------
        % Internal-only access to private/public properties
        % -----------------------------------------------------------------
        function out = getProp(h, prop)
            out = h.(prop);
        end
        
    end % hidden methods
    
end

% LocalWords:  toolchain CVar
