1
// MemFun.hh for FbTk, Fluxbox Toolkit
2
// Copyright (c) 2008 Henrik Kinnunen (fluxgen at fluxbox dot org)
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:
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
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.
22
#ifndef FBTK_MEM_FUN_HH
23
#define FBTK_MEM_FUN_HH
25
#include "SelectArg.hh"
29
/// No argument functor
30
template <typename ReturnType, typename Object>
33
typedef ReturnType (Object:: *Action)();
35
MemFun0(Object& obj, Action action):
50
template <typename ReturnType, typename Object>
51
MemFun0<ReturnType, Object>
52
MemFun( Object& obj, ReturnType (Object:: *action)() ) {
53
return MemFun0<ReturnType, Object>(obj, action);
56
/// One argument functor
57
template <typename ReturnType, typename Object, typename Arg1>
60
typedef ReturnType (Object:: *Action)(Arg1);
62
MemFun1(Object& obj, Action action):
67
void operator ()(Arg1 arg1) {
68
(m_obj.*m_action)(arg1);
76
/// One argument functor helper function
77
template <typename ReturnType, typename Object, typename Arg1>
78
MemFun1<ReturnType, Object, Arg1>
79
MemFun( Object& obj, ReturnType (Object:: *action)(Arg1) ) {
80
return MemFun1<ReturnType, Object, Arg1>(obj, action);
83
/// Two argument functor
84
template <typename ReturnType, typename Object, typename Arg1, typename Arg2>
87
typedef ReturnType (Object:: *Action)(Arg1,Arg2);
89
MemFun2(Object& obj, Action action):
94
void operator ()(Arg1 arg1, Arg2 arg2) {
95
(m_obj.*m_action)(arg1, arg2);
103
/// Two argument functor helper function
104
template <typename ReturnType, typename Object, typename Arg1, typename Arg2>
105
MemFun2<ReturnType, Object, Arg1, Arg2>
106
MemFun( Object& obj, ReturnType (Object:: *action)(Arg1,Arg2) ) {
107
return MemFun2<ReturnType, Object, Arg1, Arg2>(obj, action);
110
/// Three argument functor
111
template <typename ReturnType, typename Object,
112
typename Arg1, typename Arg2, typename Arg3>
115
typedef ReturnType (Object:: *Action)(Arg1,Arg2,Arg3);
117
MemFun3(Object& obj, Action action):
122
void operator ()(Arg1 arg1, Arg2 arg2, Arg3 arg3) {
123
(m_obj.*m_action)(arg1, arg2, arg3);
131
/// Three argument functor helper
132
template <typename ReturnType, typename Object, typename Arg1, typename Arg2, typename Arg3>
133
MemFun3<ReturnType, Object, Arg1, Arg2, Arg3>
134
MemFun( Object& obj, ReturnType (Object:: *action)(Arg1, Arg2, Arg3) ) {
135
return MemFun3<ReturnType, Object, Arg1, Arg2, Arg3>(obj, action);
138
/// Ignores all arguments
139
template <typename ReturnType, typename Object>
140
class MemFun0IgnoreArgs: public MemFun0<ReturnType, Object> {
142
typedef MemFun0<ReturnType, Object> BaseType;
144
MemFun0IgnoreArgs(Object& obj,
145
typename BaseType::Action action):
146
BaseType(obj, action) {
149
template <typename IgnoreType1, typename IgnoreType2, typename IgnoreType3>
150
void operator ()(IgnoreType1&, IgnoreType2&, IgnoreType3&) {
151
BaseType::operator ()();
154
template <typename IgnoreType1, typename IgnoreType2>
155
void operator ()(IgnoreType1&, IgnoreType2&) {
156
BaseType::operator ()();
159
template <typename IgnoreType1>
160
void operator ()(IgnoreType1&) {
161
BaseType::operator ()();
165
/// Ignores second and third argument
166
template <typename ReturnType, typename Object, typename Arg1>
167
class MemFun1IgnoreArgs: public MemFun1<ReturnType, Object, Arg1> {
169
typedef MemFun1<ReturnType, Object, Arg1> BaseType;
171
MemFun1IgnoreArgs(Object& obj, typename BaseType::Action& action):
172
BaseType(obj, action) {
175
template <typename IgnoreType1, typename IgnoreType2>
176
void operator ()(Arg1 arg1, IgnoreType1&, IgnoreType2&) {
177
BaseType::operator ()(arg1);
180
template <typename IgnoreType>
181
void operator ()(Arg1 arg1, IgnoreType&) {
182
BaseType::operator ()(arg1);
186
/// Takes two arguments but ignores the third
187
template <typename ReturnType, typename Object, typename Arg1, typename Arg2>
188
class MemFun2IgnoreArgs: public MemFun2<ReturnType, Object, Arg1, Arg2> {
190
typedef MemFun2<ReturnType, Object, Arg1, Arg2> BaseType;
192
MemFun2IgnoreArgs(Object& obj, typename BaseType::Action& action):
193
BaseType(obj, action) {
196
template < typename IgnoreType >
197
void operator ()(Arg1 arg1, Arg2 arg2, IgnoreType&) {
198
BaseType::operator ()(arg1, arg2);
202
/// Creates functor that ignores all arguments.
203
template <typename ReturnType, typename Object>
204
MemFun0IgnoreArgs<ReturnType, Object>
205
MemFunIgnoreArgs( Object& obj, ReturnType (Object:: *action)() ) {
206
return MemFun0IgnoreArgs<ReturnType, Object>(obj, action);
209
/// Creates functor that ignores second and third argument.
210
template <typename ReturnType, typename Object, typename Arg1>
211
MemFun1IgnoreArgs<ReturnType, Object, Arg1>
212
MemFunIgnoreArgs( Object& obj, ReturnType (Object:: *action)(Arg1) ) {
213
return MemFun1IgnoreArgs<ReturnType, Object, Arg1>(obj, action);
216
/// Creates functor that ignores third argument.
217
template <typename ReturnType, typename Object, typename Arg1, typename Arg2>
218
MemFun2IgnoreArgs<ReturnType, Object, Arg1, Arg2>
219
MemFunIgnoreArgs( Object& obj, ReturnType (Object:: *action)(Arg1,Arg2) ) {
220
return MemFun2IgnoreArgs<ReturnType, Object, Arg1, Arg2>(obj, action);
224
* Creates a functor that selects a specific argument of three possible ones
225
* and uses it for the single argument operator.
227
template < int ArgNum, typename Functor>
228
class MemFunSelectArgImpl {
231
MemFunSelectArgImpl(Functor func):
235
template <typename Type1, typename Type2, typename Type3>
236
void operator ()(Type1& a, Type2& b, Type3& c) {
237
m_func(STLUtil::SelectArg<ArgNum>()(a, b, c));
240
template <typename Type1, typename Type2>
241
void operator ()(Type1& a, Type2& b) {
242
m_func(STLUtil::SelectArg<ArgNum>()(a, b));
245
template <typename Type1>
246
void operator ()(Type1& a) {
254
/// Creates a functor that selects the first argument of three possible ones
255
/// and uses it for the single argument operator.
256
template <typename ReturnType, typename Object, typename Arg1>
257
MemFunSelectArgImpl<0, MemFun1<ReturnType, Object, Arg1> >
258
MemFunSelectArg0(Object& obj, ReturnType (Object:: *action)(Arg1)) {
259
return MemFunSelectArgImpl<0, MemFun1<ReturnType, Object, Arg1> >(MemFun(obj, action));
262
/// Creates a functor that selects the second argument (or first if there is
263
/// only one) of three possible onesand uses it for the single argument operator.
264
template <typename ReturnType, typename Object, typename Arg1>
265
MemFunSelectArgImpl<1, MemFun1<ReturnType, Object, Arg1> >
266
MemFunSelectArg1(Object& obj, ReturnType (Object:: *action)(Arg1)) {
267
return MemFunSelectArgImpl<1, MemFun1<ReturnType, Object, Arg1> >(MemFun(obj, action));
270
/// Creates a functor that selects the third argument (or the last argument if there is
271
/// less than three arguments) of three possible onesand uses it for the single argument operator.
272
template <typename ReturnType, typename Object, typename Arg1>
273
MemFunSelectArgImpl<2, MemFun1<ReturnType, Object, Arg1> >
274
MemFunSelectArg2(Object& obj, ReturnType (Object:: *action)(Arg1)) {
275
return MemFunSelectArgImpl<2, MemFun1<ReturnType, Object, Arg1> >(MemFun(obj, action));
280
#endif // FBTK_MEM_FUN_HH