fix doc example typo
[boost.git] / boost / bind / mem_fn.hpp
blob956e7d88853c6c30ab700086a1df487dec42e8ee
1 #ifndef BOOST_BIND_MEM_FN_HPP_INCLUDED
2 #define BOOST_BIND_MEM_FN_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
11 // mem_fn.hpp - a generalization of std::mem_fun[_ref]
13 // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
14 // Copyright (c) 2001 David Abrahams
15 // Copyright (c) 2003-2005 Peter Dimov
17 // Distributed under the Boost Software License, Version 1.0. (See
18 // accompanying file LICENSE_1_0.txt or copy at
19 // http://www.boost.org/LICENSE_1_0.txt)
21 // See http://www.boost.org/libs/bind/mem_fn.html for documentation.
24 #include <boost/config.hpp>
25 #include <boost/get_pointer.hpp>
26 #include <boost/detail/workaround.hpp>
28 namespace boost
31 #if defined(BOOST_NO_VOID_RETURNS)
33 #define BOOST_MEM_FN_CLASS_F , class F
34 #define BOOST_MEM_FN_TYPEDEF(X)
36 namespace _mfi // mem_fun_impl
39 template<class V> struct mf
42 #define BOOST_MEM_FN_RETURN return
44 #define BOOST_MEM_FN_NAME(X) inner_##X
45 #define BOOST_MEM_FN_CC
47 #include <boost/bind/mem_fn_template.hpp>
49 #undef BOOST_MEM_FN_CC
50 #undef BOOST_MEM_FN_NAME
52 #ifdef BOOST_MEM_FN_ENABLE_CDECL
54 #define BOOST_MEM_FN_NAME(X) inner_##X##_cdecl
55 #define BOOST_MEM_FN_CC __cdecl
57 #include <boost/bind/mem_fn_template.hpp>
59 #undef BOOST_MEM_FN_CC
60 #undef BOOST_MEM_FN_NAME
62 #endif
64 #ifdef BOOST_MEM_FN_ENABLE_STDCALL
66 #define BOOST_MEM_FN_NAME(X) inner_##X##_stdcall
67 #define BOOST_MEM_FN_CC __stdcall
69 #include <boost/bind/mem_fn_template.hpp>
71 #undef BOOST_MEM_FN_CC
72 #undef BOOST_MEM_FN_NAME
74 #endif
76 #ifdef BOOST_MEM_FN_ENABLE_FASTCALL
78 #define BOOST_MEM_FN_NAME(X) inner_##X##_fastcall
79 #define BOOST_MEM_FN_CC __fastcall
81 #include <boost/bind/mem_fn_template.hpp>
83 #undef BOOST_MEM_FN_CC
84 #undef BOOST_MEM_FN_NAME
86 #endif
88 #undef BOOST_MEM_FN_RETURN
90 }; // struct mf<V>
92 template<> struct mf<void>
95 #define BOOST_MEM_FN_RETURN
97 #define BOOST_MEM_FN_NAME(X) inner_##X
98 #define BOOST_MEM_FN_CC
100 #include <boost/bind/mem_fn_template.hpp>
102 #undef BOOST_MEM_FN_CC
103 #undef BOOST_MEM_FN_NAME
105 #ifdef BOOST_MEM_FN_ENABLE_CDECL
107 #define BOOST_MEM_FN_NAME(X) inner_##X##_cdecl
108 #define BOOST_MEM_FN_CC __cdecl
110 #include <boost/bind/mem_fn_template.hpp>
112 #undef BOOST_MEM_FN_CC
113 #undef BOOST_MEM_FN_NAME
115 #endif
117 #ifdef BOOST_MEM_FN_ENABLE_STDCALL
119 #define BOOST_MEM_FN_NAME(X) inner_##X##_stdcall
120 #define BOOST_MEM_FN_CC __stdcall
122 #include <boost/bind/mem_fn_template.hpp>
124 #undef BOOST_MEM_FN_CC
125 #undef BOOST_MEM_FN_NAME
127 #endif
129 #ifdef BOOST_MEM_FN_ENABLE_FASTCALL
131 #define BOOST_MEM_FN_NAME(X) inner_##X##_fastcall
132 #define BOOST_MEM_FN_CC __fastcall
134 #include <boost/bind/mem_fn_template.hpp>
136 #undef BOOST_MEM_FN_CC
137 #undef BOOST_MEM_FN_NAME
139 #endif
141 #undef BOOST_MEM_FN_RETURN
143 }; // struct mf<void>
145 #undef BOOST_MEM_FN_CLASS_F
146 #undef BOOST_MEM_FN_TYPEDEF_F
148 #define BOOST_MEM_FN_NAME(X) X
149 #define BOOST_MEM_FN_NAME2(X) inner_##X
150 #define BOOST_MEM_FN_CC
152 #include <boost/bind/mem_fn_vw.hpp>
154 #undef BOOST_MEM_FN_NAME
155 #undef BOOST_MEM_FN_NAME2
156 #undef BOOST_MEM_FN_CC
158 #ifdef BOOST_MEM_FN_ENABLE_CDECL
160 #define BOOST_MEM_FN_NAME(X) X##_cdecl
161 #define BOOST_MEM_FN_NAME2(X) inner_##X##_cdecl
162 #define BOOST_MEM_FN_CC __cdecl
164 #include <boost/bind/mem_fn_vw.hpp>
166 #undef BOOST_MEM_FN_NAME
167 #undef BOOST_MEM_FN_NAME2
168 #undef BOOST_MEM_FN_CC
170 #endif
172 #ifdef BOOST_MEM_FN_ENABLE_STDCALL
174 #define BOOST_MEM_FN_NAME(X) X##_stdcall
175 #define BOOST_MEM_FN_NAME2(X) inner_##X##_stdcall
176 #define BOOST_MEM_FN_CC __stdcall
178 #include <boost/bind/mem_fn_vw.hpp>
180 #undef BOOST_MEM_FN_NAME
181 #undef BOOST_MEM_FN_NAME2
182 #undef BOOST_MEM_FN_CC
184 #endif
186 #ifdef BOOST_MEM_FN_ENABLE_FASTCALL
188 #define BOOST_MEM_FN_NAME(X) X##_fastcall
189 #define BOOST_MEM_FN_NAME2(X) inner_##X##_fastcall
190 #define BOOST_MEM_FN_CC __fastcall
192 #include <boost/bind/mem_fn_vw.hpp>
194 #undef BOOST_MEM_FN_NAME
195 #undef BOOST_MEM_FN_NAME2
196 #undef BOOST_MEM_FN_CC
198 #endif
200 } // namespace _mfi
202 #else // #ifdef BOOST_NO_VOID_RETURNS
204 #define BOOST_MEM_FN_CLASS_F
205 #define BOOST_MEM_FN_TYPEDEF(X) typedef X;
207 namespace _mfi
210 #define BOOST_MEM_FN_RETURN return
212 #define BOOST_MEM_FN_NAME(X) X
213 #define BOOST_MEM_FN_CC
215 #include <boost/bind/mem_fn_template.hpp>
217 #undef BOOST_MEM_FN_CC
218 #undef BOOST_MEM_FN_NAME
220 #ifdef BOOST_MEM_FN_ENABLE_CDECL
222 #define BOOST_MEM_FN_NAME(X) X##_cdecl
223 #define BOOST_MEM_FN_CC __cdecl
225 #include <boost/bind/mem_fn_template.hpp>
227 #undef BOOST_MEM_FN_CC
228 #undef BOOST_MEM_FN_NAME
230 #endif
232 #ifdef BOOST_MEM_FN_ENABLE_STDCALL
234 #define BOOST_MEM_FN_NAME(X) X##_stdcall
235 #define BOOST_MEM_FN_CC __stdcall
237 #include <boost/bind/mem_fn_template.hpp>
239 #undef BOOST_MEM_FN_CC
240 #undef BOOST_MEM_FN_NAME
242 #endif
244 #ifdef BOOST_MEM_FN_ENABLE_FASTCALL
246 #define BOOST_MEM_FN_NAME(X) X##_fastcall
247 #define BOOST_MEM_FN_CC __fastcall
249 #include <boost/bind/mem_fn_template.hpp>
251 #undef BOOST_MEM_FN_CC
252 #undef BOOST_MEM_FN_NAME
254 #endif
256 #undef BOOST_MEM_FN_RETURN
258 } // namespace _mfi
260 #undef BOOST_MEM_FN_CLASS_F
261 #undef BOOST_MEM_FN_TYPEDEF
263 #endif // #ifdef BOOST_NO_VOID_RETURNS
265 #define BOOST_MEM_FN_NAME(X) X
266 #define BOOST_MEM_FN_CC
268 #include <boost/bind/mem_fn_cc.hpp>
270 #undef BOOST_MEM_FN_NAME
271 #undef BOOST_MEM_FN_CC
273 #ifdef BOOST_MEM_FN_ENABLE_CDECL
275 #define BOOST_MEM_FN_NAME(X) X##_cdecl
276 #define BOOST_MEM_FN_CC __cdecl
278 #include <boost/bind/mem_fn_cc.hpp>
280 #undef BOOST_MEM_FN_NAME
281 #undef BOOST_MEM_FN_CC
283 #endif
285 #ifdef BOOST_MEM_FN_ENABLE_STDCALL
287 #define BOOST_MEM_FN_NAME(X) X##_stdcall
288 #define BOOST_MEM_FN_CC __stdcall
290 #include <boost/bind/mem_fn_cc.hpp>
292 #undef BOOST_MEM_FN_NAME
293 #undef BOOST_MEM_FN_CC
295 #endif
297 #ifdef BOOST_MEM_FN_ENABLE_FASTCALL
299 #define BOOST_MEM_FN_NAME(X) X##_fastcall
300 #define BOOST_MEM_FN_CC __fastcall
302 #include <boost/bind/mem_fn_cc.hpp>
304 #undef BOOST_MEM_FN_NAME
305 #undef BOOST_MEM_FN_CC
307 #endif
309 // data member support
311 namespace _mfi
314 template<class R, class T> class dm
316 public:
318 typedef R const & result_type;
319 typedef T const * argument_type;
321 private:
323 typedef R (T::*F);
324 F f_;
326 template<class U> R const & call(U & u, T const *) const
328 return (u.*f_);
331 template<class U> R const & call(U & u, void const *) const
333 return (get_pointer(u)->*f_);
336 public:
338 explicit dm(F f): f_(f) {}
340 R & operator()(T * p) const
342 return (p->*f_);
345 R const & operator()(T const * p) const
347 return (p->*f_);
350 template<class U> R const & operator()(U const & u) const
352 return call(u, &u);
355 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) && !BOOST_WORKAROUND(__MWERKS__, < 0x3200)
357 R & operator()(T & t) const
359 return (t.*f_);
362 R const & operator()(T const & t) const
364 return (t.*f_);
367 #endif
369 bool operator==(dm const & rhs) const
371 return f_ == rhs.f_;
374 bool operator!=(dm const & rhs) const
376 return f_ != rhs.f_;
380 } // namespace _mfi
382 template<class R, class T> _mfi::dm<R, T> mem_fn(R T::*f)
384 return _mfi::dm<R, T>(f);
387 } // namespace boost
389 #endif // #ifndef BOOST_BIND_MEM_FN_HPP_INCLUDED