]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - sigc++/functors/macros/ptr_fun.h.m4
Import libsigc++ 2.10.8 sources
[ext/sigc++-2.0.git] / sigc++ / functors / macros / ptr_fun.h.m4
1 dnl Copyright 2002, The libsigc++ Development Team 
2 dnl 
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. 
7 dnl 
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. 
12 dnl 
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 
16 dnl
17 divert(-1)
18
19 include(template.macros.m4)
20
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.
24  *
25  * The following template arguments are used:dnl
26 FOR(1,$1,[
27  * - @e T_arg%1 Argument type used in the definition of operator()().])
28  * - @e T_return The return type of operator()().
29  *
30  * @ingroup ptr_fun
31  */
32 template <LIST(LOOP(class T_arg%1, $1), class T_return)>
33 class pointer_functor$1 : public functor_base
34 {
35   typedef T_return (*function_type)(LOOP(T_arg%1, $1));
36 protected: 
37   function_type func_ptr_;
38 public:
39   typedef T_return result_type;
40
41   /// Constructs an invalid functor.
42   pointer_functor$1() {}
43
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()().
46    */
47   explicit pointer_functor$1(function_type _A_func): func_ptr_(_A_func) {}
48
49   /** Execute the wrapped function.dnl
50 FOR(1, $1,[
51    * @param _A_a%1 Argument to be passed on to the function.])
52    * @return The return value of the function invocation.
53    */
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)); }
56 };
57
58 ])
59
60 define([PTR_FUN],[dnl
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.
64  *
65  * @ingroup ptr_fun
66  */
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); }
71
72 ])
73
74 divert(0)
75 _FIREWALL([FUNCTORS_PTR_FUN])
76 #include <sigc++/type_traits.h>
77 #include <sigc++/functors/functor_trait.h>
78
79 namespace sigc {
80
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.
86  *
87  * @par Example:
88  * @code
89  * void foo(int) {}
90  * sigc::slot<void, int> sl = sigc::ptr_fun(&foo);
91  * @endcode
92  *
93  * Use ptr_fun#() if there is an ambiguity as to the number of arguments.
94  *
95  * @par Example:
96  * @code
97  * void foo(int) {}  // choose this one
98  * void foo(float) {}
99  * void foo(int, int) {}
100  * sigc::slot<void, long> sl = sigc::ptr_fun1<int>(&foo);
101  * @endcode
102  *
103  * ptr_fun() can also be used to convert a pointer to a static member
104  * function to a functor, like so:
105  *
106  * @par Example:
107  * @code
108  * struct foo
109  * {
110  *   static void bar(int) {}
111  * };
112  * sigc::slot<void, int> sl = sigc::ptr_fun(&foo::bar);
113  * @endcode
114  *
115  * @ingroup sigcfunctors
116  */
117
118 FOR(0,CALL_SIZE,[[POINTER_FUNCTOR(%1)]])dnl
119
120 // numbered ptr_fun
121 FOR(0,CALL_SIZE,[[PTR_FUN(%1)]])dnl
122
123 // unnumbered ptr_fun
124 FOR(0,CALL_SIZE,[[PTR_FUN(%1,1)]])dnl
125
126 } /* namespace sigc */