Skip to content

Commit 81daa1d

Browse files
committed
* MappedTensors can now be specified as read-only, to protect important raw data. Attempting to write to a read-only tensor raises an error.
* Added several overloaded tests such as `ischar`, `islogical`, `isscalar`, etc. * Fixed minor indentation issue in `mapped_tensor_shim`
1 parent 9c875d6 commit 81daa1d

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

MappedTensor.m

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
properties (SetAccess = private, GetAccess = private)
150150
strRealFilename; % Binary data file on disk (real part of tensor)
151151
strCmplxFilename; % Binary data file on disk (complex part of tensor)
152+
bReadOnly = false; % Should the data be protected from writing?
152153
hRealContent; % File handle for data (real part)
153154
hCmplxContent; % File handle for data (complex part)
154155
bTemporary; % A flag which records whether a temporary file was created by MappedTensor
@@ -198,6 +199,12 @@
198199
vbKeepArg(nArg:nArg+1) = false;
199200
nArg = nArg + 1;
200201

202+
case {'readonly'}
203+
% - Read-only or read/write status was specified
204+
mtVar.bReadOnly = logical(varargin{nArg+1});
205+
vbKeepArg(nArg:nArg+1) = false;
206+
nArg = nArg + 1;
207+
201208
otherwise
202209
% - No other properties are supported
203210
error('MappedTensor:InvalidProperty', ...
@@ -447,6 +454,11 @@ function delete(mtVar)
447454

448455
% subsasgn - METHOD Overloaded subsasgn
449456
function [mtVar] = subsasgn(mtVar, subs, tfData)
457+
% - Test read-only status if tensor
458+
if (mtVar.bReadOnly)
459+
error('MappedTensor:ReadProtect', '*** MappedTensor: Attempted write to a read-only tensor.');
460+
end
461+
450462
% - Test real/complex nature of input and current tensor
451463
if (~isreal(tfData))
452464
% - The input data is complex
@@ -578,6 +590,36 @@ function delete(mtVar)
578590
bIsReal = ~mtVar.bIsComplex;
579591
end
580592

593+
% islogical - METHOD Overloaded islogical function
594+
function [bIsLogical] = islogical(mtVar)
595+
bIsLogical = isequal(mtVar.strClass, 'logical');
596+
end
597+
598+
% isnumeric - METHOD Overloaded isnumeric function
599+
function [bIsNumeric] = isnumeric(mtVar)
600+
bIsNumeric = ~islogical(mtVar);
601+
end
602+
603+
% isscalar - METHOD Overloaded isscalar function
604+
function [bIsScalar] = isscalar(mtVar)
605+
bIsScalar = numel(mtVar) == 1;
606+
end
607+
608+
% ismatrix - METHOD Overloaded ismatrix function
609+
function [bIsMatrix] = ismatrix(mtVar)
610+
bIsMatrix = ~isscalar(mtVar);
611+
end
612+
613+
% ischar - METHOD Overloaded ischar function
614+
function [bIsChar] = ischar(mtVar)
615+
bIsChar = isequal(mtVar.strClass, 'char');
616+
end
617+
618+
% isnan - METHOD Overloaded isnan function
619+
function [bIsNan] = isnan(mtVar) %#ok<MANU>
620+
bIsNan = false;
621+
end
622+
581623
%% Overloaded methods (uminus, uplus, times, mtimes, ldivide, rdivide, mldivide, mrdivide)
582624

583625
% uminus - METHOD Overloaded uminus operator (-mtVar)

private/mapped_tensor_shim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ void CmdReadChunks(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
510510

511511
} else {
512512

513-
dprintf("mts/crc: Single-element skip-read: read [%ld] bytes per element, skip [%ld] bytes.\n", nDataElemSize, nDataElemSize * (uChunkSkip-1));
513+
dprintf("mts/crc: Single-element skip-read: read [%ld] bytes per element, skip [%ld] bytes.\n", nDataElemSize, nDataElemSize * (uChunkSkip-1));
514514

515515
/* - Read an element, then skip elements */
516516
for (uElementIndex = 0; uElementIndex < uChunkSize; uElementIndex++) {

0 commit comments

Comments
 (0)