summaryrefslogtreecommitdiff
path: root/src/FbTk/MemFun.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/MemFun.hh')
-rw-r--r--src/FbTk/MemFun.hh139
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
25namespace FbTk {
26
27/// No argument functor
28template <typename ReturnType, typename Object>
29class MemFun0 {
30public:
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
42private:
43 Object& m_obj;
44 Action m_action;
45};
46
47
48template <typename ReturnType, typename Object>
49MemFun0<ReturnType, Object>
50MemFun( Object& obj, ReturnType (Object:: *action)() ) {
51 return MemFun0<ReturnType, Object>(obj, action);
52}
53
54/// One argument functor
55template <typename ReturnType, typename Object, typename Arg1>
56class MemFun1 {
57public:
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
69private:
70 Object& m_obj;
71 Action m_action;
72};
73
74/// One argument functor helper function
75template <typename ReturnType, typename Object, typename Arg1>
76MemFun1<ReturnType, Object, Arg1>
77MemFun( Object& obj, ReturnType (Object:: *action)(Arg1) ) {
78 return MemFun1<ReturnType, Object, Arg1>(obj, action);
79}
80
81/// Two argument functor
82template <typename ReturnType, typename Object, typename Arg1, typename Arg2>
83class MemFun2 {
84public:
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
96private:
97 Object& m_obj;
98 Action m_action;
99};
100
101/// Two argument functor helper function
102template <typename ReturnType, typename Object, typename Arg1, typename Arg2>
103MemFun2<ReturnType, Object, Arg1, Arg2>
104MemFun( Object& obj, ReturnType (Object:: *action)(Arg1,Arg2) ) {
105 return MemFun2<ReturnType, Object, Arg1, Arg2>(obj, action);
106}
107
108/// Three argument functor
109template <typename ReturnType, typename Object,
110 typename Arg1, typename Arg2, typename Arg3>
111class MemFun3 {
112public:
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
124private:
125 Object& m_obj;
126 Action m_action;
127};
128
129/// Three argument functor helper
130template <typename ReturnType, typename Object, typename Arg1, typename Arg2, typename Arg3>
131MemFun3<ReturnType, Object, Arg1, Arg2, Arg3>
132MemFun( 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