1 dnl Copyright 2002, The libsigc++ Development Team
3 dnl This library is free software; you can redistribute it and/or
4 dnl modify it under the terms of the GNU Lesser General Public
5 dnl License as published by the Free Software Foundation; either
6 dnl version 2.1 of the License, or (at your option) any later version.
8 dnl This library is distributed in the hope that it will be useful,
9 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
10 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 dnl Lesser General Public License for more details.
13 dnl You should have received a copy of the GNU Lesser General Public
14 dnl License along with this library; if not, write to the Free Software
15 dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 include(template.macros.m4)
21 define([POINTER_FUNCTOR],[dnl
22 /** pointer_functor$1 wraps existing non-member functions with $1 argument(s).
23 * Use the convenience function ptr_fun() to create an instance of pointer_functor$1.
25 * The following template arguments are used:dnl
27 * - @e T_arg%1 Argument type used in the definition of operator()().])
28 * - @e T_return The return type of operator()().
32 template <LIST(LOOP(class T_arg%1, $1), class T_return)>
33 class pointer_functor$1 : public functor_base
35 typedef T_return (*function_type)(LOOP(T_arg%1, $1));
37 function_type func_ptr_;
39 typedef T_return result_type;
41 /// Constructs an invalid functor.
42 pointer_functor$1() {}
44 /** Constructs a pointer_functor$1 object that wraps an existing function.
45 * @param _A_func Pointer to function that will be invoked from operator()().
47 explicit pointer_functor$1(function_type _A_func): func_ptr_(_A_func) {}
49 /** Execute the wrapped function.dnl
51 * @param _A_a%1 Argument to be passed on to the function.])
52 * @return The return value of the function invocation.
54 T_return operator()(LOOP(type_trait_take_t<T_arg%1> _A_a%1, $1)) const
55 { return func_ptr_(LOOP(_A_a%1, $1)); }
61 /** Creates a functor of type sigc::pointer_functor$1 which wraps an existing non-member function.
62 * @param _A_func Pointer to function that should be wrapped.
63 * @return Functor that executes @e _A_func on invokation.
67 template <LIST(LOOP(class T_arg%1, $1), class T_return)>
68 inline pointer_functor$1<LIST(LOOP(T_arg%1, $1), T_return)>
69 ptr_fun[]ifelse($2,, $1)(T_return (*_A_func)(LOOP(T_arg%1,$1)))
70 { return pointer_functor$1<LIST(LOOP(T_arg%1, $1), T_return)>(_A_func); }
75 _FIREWALL([FUNCTORS_PTR_FUN])
76 #include <sigc++/type_traits.h>
77 #include <sigc++/functors/functor_trait.h>
81 /** @defgroup ptr_fun ptr_fun()
82 * ptr_fun() is used to convert a pointer to a function to a functor.
83 * If the function pointer is to an overloaded type, you must specify
84 * the types using template arguments starting with the first argument.
85 * It is not necessary to supply the return type.
90 * sigc::slot<void, int> sl = sigc::ptr_fun(&foo);
93 * Use ptr_fun#() if there is an ambiguity as to the number of arguments.
97 * void foo(int) {} // choose this one
99 * void foo(int, int) {}
100 * sigc::slot<void, long> sl = sigc::ptr_fun1<int>(&foo);
103 * ptr_fun() can also be used to convert a pointer to a static member
104 * function to a functor, like so:
110 * static void bar(int) {}
112 * sigc::slot<void, int> sl = sigc::ptr_fun(&foo::bar);
115 * @ingroup sigcfunctors
118 FOR(0,CALL_SIZE,[[POINTER_FUNCTOR(%1)]])dnl
121 FOR(0,CALL_SIZE,[[PTR_FUN(%1)]])dnl
123 // unnumbered ptr_fun
124 FOR(0,CALL_SIZE,[[PTR_FUN(%1,1)]])dnl
126 } /* namespace sigc */