c++-gtk-utils
callback.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 to 2013 Chris Vine
2 
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public License
9  as published by the Free Software Foundation; either version 2.1 of
10  the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License, version 2.1, for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License, version 2.1, along with this library (see the file LGPL.TXT
19  which came with this source code package in the c++-gtk-utils
20  sub-directory); if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 However, it is not intended that the object code of a program whose
24 source code instantiates a template from this file or uses macros or
25 inline functions (of any length) should by reason only of that
26 instantiation or use be subject to the restrictions of use in the GNU
27 Lesser General Public License. With that in mind, the words "and
28 macros, inline functions and instantiations of templates (of any
29 length)" shall be treated as substituted for the words "and small
30 macros and small inline functions (ten lines or less in length)" in
31 the fourth paragraph of section 5 of that licence. This does not
32 affect any other reason why object code may be subject to the
33 restrictions in that licence (nor for the avoidance of doubt does it
34 affect the application of section 2 of that licence to modifications
35 of the source code in this file).
36 
37 */
38 
39 #ifndef CGU_CALLBACK_H
40 #define CGU_CALLBACK_H
41 
42 /**
43  * @file callback.h
44  * @brief This file provides classes for type erasure.
45  *
46  * \#include <c++-gtk-utils/callback.h>
47  *
48  * These classes provide type erasure on callable objects. They
49  * comprise a generic callback creation and execution interface for
50  * closures. There is a basic Callback::Callback type, which is an
51  * entire closure or 'thunk', where all values are bound into the
52  * object, and is completely opaque. Callback::CallbackArg<T...> is a
53  * class which takes unbound arguments of the template types when the
54  * object is dispatched. (The opaque Callback::Callback type is a
55  * typedef for Callback::CallbackArg<>: the two types are
56  * interchangeable.)
57  *
58  * The classes are normally constructed using the Callback::lambda()
59  * factory function, which takes any callable object such as a lambda
60  * expression or the return value of std::bind and returns a pointer
61  * to a Callback or CallbackArg object. When using
62  * Callback::lambda(), the unbound arguments (if any) must be passed
63  * as explicit template parameters.
64  *
65  * Callback/CallbackArg objects can also be constructed using the
66  * Callback::make() and Callback::make_ref() factory functions, which
67  * can be useful where invoking standalone functions or object
68  * methods.
69  *
70  * The Callback::make() and Callback::make_ref() functions
71  * -------------------------------------------------------
72  *
73  * The Callback::make() and Callback::make_ref() functions construct a
74  * Callback/CallbackArg object from a function pointer (or an object
75  * reference and member function pointer) together with bound
76  * arguments. They provide for a maximum of five bound arguments, and
77  * the unbound arguments (if any) must be the last (trailing)
78  * arguments of the relevant function or method to be called.
79  *
80  * Callback::make() does a direct type mapping from the bound
81  * arguments of the function or method represented by the callback
82  * object to the arguments stored by it and is for use when all bound
83  * arguments are simple fundamental types such as pointers (including
84  * C strings), integers or floating points.
85  *
86  * Callback::make_ref() is for use where bound arguments include class
87  * types or one or more of the types of the bound arguments include a
88  * const reference. It will accomplish perfect forwarding (by lvalue
89  * reference or rvalue reference) when constructing the callback and
90  * will also ensure that a copy of any object to be passed by const
91  * reference (as well as any taken by value) is kept in order to avoid
92  * dangling references. Note however that where a member function is
93  * called, the object of which the target function is a member must
94  * still be in existence when the Callback/CallbackArg object is
95  * dispatched and, unlike Callback::make(), Callback::make_ref()
96  * cannot be used with overloaded functions except with explicit
97  * disambiguation.
98  *
99  * Callback::make() can also construct a Callback/CallbackArg object
100  * from a std::function object.
101  *
102  * Callback::FunctorArg and Callback::SafeFunctorArg classes
103  * ---------------------------------------------------------
104  *
105  * Functor/FunctorArg objects hold a Callback/CallbackArg object by
106  * SharedPtr to enable them to be shared by reference counting, and
107  * SafeFunctor/SafeFunctorArg objects hold them by SharedLockPtr,
108  * which have a thread safe reference count so that they may be shared
109  * between different threads. These classes also have an operator()()
110  * method so as to be callable with function syntax.
111  *
112  * Memory allocation
113  * -----------------
114  *
115  * If the library is installed using the
116  * \--with-glib-memory-slices-no-compat configuration option, any
117  * Callback/CallbackArg object will be constructed in glib memory
118  * slices rather than in the generic C++ free store.
119  *
120  * Usage
121  * -----
122  *
123  * Using Callback::lambda():
124  *
125  * @code
126  * using namespace Cgu;
127  *
128  * // here cb1 is of type Callback::Callback*
129  * auto cb1 = Callback::lambda<>([] () {std::cout << "Hello world\n";});
130  * cb1->dispatch();
131  * delete cb1;
132  *
133  * // the same using Callback::Functor
134  * Callback::Functor f1{Callback::lambda<>([] () {std::cout << "Hello world\n";})};
135  * f1();
136  *
137  * // here cb2 is of type Callback::CallbackArg<int, int&>*
138  * auto cb2 = Callback::lambda<int, int&>([] (int i, int& j) {j = 10 * i;});
139  * int res;
140  * cb2->dispatch(2, res);
141  * std::cout << "10 times 2 is " << res << '\n';
142  * delete cb2;
143  *
144  * // the same using Callback::FunctorArg
145  * Callback::FunctorArg<int, int&> f2{Callback::lambda<int, int&>([] (int i, int& j) {j = 10 * i;})};
146  * f2(2, res);
147  * std::cout << "10 times 2 is " << res << '\n';
148  * @endcode
149  *
150  * Using Callback::make(), with a class object my_obj of type MyClass,
151  * with a method void MyClass::my_method(int, int, const char*):
152  *
153  * @code
154  * using namespace Cgu;
155  *
156  * int arg1 = 1, arg2 = 5;
157  * // here cb1 is of type Callback::Callback*
158  * auto cb1 = Callback::make(my_obj, &MyClass::my_method, arg1, arg2, "Hello\n");
159  * cb1->dispatch();
160  * delete cb1;
161  *
162  * // the same using Callback::Functor
163  * Callback::Functor f{Callback::make(my_obj, &MyClass::my_method, arg1, arg2, "Hello\n")};
164  * f();
165  *
166  * int arg1 = 1, arg2 = 5;
167  * // cb2 is of type Callback::CallbackArg<int, const char*>*
168  * auto cb = Callback::make(my_obj, &MyClass::my_method, arg1);
169  * cb->dispatch(arg2, "Hello\n");
170  * delete cb;
171  *
172  * // the same using Callback::FunctorArg
173  * Callback::FunctorArg<int, const char*> f{Callback::make(my_obj, &MyClass::my_method, arg1)};
174  * f(arg2, "Hello\n");
175  * @endcode
176  *
177  * Using Callback::make_ref(), with a class object my_obj of type
178  * MyClass, with a method void MyClass::my_method(int, const
179  * Something&):
180  *
181  * @code
182  * int arg1 = 1;
183  * Something arg2;
184  * // here cb is of type Callback::Callback*
185  * auto cb = Callback::make_ref(my_obj, &MyClass::my_method, arg1, arg2);
186  * @endcode
187  *
188  * Posting of callbacks
189  * --------------------
190  *
191  * This file also provides a Callback::post() function which will
192  * execute a callback in a glib main loop and can be used (amongst
193  * other things) to pass an event from a worker thread to the main
194  * program thread. In that respect, it provides an alternative to the
195  * Notifier class. It is passed either a pointer to a
196  * Callback::Callback object created with a call to Callback::lambda()
197  * (or Callback::make() or Callback::make_ref()), or it can be passed
198  * a callable object and the implementation will call
199  * Callback::lambda() for you.
200  *
201  * To provide for thread-safe automatic disconnection of the callback
202  * if the callback represents or calls into a non-static method of an
203  * object which may be destroyed before the callback executes in the
204  * main loop, include a Releaser as a public member of that object and
205  * pass the Releaser object as the second argument of
206  * Callback::post(). Note that for this to be race free, the lifetime
207  * of the remote object whose method is to be invoked must be
208  * determined by the thread to whose main loop the callback has been
209  * attached. When the main loop begins invoking the execution of the
210  * callback, the remote object must either wholly exist (in which case
211  * the callback will be invoked) or have been destroyed (in which case
212  * the callback will be ignored), and not be in some transient
213  * half-state governed by another thread.
214  *
215  * Advantages as against Notifier:
216  *
217  * 1. If there are a lot of different events requiring callbacks to be
218  * dispatched in the program from worker threads to the main
219  * thread, this avoids having separate Notifier objects for each
220  * event.
221  * 2. It is easier to pass arguments with varying values - they can be
222  * passed as bound arguments of the callback and no special
223  * synchronisation is normally required (the call to
224  * g_source_attach() invokes locking of the main loop which will
225  * have the effect of ensuring memory visibility). With a Notifier
226  * object it may be necessary to use an asynchronous queue to pass
227  * variable values (or to bind a reference to the data, thus
228  * normally requiring separate synchronisation).
229  * 3. Although the callback would normally be sent for execution by
230  * the main program loop, and that is the default, it can be sent
231  * for execution by any thread which has its own
232  * GMainContext/GMainLoop objects. Thus callbacks can be passed
233  * for execution between worker threads, or from the main program
234  * thread to worker threads, as well as from worker threads to the
235  * main program thread.
236  *
237  * Disadvantages as against Notifier:
238  *
239  * 1. Less efficient, as a new callback object has to be created on
240  * freestore every time the callback is invoked, together with a
241  * new SafeEmitter object if a Releaser is used to track the
242  * callback.
243  * 2. Multiple callbacks relevant to a single event cannot be invoked
244  * from a single call for the event - each callback has to be
245  * separately dispatched.
246  */
247 
248 /**
249  * @namespace Cgu::Callback
250  * @brief This namespace provides classes for type erasure.
251  *
252  * \#include <c++-gtk-utils/callback.h>
253  *
254  * These classes provide type erasure on callable objects. They
255  * comprise a generic callback creation and execution interface for
256  * closures. There is a basic Callback::Callback type, which is an
257  * entire closure or 'thunk', where all values are bound into the
258  * object, and is completely opaque. Callback::CallbackArg<T...> is a
259  * class which takes unbound arguments of the template types when the
260  * object is dispatched. (The opaque Callback::Callback type is a
261  * typedef for Callback::CallbackArg<>: the two types are
262  * interchangeable.)
263  *
264  * The classes are normally constructed using the Callback::lambda()
265  * factory function, which takes any callable object such as a lambda
266  * expression or the return value of std::bind and returns a pointer
267  * to a Callback or CallbackArg object. When using
268  * Callback::lambda(), the unbound arguments (if any) must be passed
269  * as explicit template parameters.
270  *
271  * Callback/CallbackArg objects can also be constructed using the
272  * Callback::make() and Callback::make_ref() factory functions, which
273  * can be useful where invoking standalone functions or object
274  * methods.
275  *
276  * The Callback::make() and Callback::make_ref() functions
277  * -------------------------------------------------------
278  *
279  * The Callback::make() and Callback::make_ref() functions construct a
280  * Callback/CallbackArg object from a function pointer (or an object
281  * reference and member function pointer) together with bound
282  * arguments. They provide for a maximum of five bound arguments, and
283  * the unbound arguments (if any) must be the last (trailing)
284  * arguments of the relevant function or method to be called.
285  *
286  * Callback::make() does a direct type mapping from the bound
287  * arguments of the function or method represented by the callback
288  * object to the arguments stored by it and is for use when all bound
289  * arguments are simple fundamental types such as pointers (including
290  * C strings), integers or floating points.
291  *
292  * Callback::make_ref() is for use where bound arguments include class
293  * types or one or more of the types of the bound arguments include a
294  * const reference. It will accomplish perfect forwarding (by lvalue
295  * reference or rvalue reference) when constructing the callback and
296  * will also ensure that a copy of any object to be passed by const
297  * reference (as well as any taken by value) is kept in order to avoid
298  * dangling references. Note however that where a member function is
299  * called, the object of which the target function is a member must
300  * still be in existence when the Callback/CallbackArg object is
301  * dispatched and, unlike Callback::make(), Callback::make_ref()
302  * cannot be used with overloaded functions except with explicit
303  * disambiguation.
304  *
305  * Callback::make() can also construct a Callback/CallbackArg object
306  * from a std::function object.
307  *
308  * Callback::FunctorArg and Callback::SafeFunctorArg classes
309  * ---------------------------------------------------------
310  *
311  * Functor/FunctorArg objects hold a Callback/CallbackArg object by
312  * SharedPtr to enable them to be shared by reference counting, and
313  * SafeFunctor/SafeFunctorArg objects hold them by SharedLockPtr,
314  * which have a thread safe reference count so that they may be shared
315  * between different threads. These classes also have an operator()()
316  * method so as to be callable with function syntax.
317  *
318  * Memory allocation
319  * -----------------
320  *
321  * If the library is installed using the
322  * \--with-glib-memory-slices-no-compat configuration option, any
323  * Callback/CallbackArg object will be constructed in glib memory
324  * slices rather than in the generic C++ free store.
325  *
326  * Usage
327  * -----
328  *
329  * Using Callback::lambda():
330  *
331  * @code
332  * using namespace Cgu;
333  *
334  * // here cb1 is of type Callback::Callback*
335  * auto cb1 = Callback::lambda<>([] () {std::cout << "Hello world\n";});
336  * cb1->dispatch();
337  * delete cb1;
338  *
339  * // the same using Callback::Functor
340  * Callback::Functor f1{Callback::lambda<>([] () {std::cout << "Hello world\n";})};
341  * f1();
342  *
343  * // here cb2 is of type Callback::CallbackArg<int, int&>*
344  * auto cb2 = Callback::lambda<int, int&>([] (int i, int& j) {j = 10 * i;});
345  * int res;
346  * cb2->dispatch(2, res);
347  * std::cout << "10 times 2 is " << res << '\n';
348  * delete cb2;
349  *
350  * // the same using Callback::FunctorArg
351  * Callback::FunctorArg<int, int&> f2{Callback::lambda<int, int&>([] (int i, int& j) {j = 10 * i;})};
352  * f2(2, res);
353  * std::cout << "10 times 2 is " << res << '\n';
354  * @endcode
355  *
356  * Using Callback::make(), with a class object my_obj of type MyClass,
357  * with a method void MyClass::my_method(int, int, const char*):
358  *
359  * @code
360  * using namespace Cgu;
361  *
362  * int arg1 = 1, arg2 = 5;
363  * // here cb1 is of type Callback::Callback*
364  * auto cb1 = Callback::make(my_obj, &MyClass::my_method, arg1, arg2, "Hello\n");
365  * cb1->dispatch();
366  * delete cb1;
367  *
368  * // the same using Callback::Functor
369  * Callback::Functor f{Callback::make(my_obj, &MyClass::my_method, arg1, arg2, "Hello\n")};
370  * f();
371  *
372  * int arg1 = 1, arg2 = 5;
373  * // cb2 is of type Callback::CallbackArg<int, const char*>*
374  * auto cb = Callback::make(my_obj, &MyClass::my_method, arg1);
375  * cb->dispatch(arg2, "Hello\n");
376  * delete cb;
377  *
378  * // the same using Callback::FunctorArg
379  * Callback::FunctorArg<int, const char*> f{Callback::make(my_obj, &MyClass::my_method, arg1)};
380  * f(arg2, "Hello\n");
381  * @endcode
382  *
383  * Using Callback::make_ref(), with a class object my_obj of type
384  * MyClass, with a method void MyClass::my_method(int, const
385  * Something&):
386  *
387  * @code
388  * int arg1 = 1;
389  * Something arg2;
390  * // here cb is of type Callback::Callback*
391  * auto cb = Callback::make_ref(my_obj, &MyClass::my_method, arg1, arg2);
392  * @endcode
393  *
394  * Posting of callbacks
395  * --------------------
396  *
397  * This namespace also provides a Callback::post() function which will
398  * execute a callback in a glib main loop and can be used (amongst
399  * other things) to pass an event from a worker thread to the main
400  * program thread. In that respect, it provides an alternative to the
401  * Notifier class. It is passed either a pointer to a
402  * Callback::Callback object created with a call to Callback::lambda()
403  * (or Callback::make() or Callback::make_ref()), or it can be passed
404  * a callable object and the implementation will call
405  * Callback::lambda() for you.
406  *
407  * To provide for thread-safe automatic disconnection of the callback
408  * if the callback represents or calls into a non-static method of an
409  * object which may be destroyed before the callback executes in the
410  * main loop, include a Releaser as a public member of that object and
411  * pass the Releaser object as the second argument of
412  * Callback::post(). Note that for this to be race free, the lifetime
413  * of the remote object whose method is to be invoked must be
414  * determined by the thread to whose main loop the callback has been
415  * attached. When the main loop begins invoking the execution of the
416  * callback, the remote object must either wholly exist (in which case
417  * the callback will be invoked) or have been destroyed (in which case
418  * the callback will be ignored), and not be in some transient
419  * half-state governed by another thread.
420  *
421  * Advantages as against Notifier:
422  *
423  * 1. If there are a lot of different events requiring callbacks to be
424  * dispatched in the program from worker threads to the main
425  * thread, this avoids having separate Notifier objects for each
426  * event.
427  * 2. It is easier to pass arguments with varying values - they can be
428  * passed as bound arguments of the callback and no special
429  * synchronisation is normally required (the call to
430  * g_source_attach() invokes locking of the main loop which will
431  * have the effect of ensuring memory visibility). With a Notifier
432  * object it may be necessary to use an asynchronous queue to pass
433  * variable values (or to bind a reference to the data, thus
434  * normally requiring separate synchronisation).
435  * 3. Although the callback would normally be sent for execution by
436  * the main program loop, and that is the default, it can be sent
437  * for execution by any thread which has its own
438  * GMainContext/GMainLoop objects. Thus callbacks can be passed
439  * for execution between worker threads, or from the main program
440  * thread to worker threads, as well as from worker threads to the
441  * main program thread.
442  *
443  * Disadvantages as against Notifier:
444  *
445  * 1. Less efficient, as a new callback object has to be created on
446  * freestore every time the callback is invoked, together with a
447  * new SafeEmitter object if a Releaser is used to track the
448  * callback.
449  * 2. Multiple callbacks relevant to a single event cannot be invoked
450  * from a single call for the event - each callback has to be
451  * separately dispatched.
452  */
453 
454 #include <functional> // for std::less, std::function and std::hash<T*>
455 #include <utility> // for std::move and std::forward
456 #include <cstddef> // for std::size_t
457 #include <type_traits> // for std::remove_reference, std::remove_const and std::enable_if
458 
459 #include <glib.h>
460 
462 #include <c++-gtk-utils/param.h>
464 
465 namespace Cgu {
466 
467 namespace Callback {
468 
469 /*
470  The CallbackArg class could be additionally templated to provide a
471  return value, but that would affect the simplicity of the
472  interface, and if a case were to arise where a result is needed, an
473  alternative is for users to pass an argument by reference or
474  pointer (or pointer to pointer) rather than have a return value.
475 */
476 
477 /* Declare the two basic interface types */
478 
479 template <class... FreeArgs> class CallbackArg;
480 typedef CallbackArg<> Callback;
481 
482 /* now the class definitions */
483 
484 /**
485  * @class CallbackArg callback.h c++-gtk-utils/callback.h
486  * @brief The callback interface class
487  * @sa Callback namespace
488  * @sa FunctorArg SafeFunctorArg
489  *
490  * This class provides type erasure for callable objects. The
491  * CallbackArg type is constructed on free store and can wrap any
492  * callable object, such as a lambda expression or the return value of
493  * std::bind.
494  *
495  * The class is particularly relevant where a callable object with
496  * bound values needs to be handed safely between threads, or in other
497  * cases where a callback object has to be passed by pointer (which
498  * will happen at some stage with glib or pthreads). They are
499  * therefore useful for general event passing when used together with
500  * the Callback::post() functions or as the continuation for GIO async
501  * operations, and are more efficient than constructing std::function
502  * objects on free store and passing them by pointer (they avoid one
503  * level of indirection and only require one rather than two
504  * allocations).
505  *
506  * The classes are also used in the Emitter/EmitterArg and
507  * SafeEmitter/SafeEmitterArg classes in emitter.h, which enable
508  * callable objects to be connected to an emitter and provide for
509  * automatic disconnection where a class object whose member a
510  * callback represents or calls into ceases to exist. They are also
511  * used internally to implement the Thread::Future and
512  * Thread::TaskManager classes.
513  *
514  * The template types are the types of the unbound arguments, if any.
515  * Callback::CallbackArg<> is typedef'ed to Callback::Callback. The
516  * main method of constructing a Callback/CallbackArg object is with
517  * the Callback::lambda() factory function. When using
518  * Callback::lambda(), the unbound arguments (if any) must be passed
519  * as explicit template parameters.
520  *
521  * Callback/CallbackArg classes do not provide for a return value. If
522  * a result is wanted, users should pass an unbound argument by
523  * reference or pointer (or pointer to pointer).
524  *
525  * The 2.2 series of the library generally dispenses with the need to
526  * create objects explicitly using Callback::lambda() when calling
527  * into the library itself: all the library interfaces which take a
528  * Callback/CallbackArg object also now provide an overload which
529  * takes any callable object as a template type, and the
530  * implementation calls Callback::lambda() internally for you.
531  *
532  * @b Usage
533  *
534  * These are examples:
535  *
536  * @code
537  * using namespace Cgu;
538  *
539  * // here cb1 is of type Callback::Callback*
540  * auto cb1 = Callback::lambda<>([] () {std::cout << "Hello world\n";});
541  * cb1->dispatch();
542  * delete cb1;
543  *
544  * // here cb2 is of type Callback::CallbackArg<int, int&>*
545  * auto cb2 = Callback::lambda<int, int&>([] (int i, int& j) {j = 10 * i;});
546  * int res;
547  * cb2->dispatch(2, res);
548  * std::cout << "10 times 2 is " << res << '\n';
549  * delete cb2;
550  * @endcode
551  *
552  * For further background, including about the Callback::make(),
553  * Callback::make_ref() functions, read this: Callback
554  */
555 
556 template <class... FreeArgs>
557 class CallbackArg {
558 public:
559 /* Because dispatch() is a virtual function, we cannot templatise it
560  * with a view to preserving r-value forwarding of temporary objects
561  * passed as a free argument. But this would rarely be relevant
562  * anyway - it would only be relevant if the target function were to
563  * take an argument by r-value reference and a temporary were to be
564  * passed to it. In such a case virtual dispatch is at the cost of a
565  * copy of the temporary.
566  */
567 /**
568  * This will execute the referenced function, callable object or class
569  * method encapsulated by this class. It will only throw if the
570  * dispatched function, callable object or class method throws, or if
571  * the copy constructor of a free or bound argument throws and it is
572  * not a reference argument. It is thread safe if the referenced
573  * function or class method is thread safe.
574  * @param args The unbound arguments to be passed to the referenced
575  * function, callable object or class method, if any.
576  * @note We use dispatch() to execute the callback, because the
577  * callback would normally be invoked through a base class pointer.
578  * To invoke it through operator()(), use the FunctorArg or
579  * SafeFunctorArg wrapper class.
580  */
581  virtual void dispatch(typename Cgu::Param<FreeArgs>::ParamType... args) const = 0;
582 
583 /**
584  * The constructor will not throw unless the copy constructor of an
585  * argument bound to the derived implementation class throws.
586  */
588 
589 /**
590  * The destructor will not throw unless the destructor of an argument
591  * bound to the derived implementation class throws.
592  */
593  virtual ~CallbackArg() {}
594 
595 /* these functions will be inherited by the derived callback classes */
596 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
598 #endif
599 };
600 
601 /* The four basic functor types */
602 
603 template <class... FreeArgs> class FunctorArg;
604 template <class... FreeArgs> class SafeFunctorArg;
605 typedef FunctorArg<> Functor;
607 
608 /* Functor friend functions */
609 
610 // we can use built-in operator == when comparing pointers referencing
611 // different objects of the same type
612 /**
613  * Two FunctorArg objects compare equal if the addresses of the
614  * CallbackArg objects they contain are the same. This comparison
615  * operator does not throw.
616  */
617 template <class... T>
618 bool operator==(const FunctorArg<T...>& f1, const FunctorArg<T...>& f2) noexcept {
619  return (f1.cb_s.get() == f2.cb_s.get());
620 }
621 
622 /**
623  * Two FunctorArg objects compare unequal if the addresses of the
624  * CallbackArg objects they contain are not the same. This comparison
625  * operator does not throw.
626  */
627 template <class... T>
628 bool operator!=(const FunctorArg<T...>& f1, const FunctorArg<T...>& f2) noexcept {
629  return !(f1 == f2);
630 }
631 
632 // we must use std::less rather than the < built-in operator for
633 // pointers to objects not within the same array or object: "For
634 // templates greater, less, greater_equal, and less_equal, the
635 // specializations for any pointer type yield a total order, even if
636 // the built-in operators <, >, <=, >= do not." (para 20.3.3/8).
637 /**
638  * One FunctorArg object is less than another if the address of the
639  * CallbackArg object contained by the first is regarded by std::less
640  * as less than the address of the CallbackArg object contained by the
641  * other. This comparison operator does not throw unless std::less
642  * applied to pointer types throws (which it would not do with any
643  * sane implementation).
644  */
645 template <class... T>
646 bool operator<(const FunctorArg<T...>& f1, const FunctorArg<T...>& f2) {
647  return std::less<const CallbackArg<T...>*>()(f1.cb_s.get(), f2.cb_s.get());
648 }
649 
650 /**
651  * Two SafeFunctorArg objects compare equal if the addresses of the
652  * CallbackArg objects they contain are the same. This comparison
653  * operator does not throw.
654  */
655 template <class... T>
656 bool operator==(const SafeFunctorArg<T...>& f1, const SafeFunctorArg<T...>& f2) noexcept {
657  return (f1.cb_s.get() == f2.cb_s.get());
658 }
659 
660 /**
661  * Two SafeFunctorArg objects compare unequal if the addresses of the
662  * CallbackArg objects they contain are not the same. This comparison
663  * operator does not throw.
664  */
665 template <class... T>
666 bool operator!=(const SafeFunctorArg<T...>& f1, const SafeFunctorArg<T...>& f2) noexcept {
667  return !(f1 == f2);
668 }
669 
670 /**
671  * One SafeFunctorArg object is less than another if the address of
672  * the CallbackArg object contained by the first is regarded by
673  * std::less as less than the address of the CallbackArg object
674  * contained by the other. This comparison operator does not throw
675  * unless std::less applied to pointer types throws (which it would
676  * not do with any sane implementation).
677  */
678 template <class... T>
680  return std::less<const CallbackArg<T...>*>()(f1.cb_s.get(), f2.cb_s.get());
681 }
682 
683 } // namespace Callback
684 } // namespace Cgu
685 
686 // doxygen produces long filenames that tar can't handle:
687 // we have generic documentation for std::hash specialisations
688 // in doxygen.main.in
689 #ifndef DOXYGEN_PARSING
690 
691 /* These structs allow FunctorArg and SafeFunctorArg objects to be
692  keys in unordered associative containers */
693 namespace std {
694 template <class... T>
695 struct hash<Cgu::Callback::FunctorArg<T...>> {
696  typedef std::size_t result_type;
697  typedef Cgu::Callback::FunctorArg<T...> argument_type;
698  result_type operator()(const argument_type& f) const {
699  // this is fine: std::hash structs do not normally contain data and
700  // std::hash<T*> certainly won't, so we don't have overhead constructing
701  // std::hash<T*> on the fly
702  return std::hash<const Cgu::Callback::CallbackArg<T...>*>()(f.cb_s.get());
703  }
704 };
705 template <class... T>
706 struct hash<Cgu::Callback::SafeFunctorArg<T...>> {
707  typedef std::size_t result_type;
708  typedef Cgu::Callback::SafeFunctorArg<T...> argument_type;
709  result_type operator()(const argument_type& f) const {
710  // this is fine: std::hash structs do not normally contain data and
711  // std::hash<T*> certainly won't, so we don't have overhead constructing
712  // std::hash<T*> on the fly
713  return std::hash<const Cgu::Callback::CallbackArg<T...>*>()(f.cb_s.get());
714  }
715 };
716 } // namespace std
717 
718 #endif // DOXYGEN_PARSING
719 
720 namespace Cgu {
721 namespace Callback {
722 
723 /* the functor classes */
724 
725 /**
726  * @class FunctorArg callback.h c++-gtk-utils/callback.h
727  * @brief Functor class holding a Callback::CallbackArg object.
728  * @sa SafeFunctorArg
729  * @sa Callback namespace
730  *
731  * This class wraps a CallbackArg object. The callback object is kept
732  * by SharedPtr so the functor can be copied and offers automatic
733  * lifetime management of the wrapped callback object, as well as
734  * providing an operator()() function. Ownership is taken of the
735  * CallbackArg object passed to the constructor taking a CallbackArg
736  * pointer, so that constructor should be treated like a shared
737  * pointer constructor - only pass a newly allocated object to it (or
738  * copy construct it or assign to it from another existing FunctorArg
739  * object). The template types are the types of the unbound
740  * arguments, if any. Callback::FunctorArg<> is typedef'ed to
741  * Callback::Functor.
742  *
743  * The constructor taking a Callback::CallbackArg pointer is not
744  * marked explicit, so the results of Callback::lambda(),
745  * Callback::make() or Callback::make_ref() can be passed directly to
746  * a function taking a Callback::FunctorArg argument, and implicit
747  * conversion will take place.
748  *
749  * Functor/FunctorArg classes do not provide for a return value. If
750  * a result is wanted, users should pass an unbound argument by
751  * reference or pointer (or pointer to pointer).
752  *
753  * @b Usage
754  *
755  * These are examples:
756  *
757  * @code
758  * using namespace Cgu;
759  *
760  * Callback::Functor f1{Callback::lambda<>([] () {std::cout << "Hello world\n";})};
761  * f1();
762  *
763  * int res;
764  * Callback::FunctorArg<int, int&> f2{Callback::lambda<int, int&>([] (int i, int& j) {j = 10 * i;})};
765  * f2(2, res);
766  * std::cout << "10 times 2 is " << res << '\n';
767  * @endcode
768  *
769  * For further background, including about the Callback::make(),
770  * Callback::make_ref() functions, read this: Callback
771  */
772 
773 template <class... FreeArgs>
774 class FunctorArg {
775  SharedPtr<const CallbackArg<FreeArgs...>> cb_s;
776 public:
777 /* Because CallbackArg::dispatch() is a virtual function, it is
778  * pointless templatising this function with a view to preserving
779  * r-value forwarding of temporary objects passed as a free argument,
780  * because the r-value typeness will be discarded in dispatch(). But
781  * this would rarely be relevant anyway - it would only be relevant if
782  * the target function were to take an argument by r-value reference
783  * and a temporary were to be passed to it. In such a case virtual
784  * dispatch is at the cost of a copy of the temporary.
785  */
786 /**
787  * This will execute the function, callable object or class method
788  * represented by the callback encapsulated by this object, or do
789  * nothing if this object has not been initialized with a callback.
790  * It will only throw if the executed function, callable object or
791  * class method throws, or if the copy constructor of a free or bound
792  * argument throws and it is not a reference argument. It is thread
793  * safe if the referenced function or class method is thread safe.
794  * @param args The unbound arguments to be passed to the referenced
795  * function, callable object or class method, if any.
796  */
797  void operator()(typename Cgu::Param<FreeArgs>::ParamType... args) const {
798  if (cb_s.get()) cb_s->dispatch(args...);
799  }
800 
801 /**
802  * This function does not throw.
803  * @param f The assignor.
804  * @return The functor object after assignment.
805  */
806  FunctorArg& operator=(const FunctorArg& f) {cb_s = f.cb_s; return *this;}
807 
808 /**
809  * This function does not throw.
810  * @param f The functor to be moved.
811  * @return The functor object after the move operation.
812  */
813  FunctorArg& operator=(FunctorArg&& f) {cb_s = std::move(f.cb_s); return *this;}
814 
815 /**
816  * Two FunctorArg objects compare equal if the addresses of the
817  * CallbackArg objects they contain are the same. This comparison
818  * operator does not throw.
819  */
820  friend bool operator== <>(const FunctorArg&, const FunctorArg&) noexcept;
821 
822 /**
823  * One FunctorArg object is less than another if the address of the
824  * CallbackArg object contained by the first is regarded by std::less
825  * as less than the address of the CallbackArg object contained by the
826  * other. This comparison operator does not throw unless std::less
827  * applied to pointer types throws (which it would not do with any
828  * sane implementation).
829  */
830  friend bool operator< <>(const FunctorArg&, const FunctorArg&);
831 
832  friend struct std::hash<FunctorArg>;
833 
834 /**
835  * Constructor of first FunctorArg holding the referenced callback.
836  * As it is not marked explicit, it is also a type conversion
837  * constructor.
838  * @param cb The CallbackArg object which the functor is to manage.
839  * @exception std::bad_alloc This might throw std::bad_alloc if
840  * memory is exhausted and the system throws in that case. Note that
841  * if such an exception is thrown, then this constructor will clean
842  * itself up and also delete the callback object passed to it.
843  * @note std::bad_alloc will not be thrown if the library has been
844  * installed using the \--with-glib-memory-slices-no-compat
845  * configuration option: instead glib will terminate the program if it
846  * is unable to obtain memory from the operating system.
847  */
848  FunctorArg(const CallbackArg<FreeArgs...>* cb): cb_s(cb) {}
849 
850 /**
851  * The copy constructor does not throw.
852  * @param f The assignor
853  */
854  FunctorArg(const FunctorArg& f): cb_s(f.cb_s) {}
855 
856 /**
857  * The move constructor does not throw.
858  * @param f The functor to be moved.
859  */
860  FunctorArg(FunctorArg&& f): cb_s(std::move(f.cb_s)) {}
861 
862  /**
863  * Default constructor, where a Callback::CallbackArg object is to be
864  * assigned later (via the type conversion constructor and/or the
865  * assignment operator). This constructor does not throw.
866  */
868 
869 /* Only has effect if --with-glib-memory-slices-compat or
870  --with-glib-memory-slices-no-compat option picked */
872 };
873 
874 /**
875  * @class SafeFunctorArg callback.h c++-gtk-utils/callback.h
876  * @brief Functor class holding a Callback::CallbackArg object, with
877  * thread-safe reference count.
878  * @sa FunctorArg
879  * @sa Callback namespace
880  *
881  * This class is the same as Callback::FunctorArg except that it will
882  * provide synchronisation of the reference count between threads.
883  * Use it where a functor wrapper object is to be passed between
884  * threads. The FunctorArg documentation gives details on usage.
885  *
886  * Callback::SafeFunctorArg<> is typedef'ed to Callback::SafeFunctor.
887  *
888  * For further background, read this: Callback
889  */
890 
891 template <class... FreeArgs>
892 class SafeFunctorArg {
893  SharedLockPtr<const CallbackArg<FreeArgs...>> cb_s;
894 public:
895 /**
896  * This will execute the function, callable object or class method
897  * represented by the callback encapsulated by this object, or do
898  * nothing if this object has not been initialized with a callback.
899  * It will only throw if the executed function, callable object or
900  * class method throws, or if the copy constructor of a free or bound
901  * argument throws and it is not a reference argument. It is thread
902  * safe if the referenced function or class method is thread safe.
903  * @param args The unbound arguments to be passed to the referenced
904  * function, callable object or class method, if any.
905  */
906  void operator()(typename Cgu::Param<FreeArgs>::ParamType... args) const {
907  if (cb_s.get()) cb_s->dispatch(args...);
908  }
909 
910 /**
911  * This function does not throw.
912  * @param f The assignor.
913  * @return The functor object after assignment.
914  */
915  SafeFunctorArg& operator=(const SafeFunctorArg& f) {cb_s = f.cb_s; return *this;}
916 
917 /**
918  * This function does not throw.
919  * @param f The functor to be moved.
920  * @return The functor object after the move operation.
921  */
922  SafeFunctorArg& operator=(SafeFunctorArg&& f) {cb_s = std::move(f.cb_s); return *this;}
923 
924 /**
925  * Two SafeFunctorArg objects compare equal if the addresses of the
926  * CallbackArg objects they contain are the same. This comparison
927  * operator does not throw.
928  */
929  friend bool operator== <>(const SafeFunctorArg&, const SafeFunctorArg&) noexcept;
930 
931 /**
932  * One SafeFunctorArg object is less than another if the address of
933  * the CallbackArg object contained by the first is regarded by
934  * std::less as less than the address of the CallbackArg object
935  * contained by the other. This comparison operator does not throw
936  * unless std::less applied to pointer types throws (which it would
937  * not do with any sane implementation).
938  */
939  friend bool operator< <>(const SafeFunctorArg&, const SafeFunctorArg&);
940 
941  friend struct std::hash<SafeFunctorArg>;
942 
943  /**
944  * Constructor of first SafeFunctorArg holding the referenced
945  * callback. As it is not marked explicit, it is also a type
946  * conversion constructor.
947  * @param cb The CallbackArg object which the functor is to manage.
948  * @exception std::bad_alloc This might throw std::bad_alloc if
949  * memory is exhausted and the system throws in that case. Note that
950  * if such an exception is thrown, then this constructor will clean
951  * itself up and also delete the callback object passed to it.
952  * @note std::bad_alloc will not be thrown if the library has been
953  * installed using the \--with-glib-memory-slices-no-compat
954  * configuration option: instead glib will terminate the program if it
955  * is unable to obtain memory from the operating system.
956  */
957  SafeFunctorArg(const CallbackArg<FreeArgs...>* cb): cb_s(cb) {}
958 
959 /**
960  * The copy constructor does not throw.
961  * @param f The assignor.
962  */
963  SafeFunctorArg(const SafeFunctorArg& f): cb_s(f.cb_s) {}
964 
965 /**
966  * The move constructor does not throw.
967  * @param f The functor to be moved.
968  */
969  SafeFunctorArg(SafeFunctorArg&& f): cb_s(std::move(f.cb_s)) {}
970 
971  /**
972  * Default constructor, where a Callback::CallbackArg object is to be
973  * assigned later (via the type conversion constructor and/or the
974  * assignment operator). This constructor does not throw.
975  * @note The reference count maintained with respect to the contained
976  * callback object is thread-safe, so SafeFunctorArg objects may be
977  * copied between threads by the implicit assignment operator and put
978  * in different containers in different threads. They use a
979  * SharedLockPtr object to hold the referenced callback object.
980  */
982 
983 /* Only has effect if --with-glib-memory-slices-compat or
984  --with-glib-memory-slices-no-compat option picked */
986 };
987 
988 /* the callback implementation classes */
989 
990 template <class T, class... FreeArgs>
991 class Callback0: public CallbackArg<FreeArgs...> {
992 public:
993  typedef void (T::* MemFunc)(FreeArgs...);
994 private:
995  T* obj;
996  MemFunc func;
997 public:
998  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
999  (obj->*func)(free_args...);
1000  }
1001  Callback0(T& obj_, MemFunc func_): obj(&obj_), func(func_) {}
1002 };
1003 
1004 template <bool unref, class T, class BoundArg, class... FreeArgs>
1005 class Callback1: public CallbackArg<FreeArgs...> {
1006 public:
1007  typedef void (T::* MemFunc)(BoundArg, FreeArgs...);
1008 private:
1009  T* obj;
1010  MemFunc func;
1012 public:
1013  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1014  (obj->*func)(arg, free_args...);
1015  }
1016  template <class Arg>
1017  Callback1(T& obj_, MemFunc func_,
1018  Arg&& arg_): obj(&obj_), func(func_), arg(std::forward<Arg>(arg_)) {}
1019 };
1020 
1021 template <bool unref, class T, class BoundArg1, class BoundArg2, class... FreeArgs>
1022 class Callback2: public CallbackArg<FreeArgs...> {
1023 public:
1024  typedef void (T::* MemFunc)(BoundArg1, BoundArg2, FreeArgs...);
1025 private:
1026  T* obj;
1027  MemFunc func;
1030 public:
1031  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1032  (obj->*func)(arg1, arg2, free_args...);
1033  }
1034  template <class Arg1, class Arg2>
1035  Callback2(T& obj_, MemFunc func_,
1036  Arg1&& arg1_,
1037  Arg2&& arg2_): obj(&obj_), func(func_),
1038  arg1(std::forward<Arg1>(arg1_)),
1039  arg2(std::forward<Arg2>(arg2_)) {}
1040 };
1041 
1042 template <bool unref, class T, class BoundArg1, class BoundArg2, class BoundArg3, class... FreeArgs>
1043 class Callback3: public CallbackArg<FreeArgs...> {
1044 public:
1045  typedef void (T::* MemFunc)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...);
1046 private:
1047  T* obj;
1048  MemFunc func;
1052 public:
1053  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1054  (obj->*func)(arg1, arg2, arg3, free_args...);
1055  }
1056  template <class Arg1, class Arg2, class Arg3>
1057  Callback3(T& obj_, MemFunc func_,
1058  Arg1&& arg1_,
1059  Arg2&& arg2_,
1060  Arg3&& arg3_):
1061  obj(&obj_), func(func_),
1062  arg1(std::forward<Arg1>(arg1_)),
1063  arg2(std::forward<Arg2>(arg2_)),
1064  arg3(std::forward<Arg3>(arg3_)) {}
1065 };
1066 
1067 template <bool unref, class T, class BoundArg1, class BoundArg2, class BoundArg3,
1068  class BoundArg4, class... FreeArgs>
1069 class Callback4: public CallbackArg<FreeArgs...> {
1070 public:
1071  typedef void (T::* MemFunc)(BoundArg1, BoundArg2, BoundArg3, BoundArg4, FreeArgs...);
1072 private:
1073  T* obj;
1074  MemFunc func;
1079 public:
1080  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1081  (obj->*func)(arg1, arg2, arg3, arg4, free_args...);
1082  }
1083  template <class Arg1, class Arg2, class Arg3, class Arg4>
1084  Callback4(T& obj_, MemFunc func_,
1085  Arg1&& arg1_,
1086  Arg2&& arg2_,
1087  Arg3&& arg3_,
1088  Arg4&& arg4_):
1089  obj(&obj_), func(func_),
1090  arg1(std::forward<Arg1>(arg1_)),
1091  arg2(std::forward<Arg2>(arg2_)),
1092  arg3(std::forward<Arg3>(arg3_)),
1093  arg4(std::forward<Arg4>(arg4_)) {}
1094 };
1095 
1096 template <bool unref, class T, class BoundArg1, class BoundArg2, class BoundArg3,
1097  class BoundArg4, class BoundArg5, class... FreeArgs>
1098 class Callback5: public CallbackArg<FreeArgs...> {
1099 public:
1100  typedef void (T::* MemFunc)(BoundArg1, BoundArg2, BoundArg3,
1101  BoundArg4, BoundArg5, FreeArgs...);
1102 private:
1103  T* obj;
1104  MemFunc func;
1110 public:
1111  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1112  (obj->*func)(arg1, arg2, arg3, arg4, arg5, free_args...);
1113  }
1114  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
1115  Callback5(T& obj_, MemFunc func_,
1116  Arg1&& arg1_,
1117  Arg2&& arg2_,
1118  Arg3&& arg3_,
1119  Arg4&& arg4_,
1120  Arg5&& arg5_):
1121  obj(&obj_), func(func_),
1122  arg1(std::forward<Arg1>(arg1_)),
1123  arg2(std::forward<Arg2>(arg2_)),
1124  arg3(std::forward<Arg3>(arg3_)),
1125  arg4(std::forward<Arg4>(arg4_)),
1126  arg5(std::forward<Arg5>(arg5_)) {}
1127 };
1128 
1129 /* const versions, for binding to const methods */
1130 
1131 template <class T, class... FreeArgs>
1132 class Callback0_const: public CallbackArg<FreeArgs...> {
1133 public:
1134  typedef void (T::* MemFunc)(FreeArgs...) const;
1135 private:
1136  const T* obj;
1137  MemFunc func;
1138 public:
1139  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1140  (obj->*func)(free_args...);
1141  }
1142  Callback0_const(const T& obj_, MemFunc func_): obj(&obj_), func(func_) {}
1143 };
1144 
1145 template <bool unref, class T, class BoundArg, class... FreeArgs>
1146 class Callback1_const: public CallbackArg<FreeArgs...> {
1147 public:
1148  typedef void (T::* MemFunc)(BoundArg, FreeArgs...) const;
1149 private:
1150  const T* obj;
1151  MemFunc func;
1153 public:
1154  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1155  (obj->*func)(arg, free_args...);
1156  }
1157  template <class Arg>
1158  Callback1_const(const T& obj_, MemFunc func_,
1159  Arg&& arg_): obj(&obj_), func(func_), arg(std::forward<Arg>(arg_)) {}
1160 };
1161 
1162 template <bool unref, class T, class BoundArg1, class BoundArg2, class... FreeArgs>
1163 class Callback2_const: public CallbackArg<FreeArgs...> {
1164 public:
1165  typedef void (T::* MemFunc)(BoundArg1, BoundArg2, FreeArgs...) const;
1166 private:
1167  const T* obj;
1168  MemFunc func;
1171 public:
1172  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1173  (obj->*func)(arg1, arg2, free_args...);
1174  }
1175  template <class Arg1, class Arg2>
1176  Callback2_const(const T& obj_, MemFunc func_,
1177  Arg1&& arg1_,
1178  Arg2&& arg2_): obj(&obj_), func(func_),
1179  arg1(std::forward<Arg1>(arg1_)),
1180  arg2(std::forward<Arg2>(arg2_)) {}
1181 };
1182 
1183 template <bool unref, class T, class BoundArg1, class BoundArg2, class BoundArg3, class... FreeArgs>
1184 class Callback3_const: public CallbackArg<FreeArgs...> {
1185 public:
1186  typedef void (T::* MemFunc)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...) const;
1187 private:
1188  const T* obj;
1189  MemFunc func;
1193 public:
1194  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1195  (obj->*func)(arg1, arg2, arg3, free_args...);
1196  }
1197  template <class Arg1, class Arg2, class Arg3>
1198  Callback3_const(const T& obj_, MemFunc func_,
1199  Arg1&& arg1_,
1200  Arg2&& arg2_,
1201  Arg3&& arg3_):
1202  obj(&obj_), func(func_),
1203  arg1(std::forward<Arg1>(arg1_)),
1204  arg2(std::forward<Arg2>(arg2_)),
1205  arg3(std::forward<Arg3>(arg3_)) {}
1206 };
1207 
1208 template <bool unref, class T, class BoundArg1, class BoundArg2, class BoundArg3,
1209  class BoundArg4, class... FreeArgs>
1210 class Callback4_const: public CallbackArg<FreeArgs...> {
1211 public:
1212  typedef void (T::* MemFunc)(BoundArg1, BoundArg2, BoundArg3, BoundArg4, FreeArgs...) const;
1213 private:
1214  const T* obj;
1215  MemFunc func;
1220 public:
1221  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1222  (obj->*func)(arg1, arg2, arg3, arg4, free_args...);
1223  }
1224  template <class Arg1, class Arg2, class Arg3, class Arg4>
1225  Callback4_const(const T& obj_, MemFunc func_,
1226  Arg1&& arg1_,
1227  Arg2&& arg2_,
1228  Arg3&& arg3_,
1229  Arg4&& arg4_):
1230  obj(&obj_), func(func_),
1231  arg1(std::forward<Arg1>(arg1_)),
1232  arg2(std::forward<Arg2>(arg2_)),
1233  arg3(std::forward<Arg3>(arg3_)),
1234  arg4(std::forward<Arg4>(arg4_)) {}
1235 };
1236 
1237 template <bool unref, class T, class BoundArg1, class BoundArg2, class BoundArg3,
1238  class BoundArg4, class BoundArg5, class... FreeArgs>
1239 class Callback5_const: public CallbackArg<FreeArgs...> {
1240 public:
1241  typedef void (T::* MemFunc)(BoundArg1, BoundArg2, BoundArg3,
1242  BoundArg4, BoundArg5, FreeArgs...) const;
1243 private:
1244  const T* obj;
1245  MemFunc func;
1251 public:
1252  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1253  (obj->*func)(arg1, arg2, arg3, arg4, arg5, free_args...);
1254  }
1255  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
1256  Callback5_const(const T& obj_, MemFunc func_,
1257  Arg1&& arg1_,
1258  Arg2&& arg2_,
1259  Arg3&& arg3_,
1260  Arg4&& arg4_,
1261  Arg5&& arg5_):
1262  obj(&obj_), func(func_),
1263  arg1(std::forward<Arg1>(arg1_)),
1264  arg2(std::forward<Arg2>(arg2_)),
1265  arg3(std::forward<Arg3>(arg3_)),
1266  arg4(std::forward<Arg4>(arg4_)),
1267  arg5(std::forward<Arg5>(arg5_)) {}
1268 };
1269 
1270 /* for static class methods and non-class functions */
1271 
1272 template <class... FreeArgs>
1273 class Callback0_static: public CallbackArg<FreeArgs...> {
1274 public:
1275  typedef void (*Func)(FreeArgs...);
1276 private:
1277  Func func;
1278 public:
1279  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1280  func(free_args...);
1281  }
1282  Callback0_static(Func func_): func(func_) {}
1283 };
1284 
1285 template <bool unref, class BoundArg, class... FreeArgs>
1286 class Callback1_static: public CallbackArg<FreeArgs...> {
1287 public:
1288  typedef void (*Func)(BoundArg, FreeArgs...);
1289 private:
1290  Func func;
1292 public:
1293  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1294  func(arg, free_args...);
1295  }
1296  template <class Arg>
1297  Callback1_static(Func func_, Arg&& arg_): func(func_), arg(std::forward<Arg>(arg_)) {}
1298 };
1299 
1300 template <bool unref, class BoundArg1, class BoundArg2, class... FreeArgs>
1301 class Callback2_static: public CallbackArg<FreeArgs...> {
1302 public:
1303  typedef void (*Func)(BoundArg1, BoundArg2, FreeArgs...);
1304 private:
1305  Func func;
1308 public:
1309  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1310  func(arg1, arg2, free_args...);
1311  }
1312  template <class Arg1, class Arg2>
1313  Callback2_static(Func func_, Arg1&& arg1_,
1314  Arg2&& arg2_): func(func_),
1315  arg1(std::forward<Arg1>(arg1_)),
1316  arg2(std::forward<Arg2>(arg2_)) {}
1317 };
1318 
1319 template <bool unref, class BoundArg1, class BoundArg2, class BoundArg3, class... FreeArgs>
1320 class Callback3_static: public CallbackArg<FreeArgs...> {
1321 public:
1322  typedef void (*Func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...);
1323 private:
1324  Func func;
1328 public:
1329  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1330  func(arg1, arg2, arg3, free_args...);
1331  }
1332  template <class Arg1, class Arg2, class Arg3>
1334  Arg1&& arg1_,
1335  Arg2&& arg2_,
1336  Arg3&& arg3_):
1337  func(func_),
1338  arg1(std::forward<Arg1>(arg1_)),
1339  arg2(std::forward<Arg2>(arg2_)),
1340  arg3(std::forward<Arg3>(arg3_)) {}
1341 };
1342 
1343 template <bool unref, class BoundArg1, class BoundArg2, class BoundArg3,
1344  class BoundArg4, class... FreeArgs>
1345 class Callback4_static: public CallbackArg<FreeArgs...> {
1346 public:
1347  typedef void (*Func)(BoundArg1, BoundArg2, BoundArg3, BoundArg4, FreeArgs...);
1348 private:
1349  Func func;
1354 public:
1355  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1356  func(arg1, arg2, arg3, arg4, free_args...);
1357  }
1358  template <class Arg1, class Arg2, class Arg3, class Arg4>
1360  Arg1&& arg1_,
1361  Arg2&& arg2_,
1362  Arg3&& arg3_,
1363  Arg4&& arg4_):
1364  func(func_),
1365  arg1(std::forward<Arg1>(arg1_)),
1366  arg2(std::forward<Arg2>(arg2_)),
1367  arg3(std::forward<Arg3>(arg3_)),
1368  arg4(std::forward<Arg4>(arg4_)) {}
1369 };
1370 
1371 template <bool unref, class BoundArg1, class BoundArg2, class BoundArg3,
1372  class BoundArg4, class BoundArg5, class... FreeArgs>
1373 class Callback5_static: public CallbackArg<FreeArgs...> {
1374 public:
1375  typedef void (*Func)(BoundArg1, BoundArg2, BoundArg3,
1376  BoundArg4, BoundArg5, FreeArgs...);
1377 private:
1378  Func func;
1384 public:
1385  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {
1386  func(arg1, arg2, arg3, arg4, arg5, free_args...);
1387  }
1388  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
1390  Arg1&& arg1_,
1391  Arg2&& arg2_,
1392  Arg3&& arg3_,
1393  Arg4&& arg4_,
1394  Arg5&& arg5_):
1395  func(func_),
1396  arg1(std::forward<Arg1>(arg1_)),
1397  arg2(std::forward<Arg2>(arg2_)),
1398  arg3(std::forward<Arg3>(arg3_)),
1399  arg4(std::forward<Arg4>(arg4_)),
1400  arg5(std::forward<Arg5>(arg5_)) {}
1401 };
1402 
1403 // generic class for callable objects such as lambdas
1404 template <class Lambda, class... FreeArgs>
1405 class Callback_lambda: public CallbackArg<FreeArgs...> {
1406  // making 'l' mutable means that Callback_lamdba objects can contain
1407  // mutable lambda expressions
1408  mutable Lambda l;
1409 public:
1410  void dispatch(typename Cgu::Param<FreeArgs>::ParamType... free_args) const {l(free_args...);}
1411  template <class L> Callback_lambda(L&& l_): l(std::forward<L>(l_)) {}
1412 };
1413 
1414 /* Convenience functions making callback objects on freestore. These
1415  * can for example be passed as the first argument of the
1416  * Thread::start() method in thread.h. They are also used by the
1417  * Callback::post() function.
1418 */
1419 
1420 /**
1421  * A convenience function to make Callback::CallbackArg objects
1422  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1423  * is exhausted and the system throws in that case. This exception
1424  * will not be thrown if the library has been installed using the
1425  * \--with-glib-memory-slices-no-compat configuration option (instead
1426  * glib will terminate the program if it is unable to obtain memory
1427  * from the operating system).
1428  */
1429 template <class T, class... FreeArgs>
1430 CallbackArg<FreeArgs...>* make(T& t,
1431  void (T::*func)(FreeArgs...)) {
1432  return new Callback0<T, FreeArgs...>{t, func};
1433 }
1434 
1435 /**
1436  * Since this function constructs a callback which does not take a
1437  * bound argument, it is a synonym for make() (the two are identical).
1438  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1439  * is exhausted and the system throws in that case. This exception
1440  * will not be thrown if the library has been installed using the
1441  * \--with-glib-memory-slices-no-compat configuration option (instead
1442  * glib will terminate the program if it is unable to obtain memory
1443  * from the operating system).
1444  *
1445  * Since 2.0.0-rc3
1446  */
1447 template <class T, class... FreeArgs>
1448 CallbackArg<FreeArgs...>* make_ref(T& t,
1449  void (T::*func)(FreeArgs...)) {
1450  return new Callback0<T, FreeArgs...>{t, func};
1451 }
1452 
1453 /**
1454  * A convenience function to make Callback::CallbackArg objects
1455  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1456  * is exhausted and the system throws in that case (this exception
1457  * will not be thrown if the library has been installed using the
1458  * \--with-glib-memory-slices-no-compat configuration option: instead
1459  * glib will terminate the program if it is unable to obtain memory
1460  * from the operating system). It will also throw if the copy
1461  * constructor of a bound argument throws and it is not a reference
1462  * argument.
1463  */
1464 template <class T, class BoundArg, class... FreeArgs>
1465 CallbackArg<FreeArgs...>* make(T& t,
1466  void (T::*func)(BoundArg, FreeArgs...),
1467  BoundArg arg) {
1468  return new Callback1<false, T, BoundArg, FreeArgs...>{t, func, arg};
1469 }
1470 
1471 /**
1472  * An alternative function to make Callback::CallbackArg objects,
1473  * which is for use where a target function either receives a class
1474  * type bound argument by value, or receives a bound argument by
1475  * reference to const in a case where the generated CallbackArg object
1476  * is to store a copy of that argument instead of just keeping a
1477  * reference.
1478  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1479  * is exhausted and the system throws in that case (this exception
1480  * will not be thrown if the library has been installed using the
1481  * \--with-glib-memory-slices-no-compat configuration option: instead
1482  * glib will terminate the program if it is unable to obtain memory
1483  * from the operating system). It will also throw if the copy or move
1484  * constructor of a bound argument throws.
1485  *
1486  * Since 2.0.0-rc3
1487  */
1488 template <class T, class BoundArg, class Arg, class... FreeArgs>
1489 CallbackArg<FreeArgs...>* make_ref(T& t,
1490  void (T::*func)(BoundArg, FreeArgs...),
1491  Arg&& arg) {
1492  return new Callback1<true, T, BoundArg, FreeArgs...>{t, func, std::forward<Arg>(arg)};
1493 }
1494 
1495 /**
1496  * A convenience function to make Callback::CallbackArg objects
1497  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1498  * is exhausted and the system throws in that case (this exception
1499  * will not be thrown if the library has been installed using the
1500  * \--with-glib-memory-slices-no-compat configuration option: instead
1501  * glib will terminate the program if it is unable to obtain memory
1502  * from the operating system). It will also throw if the copy
1503  * constructor of a bound argument throws and it is not a reference
1504  * argument.
1505  */
1506 template <class T, class BoundArg1, class BoundArg2, class... FreeArgs>
1507 CallbackArg<FreeArgs...>* make(T& t,
1508  void (T::*func)(BoundArg1, BoundArg2, FreeArgs...),
1509  BoundArg1 arg1,
1510  BoundArg2 arg2) {
1511  return new Callback2<false, T, BoundArg1, BoundArg2, FreeArgs...>{t, func, arg1, arg2};
1512 }
1513 
1514 /**
1515  * An alternative function to make Callback::CallbackArg objects,
1516  * which is for use where a target function either receives a class
1517  * type bound argument by value, or receives a bound argument by
1518  * reference to const in a case where the generated CallbackArg object
1519  * is to store a copy of that argument instead of just keeping a
1520  * reference.
1521  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1522  * is exhausted and the system throws in that case (this exception
1523  * will not be thrown if the library has been installed using the
1524  * \--with-glib-memory-slices-no-compat configuration option: instead
1525  * glib will terminate the program if it is unable to obtain memory
1526  * from the operating system). It will also throw if the copy or move
1527  * constructor of a bound argument throws.
1528  *
1529  * Since 2.0.0-rc3
1530  */
1531 template <class T, class BoundArg1, class BoundArg2,
1532  class Arg1, class Arg2, class... FreeArgs>
1533 CallbackArg<FreeArgs...>* make_ref(T& t,
1534  void (T::*func)(BoundArg1, BoundArg2, FreeArgs...),
1535  Arg1&& arg1,
1536  Arg2&& arg2) {
1537  return new Callback2<true, T, BoundArg1, BoundArg2, FreeArgs...>{t, func,
1538  std::forward<Arg1>(arg1),
1539  std::forward<Arg2>(arg2)};
1540 }
1541 
1542 /**
1543  * A convenience function to make Callback::CallbackArg objects
1544  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1545  * is exhausted and the system throws in that case (this exception
1546  * will not be thrown if the library has been installed using the
1547  * \--with-glib-memory-slices-no-compat configuration option: instead
1548  * glib will terminate the program if it is unable to obtain memory
1549  * from the operating system). It will also throw if the copy
1550  * constructor of a bound argument throws and it is not a reference
1551  * argument.
1552  */
1553 template <class T, class BoundArg1, class BoundArg2, class BoundArg3, class... FreeArgs>
1554 CallbackArg<FreeArgs...>* make(T& t,
1555  void (T::*func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...),
1556  BoundArg1 arg1,
1557  BoundArg2 arg2,
1558  BoundArg3 arg3) {
1559  return new Callback3<false, T, BoundArg1, BoundArg2, BoundArg3, FreeArgs...>{t, func, arg1, arg2, arg3};
1560 }
1561 
1562 /**
1563  * An alternative function to make Callback::CallbackArg objects,
1564  * which is for use where a target function either receives a class
1565  * type bound argument by value, or receives a bound argument by
1566  * reference to const in a case where the generated CallbackArg object
1567  * is to store a copy of that argument instead of just keeping a
1568  * reference.
1569  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1570  * is exhausted and the system throws in that case (this exception
1571  * will not be thrown if the library has been installed using the
1572  * \--with-glib-memory-slices-no-compat configuration option: instead
1573  * glib will terminate the program if it is unable to obtain memory
1574  * from the operating system). It will also throw if the copy or move
1575  * constructor of a bound argument throws.
1576  *
1577  * Since 2.0.0-rc3
1578  */
1579 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
1580  class Arg1, class Arg2, class Arg3, class... FreeArgs>
1581 CallbackArg<FreeArgs...>* make_ref(T& t,
1582  void (T::*func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...),
1583  Arg1&& arg1,
1584  Arg2&& arg2,
1585  Arg3&& arg3) {
1586  return new Callback3<true, T, BoundArg1, BoundArg2, BoundArg3, FreeArgs...>{t, func,
1587  std::forward<Arg1>(arg1),
1588  std::forward<Arg2>(arg2),
1589  std::forward<Arg3>(arg3)};
1590 }
1591 
1592 /**
1593  * A convenience function to make Callback::CallbackArg objects
1594  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1595  * is exhausted and the system throws in that case (this exception
1596  * will not be thrown if the library has been installed using the
1597  * \--with-glib-memory-slices-no-compat configuration option: instead
1598  * glib will terminate the program if it is unable to obtain memory
1599  * from the operating system). It will also throw if the copy
1600  * constructor of a bound argument throws and it is not a reference
1601  * argument.
1602  */
1603 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
1604  class BoundArg4, class... FreeArgs>
1605 CallbackArg<FreeArgs...>* make(T& t,
1606  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
1607  BoundArg4, FreeArgs...),
1608  BoundArg1 arg1,
1609  BoundArg2 arg2,
1610  BoundArg3 arg3,
1611  BoundArg4 arg4) {
1612  return new Callback4<false, T, BoundArg1, BoundArg2, BoundArg3,
1613  BoundArg4, FreeArgs...>{t, func, arg1, arg2, arg3, arg4};
1614 }
1615 
1616 /**
1617  * An alternative function to make Callback::CallbackArg objects,
1618  * which is for use where a target function either receives a class
1619  * type bound argument by value, or receives a bound argument by
1620  * reference to const in a case where the generated CallbackArg object
1621  * is to store a copy of that argument instead of just keeping a
1622  * reference.
1623  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1624  * is exhausted and the system throws in that case (this exception
1625  * will not be thrown if the library has been installed using the
1626  * \--with-glib-memory-slices-no-compat configuration option: instead
1627  * glib will terminate the program if it is unable to obtain memory
1628  * from the operating system). It will also throw if the copy or move
1629  * constructor of a bound argument throws.
1630  *
1631  * Since 2.0.0-rc3
1632  */
1633 template <class T, class BoundArg1, class BoundArg2, class BoundArg3, class BoundArg4,
1634  class Arg1, class Arg2, class Arg3, class Arg4, class... FreeArgs>
1635 CallbackArg<FreeArgs...>* make_ref(T& t,
1636  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
1637  BoundArg4, FreeArgs...),
1638  Arg1&& arg1,
1639  Arg2&& arg2,
1640  Arg3&& arg3,
1641  Arg4&& arg4) {
1642  return new Callback4<true, T, BoundArg1, BoundArg2, BoundArg3,
1643  BoundArg4, FreeArgs...>{t, func,
1644  std::forward<Arg1>(arg1),
1645  std::forward<Arg2>(arg2),
1646  std::forward<Arg3>(arg3),
1647  std::forward<Arg4>(arg4)};
1648 }
1649 
1650 /**
1651  * A convenience function to make Callback::CallbackArg objects
1652  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1653  * is exhausted and the system throws in that case (this exception
1654  * will not be thrown if the library has been installed using the
1655  * \--with-glib-memory-slices-no-compat configuration option: instead
1656  * glib will terminate the program if it is unable to obtain memory
1657  * from the operating system). It will also throw if the copy
1658  * constructor of a bound argument throws and it is not a reference
1659  * argument.
1660  */
1661 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
1662  class BoundArg4, class BoundArg5, class... FreeArgs>
1663 CallbackArg<FreeArgs...>* make(T& t,
1664  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
1665  BoundArg4, BoundArg5, FreeArgs...),
1666  BoundArg1 arg1,
1667  BoundArg2 arg2,
1668  BoundArg3 arg3,
1669  BoundArg4 arg4,
1670  BoundArg5 arg5) {
1671  return new Callback5<false, T, BoundArg1, BoundArg2, BoundArg3,
1672  BoundArg4, BoundArg5, FreeArgs...>{t, func, arg1, arg2, arg3, arg4, arg5};
1673 }
1674 
1675 /**
1676  * An alternative function to make Callback::CallbackArg objects,
1677  * which is for use where a target function either receives a class
1678  * type bound argument by value, or receives a bound argument by
1679  * reference to const in a case where the generated CallbackArg object
1680  * is to store a copy of that argument instead of just keeping a
1681  * reference.
1682  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1683  * is exhausted and the system throws in that case (this exception
1684  * will not be thrown if the library has been installed using the
1685  * \--with-glib-memory-slices-no-compat configuration option: instead
1686  * glib will terminate the program if it is unable to obtain memory
1687  * from the operating system). It will also throw if the copy or move
1688  * constructor of a bound argument throws.
1689  *
1690  * Since 2.0.0-rc3
1691  */
1692 template <class T, class BoundArg1, class BoundArg2, class BoundArg3, class BoundArg4, class BoundArg5,
1693  class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class... FreeArgs>
1694 CallbackArg<FreeArgs...>* make_ref(T& t,
1695  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
1696  BoundArg4, BoundArg5, FreeArgs...),
1697  Arg1&& arg1,
1698  Arg2&& arg2,
1699  Arg3&& arg3,
1700  Arg4&& arg4,
1701  Arg5&& arg5) {
1702  return new Callback5<true, T, BoundArg1, BoundArg2, BoundArg3,
1703  BoundArg4, BoundArg5, FreeArgs...>{t, func,
1704  std::forward<Arg1>(arg1),
1705  std::forward<Arg2>(arg2),
1706  std::forward<Arg3>(arg3),
1707  std::forward<Arg4>(arg4),
1708  std::forward<Arg5>(arg5)};
1709 }
1710 
1711 /* const versions, for binding to const methods */
1712 
1713 /**
1714  * A convenience function to make Callback::CallbackArg objects
1715  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1716  * is exhausted and the system throws in that case. This exception
1717  * will not be thrown if the library has been installed using the
1718  * \--with-glib-memory-slices-no-compat configuration option (instead
1719  * glib will terminate the program if it is unable to obtain memory
1720  * from the operating system).
1721  */
1722 template <class T, class... FreeArgs>
1723 CallbackArg<FreeArgs...>* make(const T& t,
1724  void (T::*func)(FreeArgs...) const) {
1725  return new Callback0_const<T, FreeArgs...>{t, func};
1726 }
1727 
1728 /**
1729  * Since this function constructs a callback which does not take a
1730  * bound argument, it is a synonym for make() (the two are identical).
1731  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1732  * is exhausted and the system throws in that case. This exception
1733  * will not be thrown if the library has been installed using the
1734  * \--with-glib-memory-slices-no-compat configuration option (instead
1735  * glib will terminate the program if it is unable to obtain memory
1736  * from the operating system).
1737  *
1738  * Since 2.0.0-rc3
1739  */
1740 template <class T, class... FreeArgs>
1741 CallbackArg<FreeArgs...>* make_ref(const T& t,
1742  void (T::*func)(FreeArgs...) const) {
1743  return new Callback0_const<T, FreeArgs...>{t, func};
1744 }
1745 
1746 /**
1747  * A convenience function to make Callback::CallbackArg objects
1748  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1749  * is exhausted and the system throws in that case (this exception
1750  * will not be thrown if the library has been installed using the
1751  * \--with-glib-memory-slices-no-compat configuration option: instead
1752  * glib will terminate the program if it is unable to obtain memory
1753  * from the operating system). It will also throw if the copy
1754  * constructor of a bound argument throws and it is not a reference
1755  * argument.
1756  */
1757 template <class T, class BoundArg, class... FreeArgs>
1758 CallbackArg<FreeArgs...>* make(const T& t,
1759  void (T::*func)(BoundArg, FreeArgs...) const,
1760  BoundArg arg) {
1761  return new Callback1_const<false, T, BoundArg, FreeArgs...>{t, func, arg};
1762 }
1763 
1764 /**
1765  * An alternative function to make Callback::CallbackArg objects,
1766  * which is for use where a target function either receives a class
1767  * type bound argument by value, or receives a bound argument by
1768  * reference to const in a case where the generated CallbackArg object
1769  * is to store a copy of that argument instead of just keeping a
1770  * reference.
1771  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1772  * is exhausted and the system throws in that case (this exception
1773  * will not be thrown if the library has been installed using the
1774  * \--with-glib-memory-slices-no-compat configuration option: instead
1775  * glib will terminate the program if it is unable to obtain memory
1776  * from the operating system). It will also throw if the copy or move
1777  * constructor of a bound argument throws.
1778  *
1779  * Since 2.0.0-rc3
1780  */
1781 template <class T, class BoundArg, class Arg, class... FreeArgs>
1782 CallbackArg<FreeArgs...>* make_ref(const T& t,
1783  void (T::*func)(BoundArg, FreeArgs...) const,
1784  Arg&& arg) {
1785  return new Callback1_const<true, T, BoundArg, FreeArgs...>{t, func, std::forward<Arg>(arg)};
1786 }
1787 
1788 /**
1789  * A convenience function to make Callback::CallbackArg objects
1790  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1791  * is exhausted and the system throws in that case (this exception
1792  * will not be thrown if the library has been installed using the
1793  * \--with-glib-memory-slices-no-compat configuration option: instead
1794  * glib will terminate the program if it is unable to obtain memory
1795  * from the operating system). It will also throw if the copy
1796  * constructor of a bound argument throws and it is not a reference
1797  * argument.
1798  */
1799 template <class T, class BoundArg1, class BoundArg2, class... FreeArgs>
1800 CallbackArg<FreeArgs...>* make(const T& t,
1801  void (T::*func)(BoundArg1, BoundArg2, FreeArgs...) const,
1802  BoundArg1 arg1,
1803  BoundArg2 arg2) {
1804  return new Callback2_const<false, T, BoundArg1, BoundArg2, FreeArgs...>{t, func, arg1, arg2};
1805 }
1806 
1807 /**
1808  * An alternative function to make Callback::CallbackArg objects,
1809  * which is for use where a target function either receives a class
1810  * type bound argument by value, or receives a bound argument by
1811  * reference to const in a case where the generated CallbackArg object
1812  * is to store a copy of that argument instead of just keeping a
1813  * reference.
1814  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1815  * is exhausted and the system throws in that case (this exception
1816  * will not be thrown if the library has been installed using the
1817  * \--with-glib-memory-slices-no-compat configuration option: instead
1818  * glib will terminate the program if it is unable to obtain memory
1819  * from the operating system). It will also throw if the copy or move
1820  * constructor of a bound argument throws.
1821  *
1822  * Since 2.0.0-rc3
1823  */
1824 template <class T, class BoundArg1, class BoundArg2,
1825  class Arg1, class Arg2, class... FreeArgs>
1826 CallbackArg<FreeArgs...>* make_ref(const T& t,
1827  void (T::*func)(BoundArg1, BoundArg2, FreeArgs...) const,
1828  Arg1&& arg1,
1829  Arg2&& arg2) {
1830  return new Callback2_const<true, T, BoundArg1, BoundArg2, FreeArgs...>{t, func,
1831  std::forward<Arg1>(arg1),
1832  std::forward<Arg2>(arg2)};
1833 }
1834 
1835 /**
1836  * A convenience function to make Callback::CallbackArg objects
1837  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1838  * is exhausted and the system throws in that case (this exception
1839  * will not be thrown if the library has been installed using the
1840  * \--with-glib-memory-slices-no-compat configuration option: instead
1841  * glib will terminate the program if it is unable to obtain memory
1842  * from the operating system). It will also throw if the copy
1843  * constructor of a bound argument throws and it is not a reference
1844  * argument.
1845  */
1846 template <class T, class BoundArg1, class BoundArg2, class BoundArg3, class... FreeArgs>
1847 CallbackArg<FreeArgs...>* make(const T& t,
1848  void (T::*func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...) const,
1849  BoundArg1 arg1,
1850  BoundArg2 arg2,
1851  BoundArg3 arg3) {
1852  return new Callback3_const<false, T, BoundArg1, BoundArg2, BoundArg3, FreeArgs...>{t, func, arg1, arg2, arg3};
1853 }
1854 
1855 /**
1856  * An alternative function to make Callback::CallbackArg objects,
1857  * which is for use where a target function either receives a class
1858  * type bound argument by value, or receives a bound argument by
1859  * reference to const in a case where the generated CallbackArg object
1860  * is to store a copy of that argument instead of just keeping a
1861  * reference.
1862  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1863  * is exhausted and the system throws in that case (this exception
1864  * will not be thrown if the library has been installed using the
1865  * \--with-glib-memory-slices-no-compat configuration option: instead
1866  * glib will terminate the program if it is unable to obtain memory
1867  * from the operating system). It will also throw if the copy or move
1868  * constructor of a bound argument throws.
1869  *
1870  * Since 2.0.0-rc3
1871  */
1872 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
1873  class Arg1, class Arg2, class Arg3, class... FreeArgs>
1874 CallbackArg<FreeArgs...>* make_ref(const T& t,
1875  void (T::*func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...) const,
1876  Arg1&& arg1,
1877  Arg2&& arg2,
1878  Arg3&& arg3) {
1879  return new Callback3_const<true, T, BoundArg1, BoundArg2, BoundArg3, FreeArgs...>{t, func,
1880  std::forward<Arg1>(arg1),
1881  std::forward<Arg2>(arg2),
1882  std::forward<Arg3>(arg3)};
1883 }
1884 
1885 /**
1886  * A convenience function to make Callback::CallbackArg objects
1887  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1888  * is exhausted and the system throws in that case (this exception
1889  * will not be thrown if the library has been installed using the
1890  * \--with-glib-memory-slices-no-compat configuration option: instead
1891  * glib will terminate the program if it is unable to obtain memory
1892  * from the operating system). It will also throw if the copy
1893  * constructor of a bound argument throws and it is not a reference
1894  * argument.
1895  */
1896 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
1897  class BoundArg4, class... FreeArgs>
1898 CallbackArg<FreeArgs...>* make(const T& t,
1899  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
1900  BoundArg4, FreeArgs...) const,
1901  BoundArg1 arg1,
1902  BoundArg2 arg2,
1903  BoundArg3 arg3,
1904  BoundArg4 arg4) {
1905  return new Callback4_const<false, T, BoundArg1, BoundArg2, BoundArg3,
1906  BoundArg4, FreeArgs...>{t, func, arg1, arg2, arg3, arg4};
1907 }
1908 
1909 /**
1910  * An alternative function to make Callback::CallbackArg objects,
1911  * which is for use where a target function either receives a class
1912  * type bound argument by value, or receives a bound argument by
1913  * reference to const in a case where the generated CallbackArg object
1914  * is to store a copy of that argument instead of just keeping a
1915  * reference.
1916  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1917  * is exhausted and the system throws in that case (this exception
1918  * will not be thrown if the library has been installed using the
1919  * \--with-glib-memory-slices-no-compat configuration option: instead
1920  * glib will terminate the program if it is unable to obtain memory
1921  * from the operating system). It will also throw if the copy or move
1922  * constructor of a bound argument throws.
1923  *
1924  * Since 2.0.0-rc3
1925  */
1926 template <class T, class BoundArg1, class BoundArg2, class BoundArg3, class BoundArg4,
1927  class Arg1, class Arg2, class Arg3, class Arg4, class... FreeArgs>
1928 CallbackArg<FreeArgs...>* make_ref(const T& t,
1929  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
1930  BoundArg4, FreeArgs...) const,
1931  Arg1&& arg1,
1932  Arg2&& arg2,
1933  Arg3&& arg3,
1934  Arg4&& arg4) {
1935  return new Callback4_const<true, T, BoundArg1, BoundArg2, BoundArg3,
1936  BoundArg4, FreeArgs...>{t, func,
1937  std::forward<Arg1>(arg1),
1938  std::forward<Arg2>(arg2),
1939  std::forward<Arg3>(arg3),
1940  std::forward<Arg4>(arg4)};
1941 }
1942 
1943 /**
1944  * A convenience function to make Callback::CallbackArg objects
1945  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1946  * is exhausted and the system throws in that case (this exception
1947  * will not be thrown if the library has been installed using the
1948  * \--with-glib-memory-slices-no-compat configuration option: instead
1949  * glib will terminate the program if it is unable to obtain memory
1950  * from the operating system). It will also throw if the copy
1951  * constructor of a bound argument throws and it is not a reference
1952  * argument.
1953  */
1954 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
1955  class BoundArg4, class BoundArg5, class... FreeArgs>
1956 CallbackArg<FreeArgs...>* make(const T& t,
1957  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
1958  BoundArg4, BoundArg5, FreeArgs...) const,
1959  BoundArg1 arg1,
1960  BoundArg2 arg2,
1961  BoundArg3 arg3,
1962  BoundArg4 arg4,
1963  BoundArg5 arg5) {
1964  return new Callback5_const<false, T, BoundArg1, BoundArg2, BoundArg3,
1965  BoundArg4, BoundArg5, FreeArgs...>{t, func, arg1, arg2, arg3, arg4, arg5};
1966 }
1967 
1968 /**
1969  * An alternative function to make Callback::CallbackArg objects,
1970  * which is for use where a target function either receives a class
1971  * type bound argument by value, or receives a bound argument by
1972  * reference to const in a case where the generated CallbackArg object
1973  * is to store a copy of that argument instead of just keeping a
1974  * reference.
1975  * @exception std::bad_alloc It might throw std::bad_alloc if memory
1976  * is exhausted and the system throws in that case (this exception
1977  * will not be thrown if the library has been installed using the
1978  * \--with-glib-memory-slices-no-compat configuration option: instead
1979  * glib will terminate the program if it is unable to obtain memory
1980  * from the operating system). It will also throw if the copy or move
1981  * constructor of a bound argument throws.
1982  *
1983  * Since 2.0.0-rc3
1984  */
1985 template <class T, class BoundArg1, class BoundArg2, class BoundArg3, class BoundArg4, class BoundArg5,
1986  class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class... FreeArgs>
1987 CallbackArg<FreeArgs...>* make_ref(const T& t,
1988  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
1989  BoundArg4, BoundArg5, FreeArgs...) const,
1990  Arg1&& arg1,
1991  Arg2&& arg2,
1992  Arg3&& arg3,
1993  Arg4&& arg4,
1994  Arg5&& arg5) {
1995  return new Callback5_const<true, T, BoundArg1, BoundArg2, BoundArg3,
1996  BoundArg4, BoundArg5, FreeArgs...>{t, func,
1997  std::forward<Arg1>(arg1),
1998  std::forward<Arg2>(arg2),
1999  std::forward<Arg3>(arg3),
2000  std::forward<Arg4>(arg4),
2001  std::forward<Arg5>(arg5)};
2002 }
2003 
2004 /* for static class methods and non-class functions */
2005 
2006 /**
2007  * A convenience function to make Callback::CallbackArg objects
2008  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2009  * is exhausted and the system throws in that case. This exception
2010  * will not be thrown if the library has been installed using the
2011  * \--with-glib-memory-slices-no-compat configuration option (instead
2012  * glib will terminate the program if it is unable to obtain memory
2013  * from the operating system).
2014  */
2015 template <class... FreeArgs>
2016 CallbackArg<FreeArgs...>* make(void (*func)(FreeArgs...)) {
2017  return new Callback0_static<FreeArgs...>{func};
2018 }
2019 
2020 /**
2021  * Since this function constructs a callback which does not take a
2022  * bound argument, it is a synonym for make() (the two are identical).
2023  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2024  * is exhausted and the system throws in that case. This exception
2025  * will not be thrown if the library has been installed using the
2026  * \--with-glib-memory-slices-no-compat configuration option (instead
2027  * glib will terminate the program if it is unable to obtain memory
2028  * from the operating system).
2029  *
2030  * Since 2.0.0-rc3
2031  */
2032 template <class... FreeArgs>
2033 CallbackArg<FreeArgs...>* make_ref(void (*func)(FreeArgs...)) {
2034  return new Callback0_static<FreeArgs...>{func};
2035 }
2036 
2037 /**
2038  * A convenience function to make Callback::CallbackArg objects
2039  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2040  * is exhausted and the system throws in that case (this exception
2041  * will not be thrown if the library has been installed using the
2042  * \--with-glib-memory-slices-no-compat configuration option: instead
2043  * glib will terminate the program if it is unable to obtain memory
2044  * from the operating system). It will also throw if the copy
2045  * constructor of a bound argument throws and it is not a reference
2046  * argument.
2047  */
2048 template <class BoundArg, class... FreeArgs>
2049 CallbackArg<FreeArgs...>* make(void (*func)(BoundArg, FreeArgs...),
2050  BoundArg arg) {
2051  return new Callback1_static<false, BoundArg, FreeArgs...>{func, arg};
2052 }
2053 
2054 /**
2055  * An alternative function to make Callback::CallbackArg objects,
2056  * which is for use where a target function either receives a class
2057  * type bound argument by value, or receives a bound argument by
2058  * reference to const in a case where the generated CallbackArg object
2059  * is to store a copy of that argument instead of just keeping a
2060  * reference.
2061  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2062  * is exhausted and the system throws in that case (this exception
2063  * will not be thrown if the library has been installed using the
2064  * \--with-glib-memory-slices-no-compat configuration option: instead
2065  * glib will terminate the program if it is unable to obtain memory
2066  * from the operating system). It will also throw if the copy or move
2067  * constructor of a bound argument throws.
2068  *
2069  * Since 2.0.0-rc3
2070  */
2071 template <class BoundArg, class Arg, class... FreeArgs>
2072 CallbackArg<FreeArgs...>* make_ref(void (*func)(BoundArg, FreeArgs...),
2073  Arg&& arg) {
2074  return new Callback1_static<true, BoundArg, FreeArgs...>{func, std::forward<Arg>(arg)};
2075 }
2076 
2077 /**
2078  * A convenience function to make Callback::CallbackArg objects
2079  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2080  * is exhausted and the system throws in that case (this exception
2081  * will not be thrown if the library has been installed using the
2082  * \--with-glib-memory-slices-no-compat configuration option: instead
2083  * glib will terminate the program if it is unable to obtain memory
2084  * from the operating system). It will also throw if the copy
2085  * constructor of a bound argument throws and it is not a reference
2086  * argument.
2087  */
2088 template <class BoundArg1, class BoundArg2, class... FreeArgs>
2089 CallbackArg<FreeArgs...>* make(void (*func)(BoundArg1, BoundArg2, FreeArgs...),
2090  BoundArg1 arg1,
2091  BoundArg2 arg2) {
2092  return new Callback2_static<false, BoundArg1, BoundArg2, FreeArgs...>{func, arg1, arg2};
2093 }
2094 
2095 /**
2096  * An alternative function to make Callback::CallbackArg objects,
2097  * which is for use where a target function either receives a class
2098  * type bound argument by value, or receives a bound argument by
2099  * reference to const in a case where the generated CallbackArg object
2100  * is to store a copy of that argument instead of just keeping a
2101  * reference.
2102  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2103  * is exhausted and the system throws in that case (this exception
2104  * will not be thrown if the library has been installed using the
2105  * \--with-glib-memory-slices-no-compat configuration option: instead
2106  * glib will terminate the program if it is unable to obtain memory
2107  * from the operating system). It will also throw if the copy or move
2108  * constructor of a bound argument throws.
2109  *
2110  * Since 2.0.0-rc3
2111  */
2112 template <class BoundArg1, class BoundArg2, class Arg1, class Arg2, class... FreeArgs>
2113 CallbackArg<FreeArgs...>* make_ref(void (*func)(BoundArg1, BoundArg2, FreeArgs...),
2114  Arg1&& arg1,
2115  Arg2&& arg2) {
2116  return new Callback2_static<true, BoundArg1, BoundArg2, FreeArgs...>{func,
2117  std::forward<Arg1>(arg1),
2118  std::forward<Arg2>(arg2)};
2119 }
2120 
2121 /**
2122  * A convenience function to make Callback::CallbackArg objects
2123  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2124  * is exhausted and the system throws in that case (this exception
2125  * will not be thrown if the library has been installed using the
2126  * \--with-glib-memory-slices-no-compat configuration option: instead
2127  * glib will terminate the program if it is unable to obtain memory
2128  * from the operating system). It will also throw if the copy
2129  * constructor of a bound argument throws and it is not a reference
2130  * argument.
2131  */
2132 template <class BoundArg1, class BoundArg2, class BoundArg3, class... FreeArgs>
2133 CallbackArg<FreeArgs...>* make(void (*func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...),
2134  BoundArg1 arg1,
2135  BoundArg2 arg2,
2136  BoundArg3 arg3) {
2137  return new Callback3_static<false, BoundArg1, BoundArg2, BoundArg3, FreeArgs...>{func, arg1, arg2, arg3};
2138 }
2139 
2140 /**
2141  * An alternative function to make Callback::CallbackArg objects,
2142  * which is for use where a target function either receives a class
2143  * type bound argument by value, or receives a bound argument by
2144  * reference to const in a case where the generated CallbackArg object
2145  * is to store a copy of that argument instead of just keeping a
2146  * reference.
2147  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2148  * is exhausted and the system throws in that case (this exception
2149  * will not be thrown if the library has been installed using the
2150  * \--with-glib-memory-slices-no-compat configuration option: instead
2151  * glib will terminate the program if it is unable to obtain memory
2152  * from the operating system). It will also throw if the copy or move
2153  * constructor of a bound argument throws.
2154  *
2155  * Since 2.0.0-rc3
2156  */
2157 template <class BoundArg1, class BoundArg2, class BoundArg3,
2158  class Arg1, class Arg2, class Arg3, class... FreeArgs>
2159 CallbackArg<FreeArgs...>* make_ref(void (*func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...),
2160  Arg1&& arg1,
2161  Arg2&& arg2,
2162  Arg3&& arg3) {
2163  return new Callback3_static<true, BoundArg1, BoundArg2, BoundArg3, FreeArgs...>{func,
2164  std::forward<Arg1>(arg1),
2165  std::forward<Arg2>(arg2),
2166  std::forward<Arg3>(arg3)};
2167 }
2168 
2169 /**
2170  * A convenience function to make Callback::CallbackArg objects
2171  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2172  * is exhausted and the system throws in that case (this exception
2173  * will not be thrown if the library has been installed using the
2174  * \--with-glib-memory-slices-no-compat configuration option: instead
2175  * glib will terminate the program if it is unable to obtain memory
2176  * from the operating system). It will also throw if the copy
2177  * constructor of a bound argument throws and it is not a reference
2178  * argument.
2179  */
2180 template <class BoundArg1, class BoundArg2, class BoundArg3,
2181  class BoundArg4, class... FreeArgs>
2182 CallbackArg<FreeArgs...>* make(void (*func)(BoundArg1, BoundArg2, BoundArg3,
2183  BoundArg4, FreeArgs...),
2184  BoundArg1 arg1,
2185  BoundArg2 arg2,
2186  BoundArg3 arg3,
2187  BoundArg4 arg4) {
2188  return new Callback4_static<false, BoundArg1, BoundArg2, BoundArg3,
2189  BoundArg4, FreeArgs...>{func, arg1, arg2, arg3, arg4};
2190 }
2191 
2192 /**
2193  * An alternative function to make Callback::CallbackArg objects,
2194  * which is for use where a target function either receives a class
2195  * type bound argument by value, or receives a bound argument by
2196  * reference to const in a case where the generated CallbackArg object
2197  * is to store a copy of that argument instead of just keeping a
2198  * reference.
2199  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2200  * is exhausted and the system throws in that case (this exception
2201  * will not be thrown if the library has been installed using the
2202  * \--with-glib-memory-slices-no-compat configuration option: instead
2203  * glib will terminate the program if it is unable to obtain memory
2204  * from the operating system). It will also throw if the copy or move
2205  * constructor of a bound argument throws.
2206  *
2207  * Since 2.0.0-rc3
2208  */
2209 template <class BoundArg1, class BoundArg2, class BoundArg3, class BoundArg4,
2210  class Arg1, class Arg2, class Arg3, class Arg4, class... FreeArgs>
2211 CallbackArg<FreeArgs...>* make_ref(void (*func)(BoundArg1, BoundArg2, BoundArg3,
2212  BoundArg4, FreeArgs...),
2213  Arg1&& arg1,
2214  Arg2&& arg2,
2215  Arg3&& arg3,
2216  Arg4&& arg4) {
2217  return new Callback4_static<true, BoundArg1, BoundArg2, BoundArg3,
2218  BoundArg4, FreeArgs...>{func,
2219  std::forward<Arg1>(arg1),
2220  std::forward<Arg2>(arg2),
2221  std::forward<Arg3>(arg3),
2222  std::forward<Arg4>(arg4)};
2223 }
2224 
2225 /**
2226  * A convenience function to make Callback::CallbackArg objects
2227  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2228  * is exhausted and the system throws in that case (this exception
2229  * will not be thrown if the library has been installed using the
2230  * \--with-glib-memory-slices-no-compat configuration option: instead
2231  * glib will terminate the program if it is unable to obtain memory
2232  * from the operating system). It will also throw if the copy
2233  * constructor of a bound argument throws and it is not a reference
2234  * argument.
2235  */
2236 template <class BoundArg1, class BoundArg2, class BoundArg3,
2237  class BoundArg4, class BoundArg5, class... FreeArgs>
2238 CallbackArg<FreeArgs...>* make(void (*func)(BoundArg1, BoundArg2, BoundArg3,
2239  BoundArg4, BoundArg5, FreeArgs...),
2240  BoundArg1 arg1,
2241  BoundArg2 arg2,
2242  BoundArg3 arg3,
2243  BoundArg4 arg4,
2244  BoundArg5 arg5) {
2245  return new Callback5_static<false, BoundArg1, BoundArg2, BoundArg3,
2246  BoundArg4, BoundArg5, FreeArgs...>{func, arg1, arg2, arg3, arg4, arg5};
2247 }
2248 
2249 /**
2250  * An alternative function to make Callback::CallbackArg objects,
2251  * which is for use where a target function either receives a class
2252  * type bound argument by value, or receives a bound argument by
2253  * reference to const in a case where the generated CallbackArg object
2254  * is to store a copy of that argument instead of just keeping a
2255  * reference.
2256  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2257  * is exhausted and the system throws in that case (this exception
2258  * will not be thrown if the library has been installed using the
2259  * \--with-glib-memory-slices-no-compat configuration option: instead
2260  * glib will terminate the program if it is unable to obtain memory
2261  * from the operating system). It will also throw if the copy or move
2262  * constructor of a bound argument throws.
2263  *
2264  * Since 2.0.0-rc3
2265  */
2266 template <class BoundArg1, class BoundArg2, class BoundArg3, class BoundArg4, class BoundArg5,
2267  class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class... FreeArgs>
2268 CallbackArg<FreeArgs...>* make_ref(void (*func)(BoundArg1, BoundArg2, BoundArg3,
2269  BoundArg4, BoundArg5, FreeArgs...),
2270  Arg1&& arg1,
2271  Arg2&& arg2,
2272  Arg3&& arg3,
2273  Arg4&& arg4,
2274  Arg5&& arg5) {
2275  return new Callback5_static<true, BoundArg1, BoundArg2, BoundArg3,
2276  BoundArg4, BoundArg5, FreeArgs...>{func,
2277  std::forward<Arg1>(arg1),
2278  std::forward<Arg2>(arg2),
2279  std::forward<Arg3>(arg3),
2280  std::forward<Arg4>(arg4),
2281  std::forward<Arg5>(arg5)};
2282 }
2283 
2284 /* for std::function objects */
2285 
2286 /**
2287  * A convenience function to make Callback::CallbackArg objects from
2288  * std::function objects.
2289  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2290  * is exhausted and the system throws in that case (this exception
2291  * will not be thrown if the library has been installed using the
2292  * \--with-glib-memory-slices-no-compat configuration option: instead
2293  * glib will terminate the program if it is unable to obtain memory
2294  * from the operating system). It will also throw if the copy
2295  * constructor of a bound argument throws and it is not a reference
2296  * argument.
2297  */
2298 template <class... FreeArgs>
2299 CallbackArg<FreeArgs...>* make(const std::function<void(FreeArgs...)>& f) {
2300  typedef std::function<void(FreeArgs...)> LType;
2301  return new Callback_lambda<LType, FreeArgs...>{f};
2302 }
2303 
2304 /**
2305  * A convenience function to make Callback::Callback objects from
2306  * std::function objects. Since this function takes no bound argument
2307  * (and bound arguments are bound into the std::function object), it
2308  * is a synonym for make() (the two are identical).
2309  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2310  * is exhausted and the system throws in that case (this exception
2311  * will not be thrown if the library has been installed using the
2312  * \--with-glib-memory-slices-no-compat configuration option: instead
2313  * glib will terminate the program if it is unable to obtain memory
2314  * from the operating system). It will also throw if the copy
2315  * constructor of a bound argument throws and it is not a reference
2316  * argument.
2317  */
2318 template <class... FreeArgs>
2319 CallbackArg<FreeArgs...>* make_ref(const std::function<void(FreeArgs...)>& f) {
2320  typedef std::function<void(FreeArgs...)> LType;
2321  return new Callback_lambda<LType, FreeArgs...>{f};
2322 }
2323 
2324 /**
2325  * A convenience function to make Callback::CallbackArg objects from
2326  * std::function objects.
2327  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2328  * is exhausted and the system throws in that case (this exception
2329  * will not be thrown if the library has been installed using the
2330  * \--with-glib-memory-slices-no-compat configuration option: instead
2331  * glib will terminate the program if it is unable to obtain memory
2332  * from the operating system). It will also throw if the copy
2333  * constructor of a bound argument throws and it is not a reference
2334  * argument.
2335  */
2336 template <class... FreeArgs>
2337 CallbackArg<FreeArgs...>* make(std::function<void(FreeArgs...)>&& f) {
2338  typedef std::function<void(FreeArgs...)> LType;
2339  return new Callback_lambda<LType, FreeArgs...>{std::move(f)};
2340 }
2341 
2342 /**
2343  * A convenience function to make Callback::Callback objects from
2344  * std::function objects. Since this function takes no bound argument
2345  * (and bound arguments are bound into the std::function object), it
2346  * is a synonym for make() (the two are identical).
2347  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2348  * is exhausted and the system throws in that case (this exception
2349  * will not be thrown if the library has been installed using the
2350  * \--with-glib-memory-slices-no-compat configuration option: instead
2351  * glib will terminate the program if it is unable to obtain memory
2352  * from the operating system). It will also throw if the copy or move
2353  * constructor of a bound argument throws and it is not a reference
2354  * argument.
2355  */
2356 template <class... FreeArgs>
2357 CallbackArg<FreeArgs...>* make_ref(std::function<void(FreeArgs...)>&& f) {
2358  typedef std::function<void(FreeArgs...)> LType;
2359  return new Callback_lambda<LType, FreeArgs...>{std::move(f)};
2360 }
2361 
2362 // This helper function to construct Callback_lambda objects could be
2363 // implemented as a further overload of Callback::make(). No best
2364 // match ambiguities would arise, because even when Callback::make()
2365 // is passed a function pointer without bound arguments, the overload
2366 // of Callback::make taking a function pointer (as opposed to a
2367 // generic callable object) would still comprise the best match.
2368 // However, to construct Callback_lambda objects, the unbound
2369 // arguments need to be specified by hand, which doesn't happen with
2370 // Callback::make() (it would only be necessary to specify an explicit
2371 // type where a mutable reference argument is to be bound to the
2372 // callback object). It seems to me to be less confusing to the user
2373 // therefore to have a separate Callback::lambda() helper function.
2374 // However, if you disagree please let me know.
2375 
2376 // template parameter packs do not need to be placed last in the case
2377 // of function templates, as type deduction is available for the last
2378 // parameter: there is in fact no function parameter pack in
2379 // Callback::lambda() (function parameter packs must come last).
2380 /**
2381  * A convenience function to make Callback::CallbackArg objects from
2382  * C++11 lambda expressions, or from any other arbitrary callable
2383  * object. The types of the unbound arguments (if any) must be
2384  * explicitly specified as template parameters, as they cannot be
2385  * deduced. From version 2.0.10, this function can be called for
2386  * lambda expressions which are declared mutable (in version 2.0.9,
2387  * this function could only be called for non-mutable lambda
2388  * expressions). From version 2.0.16, this function can be passed
2389  * callable objects which are lvalues as well as rvalues (prior to
2390  * version 2.0.16, it could only be passed callable objects which are
2391  * rvalues).
2392  * @exception std::bad_alloc It might throw std::bad_alloc if memory
2393  * is exhausted and the system throws in that case (this exception
2394  * will not be thrown if the library has been installed using the
2395  * \--with-glib-memory-slices-no-compat configuration option: instead
2396  * glib will terminate the program if it is unable to obtain memory
2397  * from the operating system). It will also throw if the copy or move
2398  * constructor of an object captured by the lambda expression throws.
2399  *
2400  * Since 2.0.9
2401  */
2402 template <class... FreeArgs, class Lambda>
2403 CallbackArg<FreeArgs...>* lambda(Lambda&& l) {
2404  typedef typename std::remove_const<typename std::remove_reference<Lambda>::type>::type LType;
2405  return new Callback_lambda<LType, FreeArgs...>{std::forward<Lambda>(l)};
2406 }
2407 
2408 #ifndef DOXYGEN_PARSING
2409 /*
2410  * DEPRECATED. These make_val() functions are retained for API
2411  * compatibility only, but should not be used in new code and are not
2412  * documented. Use make_ref() instead.
2413  */
2414 template <class T, class... FreeArgs>
2415 CallbackArg<FreeArgs...>* make_val(T& t,
2416  void (T::*func)(FreeArgs...)) {
2417  return new Callback0<T, FreeArgs...>{t, func};
2418 }
2419 template <class T, class BoundArg, class... FreeArgs>
2420 CallbackArg<FreeArgs...>* make_val(T& t,
2421  void (T::*func)(BoundArg, FreeArgs...),
2422  const BoundArg& arg) {
2423  return new Callback1<false, T, BoundArg, FreeArgs...>{t, func, arg};
2424 }
2425 template <class T, class BoundArg1, class BoundArg2, class... FreeArgs>
2426 CallbackArg<FreeArgs...>* make_val(T& t,
2427  void (T::*func)(BoundArg1, BoundArg2, FreeArgs...),
2428  const BoundArg1& arg1,
2429  const BoundArg2& arg2) {
2430  return new Callback2<false, T, BoundArg1, BoundArg2, FreeArgs...>{t, func, arg1, arg2};
2431 }
2432 template <class T, class BoundArg1, class BoundArg2, class BoundArg3, class... FreeArgs>
2433 CallbackArg<FreeArgs...>* make_val(T& t,
2434  void (T::*func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...),
2435  const BoundArg1& arg1,
2436  const BoundArg2& arg2,
2437  const BoundArg3& arg3) {
2438  return new Callback3<false, T, BoundArg1, BoundArg2, BoundArg3, FreeArgs...>{t, func, arg1, arg2, arg3};
2439 }
2440 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
2441  class BoundArg4, class... FreeArgs>
2442 CallbackArg<FreeArgs...>* make_val(T& t,
2443  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
2444  BoundArg4, FreeArgs...),
2445  const BoundArg1& arg1,
2446  const BoundArg2& arg2,
2447  const BoundArg3& arg3,
2448  const BoundArg4& arg4) {
2449  return new Callback4<false, T, BoundArg1, BoundArg2, BoundArg3,
2450  BoundArg4, FreeArgs...>{t, func, arg1, arg2, arg3, arg4};
2451 }
2452 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
2453  class BoundArg4, class BoundArg5, class... FreeArgs>
2454 CallbackArg<FreeArgs...>* make_val(T& t,
2455  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
2456  BoundArg4, BoundArg5, FreeArgs...),
2457  const BoundArg1& arg1,
2458  const BoundArg2& arg2,
2459  const BoundArg3& arg3,
2460  const BoundArg4& arg4,
2461  const BoundArg5& arg5) {
2462  return new Callback5<false, T, BoundArg1, BoundArg2, BoundArg3,
2463  BoundArg4, BoundArg5, FreeArgs...>{t, func, arg1, arg2, arg3, arg4, arg5};
2464 }
2465 template <class T, class... FreeArgs>
2466 CallbackArg<FreeArgs...>* make_val(const T& t,
2467  void (T::*func)(FreeArgs...) const) {
2468  return new Callback0_const<T, FreeArgs...>{t, func};
2469 }
2470 template <class T, class BoundArg, class... FreeArgs>
2471 CallbackArg<FreeArgs...>* make_val(const T& t,
2472  void (T::*func)(BoundArg, FreeArgs...) const,
2473  const BoundArg& arg) {
2474  return new Callback1_const<false, T, BoundArg, FreeArgs...>{t, func, arg};
2475 }
2476 template <class T, class BoundArg1, class BoundArg2, class... FreeArgs>
2477 CallbackArg<FreeArgs...>* make_val(const T& t,
2478  void (T::*func)(BoundArg1, BoundArg2, FreeArgs...) const,
2479  const BoundArg1& arg1,
2480  const BoundArg2& arg2) {
2481  return new Callback2_const<false, T, BoundArg1, BoundArg2, FreeArgs...>{t, func, arg1, arg2};
2482 }
2483 template <class T, class BoundArg1, class BoundArg2, class BoundArg3, class... FreeArgs>
2484 CallbackArg<FreeArgs...>* make_val(const T& t,
2485  void (T::*func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...) const,
2486  const BoundArg1& arg1,
2487  const BoundArg2& arg2,
2488  const BoundArg3& arg3) {
2489  return new Callback3_const<false, T, BoundArg1, BoundArg2, BoundArg3, FreeArgs...>{t, func, arg1, arg2, arg3};
2490 }
2491 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
2492  class BoundArg4, class... FreeArgs>
2493 CallbackArg<FreeArgs...>* make_val(const T& t,
2494  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
2495  BoundArg4, FreeArgs...) const,
2496  const BoundArg1& arg1,
2497  const BoundArg2& arg2,
2498  const BoundArg3& arg3,
2499  const BoundArg4& arg4) {
2500  return new Callback4_const<false, T, BoundArg1, BoundArg2, BoundArg3,
2501  BoundArg4, FreeArgs...>{t, func, arg1, arg2, arg3, arg4};
2502 }
2503 template <class T, class BoundArg1, class BoundArg2, class BoundArg3,
2504  class BoundArg4, class BoundArg5, class... FreeArgs>
2505 CallbackArg<FreeArgs...>* make_val(const T& t,
2506  void (T::*func)(BoundArg1, BoundArg2, BoundArg3,
2507  BoundArg4, BoundArg5, FreeArgs...) const,
2508  const BoundArg1& arg1,
2509  const BoundArg2& arg2,
2510  const BoundArg3& arg3,
2511  const BoundArg4& arg4,
2512  const BoundArg5& arg5) {
2513  return new Callback5_const<false, T, BoundArg1, BoundArg2, BoundArg3,
2514  BoundArg4, BoundArg5, FreeArgs...>{t, func, arg1, arg2, arg3, arg4, arg5};
2515 }
2516 template <class... FreeArgs>
2517 CallbackArg<FreeArgs...>* make_val(void (*func)(FreeArgs...)) {
2518  return new Callback0_static<FreeArgs...>{func};
2519 }
2520 template <class BoundArg, class... FreeArgs>
2521 CallbackArg<FreeArgs...>* make_val(void (*func)(BoundArg, FreeArgs...),
2522  const BoundArg& arg) {
2523  return new Callback1_static<false, BoundArg, FreeArgs...>{func, arg};
2524 }
2525 template <class BoundArg1, class BoundArg2, class... FreeArgs>
2526 CallbackArg<FreeArgs...>* make_val(void (*func)(BoundArg1, BoundArg2, FreeArgs...),
2527  const BoundArg1& arg1,
2528  const BoundArg2& arg2) {
2529  return new Callback2_static<false, BoundArg1, BoundArg2, FreeArgs...>{func, arg1, arg2};
2530 }
2531 template <class BoundArg1, class BoundArg2, class BoundArg3, class... FreeArgs>
2532 CallbackArg<FreeArgs...>* make_val(void (*func)(BoundArg1, BoundArg2, BoundArg3, FreeArgs...),
2533  const BoundArg1& arg1,
2534  const BoundArg2& arg2,
2535  const BoundArg3& arg3) {
2536  return new Callback3_static<false, BoundArg1, BoundArg2, BoundArg3, FreeArgs...>{func, arg1, arg2, arg3};
2537 }
2538 template <class BoundArg1, class BoundArg2, class BoundArg3,
2539  class BoundArg4, class... FreeArgs>
2540 CallbackArg<FreeArgs...>* make_val(void (*func)(BoundArg1, BoundArg2, BoundArg3,
2541  BoundArg4, FreeArgs...),
2542  const BoundArg1& arg1,
2543  const BoundArg2& arg2,
2544  const BoundArg3& arg3,
2545  const BoundArg4& arg4) {
2546  return new Callback4_static<false, BoundArg1, BoundArg2, BoundArg3,
2547  BoundArg4, FreeArgs...>{func, arg1, arg2, arg3, arg4};
2548 }
2549 template <class BoundArg1, class BoundArg2, class BoundArg3,
2550  class BoundArg4, class BoundArg5, class... FreeArgs>
2551 CallbackArg<FreeArgs...>* make_val(void (*func)(BoundArg1, BoundArg2, BoundArg3,
2552  BoundArg4, BoundArg5, FreeArgs...),
2553  const BoundArg1& arg1,
2554  const BoundArg2& arg2,
2555  const BoundArg3& arg3,
2556  const BoundArg4& arg4,
2557  const BoundArg5& arg5) {
2558  return new Callback5_static<false, BoundArg1, BoundArg2, BoundArg3,
2559  BoundArg4, BoundArg5, FreeArgs...>{func, arg1, arg2, arg3, arg4, arg5};
2560 }
2561 template <class... FreeArgs>
2562 CallbackArg<FreeArgs...>* make_val(const std::function<void(FreeArgs...)>& f) {
2563  typedef std::function<void(FreeArgs...)> LType;
2564  return new Callback_lambda<LType, FreeArgs...>{f};
2565 }
2566 template <class... FreeArgs>
2567 CallbackArg<FreeArgs...>* make_val(std::function<void(FreeArgs...)>&& f) {
2568  typedef std::function<void(FreeArgs...)> LType;
2569  return new Callback_lambda<LType, FreeArgs...>{std::move(f)};
2570 }
2571 #endif // DOXYGEN_PARSING
2572 
2573 } // namespace Callback
2574 
2575 class Releaser;
2576 
2577 namespace Callback {
2578 
2579 /**
2580  * Posts a callback for execution by a glib main loop. It is
2581  * thread-safe provided that (if glib < 2.32 is used) g_thread_init()
2582  * has been called. glib >= 2.32 does not require g_thread_init() to
2583  * be called. This function will not throw.
2584  * @param cb The callback object. Ownership is taken of this object,
2585  * and it will be deleted when it has been finished with.
2586  * @param priority The priority to be given to the callback in the
2587  * main loop. In ascending order of priorities, priorities are
2588  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
2589  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
2590  * G_PRIORITY_DEFAULT_IDLE. This determines the order in which the
2591  * callback will appear in the event list in the main loop, not the
2592  * priority which the OS will adopt
2593  * @param context The glib main loop context in which the callback is
2594  * to be executed (the default of NULL will cause the callback to be
2595  * executed in the main program loop, and this is usually what is
2596  * wanted).
2597  * @note Cancellation of the receiving thread is blocked when the
2598  * callback executes.
2599  */
2600 void post(const Callback* cb, gint priority = G_PRIORITY_DEFAULT_IDLE,
2601  GMainContext* context = 0);
2602 
2603 /**
2604  * Posts a callback for execution by a glib main loop. It is
2605  * thread-safe provided that (if glib < 2.32 is used) g_thread_init()
2606  * has been called. glib >= 2.32 does not require g_thread_init() to
2607  * be called. This function will not throw.
2608  * @param cb The callback object. Ownership is taken of this object,
2609  * and it will be deleted when it has been finished with.
2610  * @param r A Releaser object for automatic disconnection of the
2611  * callback before it executes in the main loop (mainly relevant if
2612  * the callback represents a non-static member function of an object
2613  * which may be destroyed before the callback executes).
2614  * @param priority The priority to be given to the callback in the
2615  * main loop. In ascending order of priorities, priorities are
2616  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
2617  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
2618  * G_PRIORITY_DEFAULT_IDLE. This determines the order in which the
2619  * callback will appear in the event list in the main loop, not the
2620  * priority which the OS will adopt.
2621  * @param context The glib main loop context in which the callback is
2622  * to be executed (the default of NULL will cause the callback to be
2623  * executed in the main program loop, and this is usually what is
2624  * wanted).
2625  * @exception std::bad_alloc This function might throw std::bad_alloc
2626  * if memory is exhausted and the system throws in that case. If it
2627  * does so, the Callback object will be disposed of.
2628  * @exception Cgu::Thread::MutexError This method might throw
2629  * Cgu:Thread::MutexError if initialisation of the mutex in a
2630  * SafeEmitterArg object constructed by this method fails. If it does
2631  * so, the Callback object will be disposed of. (It is often not
2632  * worth checking for this exception, as it means either memory is
2633  * exhausted or pthread has run out of other resources to create new
2634  * mutexes.)
2635  * @note 1. Cancellation of the receiving thread is blocked when the
2636  * callback executes.
2637  * @note 2. By virtue of the Releaser object, it is in theory possible
2638  * (if memory is exhausted and the system throws in that case) that an
2639  * internal SafeEmitterArg object will throw std::bad_alloc when
2640  * emitting/executing the callback in the glib main loop, with the
2641  * result that the relevant callback will not execute (instead the
2642  * exception will be consumed and a g_critical() warning will be
2643  * issued). This is rarely of any relevance because glib will abort
2644  * the program if it is itself unable to obtain memory from the
2645  * operating system. However, where it is relevant, design the
2646  * program so that it is not necessary to provide a releaser object.
2647  */
2648 void post(const Callback* cb, Releaser& r,
2649  gint priority = G_PRIORITY_DEFAULT_IDLE, GMainContext* context = 0);
2650 
2651 
2652 /**
2653  * Posts a callable object for execution by a glib main loop. It is
2654  * thread-safe provided that (if glib < 2.32 is used) g_thread_init()
2655  * has been called. glib >= 2.32 does not require g_thread_init() to
2656  * be called. This function will not throw unless the copy or move
2657  * constructor of the callable object throws.
2658  * @param func A callable object, such as formed by a lambda
2659  * expression or the result of std::bind. It must be fully bound
2660  * (that is, its must take no arguments when called).
2661  * @param priority The priority to be given to the callable object in
2662  * the main loop. In ascending order of priorities, priorities are
2663  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
2664  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
2665  * G_PRIORITY_DEFAULT_IDLE. This determines the order in which the
2666  * callback will appear in the event list in the main loop, not the
2667  * priority which the OS will adopt
2668  * @param context The glib main loop context in which the callable
2669  * object is to be executed (the default of NULL will cause the
2670  * callback to be executed in the main program loop, and this is
2671  * usually what is wanted).
2672  * @note Cancellation of the receiving thread is blocked when the
2673  * callback executes.
2674  *
2675  * Since 2.1.0
2676  */
2677 // we need to use enable_if so that where this function is passed a
2678 // pointer to non-const Callback, or some other convertible pointer,
2679 // this templated overload is dropped from the overload set, in order
2680 // to support the Callback pointer overloads of this function. This
2681 // overload calls into the version of this function taking a pointer
2682 // to const Callback in order to perform type erasure.
2683 template <class F,
2684  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<F>::type,
2685  const Callback*>::value>::type>
2686 void post(F&& func, gint priority = G_PRIORITY_DEFAULT_IDLE,
2687  GMainContext* context = 0) {
2688  post(lambda<>(std::forward<F>(func)), priority, context);
2689 }
2690 
2691 /**
2692  * Posts a callable object for execution by a glib main loop. It is
2693  * thread-safe provided that (if glib < 2.32 is used) g_thread_init()
2694  * has been called. glib >= 2.32 does not require g_thread_init() to
2695  * be called.
2696  * @param func A callable object, such as formed by a lambda
2697  * expression or the result of std::bind. It must be fully bound
2698  * (that is, its must take no arguments when called).
2699  * @param r A Releaser object for automatic disconnection of the
2700  * callback before it executes in the main loop (mainly relevant if
2701  * the callback represents or calls into a non-static member function
2702  * of an object which may be destroyed before the callback executes).
2703  * @param priority The priority to be given to the callback in the
2704  * main loop. In ascending order of priorities, priorities are
2705  * G_PRIORITY_LOW, G_PRIORITY_DEFAULT_IDLE, G_PRIORITY_HIGH_IDLE,
2706  * G_PRIORITY_DEFAULT and G_PRIORITY_HIGH. The default is
2707  * G_PRIORITY_DEFAULT_IDLE. This determines the order in which the
2708  * callback will appear in the event list in the main loop, not the
2709  * priority which the OS will adopt.
2710  * @param context The glib main loop context in which the callable
2711  * object is to be executed (the default of NULL will cause the
2712  * callback to be executed in the main program loop, and this is
2713  * usually what is wanted).
2714  * @exception std::bad_alloc This function might throw std::bad_alloc
2715  * if memory is exhausted and the system throws in that case.
2716  * @exception Cgu::Thread::MutexError This method might throw
2717  * Cgu:Thread::MutexError if initialisation of the mutex in a
2718  * SafeEmitterArg object constructed by this method fails. (It is
2719  * often not worth checking for this exception, as it means either
2720  * memory is exhausted or pthread has run out of other resources to
2721  * create new mutexes.)
2722  * @note 1. This function may also throw if the copy or move
2723  * constructor of the callable object throws.
2724  * @note 2. Cancellation of the receiving thread is blocked when the
2725  * callback executes.
2726  * @note 3. By virtue of the Releaser object, it is in theory possible
2727  * (if memory is exhausted and the system throws in that case) that an
2728  * internal SafeEmitterArg object will throw std::bad_alloc when
2729  * emitting/executing the callback in the glib main loop, with the
2730  * result that the relevant callback will not execute (instead the
2731  * exception will be consumed and a g_critical() warning will be
2732  * issued). This is rarely of any relevance because glib will abort
2733  * the program if it is itself unable to obtain memory from the
2734  * operating system. However, where it is relevant, design the
2735  * program so that it is not necessary to provide a releaser object.
2736  *
2737  * Since 2.1.0
2738  */
2739 // we need to use enable_if so that where this function is passed a
2740 // pointer to non-const Callback, or some other convertible pointer,
2741 // this templated overload is dropped from the overload set, in order
2742 // to support the Callback pointer overloads of this function. This
2743 // overload calls into the version of this function taking a pointer
2744 // to const Callback in order to perform type erasure.
2745 template <class F,
2746  class = typename std::enable_if<!std::is_convertible<typename std::remove_reference<F>::type,
2747  const Callback*>::value>::type>
2748 void post(F&& func, Releaser& r,
2749  gint priority = G_PRIORITY_DEFAULT_IDLE, GMainContext* context = 0) {
2750  post(lambda<>(std::forward<F>(func)), r, priority, context);
2751 }
2752 
2753 } // namespace Callback
2754 
2755 } // namespace Cgu
2756 
2757 #endif