/src/lib/signal/signal_4.e
Specman e | 113 lines | 65 code | 11 blank | 37 comment | 2 complexity | aee08b9e3fc237e2bff64ef0a817d73a MD5 | raw file
1-- This file is part of a Liberty Eiffel library. 2-- See the full copyright at the end. 3-- 4class SIGNAL_4[E_, F_, G_, H_] 5 -- 6 -- See tutorial/signal/signals.txt for usage 7 -- 8 9create {ANY} 10 make 11 12feature {} 13 callbacks: FAST_ARRAY[PROCEDURE[TUPLE[E_, F_, G_, H_]]] 14 15 index, last: INTEGER 16 -- work to do while emit is between index and last. 17 18 make 19 -- Initialize new signal object 20 do 21 create callbacks.make(0) 22 ensure 23 callbacks.is_empty 24 end 25 26feature {ANY} 27 connect (p: PROCEDURE[TUPLE[E_, F_, G_, H_]]) 28 -- Connect procedure to be called when signal is emitted 29 -- See also last_connect_id 30 require 31 p /= Void 32 do 33 callbacks.add_last(p) 34 ensure 35 not callbacks.is_empty 36 last_connect_id = p 37 end 38 39 emit (val1: E_; val2: F_; val3: G_; val4: H_) 40 -- Emit signal, ie. already registered procedure will be called 41 -- in registration order except if removed by another before. 42 do 43 from 44 index := callbacks.lower 45 last := callbacks.upper 46 until 47 index > last 48 loop 49 callbacks.item(index).call([val1, val2, val3, val4]) 50 index := index + 1 51 end 52 end 53 54 last_connect_id: PROCEDURE[TUPLE[E_, F_, G_, H_]] 55 -- return identifier on the last connect which may be used 56 -- for disconnect (unregister procedure) 57 require 58 not is_empty 59 do 60 Result := callbacks.last 61 ensure 62 Result /= Void 63 end 64 65 disconnect (connect_identifier: PROCEDURE[TUPLE[E_, F_, G_, H_]]) 66 -- Unregister procedure for this signal. If the same 67 -- procedure was registered many times, only first is removed. 68 local 69 i: INTEGER 70 do 71 i := callbacks.fast_first_index_of(connect_identifier) 72 if callbacks.valid_index(i) then 73 callbacks.remove(i) 74 last := last - 1 75 if i <= index then 76 index := index - 1 77 end 78 end 79 ensure 80 old callbacks.fast_has(connect_identifier) implies callbacks.count = old callbacks.count - 1 81 old (not callbacks.fast_has(connect_identifier)) implies callbacks.count = old callbacks.count 82 end 83 84 is_empty: BOOLEAN 85 -- return True if no callback is registered for this signal 86 do 87 Result := callbacks.is_empty 88 end 89 90invariant 91 callbacks /= Void 92 93end -- class SIGNAL_4 94-- 95-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file. 96-- 97-- Permission is hereby granted, free of charge, to any person obtaining a copy 98-- of this software and associated documentation files (the "Software"), to deal 99-- in the Software without restriction, including without limitation the rights 100-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 101-- copies of the Software, and to permit persons to whom the Software is 102-- furnished to do so, subject to the following conditions: 103-- 104-- The above copyright notice and this permission notice shall be included in 105-- all copies or substantial portions of the Software. 106-- 107-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 108-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 109-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 110-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 111-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 112-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 113-- THE SOFTWARE.