note description: "Objects that traverse object graphs starting at the root object in a depth first manner." library: "Free implementation of ELKS library" legal: "See notice at end of class." status: "See notice at end of class." author: "Stephanie Balzer" date: "$Date: 2013-03-27 07:34:50 +0000 (Wed, 27 Mar 2013) $" revision: "$Revision: 92359 $" class OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE create default_create feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end feature -- Access generating_type: TYPE [detachable OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE] -- 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 object_action: detachable PROCEDURE [separate ANY] -- Action called on every object in object graph -- (from OBJECT_GRAPH_TRAVERSABLE) on_processing_object_action: detachable PROCEDURE [REFLECTED_OBJECT] -- Action called on every object in the object graph. -- Note: The argument is reused later for a different object. Do not alias it. -- (from OBJECT_GRAPH_TRAVERSABLE) on_processing_reference_action: detachable PROCEDURE [REFLECTED_OBJECT, REFLECTED_OBJECT] -- Action called on every reference in the object graph. -- Note: The arguments are reused later for different objects. Do not alias them. -- (from OBJECT_GRAPH_TRAVERSABLE) root_object: detachable ANY -- Starting point of graph traversing -- (from OBJECT_GRAPH_TRAVERSABLE) visited_objects: detachable ARRAYED_LIST [separate ANY] -- List referencing objects of object graph that have been visited in traverse. -- (from OBJECT_GRAPH_TRAVERSABLE) visited_types: detachable HASH_TABLE [INTEGER_32, INTEGER_32] -- List of all types encountered during traversal -- (from OBJECT_GRAPH_TRAVERSABLE) 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: OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE): 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: OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE): 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: OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE): 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 has_failed: BOOLEAN -- Was the last traversal successful? -- (from OBJECT_GRAPH_TRAVERSABLE) has_reference_with_copy_semantics: BOOLEAN -- Does the traversed graph of objects contain reference with copy semantics? -- (from OBJECT_GRAPH_TRAVERSABLE) is_exception_on_copy_suppressed: BOOLEAN -- Do we suppress an exception when object_action is called on a copied object? -- A copy happens when encountering tuples containing copy-semantics references -- or with SPECIAL[XX], where XX is a user-defined expanded type. -- (from OBJECT_GRAPH_TRAVERSABLE) is_exception_propagated: BOOLEAN -- Does the algorithm propagate any exceptions to the user? -- (from OBJECT_GRAPH_TRAVERSABLE) is_object_action_set: BOOLEAN -- Is procedure to call on every object set? -- (from OBJECT_GRAPH_TRAVERSABLE) do Result := object_action /= Void end is_root_object_set: BOOLEAN -- Is starting point of graph traversing set? -- (from OBJECT_GRAPH_TRAVERSABLE) do Result := root_object /= Void end is_skip_copy_semantics_reference: BOOLEAN -- Do we skip copy semantics reference from visited_objects during traversal? -- (from OBJECT_GRAPH_TRAVERSABLE) is_skip_transient: BOOLEAN -- Do we skip transient attribute during traversal? -- (from OBJECT_GRAPH_TRAVERSABLE) 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 feature -- Element change set_is_exception_on_copy_suppressed (v: like is_exception_on_copy_suppressed) -- Set set_is_exception_on_copy_suppressed with v. -- (from OBJECT_GRAPH_TRAVERSABLE) do is_exception_on_copy_suppressed := v ensure -- from OBJECT_GRAPH_TRAVERSABLE is_exception_on_copy_suppressed_set: is_exception_on_copy_suppressed = v end set_is_exception_propagated (v: like is_exception_propagated) -- Set is_exception_propagated with v. -- (from OBJECT_GRAPH_TRAVERSABLE) do is_exception_propagated := v ensure -- from OBJECT_GRAPH_TRAVERSABLE is_exception_propagated_set: is_exception_propagated = v end set_is_skip_copy_semantics_reference (v: like is_skip_copy_semantics_reference) -- Set is_skip_copy_semantics_reference with v. -- (from OBJECT_GRAPH_TRAVERSABLE) do is_skip_copy_semantics_reference := v ensure -- from OBJECT_GRAPH_TRAVERSABLE is_skip_copy_semantics_reference_set: is_skip_copy_semantics_reference = v end set_is_skip_transient (v: like is_skip_transient) -- Set is_skip_transient with v. -- (from OBJECT_GRAPH_TRAVERSABLE) do is_skip_transient := v ensure -- from OBJECT_GRAPH_TRAVERSABLE is_skip_transient_set: is_skip_transient = v end set_object_action (an_object_action: like object_action) obsolete "Use set_on_processing_object_action to read and manipulate objects via REFLECTED_OBJECT. [2017-05-31]" -- Set object_action with an_object_action. -- (from OBJECT_GRAPH_TRAVERSABLE) require -- from OBJECT_GRAPH_TRAVERSABLE an_object_action_not_void: an_object_action /= Void do object_action := an_object_action ensure -- from OBJECT_GRAPH_TRAVERSABLE an_object_action_set: object_action = an_object_action and is_object_action_set end set_on_processing_object_action (an_action: like on_processing_object_action) -- Set on_processing_object_action with an_action -- (from OBJECT_GRAPH_TRAVERSABLE) require -- from OBJECT_GRAPH_TRAVERSABLE an_action_not_void: an_action /= Void do on_processing_object_action := an_action ensure -- from OBJECT_GRAPH_TRAVERSABLE on_processing_object_action_set: on_processing_object_action = an_action end set_on_processing_reference_action (an_action: like on_processing_reference_action) -- Set on_processing_object_action with an_action -- (from OBJECT_GRAPH_TRAVERSABLE) require -- from OBJECT_GRAPH_TRAVERSABLE an_action_not_void: an_action /= Void do on_processing_reference_action := an_action ensure -- from OBJECT_GRAPH_TRAVERSABLE on_processing_reference_action_set: on_processing_reference_action = an_action end set_root_object (an_object: like root_object) -- Set root_object with 'an_object'. -- (from OBJECT_GRAPH_TRAVERSABLE) require -- from OBJECT_GRAPH_TRAVERSABLE an_object_not_void: an_object /= Void do root_object := an_object ensure -- from OBJECT_GRAPH_TRAVERSABLE root_object_set: root_object = an_object and is_root_object_set end 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: OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE) -- 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: OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE) -- 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: OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE -- 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: OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE) -- 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: OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE -- 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: OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE -- 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 OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE 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 OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE -- 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 frozen traverse -- Traverse object structure starting at 'root_object' and call object_action -- on every object in the graph. -- (from OBJECT_GRAPH_TRAVERSABLE) require -- from OBJECT_GRAPH_TRAVERSABLE root_object_available: is_root_object_set local l_marker: detachable OBJECT_GRAPH_MARKER l_has_lock: BOOLEAN l_retried: BOOLEAN do if not l_retried and then attached root_object as l_root then create l_marker; l_marker.lock_marking l_has_lock := True internal_traverse (l_root); l_marker.unlock_marking has_failed := False end rescue l_retried := True has_failed := True if l_has_lock and then l_marker /= Void then l_marker.unlock_marking end if not is_exception_propagated then retry end end wipe_out -- Clear current to default values -- (from OBJECT_GRAPH_TRAVERSABLE) do visited_objects := Void visited_types := Void object_action := Void root_object := Void ensure -- from OBJECT_GRAPH_TRAVERSABLE visited_objects_reset: visited_objects = Void object_action_not_set: not is_object_action_set root_object_not_set: not is_root_object_set end feature {NONE} -- Implementation Default_size: INTEGER_32 = 200 -- Default size for containers used during object traversal -- (from OBJECT_GRAPH_TRAVERSABLE) internal_traverse (a_root_object: ANY) -- Traverse object structure starting at 'root_object' and call object_action -- on every object in the graph. -- (from OBJECT_GRAPH_TRAVERSABLE) local i, nb: INTEGER_32 l_object: separate ANY l_field: like {TUPLE}.reference_item l_field_type: INTEGER_32 l_marker: OBJECT_GRAPH_MARKER l_objects_to_visit: like new_dispenser l_visited: like visited_objects l_visited_types: like visited_types l_action: like object_action l_on_processing_object_action: like on_processing_object_action l_spec: like visited_objects.area l_reflected_reference_object: REFLECTED_REFERENCE_OBJECT l_reflected_object: REFLECTED_OBJECT l_has_reference_with_copy_semantics: BOOLEAN l_on_processing_reference_action: like on_processing_reference_action l_referee_object_cache: REFLECTED_REFERENCE_OBJECT l_referee_object: REFLECTED_OBJECT do from create l_marker create l_reflected_reference_object.make (a_root_object); l_marker.mark (a_root_object) create l_visited.make (Default_size) create l_visited_types.make (Default_size) l_objects_to_visit := new_dispenser; l_objects_to_visit.put (a_root_object) l_action := object_action l_on_processing_object_action := on_processing_object_action l_on_processing_reference_action := on_processing_reference_action create l_referee_object_cache.make (a_root_object) until l_objects_to_visit.is_empty loop l_object := l_objects_to_visit.item; l_objects_to_visit.remove if attached {REFLECTED_OBJECT} l_object as l_copy_semantics_object then check embedded_or_copy_semantics: attached {REFLECTED_COPY_SEMANTICS_OBJECT} l_copy_semantics_object or (attached {REFLECTED_REFERENCE_OBJECT} l_copy_semantics_object as l_ref_obj and then l_ref_obj.physical_offset /= 0) end l_has_reference_with_copy_semantics := True l_reflected_object := l_copy_semantics_object if not is_skip_copy_semantics_reference then l_visited.extend (l_copy_semantics_object.object) end else l_reflected_reference_object.set_object (l_object) l_reflected_object := l_reflected_reference_object if l_reflected_reference_object.is_expanded then l_has_reference_with_copy_semantics := True if not is_skip_copy_semantics_reference then l_visited.extend (l_object) end else l_visited.extend (l_object) end end; l_visited_types.put (l_reflected_object.dynamic_type, l_reflected_object.dynamic_type) if l_action /= Void or l_on_processing_object_action /= Void then if not is_exception_on_copy_suppressed and then l_reflected_object.is_expanded then raise_unwanted_copy_exception else if l_action /= Void then l_action.call ([l_object]) end if attached l_on_processing_object_action then l_on_processing_object_action.call ([l_reflected_object]) end end end if l_reflected_object.is_special then if l_reflected_object.is_special_of_reference then if attached {SPECIAL [detachable ANY]} l_object as l_sp then from i := 0 nb := l_sp.count until i = nb loop if l_reflected_object.is_special_copy_semantics_item (i) then l_referee_object := l_reflected_object.special_copy_semantics_item (i) l_field := l_referee_object; l_objects_to_visit.put (l_field) if attached l_on_processing_reference_action then l_on_processing_reference_action.call ([l_reflected_object, l_referee_object]) end else l_field := l_sp.item (i) if l_field /= Void and then not l_marker.is_marked (l_field) then l_marker.mark (l_field); l_objects_to_visit.put (l_field) end if attached l_on_processing_reference_action and attached l_field then l_referee_object_cache.set_object (l_field) l_referee_object := l_referee_object_cache; l_on_processing_reference_action.call ([l_reflected_object, l_referee_object]) end end i := i + 1 end end elseif l_reflected_object.is_special_of_expanded then if attached {SPECIAL [ANY]} l_object as l_sp then from i := 0 nb := l_sp.count until i = nb loop l_field := l_sp.item (i); l_objects_to_visit.put (l_field) if attached l_on_processing_reference_action then if is_exception_on_copy_suppressed then l_referee_object_cache.set_object (l_field) l_referee_object := l_referee_object_cache; l_on_processing_reference_action.call ([l_reflected_object, l_referee_object]) else raise_unwanted_copy_exception end end i := i + 1 end end end elseif l_reflected_object.is_tuple then if attached {TUPLE} l_object as l_tuple_obj then from i := 1 nb := l_tuple_obj.count + 1 until i = nb loop if l_tuple_obj.is_reference_item (i) then l_field := l_tuple_obj.reference_item (i) if l_field /= Void and then not l_marker.is_marked (l_field) then l_marker.mark (l_field); l_objects_to_visit.put (l_field) end if attached l_on_processing_reference_action and attached l_field then if is_exception_on_copy_suppressed then l_referee_object_cache.set_object (l_field) l_referee_object := l_referee_object_cache; l_on_processing_reference_action.call ([l_reflected_object, l_referee_object]) else raise_unwanted_copy_exception end end end i := i + 1 end end else from i := 1 nb := l_reflected_object.field_count + 1 variant nb - i until i = nb loop l_field_type := l_reflected_object.field_type (i) if l_field_type = {REFLECTOR_CONSTANTS}.reference_type or l_field_type = {REFLECTOR_CONSTANTS}.expanded_type then if not is_skip_transient or else not l_reflected_object.is_field_transient (i) then if l_field_type = {REFLECTOR_CONSTANTS}.expanded_type then l_referee_object := l_reflected_object.expanded_field (i); l_objects_to_visit.put (l_referee_object) if attached l_on_processing_reference_action then l_on_processing_reference_action.call ([l_reflected_object, l_referee_object]) end elseif l_reflected_object.is_copy_semantics_field (i) then l_referee_object := l_reflected_object.copy_semantics_field (i); l_objects_to_visit.put (l_referee_object) if attached l_on_processing_reference_action then l_on_processing_reference_action.call ([l_reflected_object, l_referee_object]) end else l_field := l_reflected_object.reference_field (i) if l_field /= Void then if not l_marker.is_marked (l_field) then l_marker.mark (l_field); l_objects_to_visit.put (l_field) end if attached l_on_processing_reference_action then l_referee_object_cache.set_object (l_field) l_referee_object := l_referee_object_cache; l_on_processing_reference_action.call ([l_reflected_object, l_referee_object]) end end end end end i := i + 1 end end end from i := 0 nb := l_visited.count l_spec := l_visited.area until i = nb loop l_marker.unmark (l_spec.item (i)) i := i + 1 end visited_objects := l_visited visited_types := l_visited_types has_reference_with_copy_semantics := l_has_reference_with_copy_semantics end new_dispenser: ARRAYED_STACK [separate ANY] -- Create the dispenser to use for storing visited objects. do create Result.make (Default_size) ensure -- from OBJECT_GRAPH_TRAVERSABLE new_dispenser_not_void: Result /= Void end raise_unwanted_copy_exception -- (from OBJECT_GRAPH_TRAVERSABLE) local l_exception: DEVELOPER_EXCEPTION do create l_exception; l_exception.set_description ("An agent has been invoked on a copy of an object during object graph traversal.%NThis happens in TUPLE containing objects with copy-semantics or in SPECIAL[XX],%Nwhere XX is a user-defined expanded type."); l_exception.raise 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 invariant -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) note copyright: "Copyright (c) 1984-2013, 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 OBJECT_GRAPH_DEPTH_FIRST_TRAVERSABLE
Generated by ISE EiffelStudio