Fixed ERROR_HANDLER.destroy

Fixed and export ERROR_HANDLER.remove_synchronized_handler
Added comments
Added associated autotests
This commit is contained in:
Jocelyn Fiat
2012-02-15 11:03:30 +01:00
parent fbec89f354
commit 145b129b28
4 changed files with 95 additions and 23 deletions

View File

@@ -21,13 +21,13 @@ feature -- Access
result_not_zero: Result /= 0 result_not_zero: Result /= 0
end end
name: STRING name: READABLE_STRING_8
deferred deferred
ensure ensure
result_attached: Result /= Void result_attached: Result /= Void
end end
message: detachable STRING_32 message: detachable READABLE_STRING_32
-- Potential error message -- Potential error message
deferred deferred
end end
@@ -80,7 +80,7 @@ invariant
name_attached: name /= Void name_attached: name /= Void
note note
copyright: "Copyright (c) 1984-2011, Eiffel Software and others" copyright: "2011-2012, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software

View File

@@ -16,12 +16,12 @@ create
feature {NONE} -- Initialization feature {NONE} -- Initialization
make (a_code: INTEGER; a_name: STRING; a_message: detachable like message) make (a_code: INTEGER; a_name: like name; a_message: detachable like message)
-- Initialize `Current'. -- Initialize `Current'.
do do
code := a_code code := a_code
name := a_name name := a_name
if attached a_message then if a_message /= Void then
message := a_message message := a_message
else else
message := "Error: " + a_name + " (code=" + a_code.out + ")" message := "Error: " + a_name + " (code=" + a_code.out + ")"
@@ -32,9 +32,9 @@ feature -- Access
code: INTEGER code: INTEGER
name: STRING name: READABLE_STRING_8
message: STRING_32 message: detachable READABLE_STRING_32
feature -- Visitor feature -- Visitor
@@ -45,7 +45,7 @@ feature -- Visitor
end end
note note
copyright: "Copyright (c) 1984-2011, Eiffel Software and others" copyright: "2011-2012, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software

View File

@@ -34,6 +34,7 @@ feature -- Status
end end
count: INTEGER count: INTEGER
-- Number of error
do do
Result := errors.count Result := errors.count
end end
@@ -63,10 +64,9 @@ feature -- Events
feature -- Synchronization feature -- Synchronization
add_synchronized_handler (h: ERROR_HANDLER) add_synchronized_handler (h: ERROR_HANDLER)
-- Add synchronization between `h' and `Current'
--| the same handler can be added more than once --| the same handler can be added more than once
--| it will be synchronized only once --| it will be synchronized only once
require
no_cycle: not has_synchronized_handler_in_cycle (h)
local local
lst: like synchronized_handlers lst: like synchronized_handlers
do do
@@ -84,9 +84,20 @@ feature -- Synchronization
end end
end end
has_synchronized_handler_in_cycle (h: ERROR_HANDLER): BOOLEAN remove_synchronized_handler (h: ERROR_HANDLER)
-- Remove synchronization between `h' and `Current'
do do
Result := False if attached synchronized_handlers as lst and then not lst.is_empty then
synchronized_handlers := Void
lst.prune_all (h)
h.remove_synchronized_handler (Current)
synchronized_handlers := lst
if lst.is_empty then
synchronized_handlers := Void
end
end
end end
feature {ERROR_HANDLER} -- Synchronization implementation feature {ERROR_HANDLER} -- Synchronization implementation
@@ -143,16 +154,6 @@ feature {ERROR_HANDLER} -- Synchronization implementation
end end
end end
remove_synchronized_handler (h: ERROR_HANDLER)
do
if attached synchronized_handlers as lst then
lst.prune_all (h)
if lst.is_empty then
synchronized_handlers := Void
end
end
end
feature {NONE} -- Event: implementation feature {NONE} -- Event: implementation
on_error_added (e: ERROR) on_error_added (e: ERROR)
@@ -177,6 +178,7 @@ feature {NONE} -- Event: implementation
end end
on_reset on_reset
-- `reset' was just called
local local
sync_list: LINKED_LIST [ERROR_HANDLER] sync_list: LINKED_LIST [ERROR_HANDLER]
do do
@@ -237,6 +239,7 @@ feature -- Basic operation
feature -- Access feature -- Access
as_single_error: detachable ERROR as_single_error: detachable ERROR
-- All error(s) concatenated into one single error.
do do
if count > 1 then if count > 1 then
create {ERROR_GROUP} Result.make (errors) create {ERROR_GROUP} Result.make (errors)
@@ -248,6 +251,7 @@ feature -- Access
end end
as_string_representation: STRING as_string_representation: STRING
-- String representation of all error(s).
require require
has_error has_error
do do
@@ -271,20 +275,30 @@ feature -- Element changes
end end
reset reset
-- Reset error handler
do do
if errors.count > 0 then if errors.count > 0 then
errors.wipe_out errors.wipe_out
on_reset on_reset
end end
ensure
has_no_error: not has_error
count = 0
end end
destroy destroy
-- Destroy Current, and remove any synchronization
do do
error_added_actions.wipe_out error_added_actions.wipe_out
if attached synchronized_handlers as lst then if attached synchronized_handlers as lst then
lst.item.remove_synchronized_handler (Current) across
lst as c
loop
c.item.remove_synchronized_handler (Current)
end
end end
synchronized_handlers := Void synchronized_handlers := Void
reset
end end
note note

View File

@@ -108,6 +108,64 @@ feature -- Test routines
end end
test_remove_sync
note
testing: "error"
local
h1, h2, h3: ERROR_HANDLER
-- cl: CELL [INTEGER]
do
create h1.make
create h2.make
create h3.make
h1.add_synchronized_handler (h2)
h2.add_synchronized_handler (h3)
h3.add_synchronized_handler (h1)
h1.add_custom_error (123, "abc", "abc error occurred")
h1.add_custom_error (456, "def", "def error occurred")
assert ("has 2 errors", h1.count = 2 and h2.count = h1.count and h3.count = h2.count)
h2.remove_synchronized_handler (h3)
h2.remove_synchronized_handler (h1)
h1.add_custom_error (789, "ghi", "ghi error occurred")
assert ("correct count of errors", h1.count = 3 and h2.count = 2 and h3.count = h1.count)
h1.reset
assert ("reset then no error", h1.count = 0 and h2.count = 2 and h3.count = h1.count)
end
test_destroy
note
testing: "error"
local
h1, h2, h3: ERROR_HANDLER
-- cl: CELL [INTEGER]
do
create h1.make
create h2.make
create h3.make
h1.add_synchronized_handler (h2)
h2.add_synchronized_handler (h3)
h3.add_synchronized_handler (h1)
h1.add_custom_error (123, "abc", "abc error occurred")
h1.add_custom_error (456, "def", "def error occurred")
assert ("has 2 errors", h1.count = 2 and h2.count = h1.count and h3.count = h2.count)
h2.destroy
h1.add_custom_error (789, "ghi", "ghi error occurred")
assert ("correct count of errors", h1.count = 3 and h2.count = 0 and h3.count = h1.count)
h1.reset
assert ("reset then no error", h1.count = 0 and h2.count = h1.count and h3.count = h1.count)
end
note note
copyright: "Copyright (c) 1984-2011, Eiffel Software and others" copyright: "Copyright (c) 1984-2011, Eiffel Software and others"