classdef onCleanup < handle
    properties(SetAccess = 'immutable', GetAccess = 'public', Transient)
        task (1,1) function_handle = @nop;
    end

    methods
        function obj = onCleanup(functionHandle)
            arguments
                functionHandle (1, 1) function_handle = @nop
            end
            obj.task = functionHandle;
        end
    end
    
    methods (Hidden = true)
        function obj = saveobj(obj)
            persistent haveAlreadyWarned;
            if isempty(haveAlreadyWarned)
                warning(message('MATLAB:onCleanup:DoNotSave'));
                haveAlreadyWarned = true;
            end
        end

        function delete(obj)
            obj.task();
        end
    end
end

function nop
end

%#codegen

%   Copyright 2007-2022 The MathWorks, Inc.
