note description: "Serialize and deserialize multiple objects into one file." legal: "See notice at end of class." status: "See notice at end of class." date: "$Date: 2017-03-23 19:18:26 +0000 (Thu, 23 Mar 2017) $" revision: "$Revision: 100033 $" class SED_MULTI_OBJECT_SERIALIZATION create default_create feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) do create error_message.make_empty end feature -- Access deserialized_object: detachable ANY -- Last deserialized object error_message: STRING_32 -- Reason for failure generating_type: TYPE [detachable SED_MULTI_OBJECT_SERIALIZATION] -- Type of current object -- (type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generating_type_not_void: Result /= Void end generator: STRING_8 -- Name of current object's generating class -- (base class of the type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generator_not_void: Result /= Void generator_not_empty: not Result.is_empty end last_file_position: INTEGER_32 -- Position after last serialization feature {NONE} -- Access basic_deserializer (a_reader: SED_READER_WRITER): SED_BASIC_DESERIALIZER -- New instance of session based on a_reader. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_reader_not_void: a_reader /= Void a_reader_ready: a_reader.is_ready_for_reading do Result := Internal_basic_deserializer (a_reader); Result.set_deserializer (a_reader) ensure -- from SED_STORABLE_FACILITIES basic_deserializer_not_void: Result /= Void end basic_serializer (a_writer: SED_READER_WRITER): SED_BASIC_SERIALIZER -- New instance of session based on a_writer. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing do Result := Internal_basic_serializer (a_writer); Result.set_serializer (a_writer) ensure -- from SED_STORABLE_FACILITIES basic_serializer_not_void: Result /= Void end recoverable_deserializer (a_reader: SED_READER_WRITER): SED_RECOVERABLE_DESERIALIZER -- New instance of session based on a_reader. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_reader_not_void: a_reader /= Void a_reader_ready: a_reader.is_ready_for_reading do Result := Internal_recoverable_deserializer (a_reader); Result.set_deserializer (a_reader) ensure -- from SED_STORABLE_FACILITIES recoverable_deserializer_not_void: Result /= Void end recoverable_serializer (a_writer: SED_READER_WRITER): SED_RECOVERABLE_SERIALIZER -- New instance of session based on a_writer. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing do Result := Internal_recoverable_serializer (a_writer); Result.set_serializer (a_writer) ensure -- from SED_STORABLE_FACILITIES independent_serializer_not_void: Result /= Void end session_deserializer (a_reader: SED_READER_WRITER): SED_SESSION_DESERIALIZER -- New instance of session based on a_reader. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_reader_not_void: a_reader /= Void a_reader_ready: a_reader.is_ready_for_reading do Result := Internal_session_deserializer (a_reader); Result.set_deserializer (a_reader) ensure -- from SED_STORABLE_FACILITIES session_deserializer_not_void: Result /= Void end session_serializer (a_writer: SED_READER_WRITER): SED_SESSION_SERIALIZER -- New instance of session based on a_writer. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing do Result := Internal_session_serializer (a_writer); Result.set_serializer (a_writer) ensure -- from SED_STORABLE_FACILITIES session_serializer_not_void: Result /= Void end feature -- Comparison frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void -- or attached to isomorphic object structures? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_deep_equal (b) end ensure -- from ANY instance_free: class shallow_implies_deep: standard_equal (a, b) implies Result both_or_none_void: (a = Void) implies (Result = (b = Void)) same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b)) symmetric: Result implies deep_equal (b, a) end frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached -- to objects considered equal? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b)) end frozen is_deep_equal alias "≡≡≡" (other: SED_MULTI_OBJECT_SERIALIZATION): BOOLEAN -- Are Current and other attached to isomorphic object structures? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY shallow_implies_deep: standard_is_equal (other) implies Result same_type: Result implies same_type (other) symmetric: Result implies other.is_deep_equal (Current) end is_equal (other: SED_MULTI_OBJECT_SERIALIZATION): BOOLEAN -- Is other attached to an object considered -- equal to current object? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY symmetric: Result implies other ~ Current consistent: standard_is_equal (other) implies Result end frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached to -- field-by-field identical objects of the same type? -- Always uses default object comparison criterion. -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.standard_is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b)) end frozen standard_is_equal alias "≜" (other: SED_MULTI_OBJECT_SERIALIZATION): BOOLEAN -- Is other attached to an object of the same type -- as current object, and field-by-field identical to it? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY same_type: Result implies same_type (other) symmetric: Result implies other.standard_is_equal (Current) end feature -- Status report conforms_to (other: ANY): BOOLEAN -- Does type of current object conform to type -- of other (as per Eiffel: The Language, chapter 13)? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" end same_type (other: ANY): BOOLEAN -- Is type of current object identical to type of other? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY definition: Result = (conforms_to (other) and other.conforms_to (Current)) end successful: BOOLEAN -- Was last retrieval successful? feature -- Duplication frozen clone (other: detachable ANY): like other obsolete "Use `twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- equal to other -- -- For non-void other, clone calls copy; -- to change copying/cloning semantics, redefine copy. -- (from ANY) do if other /= Void then Result := other.twin end ensure -- from ANY instance_free: class equal: Result ~ other end copy (other: SED_MULTI_OBJECT_SERIALIZATION) -- Update current object using fields of object attached -- to other, so as to yield equal objects. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_equal: Current ~ other end frozen deep_clone (other: detachable ANY): like other obsolete "Use `deep_twin' instead. [2017-05-31]" -- Void if other is void: otherwise, new object structure -- recursively duplicated from the one attached to other -- (from ANY) do if other /= Void then Result := other.deep_twin end ensure -- from ANY instance_free: class deep_equal: deep_equal (other, Result) end frozen deep_copy (other: SED_MULTI_OBJECT_SERIALIZATION) -- Effect equivalent to that of: -- copy (other . deep_twin) -- (from ANY) require -- from ANY other_not_void: other /= Void do copy (other.deep_twin) ensure -- from ANY deep_equal: deep_equal (Current, other) end frozen deep_twin: SED_MULTI_OBJECT_SERIALIZATION -- New object structure recursively duplicated from Current. -- (from ANY) external "built_in" ensure -- from ANY deep_twin_not_void: Result /= Void deep_equal: deep_equal (Current, Result) end frozen standard_clone (other: detachable ANY): like other obsolete "Use `standard_twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) do if other /= Void then Result := other.standard_twin end ensure -- from ANY instance_free: class equal: standard_equal (Result, other) end frozen standard_copy (other: SED_MULTI_OBJECT_SERIALIZATION) -- Copy every field of other onto corresponding field -- of current object. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_standard_equal: standard_is_equal (other) end frozen standard_twin: SED_MULTI_OBJECT_SERIALIZATION -- New object field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) external "built_in" ensure -- from ANY standard_twin_not_void: Result /= Void equal: standard_equal (Result, Current) end frozen twin: SED_MULTI_OBJECT_SERIALIZATION -- New object equal to Current -- twin calls copy; to change copying/twinning semantics, redefine copy. -- (from ANY) external "built_in" ensure -- from ANY twin_not_void: Result /= Void is_equal: Result ~ Current end feature -- Basic operations frozen as_attached: attached SED_MULTI_OBJECT_SERIALIZATION obsolete "Remove calls to this feature. [2017-05-31]" -- Attached version of Current. -- (Can be used during transitional period to convert -- non-void-safe classes to void-safe ones.) -- (from ANY) do Result := Current end frozen default: detachable SED_MULTI_OBJECT_SERIALIZATION -- Default value of object's type -- (from ANY) do end frozen default_pointer: POINTER -- Default value of type POINTER -- (Avoid the need to write p.default for -- some p of type POINTER.) -- (from ANY) do ensure -- from ANY instance_free: class end default_rescue -- Process exception for routines with no Rescue clause. -- (Default: do nothing.) -- (from ANY) do end frozen do_nothing -- Execute a null action. -- (from ANY) do ensure -- from ANY instance_free: class end feature -- Basic Operations deserialize (path: READABLE_STRING_GENERAL; a_pos: INTEGER_32) -- Deserialize object previously serialized in path at position a_pos. require non_void_path: path /= Void valid_path: (create {RAW_FILE}.make_with_name (path)).exists local retried: BOOLEAN l_raw_file: detachable RAW_FILE l_reader: SED_MEDIUM_READER_WRITER do if not retried then create l_raw_file.make_with_name (path) if l_raw_file.exists then l_raw_file.open_read; l_raw_file.go (a_pos) create l_reader.make (l_raw_file); l_reader.set_for_reading if l_reader.is_ready_for_reading then deserialized_object := retrieved (l_reader, True) successful := deserialized_object /= Void else successful := False end else successful := False end else successful := False end if l_raw_file /= Void and then not l_raw_file.is_closed then l_raw_file.close end ensure deserialized_object_set_if_no_error: successful implies deserialized_object /= Void rescue debug ("log_exceptions") log_last_exception end retried := True retry end serialize (a: ANY; path: READABLE_STRING_GENERAL; is_appending: BOOLEAN) -- Serialize object a at the end of file path if is_appending, otherwise -- reset content of path. -- Set last_file_position after storing. require non_void_object: a /= Void non_void_path: path /= Void local l_raw_file: detachable RAW_FILE l_writer: SED_MEDIUM_READER_WRITER retried: BOOLEAN do if not retried then create l_raw_file.make_with_name (path) if is_appending then l_raw_file.open_append else l_raw_file.open_write end create l_writer.make (l_raw_file); l_writer.set_for_writing store (a, l_writer) last_file_position := l_raw_file.count successful := True else successful := False create error_message.make (100); error_message.append_string_general ("Cannot store into "); error_message.append_string_general (path) end if l_raw_file /= Void and then not l_raw_file.is_closed then l_raw_file.close end rescue debug ("log_exceptions") log_last_exception end retried := True retry end feature {NONE} -- Data Internal_basic_deserializer (a_reader: SED_READER_WRITER): SED_BASIC_DESERIALIZER -- New instance of session based on a_reader. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_reader_not_void: a_reader /= Void a_reader_ready: a_reader.is_ready_for_reading once create Result.make (a_reader) ensure -- from SED_STORABLE_FACILITIES basic_deserializer_not_void: Result /= Void end Internal_basic_serializer (a_writer: SED_READER_WRITER): SED_BASIC_SERIALIZER -- New instance of session based on a_writer. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing once create Result.make (a_writer) ensure -- from SED_STORABLE_FACILITIES basic_serializer_not_void: Result /= Void end Internal_recoverable_deserializer (a_reader: SED_READER_WRITER): SED_RECOVERABLE_DESERIALIZER -- New instance of session based on a_reader. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_reader_not_void: a_reader /= Void a_reader_ready: a_reader.is_ready_for_reading once create Result.make (a_reader) ensure -- from SED_STORABLE_FACILITIES recoverable_deserializer_not_void: Result /= Void end Internal_recoverable_serializer (a_writer: SED_READER_WRITER): SED_RECOVERABLE_SERIALIZER -- New instance of session based on a_writer. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing once create Result.make (a_writer) ensure -- from SED_STORABLE_FACILITIES internal_recoverable_serializer_not_void: Result /= Void end Internal_session_deserializer (a_reader: SED_READER_WRITER): SED_SESSION_DESERIALIZER -- New instance of session based on a_reader. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_reader_not_void: a_reader /= Void a_reader_ready: a_reader.is_ready_for_reading once create Result.make (a_reader) ensure -- from SED_STORABLE_FACILITIES session_deserializer_not_void: Result /= Void end Internal_session_serializer (a_writer: SED_READER_WRITER): SED_SESSION_SERIALIZER -- New instance of session based on a_writer. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing once create Result.make (a_writer) ensure -- from SED_STORABLE_FACILITIES session_serializer_not_void: Result /= Void end feature {NONE} -- Logging log_last_exception -- Log last exception. do end feature -- Output Io: STD_FILES -- Handle to standard file setup -- (from ANY) once create Result; Result.set_output_default ensure -- from ANY instance_free: class io_not_void: Result /= Void end out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) do Result := tagged_out ensure -- from ANY out_not_void: Result /= Void end print (o: detachable ANY) -- Write terse external representation of o -- on standard output. -- (from ANY) local s: READABLE_STRING_8 do if attached o then s := o.out if attached {READABLE_STRING_32} s as s32 then Io.put_string_32 (s32) elseif attached {READABLE_STRING_8} s as s8 then Io.put_string (s8) else Io.put_string_32 (s.as_string_32) end end ensure -- from ANY instance_free: class end frozen tagged_out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) external "built_in" ensure -- from ANY tagged_out_not_void: Result /= Void end feature -- Platform Operating_environment: OPERATING_ENVIRONMENT -- Objects available from the operating system -- (from ANY) once create Result ensure -- from ANY instance_free: class operating_environment_not_void: Result /= Void end feature {NONE} -- Retrieval frozen internal_correct_mismatch -- Called from runtime to perform a proper dynamic dispatch on correct_mismatch -- from MISMATCH_CORRECTOR. -- (from ANY) local l_msg: STRING_32 l_exc: EXCEPTIONS do if attached {MISMATCH_CORRECTOR} Current as l_corrector then l_corrector.correct_mismatch else create l_msg.make_from_string ("Mismatch: ".as_string_32) create l_exc; l_msg.append (generating_type.name_32); l_exc.raise_retrieval_exception (l_msg) end end feature -- Serialization routines basic_store (an_object: ANY; a_writer: SED_READER_WRITER; a_optimized_for_retrieval: BOOLEAN) -- Serialization of an_object using a_writer. -- Object stored can only be retrieved by execution of same program executable. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES an_object_not_void: an_object /= Void a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing local l_serializer: SED_BASIC_SERIALIZER do l_serializer := basic_serializer (a_writer); a_writer.write_header; a_writer.write_natural_32 (Eiffel_basic_store); l_serializer.set_root_object (an_object); l_serializer.encode; a_writer.write_footer end independent_store (an_object: ANY; a_writer: SED_READER_WRITER; a_optimized_for_retrieval: BOOLEAN) obsolete "Use `store' instead. [2017-05-31]" -- Serialization of an_object using a_writer. -- Object stored can only be retrieved by programs having the same set of types. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES an_object_not_void: an_object /= Void a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing local l_serializer: SED_RECOVERABLE_SERIALIZER do l_serializer := recoverable_serializer (a_writer); a_writer.write_header; a_writer.write_natural_32 (Eiffel_recoverable_store); l_serializer.set_root_object (an_object); l_serializer.encode; a_writer.write_footer end retrieved (a_reader: SED_READER_WRITER; a_is_gc_enabled: BOOLEAN): detachable ANY -- Deserialization of object from a_reader. -- Garbage collection will be enabled if a_is_gc_enabled. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_reader_not_void: a_reader /= Void a_reader_ready: a_reader.is_ready_for_reading local l_deserializer: detachable SED_SESSION_DESERIALIZER l_retrieved_errors: like retrieved_errors do a_reader.read_header inspect a_reader.read_natural_32 when Eiffel_session_store then l_deserializer := session_deserializer (a_reader) when Eiffel_basic_store then l_deserializer := basic_deserializer (a_reader) when Eiffel_recoverable_store then l_deserializer := recoverable_deserializer (a_reader) when Eiffel_independent_store then create l_retrieved_errors.make (1); l_retrieved_errors.extend ((create {SED_ERROR_FACTORY}).new_obsolete_storable_type) retrieved_errors := l_retrieved_errors else create l_retrieved_errors.make (1); l_retrieved_errors.extend ((create {SED_ERROR_FACTORY}).new_unknown_storable_type) retrieved_errors := l_retrieved_errors end if l_deserializer /= Void then l_deserializer.decode (a_is_gc_enabled) retrieved_errors := l_deserializer.errors if not l_deserializer.has_error then Result := l_deserializer.last_decoded_object; a_reader.read_footer end end end retrieved_error: detachable SED_ERROR obsolete "Use `retrieved_errors' directly. [2017-05-31]" -- Error set from last call to retrieved. -- (from SED_STORABLE_FACILITIES) do if attached retrieved_errors as l_errors and then not l_errors.is_empty then Result := l_errors.last end end retrieved_errors: detachable ARRAYED_LIST [SED_ERROR] -- Errors set from last call to retrieved. -- (from SED_STORABLE_FACILITIES) retrieved_from_medium (a_medium: IO_MEDIUM): detachable ANY -- Deserialization of object from a_reader. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES a_medium_not_void: a_medium /= Void a_medium_open_for_reading: a_medium.is_open_read a_medium_support_storable: a_medium.support_storable do Result := retrieved (create {SED_MEDIUM_READER_WRITER}.make_for_reading (a_medium), False) end session_store (an_object: ANY; a_writer: SED_READER_WRITER; a_optimized_for_retrieval: BOOLEAN) -- Serialization of an_object using a_writer. -- Object stored can only be retrieved during current program execution. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES an_object_not_void: an_object /= Void a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing local l_serializer: SED_SESSION_SERIALIZER do l_serializer := session_serializer (a_writer); a_writer.write_header; a_writer.write_natural_32 (Eiffel_session_store); l_serializer.set_root_object (an_object); l_serializer.encode; a_writer.write_footer end store (an_object: ANY; a_writer: SED_READER_WRITER) -- Serialization of an_object using a_writer. -- Object stored can only be retrieved by programs having the same set of types. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES an_object_not_void: an_object /= Void a_writer_not_void: a_writer /= Void a_writer_ready: a_writer.is_ready_for_writing local l_serializer: SED_RECOVERABLE_SERIALIZER do l_serializer := recoverable_serializer (a_writer); a_writer.write_header; a_writer.write_natural_32 (Eiffel_recoverable_store); l_serializer.set_root_object (an_object); l_serializer.encode; a_writer.write_footer end store_in_medium (an_object: ANY; a_medium: IO_MEDIUM) -- Serialization of an_object in a_medium -- Object stored can only be retrieved by programs having the same set of types. -- (from SED_STORABLE_FACILITIES) require -- from SED_STORABLE_FACILITIES an_object_not_void: an_object /= Void a_medium_not_void: a_medium /= Void a_medium_writable: a_medium.is_open_write a_medium_support_storable: a_medium.support_storable do store (an_object, create {SED_MEDIUM_READER_WRITER}.make_for_writing (a_medium)) end feature -- Storable type Eiffel_basic_store: NATURAL_32 = 2 -- (from SED_STORABLE_FACILITIES) Eiffel_independent_store: NATURAL_32 = 3 -- (from SED_STORABLE_FACILITIES) Eiffel_recoverable_store: NATURAL_32 = 4 -- Various type of storable mechanism. -- (from SED_STORABLE_FACILITIES) Eiffel_session_store: NATURAL_32 = 1 -- (from SED_STORABLE_FACILITIES) invariant -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) note library: "EiffelBase: Library of reusable components for Eiffel." copyright: "Copyright (c) 1984-2017, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software 5949 Hollister Ave., Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Website http://www.eiffel.com Customer support http://support.eiffel.com ]" end -- class SED_MULTI_OBJECT_SERIALIZATION
Generated by ISE EiffelStudio