% FevalOnAllFuture C++ parfevalOnAll Future

% Copyright 2021 The MathWorks, Inc.
classdef CppFevalOnAllFuture < parallel.internal.queue.CppBackedFuture

    methods (Access = protected)
        function submitScalar(obj, cppQueue)
            obj.broadcast(cppQueue);
        end
        
        function throwIfInvalidFuturesForFetchOutputsScalar(~)
            % fetchOutputs doesn't work on arryas of futures containing
            % CppFevalOnAll futures
            error(message('MATLAB:parallel:future:FetchOutputsScalarFevalOnAll'));
        end
        
        function setReadScalar(~)
            % The read property does not exist for parfevalOnAll futures,
            % since it's only useful for fetchNext
        end
        
        % fetchOutputs does not work for arrays of fevalOnAllFutures, so
        % this function only handles scalar futures
        function c = getAllOutputsCell(f, ~)
            c = f.OutputArguments;
        end
        
        function c = getAllOutputsScalar(f, ~)
            c = f.OutputArguments{:};
        end
        
        % fetchOutputs does not work for arrays of fevalOnAllFutures, so
        % this function only handles scalar futures        
        function tf = hasAnyErrors(f, ~)
            tf = any(~cellfun(@isempty, f.Error));
        end
        
        function c = getAllErrorsCellScalar(f)
            c = f.Error;
        end
    end

    methods (Hidden)
        function obj = CppFevalOnAllFuture(varargin)
            if nargin == 3 && ...
                       parallel.internal.queue.FutureCreation.isVectorizedCreation(varargin{3})
                error(message('MATLAB:parallel:future:FevalOnAllVectorizedCreation'));
            end
            
            outputCacheFcn = ...
                @parallel.internal.queue.CppFutureOutputHelper.buildForBroadcastRequest;
            cppCtor = ...
                 @(id, args, maxDiaryOutput, maxNumRetries) parallel.internal.queue.FevalTask(id, args, maxDiaryOutput, maxNumRetries, true);
             
            obj@parallel.internal.queue.CppBackedFuture(outputCacheFcn, cppCtor, varargin{:});
        end
    end
end
