diff options
author | Henrik Kinnunen <fluxgen@fluxbox.org> | 2008-09-18 20:24:35 (GMT) |
---|---|---|
committer | Henrik Kinnunen <fluxgen@fluxbox.org> | 2008-09-18 20:24:35 (GMT) |
commit | e4d4717703b365bc14f189bf36b3edb1e4430b90 (patch) | |
tree | 1e678e6d0569714ab8c78fa0900c18ee1bb0f10a /src/FbTk/MemFun.hh | |
parent | 8e97963e4211963f960c52c8a8f4bf5cd135ad2f (diff) | |
download | fluxbox-e4d4717703b365bc14f189bf36b3edb1e4430b90.zip fluxbox-e4d4717703b365bc14f189bf36b3edb1e4430b90.tar.bz2 |
Added new Signal/Slot system in FbTk
This is suppose to replace the obsolete Subject/Observer classes.
See the src/tests/testSignals.cc for basic usage.
Diffstat (limited to 'src/FbTk/MemFun.hh')
-rw-r--r-- | src/FbTk/MemFun.hh | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/FbTk/MemFun.hh b/src/FbTk/MemFun.hh new file mode 100644 index 0000000..4c834dd --- /dev/null +++ b/src/FbTk/MemFun.hh | |||
@@ -0,0 +1,139 @@ | |||
1 | // MemFun.hh for FbTk, Fluxbox Toolkit | ||
2 | // Copyright (c) 2008 Henrik Kinnunen (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 | #ifndef FBTK_MEM_FUN_HH | ||
23 | #define FBTK_MEM_FUN_HH | ||
24 | |||
25 | namespace FbTk { | ||
26 | |||
27 | /// No argument functor | ||
28 | template <typename ReturnType, typename Object> | ||
29 | class MemFun0 { | ||
30 | public: | ||
31 | typedef ReturnType (Object:: *Action)(); | ||
32 | |||
33 | MemFun0(Object& obj, Action action): | ||
34 | m_obj(obj), | ||
35 | m_action(action) { | ||
36 | } | ||
37 | |||
38 | void operator ()() { | ||
39 | (m_obj.*m_action)(); | ||
40 | } | ||
41 | |||
42 | private: | ||
43 | Object& m_obj; | ||
44 | Action m_action; | ||
45 | }; | ||
46 | |||
47 | |||
48 | template <typename ReturnType, typename Object> | ||
49 | MemFun0<ReturnType, Object> | ||
50 | MemFun( Object& obj, ReturnType (Object:: *action)() ) { | ||
51 | return MemFun0<ReturnType, Object>(obj, action); | ||
52 | } | ||
53 | |||
54 | /// One argument functor | ||
55 | template <typename ReturnType, typename Object, typename Arg1> | ||
56 | class MemFun1 { | ||
57 | public: | ||
58 | typedef ReturnType (Object:: *Action)(Arg1); | ||
59 | |||
60 | MemFun1(Object& obj, Action action): | ||
61 | m_obj(obj), | ||
62 | m_action(action) { | ||
63 | } | ||
64 | |||
65 | void operator ()(Arg1 arg1) { | ||
66 | (m_obj.*m_action)(arg1); | ||
67 | } | ||
68 | |||
69 | private: | ||
70 | Object& m_obj; | ||
71 | Action m_action; | ||
72 | }; | ||
73 | |||
74 | /// One argument functor helper function | ||
75 | template <typename ReturnType, typename Object, typename Arg1> | ||
76 | MemFun1<ReturnType, Object, Arg1> | ||
77 | MemFun( Object& obj, ReturnType (Object:: *action)(Arg1) ) { | ||
78 | return MemFun1<ReturnType, Object, Arg1>(obj, action); | ||
79 | } | ||
80 | |||
81 | /// Two argument functor | ||
82 | template <typename ReturnType, typename Object, typename Arg1, typename Arg2> | ||
83 | class MemFun2 { | ||
84 | public: | ||
85 | typedef ReturnType (Object:: *Action)(Arg1,Arg2); | ||
86 | |||
87 | MemFun2(Object& obj, Action action): | ||
88 | m_obj(obj), | ||
89 | m_action(action) { | ||
90 | } | ||
91 | |||
92 | void operator ()(Arg1 arg1, Arg2 arg2) { | ||
93 | (m_obj.*m_action)(arg1, arg2); | ||
94 | } | ||
95 | |||
96 | private: | ||
97 | Object& m_obj; | ||
98 | Action m_action; | ||
99 | }; | ||
100 | |||
101 | /// Two argument functor helper function | ||
102 | template <typename ReturnType, typename Object, typename Arg1, typename Arg2> | ||
103 | MemFun2<ReturnType, Object, Arg1, Arg2> | ||
104 | MemFun( Object& obj, ReturnType (Object:: *action)(Arg1,Arg2) ) { | ||
105 | return MemFun2<ReturnType, Object, Arg1, Arg2>(obj, action); | ||
106 | } | ||
107 | |||
108 | /// Three argument functor | ||
109 | template <typename ReturnType, typename Object, | ||
110 | typename Arg1, typename Arg2, typename Arg3> | ||
111 | class MemFun3 { | ||
112 | public: | ||
113 | typedef ReturnType (Object:: *Action)(Arg1,Arg2,Arg3); | ||
114 | |||
115 | MemFun3(Object& obj, Action action): | ||
116 | m_obj(obj), | ||
117 | m_action(action) { | ||
118 | } | ||
119 | |||
120 | void operator ()(Arg1 arg1, Arg2 arg2, Arg3 arg3) { | ||
121 | (m_obj.*m_action)(arg1, arg2, arg3); | ||
122 | } | ||
123 | |||
124 | private: | ||
125 | Object& m_obj; | ||
126 | Action m_action; | ||
127 | }; | ||
128 | |||
129 | /// Three argument functor helper | ||
130 | template <typename ReturnType, typename Object, typename Arg1, typename Arg2, typename Arg3> | ||
131 | MemFun3<ReturnType, Object, Arg1, Arg2, Arg3> | ||
132 | MemFun( Object& obj, ReturnType (Object:: *action)(Arg1, Arg2, Arg3) ) { | ||
133 | return MemFun3<ReturnType, Object, Arg1, Arg2, Arg3>(obj, action); | ||
134 | } | ||
135 | |||
136 | } // namespace FbTk | ||
137 | |||
138 | #endif // FBTK_MEM_FUN_HH | ||
139 | |||