function out = tocBytes(aPool, previousState)
%

% Copyright 2016-2020 The MathWorks, Inc.

if numel(aPool) > 1
    % If a pool is provided, it must be a scalar pool.
    error(message("MATLAB:parallel:pool:ScalarPoolRequired", "tocBytes"));
end
if nargin == 1
    if ~isempty(aPool)
        % Normally we just ask the pool we have been given for the previous
        % ticBytes result.
        previousState = aPool.PreviousTicBytesResult;
    else
        % But empty pools do not have any fields we can interrogate, so we
        % need to ask the static method of the parallel.Pool class for this
        % information.
        previousState = parallel.Pool.hEmptyPoolPreviousTicBytesResult;
    end
    % Have we been given a previous state? If it is empty then we need to throw
    % an error.
    if isempty(previousState)
        error(message('MATLAB:parallel:pool:TicBytesNotCalled'));
    end
else
    if ~(isa(previousState, 'parallel.pool.TicBytesResult') && numel(previousState) == 1)
        error(message('MATLAB:parallel:pool:TicBytesResultRequired'));
    end
end

% Ask the pool about it's current state
currentState = parallel.pool.TicBytesResult(aPool);

% Compute how many bytes have been transferred between the current
% state and the previous state. Note that the opaque state object
% will throw errors if the pools are different.
if nargout == 0
    dispMinus(currentState, previousState);
else
    out = currentState - previousState;
end
end
