summaryrefslogtreecommitdiff
path: root/sigc++/functors/mem_fun.h
blob: 2071752946e0d8eb46de441f3fdb0ac9b140c7ed (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
287
288
289
290
291
292
/*
 * Copyright 2003 - 2016, The libsigc++ Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef SIGC_FUNCTORS_MEM_FUN_H
#define SIGC_FUNCTORS_MEM_FUN_H
#include <sigc++/type_traits.h>
#include <sigc++/limit_reference.h>
#include <sigc++/member_method_trait.h>
#include <functional>
#include <utility>

// 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.

namespace sigc
{

/** @defgroup mem_fun mem_fun()
 * %mem_fun() creates a functor from a pointer to a method.
 *
 * Optionally, a reference to an object can be bound to the functor.
 *
 * @note If the object type inherits from sigc::trackable, and the
 * functor returned from %mem_fun() is assigned to
 * a @ref sigc::slot<T_return(T_arg...)> "sigc::slot", the functor
 * will be automatically cleared when the object goes out of scope. Invoking
 * that slot will then have no effect and will not try to use the destroyed
 * instance.
 *
 * @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);
 * // Note: f is not a slot. It will not be invalidated when my_foo is deleted.
 * auto f = sigc::mem_fun(my_foo, &foo::bar); // Usually not what you want.
 * @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
 *
 * If the member function pointer is to an overloaded type, you must specify
 * the types using template arguments.
 *
 * @par Example:
 * @code
 * struct foo : public sigc::trackable
 * {
 *   void bar(int) {} // choose this one
 *   void bar(float) {}
 *   void bar(int, int) {}
 * };
 * foo my_foo;
 * sigc::slot<void(int)> sl = sigc::mem_fun1<void, foo, foo, int>(my_foo, &foo::bar);
 * @endcode
 *
 * @ingroup sigcfunctors
 */

template<typename T_func, typename... T_arg>
class mem_functor
{
public:
  using object_type = typename internal::member_method_class<T_func>::type;

  using function_type = T_func;

  using obj_type_with_modifier = typename std::
    conditional_t<internal::member_method_is_const<T_func>::value, const object_type, object_type>;

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

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

  /** Execute the wrapped method operating on the passed instance.
   * @param obj Reference to instance the method should operate on.
   * @param a Arguments to be passed on to the method.
   * @return The return value of the method invocation.
   */
  decltype(auto) operator()(obj_type_with_modifier& obj, type_trait_take_t<T_arg>... a) const
  {
    return std::invoke(func_ptr_, obj, std::forward<type_trait_take_t<T_arg>>(a)...);
  }

protected:
  function_type func_ptr_;
};

template<typename T_func, typename... T_arg>
class bound_mem_functor : mem_functor<T_func, T_arg...>
{
  using base_type = mem_functor<T_func, T_arg...>;

public:
  using function_type = typename base_type::function_type;

  using object_type = typename base_type::object_type;

  using obj_type_with_modifier = typename std::
    conditional_t<internal::member_method_is_const<T_func>::value, const object_type, object_type>;
  using T_limit_reference =
    typename std::conditional_t<internal::member_method_is_const<T_func>::value,
      limit_reference<const object_type>,
      limit_reference<object_type>>;

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

  /** Execute the wrapped method operating on the stored instance.
   * @param a Arguments 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) const
  {
    return std::invoke(
      this->func_ptr_, obj_.invoke(), std::forward<type_trait_take_t<T_arg>>(a)...);
  }

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

#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_$1mem_functor performs a functor
 * on the object instance stored in the sigc::bound_$1mem_functor object.
 *
 * @ingroup mem_fun
 */
template<typename T_func, typename... T_arg>
struct visitor<bound_mem_functor<T_func, T_arg...>>
{
  template<typename T_action>
  static void do_visit_each(const T_action& action,
    const bound_mem_functor<T_func, T_arg...>& target)
  {
    sigc::visit_each(action, target.obj_);
  }
};
#endif // DOXYGEN_SHOULD_SKIP_THIS

/** Creates a functor of type sigc::mem_functor which wraps a  method.
 * @param func Pointer to method that should be wrapped.
 * @return Functor that executes func on invocation.
 *
 * @ingroup mem_fun
 */
template<typename T_return, typename T_obj, typename... T_arg>
inline decltype(auto) mem_fun(T_return (T_obj::*func)(T_arg...))
{
  return mem_functor<T_return (T_obj::*)(T_arg...), T_arg...>(func);
}

/** Creates a functor of type sigc::const_mem_functor which wraps a const method.
 * @param func Pointer to method that should be wrapped.
 * @return Functor that executes func on invocation.
 *
 * @ingroup mem_fun
 */
template<typename T_return, typename T_obj, typename... T_arg>
inline decltype(auto)
mem_fun(T_return (T_obj::*func)(T_arg...) const)
{
  return mem_functor<T_return (T_obj::*)(T_arg...) const, T_arg...>(func);
}

/** Creates a functor of type sigc::volatile_mem_functor which wraps a volatile method.
 * @param func Pointer to method that should be wrapped.
 * @return Functor that executes func on invocation.
 *
 * @ingroup mem_fun
 */
template<typename T_return, typename T_obj, typename... T_arg>
inline decltype(auto) mem_fun(T_return (T_obj::*func)(T_arg...) volatile)
{
  return mem_functor<T_return (T_obj::*)(T_arg...) volatile, T_arg...>(func);
}

/** Creates a functor of type sigc::const_volatile_mem_functor which wraps a const volatile method.
 * @param func Pointer to method that should be wrapped.
 * @return Functor that executes func on invocation.
 *
 * @ingroup mem_fun
 */
template<typename T_return, typename T_obj, typename... T_arg>
inline decltype(auto)
mem_fun(T_return (T_obj::*func)(T_arg...) const volatile)
{
  return mem_functor<T_return (T_obj::*)(T_arg...) const volatile, T_arg...>(func);
}

/** Creates a functor of type sigc::bound_mem_functor which encapsulates a method and an object
 * instance.
 * @param obj Reference to object instance the functor should operate on.
 * @param func Pointer to method that should be wrapped.
 * @return Functor that executes @e func on invocation.
 *
 * @ingroup mem_fun
 */
template<typename T_return, typename T_obj, typename T_obj2, typename... T_arg>
inline decltype(auto)
mem_fun(/**/ T_obj& obj, T_return (T_obj2::*func)(T_arg...))
{
  return bound_mem_functor<T_return (T_obj::*)(T_arg...), T_arg...>(obj, func);
}

/** Creates a functor of type sigc::bound_const_mem_functor which encapsulates a method and an
 * object instance.
 * @param obj Reference to object instance the functor should operate on.
 * @param func Pointer to method that should be wrapped.
 * @return Functor that executes @e func on invocation.
 *
 * @ingroup mem_fun
 */
template<typename T_return, typename T_obj, typename T_obj2, typename... T_arg>
inline decltype(auto)
mem_fun(/*const*/ T_obj& obj, T_return (T_obj2::*func)(T_arg...) const)
{
  return bound_mem_functor<T_return (T_obj::*)(T_arg...) const, T_arg...>(obj, func);
}

/** Creates a functor of type sigc::bound_volatile_mem_functor which encapsulates a method and an
 * object instance.
 * @param obj Reference to object instance the functor should operate on.
 * @param func Pointer to method that should be wrapped.
 * @return Functor that executes @e func on invocation.
 *
 * @ingroup mem_fun
 */
template<typename T_return, typename T_obj, typename T_obj2, typename... T_arg>
inline decltype(auto)
mem_fun(/**/ T_obj& obj, T_return (T_obj2::*func)(T_arg...) volatile)
{
  return bound_mem_functor<T_return (T_obj::*)(T_arg...) volatile, T_arg...>(obj, func);
}

/** Creates a functor of type sigc::bound_const_volatile_mem_functor which encapsulates a method and
 * an object instance.
 * @param obj Reference to object instance the functor should operate on.
 * @param func Pointer to method that should be wrapped.
 * @return Functor that executes @e func on invocation.
 *
 * @ingroup mem_fun
 */
template<typename T_return, typename T_obj, typename T_obj2, typename... T_arg>
inline decltype(auto)
mem_fun(/*const*/ T_obj& obj, T_return (T_obj2::*func)(T_arg...) const volatile)
{
  return bound_mem_functor<T_return (T_obj::*)(T_arg...) const volatile, T_arg...>(obj, func);
}

} /* namespace sigc */
#endif /* SIGC_FUNCTORS_MEM_FUN_H */