/src/lib/abilities/observable.e

http://github.com/tybor/Liberty · Specman e · 70 lines · 29 code · 7 blank · 34 comment · 0 complexity · fbc57d5ac489222caa6bc9fcfa60ebf4 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class OBSERVABLE
  5. --
  6. -- Part of the ''Observer'' Design Pattern. An object of this class should notify its observers whenever its
  7. -- state changes. Use the `notify' feature for that.
  8. --
  9. -- See also OBSERVER.
  10. --
  11. feature {ANY}
  12. add (o: OBSERVER[like Current])
  13. -- Add an observer that should be notified
  14. do
  15. observers.add_last(o)
  16. end
  17. remove (o: OBSERVER[like Current])
  18. -- Remove an observer that should not be notified anymore
  19. require
  20. has(o)
  21. local
  22. i: INTEGER
  23. do
  24. i := observers.first_index_of(o)
  25. observers.remove(i)
  26. end
  27. has (o: OBSERVER[like Current]): BOOLEAN
  28. -- True if the observer will be notified when the state of Current changes
  29. do
  30. Result := observers.has(o)
  31. end
  32. feature {}
  33. notify
  34. -- Notify all the observers that the state of Current changed
  35. do
  36. observers.for_each(agent {OBSERVER[like Current]}.update(Current))
  37. end
  38. feature {}
  39. observers: COLLECTION[OBSERVER[like Current]]
  40. -- The collection of observers
  41. invariant
  42. notifiable: observers /= Void
  43. end -- class OBSERVABLE
  44. --
  45. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  46. --
  47. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  48. -- of this software and associated documentation files (the "Software"), to deal
  49. -- in the Software without restriction, including without limitation the rights
  50. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  51. -- copies of the Software, and to permit persons to whom the Software is
  52. -- furnished to do so, subject to the following conditions:
  53. --
  54. -- The above copyright notice and this permission notice shall be included in
  55. -- all copies or substantial portions of the Software.
  56. --
  57. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  58. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  59. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  60. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  61. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  62. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  63. -- THE SOFTWARE.