% WarningSuppressor - custom cleanup-style object for warning suppression which
% can also tell you if the suppressed warning occurred since object creation.

% Copyright 2013-2020 The MathWorks, Inc.
classdef (Hidden) WarningSuppressor < handle
    properties (GetAccess = private, SetAccess = immutable)
        SuppressedWarningID
        WarningState
        WarningsCollector
        LastWarningMessage
        LastWarningID
    end
    methods
        function obj = WarningSuppressor(idToSuppress)
            obj.SuppressedWarningID = idToSuppress;
            obj.WarningState = warning('off', idToSuppress);
            [obj.LastWarningMessage, obj.LastWarningID] = lastwarn();
            obj.WarningsCollector = parallel.internal.general.WarningsCollector();
            obj.WarningsCollector.CollectDisabledWarnings = true;
        end

        function tf = didSuppressedWarningOccur(obj)
        % Return true the SuppressedWarningID occurred.
            tf = ismember(obj.SuppressedWarningID, {obj.WarningsCollector.Warnings.identifier});
        end

        function delete(obj)
        % Restore previous state
            warning(obj.WarningState);
            lastwarn(obj.LastWarningMessage, obj.LastWarningID);
        end
    end
end
