From 0223977ead33796133df1b37d5e081da3dc7280e Mon Sep 17 00:00:00 2001 From: Alex Buts Date: Thu, 28 Oct 2021 17:10:32 +0100 Subject: [PATCH] Re pace-neutrons/Herbert#356 added recursive option to to_bare_struct method IX_experiment got empty constructor as input --- .../instrument/@IX_experiment/IX_experiment.m | 9 +++++- .../@serializable/private/to_bare_struct_.m | 10 +++++-- .../@serializable/private/to_struct_.m | 2 +- .../classes/@serializable/serializable.m | 28 ++++++++++++++++--- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/herbert_core/classes/instrument_classes/instrument/@IX_experiment/IX_experiment.m b/herbert_core/classes/instrument_classes/instrument/@IX_experiment/IX_experiment.m index 53a01308d8..fb22847636 100644 --- a/herbert_core/classes/instrument_classes/instrument/@IX_experiment/IX_experiment.m +++ b/herbert_core/classes/instrument_classes/instrument/@IX_experiment/IX_experiment.m @@ -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; diff --git a/herbert_core/utilities/classes/@serializable/private/to_bare_struct_.m b/herbert_core/utilities/classes/@serializable/private/to_bare_struct_.m index 4cfca1de7b..8c1dcfd12b 100644 --- a/herbert_core/utilities/classes/@serializable/private/to_bare_struct_.m +++ b/herbert_core/utilities/classes/@serializable/private/to_bare_struct_.m @@ -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 @@ -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 diff --git a/herbert_core/utilities/classes/@serializable/private/to_struct_.m b/herbert_core/utilities/classes/@serializable/private/to_struct_.m index b40ddad375..a671294a11 100644 --- a/herbert_core/utilities/classes/@serializable/private/to_struct_.m +++ b/herbert_core/utilities/classes/@serializable/private/to_struct_.m @@ -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); diff --git a/herbert_core/utilities/classes/@serializable/serializable.m b/herbert_core/utilities/classes/@serializable/serializable.m index 536fb5b81a..a0ac6523b5 100644 --- a/herbert_core/utilities/classes/@serializable/serializable.m +++ b/herbert_core/utilities/classes/@serializable/serializable.m @@ -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 % @@ -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 %------------------------------------------------------------------ @@ -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