summaryrefslogtreecommitdiff
path: root/sigc++/functors/macros/mem_fun.h.m4
blob: 9c83afd64c2c5c43f8d357ef5f3975f0ac0afbae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
dnl Copyright 2002, The libsigc++ Development Team
dnl
dnl This library is free software; you can redistribute it and/or
dnl modify it under the terms of the GNU Lesser General Public
dnl License as published by the Free Software Foundation; either
dnl version 2.1 of the License, or (at your option) any later version.
dnl
dnl This library is distributed in the hope that it will be useful,
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
dnl Lesser General Public License for more details.
dnl
dnl You should have received a copy of the GNU Lesser General Public
dnl License along with this library; if not, write to the Free Software
dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
dnl
divert(-1)

include(template.macros.m4)

define([MEMBER_FUNCTOR],[dnl
/** [$1]mem_functor wraps $3 methods with argument(s).
 * Use the convenience function mem_fun() to create an instance of [$1]mem_functor.
 *
 * The following template arguments are used:
 * - @e T_arg... Argument types used in the definition of operator()().
 * - @e T_return The return type of operator()().
 * - @e T_obj The object type.
 *
 * @ingroup mem_fun
 */
template<class T_return, class T_obj, class... T_arg>
using [$1]mem_functor =
  mem_functor_base<
    T_return (T_obj::*)(T_arg...) $3,
    $2 T_obj,
    T_return, T_obj, T_arg...>;
])

define([BOUND_MEMBER_FUNCTOR],[dnl

/** bound_[$1]mem_functor encapsulates a $3 method with arguments and an object instance.
 * Use the convenience function mem_fun() to create an instance of bound_[$1]mem_functor.
 *
 * The following template arguments are used:
 * - @e T_arg... Argument type used in the definition of operator()().
 * - @e T_return The return type of operator()().
 * - @e T_obj The object type.
 *
 * @ingroup mem_fun
 */
template<class T_return, class T_obj, class... T_arg>
using bound_[$1]mem_functor =
  bound_mem_functor_base<
    [$1]limit_reference<T_obj>,
    T_return (T_obj::*)(T_arg...) $3,
    $2 T_obj,
    T_return, T_obj, T_arg...>;

#ifndef DOXYGEN_SHOULD_SKIP_THIS
//template specialization of visitor<>::do_visit_each<>(action, functor):
/** Performs a functor on each of the targets of a functor.
 * The function overload for sigc::bound_[$1]mem_functor performs a functor
 * on the object instance stored in the sigc::bound_[$1]mem_functor object.
 *
 * @ingroup mem_fun
 */
template <class T_return, class T_obj, class... T_arg>
struct visitor<bound_[$1]mem_functor<T_return, T_obj, T_arg...> >
{
  template <class T_action>
  static void do_visit_each(const T_action& _A_action,
                            const bound_[$1]mem_functor<T_return, T_obj, T_arg...>& _A_target)
  {
    sigc::visit_each(_A_action, _A_target.obj_);
  }
};
#endif // DOXYGEN_SHOULD_SKIP_THIS
])

define([MEM_FUN],[dnl
/** Creates a functor of type sigc::[$1]mem_functor which wraps a $3 method.
 * @param _A_func Pointer to method that should be wrapped.
 * @return Functor that executes _A_func on invokation.
 *
 * @ingroup mem_fun
 */
template <class T_return, class T_obj, class... T_arg>
inline decltype(auto)
mem_fun(T_return (T_obj::*_A_func)(T_arg...) $3)
{ return [$1]mem_functor<T_return, T_obj, T_arg...>(_A_func); }

])
define([BOUND_MEM_FUN],[dnl
/** Creates a functor of type sigc::bound_[$1]mem_functor which encapsulates a method and an object instance.
 * @param _A_obj Pointer to object instance the functor should operate on.
 * @param _A_func Pointer to method that should be wrapped.
 * @return Functor that executes @e _A_func on invokation.
 *
 * @ingroup mem_fun
 */
template <class T_return, class T_obj, class T_obj2, class... T_arg>
inline decltype(auto)
mem_fun(/*$2*/ T_obj* _A_obj, T_return (T_obj2::*_A_func)(T_arg...) $3)
{ return bound_[$1]mem_functor<T_return, T_obj, T_arg...>(_A_obj, _A_func); }

/** Creates a functor of type sigc::bound_[$1]mem_functor which encapsulates a method and an object instance.
 * @param _A_obj Reference to object instance the functor should operate on.
 * @param _A_func Pointer to method that should be wrapped.
 * @return Functor that executes @e _A_func on invokation.
 *
 * @ingroup mem_fun
 */
template <class T_return, class T_obj, class T_obj2, class... T_arg>
inline decltype(auto)
mem_fun(/*$2*/ T_obj& _A_obj, T_return (T_obj2::*_A_func)(T_arg...) $3)
{ return bound_[$1]mem_functor<T_return, T_obj, T_arg...>(_A_obj, _A_func); }

])

divert(0)

// implementation notes:
//  - we do not use bind here, because it would introduce
//    an extra copy and complicate the header include order if bind is
//    to have automatic conversion for member pointers.
_FIREWALL([FUNCTORS_MEM_FUN])
#include <sigc++/type_traits.h>
#include <sigc++/limit_reference.h>

