%DisarmableOncleanup
%
% Enabling behaviour similar to onCleanup, except that it can be toggled on/off

% Copyright 2013 The MathWorks, Inc.

classdef DisarmableOncleanup < handle
    
    properties (Access = private)
        TriggerOncleanup = true;
    end
    
    properties (SetAccess = immutable, GetAccess = private)
        Callback
    end
    
    methods
        function obj = DisarmableOncleanup(callback)
            obj.Callback = callback;
        end
        
        function delete(obj)
            if obj.TriggerOncleanup
                try
                    obj.Callback();
                catch err
                    dctSchedulerMessage(1, ['Failed to run the DisarmableOncleanup callback due to the following ', ...
                        'error:\n%s'], err.message);
                end
            end
        end
        
        function disarm(obj)
            obj.TriggerOncleanup = false;
        end
    end
end
