Skip to content

Commit

Permalink
Re pace-neutrons/Herbert#356 added recursive option to to_bare_struct…
Browse files Browse the repository at this point in the history
… method

IX_experiment got empty constructor as input
  • Loading branch information
abuts committed Oct 28, 2021
1 parent c2b049c commit 0223977
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
end

methods
function obj = IX_experiment(filename, filepath, efix,emode,cu,cv,psi,omega,dpsi,gl,gs,en,uoffset,u_to_rlu,ulen,ulabel)
function obj = IX_experiment(varargin)
if nargin==0
return
end
obj = obj.init(filename, filepath, efix,emode,cu,cv,psi,omega,dpsi,gl,gs,en,uoffset,u_to_rlu,ulen,ulabel);
end

function obj = init(obj,filename, filepath, efix,emode,cu,cv,psi,omega,dpsi,gl,gs,en,uoffset,u_to_rlu,ulen,ulabel)
%IX_EXPERIMENT Construct an instance of this class
% Detailed explanation goes here
obj.filename = filename;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function struc = to_bare_struct_(obj)
function struc = to_bare_struct_(obj,recursively)
% Convert serializable object into a special structure, which allow
% serialization and recovery using from_class_struct operation
%
% Inputs:
% obj -- the instance of the object to convert to a structure.
% the fields to use
% recursively -- if true, all serializable subobjects of the
% class are converted to bare structure,
% Returns:
% struc -- structure, containing information, fully defining the
% serializabe class
Expand All @@ -16,7 +18,11 @@
fldn = flds{i};
val = obj(j).(fldn);
if isa(val,'serializable') % else keep object as before. Serializer should handle it by its own way
val= to_struct(val);
if recursively
val= to_bare_struct_(val,recursively);
else
val= to_struct(val);
end
end
cell_dat{i,j} = val;
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
% struc -- structure, containing information, fully defining the
% serializabe class

struc = obj.to_bare_struct();
struc = obj.to_bare_struct(false);
if numel(obj)>1
struc = struct('serial_name',class(obj),...
'array_dat',struc);
Expand Down
28 changes: 24 additions & 4 deletions herbert_core/utilities/classes/@serializable/serializable.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
%
strc = to_struct_(obj);
end
function strc = to_bare_struct(obj)
function strc = to_bare_struct(obj,varargin)
% Convert serializable object into a special structure, which allow
% serialization and recovery using "from_class_struct" operation
%
Expand All @@ -68,11 +68,31 @@
% pbject.
%
% Input:
% obj -- class or array of classes object
% obj -- class or array of classes objects
% Optional:
% '-recursively' -- key-word '-recusrsively' or logical variable.
% true/false
% If provided and true, all 'serializable'
% subfields of the current object are also
% converted to bare structure. If false, or
% absent, they are converted using to_struct
% method
%
% Returns:
% struc -- structure, or structure array, containing the full
% information, necessary to restore the initial object
strc = to_bare_struct_(obj);
if nargin>1
if isnumeric(varargin{1})
recursively = logical(varargin{1});
elseif ischar(varargin{1}) && strncmpi(varargin{1},'-r')
recursively = true;
else
recursively = false;
end
else
recursively = false;
end
strc = to_bare_struct_(obj,recursively);
end

%------------------------------------------------------------------
Expand All @@ -91,7 +111,7 @@
% Returns size of the serialized object
%
% Overload with specific function to avoid conversion to a
% structure.
% structure, which may be expensive
struc = to_struct(obj);
size = serial_size(struc);
end
Expand Down

0 comments on commit 0223977

Please sign in to comment.