Skip to content

Commit

Permalink
Re #356 Completed (not tested) serializable class
Browse files Browse the repository at this point in the history
  • Loading branch information
abuts committed Oct 13, 2021
1 parent 4aaa552 commit 1053494
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 78 deletions.
20 changes: 20 additions & 0 deletions herbert_core/utilities/classes/@serializable/from_struct.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function obj = from_struct(obj,inputs)
% Restore object from the fields, previously obtained by struct method
%
struct_fields = fieldnames(inputs);
if numel(inputs)>1
obj = repmat(obj,numel(inputs));
for i=1:numel(inputs)
obj(i) = set_obj(obj(i),inputs(i),struct_fields);
end
obj = reshape(obj,size(inputs));
else
obj = set_obj(obj,inputs,struct_fields);
end
%
function obj = set_obj(obj,inputs,flds)
%
for i=1:numel(flds)
fld = flds{i};
obj.(fld) = inputs.(fld);
end
64 changes: 14 additions & 50 deletions herbert_core/utilities/classes/@serializable/private/loadobj_.m
Original file line number Diff line number Diff line change
@@ -1,61 +1,25 @@
function obj = loadobj_(S,class_instance)
% Restore object from the data as they are stord on a hard drive
%
% Input:
% S -- the structure,
%
% Output:
% -------
% obj An instance of class_instance object or array of objects
%
ver_requeste = class_instance.
if isstruct(S)
obj = PixelData();
if isfield(S,'version')
% load PixelData objects, written when saveobj method was
% written
if S.version == 1
if isfield(S,'array_data') % multidimensional array of pixel data
S = S.array_data;
obj = repmat(obj,size(S));
for i=1:numel(S)
if i>1 % cloning handle object as repmat makes the handles
% identical
obj(i) = PixelData();
end
obj(i).set_data('all',S(i).data);
obj(i).set_range(S(i).pix_range);
obj(i).file_path_ = S(i).file_path;
end
else % Single object
obj.set_data('all',S.data);
obj.set_range(S.pix_range);
obj.file_path_ = S.file_path;
end
ver_requested = class_instance.classVersion();
if isfield(S,'version')
if S.version == ver_requested
S = rmfield(S,'version');
if isfield(S,'array_data')
obj = from_struct(class_instance,S.array_data);
else
error('HORACE:PixelData:invalid_argument',...
'Unknown PixelData input structire version');
obj = from_struct(class_instance,S);
end
else % previous version(s), written without version info
if isfield(S,'data_')
for i=1:numel(S)
set_data(obj(i),'all',S(i).data_);
obj(i).reset_changed_coord_range('coordinates')
end
elseif isfield(S,'raw_data_')
for i=1:numel(S)
obj(i).set_data('all',S(i).raw_data_);
obj(i).set_range(S(i).pix_range_);
obj(i).file_path_ = S(i).file_path_;
end
else
error('HORACE:PixelData:invalid_argument',...
'Unknown PixelData input structire version');
end
end
else
if isempty(S.page_memory_size_)
% This if statement allows us to load old PixelData objects that
% were saved in .mat files that do not have the 'page_memory_size_'
% property
S.page_memory_size_ = PixelData.DEFAULT_PAGE_SIZE;
else
obj = from_old_struct(class_instance,S);
end
obj = PixelData(S);
else % previous version(s), written without version info
obj = from_old_struct(S);
end
51 changes: 28 additions & 23 deletions herbert_core/utilities/classes/@serializable/serializable.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
ver = classVersion(obj);
end
methods(Abstract,Static)
% Static method used my Matlab load function to support custom
% Static method used by Matlab load function to support custom
% loading. The method has to be overloaded in the class using
% loadobj_generic function in the form:
%
Expand All @@ -21,12 +21,22 @@
% obj = class_instance.loadobj_generic(S,class_instance)
% end
%
% where
% where EmpytClassConstructor is the empty constructor of the
% class to recover from the record
obj = loadobj(S);
end


methods
% convert class into a plain structure using independent properties
% obtained from indepFields method
str = struct(obj);
%
%------------------------------------------------------------------
% resore object from a plain structure, previously obtained by
% struct operation
obj = from_struct(obj,inputs);
%
%======================================================================
% Custom loadobj and saveobj
% - to enable custom saving to .mat files and bytestreams
Expand All @@ -47,7 +57,7 @@
% S Structure created from obj that is to be saved

% The following is generic code. Overload if really necessary
S = to_struct(obj);
S = struct(obj);
ver = obj.classVersion();
if numel(obj)>1
S = struct('version',ver,...
Expand All @@ -57,10 +67,20 @@
end
end

% convert class into a plain structure using independent properties
% obtained from indepFields method
str = to_struct(obj,varargin);
%
function obj = from_old_struct(obj,inputs)
% restore object from the old structure, which describes the
% previous version of the object.
%
% Generally, this function interfaces the current from_struct
% function, but when the old strucure substantially differs from
% the moden structure, this method needs particular overloading
% for loadob to recover new structure from an old structure.
if isfield(inputs,'version')
inputs = rmfield(inputs,'version');
end
obj = from_struct(obj,inputs);
end
%
function obj = serializable()
% generic class constructor. Does nothing
Expand All @@ -77,8 +97,8 @@
% S Either (1) an object of the class, or (2) a structure
% or structure array previously obtained by saveobj
% method
% class_instance -- the instance of the class to recover from
% input S
% class_instance -- the instance of a serializable class to
% recover from input S
%
% Output:
% -------
Expand All @@ -91,21 +111,6 @@
obj = loadobj_(S,class_instance);
end
end
%------------------------------------------------------------------
% resore object from a plain structure, previously obtained by
% struct operation
[obj,remains] = from_struct(inputs);

function [obj,remains] = from_old_struct(inputs)
% restore object from the old structure, which describes the
% previous version of the object.
%
% Generally, this function interfaces the current from_struct
% function, but when the old strucure substantially differs from
% the moden structure, this method needs particular overloading
% for loadob to recover new structure from an old structure.
[obj,remains] = from_struct(inputs);
end
end

end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function struc = to_struct(obj)
function struc = struct(obj)
% Convert sqw-type object into a structure
%
% Inputs:
Expand All @@ -11,10 +11,9 @@
for i=1:numel(flds)
fldn = flds{i};
val = obj(j).(fldn);
if isa(val,'serializeable')
val = to_struct(val);
elseif isobject(val) % should be convertable to standard structure
% not fully generic but let's not overcomplicate
if isobject(val) % should be convertable to standard structure
% not fully generic but let's not overcomplicate -- all our
% classes should have this method
val= struct(val);
end
cell_dat{i,j} = val;
Expand Down

0 comments on commit 1053494

Please sign in to comment.