% construct - construct unit for serial connection of two arrows
function u=construct(o,sizes_in)
	u1=[]; u2=[];
	try
		u1=construct(o.unit1,sizes_in);
		u2=construct(o.unit2,u1.sizes_out);
		n_int=nargout(o.unit1);
		n_out=nargout(o.unit2);
	catch ex
		if ~isempty(u2), u2.dispose(); end
		if ~isempty(u1), u1.dispose(); end
		rethrow(ex);
	end


	u=mkunit(o);
	u.starting= @starting;
	u.stopping= @stopping;
	u.dispose = @dispose;
	if n_int==1 
		if n_out==1
			if nargin(o.unit1)==1
				u.process = @proc_111;
			else
				u.process = @proc_n11;
			end
		else
			if nargin(o.unit1)==1
				u.process = @proc_11n;
			else
				u.process = @proc_n1n;
			end
		end
	else
		if nargin(o.unit1)==1 && n_out==1
			u.process = @proc_1n1;
		else
			u.process = @proc_val;
		end
	end
	u.get_state = @get_state;
	u.set_state = @set_state;
	u.sizes_out = u2.sizes_out;
	u.viewables = [u1.viewables;u2.viewables];

	function y=proc_111(x),        y=u2.process(u1.process(x)); end
	function y=proc_n11(varargin), y=u2.process(u1.process(varargin{:})); end
	function varargout=proc_11n(x), [varargout{1:n_out}]=u2.process(u1.process(x)); end
	function varargout=proc_n1n(varargin)
		[varargout{1:n_out}] = u2.process(u1.process(varargin{:}));
	end

	function y=proc_1n1(x)
		[tempvals{1:n_int}] = u1.process(x);
		y=u2.process(tempvals{:});
	end

	function varargout=proc_val(varargin)
		[tempvals{1:n_int}] = u1.process(varargin{:});
		[varargout{1:n_out}] = u2.process(tempvals{:});
	end


	function dispose, u1.dispose(); u2.dispose(); end
	function starting, u1.starting(); u2.starting(); end
	function stopping, u1.stopping(); u2.stopping(); end

	function s=get_state, s = {u1.get_state(),u2.get_state()}; end
	function set_state(s), u1.set_state(s{1}); u2.set_state(s{2}); end
end
