2a5a4e9236995186985fdb5b71a77c54560cc83a
[senf.git] / senf / Utils / membind.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief membind  public header */
25
26 /** \defgroup membind Bound Member Functions
27
28     The membind() family of function templates simplifies the creation of simple bound member
29     function pointers:
30
31     \code
32       struct Foo {
33           int test(int x);
34       };
35
36       Foo * foo = ...;
37       boost::function<int (int)> f = senf::membind(&Foo::test,foo);
38       int rv = f(1); // Calls foo->test(1)
39     \endcode
40
41     senf::membind() takes either a pointer or an object as second argument. When passing an object,
42     <em>that object will be copied into the bound member function returned.</em>
43
44     \idea Make the \a ob argument type an additional P template parameter (using call_traits for the
45     exact arg type? Probably we'll get deduction problems then) . The only operation this object
46     must support is ob->*fn. This would allow the use of smart pointers. We should keep the T &
47     version to still support ob.*fn use.
48  */
49
50 #ifndef HH_SENF_Utils_membind_
51 #define HH_SENF_Utils_membind_ 1
52
53 // Custom includes
54 #include <boost/bind.hpp>
55 #include <boost/function.hpp>
56 #include <senf/config.hh>
57
58 ///////////////////////////////hh.p////////////////////////////////////////
59
60 /** \brief Get function pointer
61
62     This macro will get a function pointer of a possibly overloaded function:
63
64     \code
65     void foo(int i);
66     int foo();
67
68     SENF_FNP(void, foo, (int i)) // Get the address of the first overload
69     \endcode
70
71     The macro arguments are the return type, function name and function arguments just as specified
72     in the declaration.
73
74     This macro only works for functions at namespace scope or for class static functions. For member
75     functions use \ref SENF_MEMFNP() or \ref SENF_MEMBINDFNP().
76
77     \hideinitializer
78     \ingroup membind
79  */
80 #define SENF_FNP(ret, fn, args) \
81     static_cast<ret (*) args>(& fn)
82
83 /** \brief Get function pointer
84
85     This macro will get a member function pointer of a possibly overloaded member function:
86
87     \code
88     struct Foo
89     {
90         void foo(int i);
91         int foo() const;
92     };
93
94     SENF_MEMFNP(int, Foo, foo, () const) // Get the address of the first overload
95     \endcode
96
97     The macro arguments are the return type, class name, function name and function arguments just
98     as specified in the declaration.
99
100     This macro only works for member functions. For namespace scope functions or class static
101     functions use SENF_FNP.
102
103     This macro returns a member function pointer. To automatically bind this pointer to \c this, use
104     \ref SENF_MEMBINDFNP() or use senf::membind() to bind to some other instance besides \c this.
105
106     \hideinitializer
107     \ingroup membind
108  */
109 #define SENF_MEMFNP(ret, cls, fn, args) \
110     static_cast<ret (cls::*) args>(& cls :: fn)
111
112 /** \brief Get function pointer
113
114     This macro will get a member function pointer of a possibly overloaded member function and bind
115     it to \c this returning a boost::function object resembling an ordinary non-member function (see
116     senf::membind()).
117
118     \code
119     struct Foo
120     {
121         void foo(int i);
122         int foo() const;
123
124         Foo()
125         {
126             // Get bound member function for second overload
127             SENF_MEMBINDFNP(int, Foo, foo, () const)
128         }
129
130     };
131     \endcode
132
133     The macro arguments are the return type, class name, function name and function arguments just
134     as specified in the declaration.
135
136     This macro only works for member functions. For namespace scope functions or class static
137     functions use SENF_FNP.
138
139     This macro returns a bound member function (a \c boost::function object instance). To get an
140     ordinary member function pointer use \ref SENF_MEMFNP(), for non-member function or class static
141     functions use \ref MEM_FNP().
142
143     \hideinitializer
144     \ingroup membind
145  */
146 #define SENF_MEMBINDFNP(ret, cls, fn, args) \
147     senf::membind(SENF_MEMFNP(ret, cls, fn, args), this)
148
149 namespace senf {
150
151 #define scOBTYPE *
152 #include <senf/Utils/impl/membind.hh>
153 #undef scOBTYPE
154
155 #define scOBTYPE &
156 #include <senf/Utils/impl/membind.hh>
157 #undef scOBTYPE
158
159 #ifdef DOXYGEN
160
161     /// \addtogroup membind
162     /// @{
163
164     /** \brief Build bound member function object
165
166         membind() supports up to 9 function parameters (represented as
167         \a Args here). The \a ob argument can be either a pointer or a
168         reference to \a T
169         \param[in] fn member function pointer
170         \param[in] ob object instance to bind this pointer to
171         \returns Boost.Function object representing a bound call of \a
172             fn on \a ob
173      */
174     template <typename R, typename T, typename Args>
175     boost::function<R (Args)> membind(R (T::* fn)( Args ), T * ob);
176
177     /// @}
178
179 #endif
180
181 }
182
183 ///////////////////////////////hh.e////////////////////////////////////////
184 //#include "membind.cci"
185 //#include "membind.ct"
186 //#include "membind.cti"
187 #endif
188
189 \f
190 // Local Variables:
191 // mode: c++
192 // fill-column: 100
193 // c-file-style: "senf"
194 // indent-tabs-mode: nil
195 // ispell-local-dictionary: "american"
196 // compile-command: "scons -u test"
197 // comment-column: 40
198 // End: