diff --git a/herbert_core/utilities/classes/@serializable/from_struct.m b/herbert_core/utilities/classes/@serializable/from_struct.m new file mode 100644 index 000000000..12a874d69 --- /dev/null +++ b/herbert_core/utilities/classes/@serializable/from_struct.m @@ -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 diff --git a/herbert_core/utilities/classes/@serializable/private/loadobj_.m b/herbert_core/utilities/classes/@serializable/private/loadobj_.m index 92141905b..c23702048 100644 --- a/herbert_core/utilities/classes/@serializable/private/loadobj_.m +++ b/herbert_core/utilities/classes/@serializable/private/loadobj_.m @@ -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 diff --git a/herbert_core/utilities/classes/@serializable/serializable.m b/herbert_core/utilities/classes/@serializable/serializable.m index 7162d6307..ee48b87da 100644 --- a/herbert_core/utilities/classes/@serializable/serializable.m +++ b/herbert_core/utilities/classes/@serializable/serializable.m @@ -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: % @@ -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 @@ -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,... @@ -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 @@ -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: % ------- @@ -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 diff --git a/herbert_core/utilities/classes/@serializable/to_struct.m b/herbert_core/utilities/classes/@serializable/struct.m similarity index 67% rename from herbert_core/utilities/classes/@serializable/to_struct.m rename to herbert_core/utilities/classes/@serializable/struct.m index 6d3300261..4d2d8e6d1 100644 --- a/herbert_core/utilities/classes/@serializable/to_struct.m +++ b/herbert_core/utilities/classes/@serializable/struct.m @@ -1,4 +1,4 @@ -function struc = to_struct(obj) +function struc = struct(obj) % Convert sqw-type object into a structure % % Inputs: @@ -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;