/src/lib/signal/signal_2.e

http://github.com/tybor/Liberty · Specman e · 113 lines · 65 code · 11 blank · 37 comment · 2 complexity · 0d94acc77456e8ab4bc890edf7e5d497 MD5 · raw file

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