classdef MyClass < nelson.mixin.indexing.RedefinesParen
| Parameter | Description |
|---|---|
| obj | an object of a class deriving from nelson.mixin.indexing.RedefinesParen. |
| Parameter | Description |
|---|---|
| obj | an object supporting customized parenthesis indexing. |
Derive from nelson.mixin.indexing.RedefinesParen to give a class its own parentheses indexing behavior, for example to build a container that indexes its stored data rather than an array of objects.
A subclass implements these protected methods:
parenReference(obj, indexOp) - value of obj(...).
parenAssign(obj, indexOp, value) - result of obj(...) = value.
parenDelete(obj, indexOp) - result of obj(...) = [].
parenListLength(obj, indexOp, indexContext) - number of values a paren reference produces.
and these public methods: size(obj), cat(dim, ...) and the static empty(...).
The indexOp argument is a nelson.indexing.IndexingOperation array. For a chained expression such as obj(i).field the hook is called once with the whole chain: numel(indexOp) is the number of operations (2 here), indexOp(1) is the parentheses operation and indexOp(2) the .field operation. Apply the whole chain to the stored data with the dynamic form obj.Data.(indexOp), or read a single level through indexOp(k).Indices{...} / indexOp(k).Name. The base class provides numel, length, isempty, ndims, end, horzcat and vertcat in terms of the abstract size/cat.
A single-level index (obj(i)) calls the hook with a scalar indexOp (numel(indexOp) == 1). A few chain shapes are still evaluated one step at a time (the hook sees a single operation, the value is identical): a level of the form .method(args), a chain using the end keyword after the first level, and chained assignment.
classdef Bag < nelson.mixin.indexing.RedefinesParen
properties
Data
end
methods
function obj = Bag(x)
obj.Data = x;
end
function varargout = size(obj, varargin)
varargout{1} = size(obj.Data, varargin{:});
end
function obj = cat(dim, varargin)
acc = [];
for k = 1:numel(varargin)
acc = cat(dim, acc, varargin{k}.Data);
end
obj = Bag(acc);
end
end
methods (Access = protected)
function varargout = parenReference(obj, indexOp)
% indexOp may hold the whole chain; apply it to the stored data.
[varargout{1:nargout}] = obj.Data.(indexOp);
end
function obj = parenAssign(obj, indexOp, varargin)
obj.Data.(indexOp) = varargin{1};
end
function obj = parenDelete(obj, indexOp)
obj.Data.(indexOp) = [];
end
function n = parenListLength(obj, indexOp, ctx)
n = 1;
end
end
end
| Version | Description |
|---|---|
| 2.0.0 | initial version |