diff options
author | Henrik Kinnunen <fluxgen@fluxbox.org> | 2008-04-27 19:22:18 (GMT) |
---|---|---|
committer | Henrik Kinnunen <fluxgen@fluxbox.org> | 2008-04-27 19:22:18 (GMT) |
commit | 80b10f7772b91f1f4a61eace4e5235a79060e1f0 (patch) | |
tree | 27057e59c41188e7c2f724614233265fed03c9b3 /src/FbTk/SimpleObserver.hh | |
parent | 40e17b4d0ef1b9503f21509e697dd4dcb24d2bbf (diff) | |
download | fluxbox-80b10f7772b91f1f4a61eace4e5235a79060e1f0.zip fluxbox-80b10f7772b91f1f4a61eace4e5235a79060e1f0.tar.bz2 |
Added SimpleObserver class.
This class works in the same way as the SimpleCommand class.
Use it with the makeObserver function.
It calls the receiver's member function when the subject sends
a signal.
Diffstat (limited to 'src/FbTk/SimpleObserver.hh')
-rw-r--r-- | src/FbTk/SimpleObserver.hh | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/FbTk/SimpleObserver.hh b/src/FbTk/SimpleObserver.hh new file mode 100644 index 0000000..aafdf59 --- /dev/null +++ b/src/FbTk/SimpleObserver.hh | |||
@@ -0,0 +1,70 @@ | |||
1 | // SimpleObserver.hh | ||
2 | // Copyright (c) 2008 Fluxbox Team (fluxgen at fluxbox dot org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | #include "Observer.hh" | ||
23 | #include "SimpleCommand.hh" | ||
24 | |||
25 | namespace FbTk { | ||
26 | |||
27 | /** Functor for observers, instead of using this directly use makeObserver. | ||
28 | * Usage: | ||
29 | * @code | ||
30 | * class SomeClass { | ||
31 | * public: | ||
32 | * void doAction(); | ||
33 | * }; | ||
34 | * | ||
35 | * SomeClass some; | ||
36 | * | ||
37 | * Observer* obs = makeProxyObserver(some, &SomeClass::doAction); | ||
38 | * SomeSubject subj; | ||
39 | * subj.attach(obs); | ||
40 | * @endcode | ||
41 | */ | ||
42 | template <typename Receiver> | ||
43 | class SimpleObserver: public Observer { | ||
44 | public: | ||
45 | typedef void (Receiver::* Action)(); | ||
46 | SimpleObserver(Receiver &r, Action a): | ||
47 | m_receiver(r), m_action(a) { | ||
48 | |||
49 | } | ||
50 | void update(Subject *changedSubj) { | ||
51 | (m_receiver.*m_action)(); | ||
52 | } | ||
53 | private: | ||
54 | Receiver &m_receiver; | ||
55 | Action m_action; | ||
56 | }; | ||
57 | |||
58 | // Helpers | ||
59 | /** Creates an observer that takes no arguments. | ||
60 | * @param receiver The receiving instance. | ||
61 | * @param action A function in the receiving class. | ||
62 | * @return allocated simple observer. @see SimpleObserver | ||
63 | */ | ||
64 | template <typename Receiver, typename Action> | ||
65 | Observer *makeObserver(Receiver &receiver, Action action) { | ||
66 | return new SimpleObserver<Receiver>( receiver, action ); | ||
67 | } | ||
68 | |||
69 | } | ||
70 | |||