namespace sigc {

/** @defgroup mem_fun mem_fun()
 * mem_fun() is used to convert a pointer to a method to a functor.
 *
 * Optionally, a reference or pointer to an object can be bound to the functor.
 * Note that only if the object type inherits from sigc::trackable is
 * the slot automatically cleared when the object goes out of scope!
 *
 * If the member function pointer is to an overloaded type, you must specify
 * the types using template arguments starting with the first argument.
 * It is not necessary to supply the return type.
 *
 * @par Example:
 * @code
 * struct foo : public sigc::trackable
 * {
 *   void bar(int) {}
 * };
 * foo my_foo;
 * sigc::slot<void, int> sl = sigc::mem_fun(my_foo, &foo::bar);
 * @endcode
 *
 * For const methods mem_fun() takes a const reference or pointer to an object.
 *
 * @par Example:
 * @code
 * struct foo : public sigc::trackable
 * {
 *   void bar(int) const {}
 * };
 * const foo my_foo;
 * sigc::slot<void, int> sl = sigc::mem_fun(my_foo, &foo::bar);
 * @endcode
 *
 * Use mem_fun#() if there is an ambiguity as to the number of arguments.
 *
 * @par Example:
 * @code
 * struct foo : public sigc::trackable
 * {
 *   void bar(int) {}
 *   void bar(float) {}
 *   void bar(int, int) {}
 * };
 * foo my_foo;
 * sigc::slot<void, int> sl = sigc::mem_fun1<int>(my_foo, &foo::bar);
 * @endcode
 *
 * @ingroup sigcfunctors
 */

template <class T_func, class T_obj_with_modifier, class T_return, class T_obj, class... T_arg>
class mem_functor_base : public functor_base
{
public:
  using function_type = T_func;
  using result_type = T_return;

  /// Constructs an invalid functor.
  mem_functor_base() : func_ptr_(nullptr) {}

  /** Constructs a mem_functor object that wraps the passed method.
   * @param _A_func Pointer to method will be invoked from operator()().
   */
  explicit mem_functor_base(function_type _A_func) : func_ptr_(_A_func) {}

  /** Execute the wrapped method operating on the passed instance.
   * @param _A_obj Pointer to instance the method should operate on.
   * @param _A_a... Argument to be passed on to the method.
   * @return The return value of the method invocation.
   */
  decltype(auto)
  operator()(T_obj_with_modifier* _A_obj, type_trait_take_t<T_arg>... _A_a) const
    { return (_A_obj->*(this->func_ptr_))(_A_a...); }

  /** Execute the wrapped method operating on the passed instance.
   * @param _A_obj Reference to instance the method should operate on.
   * @param _A_a... Argument to be passed on to the method.
   * @return The return value of the method invocation.
   */
  decltype(auto)
  operator()(T_obj_with_modifier& _A_obj, type_trait_take_t<T_arg>... _A_a) const
    { return (_A_obj.*func_ptr_)(_A_a...); }

protected:
  function_type func_ptr_;
};

MEMBER_FUNCTOR([],[],[])
MEMBER_FUNCTOR([const_],[const],[const])
MEMBER_FUNCTOR([volatile_],[],[volatile])
MEMBER_FUNCTOR([const_volatile_],[const],[const volatile])

//TODO: Change T_limit_reference to a template template parameter,
//but without having to specify both of the limit_reference<typename, typename>
//typenames?
template <class T_limit_reference,
  class T_func, class T_obj_with_modifier,
  class T_return, class T_obj, class... T_arg>
class bound_mem_functor_base
: mem_functor_base<T_func, T_obj_with_modifier, T_return, T_obj, T_arg...>
{
  using base_type = mem_functor_base<T_func, T_obj_with_modifier, T_return, T_obj, T_arg...>;
public:
  using function_type = typename base_type::function_type;
  using result_type = typename base_type::result_type;

  /** Constructs a bound_mem_functor_base object that wraps the passed method.
   * @param _A_obj Pointer to instance the method will operate on.
   * @param _A_func Pointer to method will be invoked from operator()().
   */
  bound_mem_functor_base(T_obj_with_modifier* _A_obj, function_type _A_func)
    : base_type(_A_func),
      obj_(*_A_obj)
    {}

  /** Constructs a bound_mem_functor_base object that wraps the passed method.
   * @param _A_obj Reference to instance the method will operate on.
   * @param _A_func Pointer to method will be invoked from operator()().
   */
  bound_mem_functor_base(T_obj_with_modifier& _A_obj, function_type _A_func)
    : base_type(_A_func),
      obj_(_A_obj)
    {}

  /** Execute the wrapped method operating on the stored instance.
   * @param _A_a... Argument to be passed on to the method.
   * @return The return value of the method invocation.
   */
  decltype(auto)
  operator()(type_trait_take_t<T_arg>... _A_a) const
    { return (obj_.invoke().*(this->func_ptr_))(_A_a...); }

//protected: TODO?
  // Reference to stored object instance.
  // This is the handler object, such as TheObject in void TheObject::signal_handler().
  //TODO? T_limit_reference<T_obj> obj_;
  T_limit_reference obj_;
};

BOUND_MEMBER_FUNCTOR([],[],[])
BOUND_MEMBER_FUNCTOR([const_],[const],[const])
BOUND_MEMBER_FUNCTOR([volatile_],[],[volatile])
BOUND_MEMBER_FUNCTOR([const_volatile_],[const],[const volatile])

MEM_FUN([],[],[])
MEM_FUN([const_],[const],[const])
MEM_FUN([volatile_],[],[volatile])
MEM_FUN([const_volatile_],[const],[const volatile])
BOUND_MEM_FUN([],[],[])
BOUND_MEM_FUN([const_],[const],[const])
BOUND_MEM_FUN([volatile_],[],[volatile])
BOUND_MEM_FUN([const_volatile_],[const],[const volatile])

} /* namespace sigc */