%AUTOTRANSFER - automatically transfers the value out of the SPMD block

% Copyright 2008-2022 The MathWorks, Inc.

classdef AutoTransfer
    properties
        Worker
        Value
    end
    
    methods
        function obj = AutoTransfer( value, opt_worker )
            if nargin == 1
                obj.Worker = 1;
            else
                obj.Worker = opt_worker;
            end
            if spmdIndex == obj.Worker
                obj.Value = value;
            else
                obj.Value = [];
            end
        end
        
        function [factory, userData] = getRemoteFromSPMD( obj )
            userData = {obj.Value, obj.Worker};
            factory = @iBuildAutoDeref;
        end
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% build the autoderef. userDataCell is a cell array of all the user datas,
% each of which is a 2-element cell array.
function remote = iBuildAutoDeref( userDataCell )
ud1 = userDataCell{1};
workerWithData = ud1{2};
remote = distributedutil.AutoDeref( userDataCell{workerWithData}{1} );
end
