PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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