mem_fn.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #ifndef BOOST_BIND_MEM_FN_HPP_INCLUDED
  2. #define BOOST_BIND_MEM_FN_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // mem_fn.hpp - a generalization of std::mem_fun[_ref]
  9. //
  10. // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
  11. // Copyright (c) 2001 David Abrahams
  12. // Copyright (c) 2003-2005 Peter Dimov
  13. //
  14. // Distributed under the Boost Software License, Version 1.0. (See
  15. // accompanying file LICENSE_1_0.txt or copy at
  16. // http://www.boost.org/LICENSE_1_0.txt)
  17. //
  18. // See http://www.boost.org/libs/bind/mem_fn.html for documentation.
  19. //
  20. #include <boost/config.hpp>
  21. #include <boost/get_pointer.hpp>
  22. #include <boost/detail/workaround.hpp>
  23. namespace boost
  24. {
  25. #if defined(BOOST_NO_VOID_RETURNS)
  26. #define BOOST_MEM_FN_CLASS_F , class F
  27. #define BOOST_MEM_FN_TYPEDEF(X)
  28. namespace _mfi // mem_fun_impl
  29. {
  30. template<class V> struct mf
  31. {
  32. #define BOOST_MEM_FN_RETURN return
  33. #define BOOST_MEM_FN_NAME(X) inner_##X
  34. #define BOOST_MEM_FN_CC
  35. #include <boost/bind/mem_fn_template.hpp>
  36. #undef BOOST_MEM_FN_CC
  37. #undef BOOST_MEM_FN_NAME
  38. #if defined(BOOST_MEM_FN_ENABLE_CDECL) && !defined(_M_X64)
  39. #define BOOST_MEM_FN_NAME(X) inner_##X##_cdecl
  40. #define BOOST_MEM_FN_CC __cdecl
  41. #include <boost/bind/mem_fn_template.hpp>
  42. #undef BOOST_MEM_FN_CC
  43. #undef BOOST_MEM_FN_NAME
  44. #endif
  45. #if defined(BOOST_MEM_FN_ENABLE_STDCALL) && !defined(_M_X64)
  46. #define BOOST_MEM_FN_NAME(X) inner_##X##_stdcall
  47. #define BOOST_MEM_FN_CC __stdcall
  48. #include <boost/bind/mem_fn_template.hpp>
  49. #undef BOOST_MEM_FN_CC
  50. #undef BOOST_MEM_FN_NAME
  51. #endif
  52. #if defined(BOOST_MEM_FN_ENABLE_FASTCALL) && !defined(_M_X64)
  53. #define BOOST_MEM_FN_NAME(X) inner_##X##_fastcall
  54. #define BOOST_MEM_FN_CC __fastcall
  55. #include <boost/bind/mem_fn_template.hpp>
  56. #undef BOOST_MEM_FN_CC
  57. #undef BOOST_MEM_FN_NAME
  58. #endif
  59. #undef BOOST_MEM_FN_RETURN
  60. }; // struct mf<V>
  61. template<> struct mf<void>
  62. {
  63. #define BOOST_MEM_FN_RETURN
  64. #define BOOST_MEM_FN_NAME(X) inner_##X
  65. #define BOOST_MEM_FN_CC
  66. #include <boost/bind/mem_fn_template.hpp>
  67. #undef BOOST_MEM_FN_CC
  68. #undef BOOST_MEM_FN_NAME
  69. #if defined(BOOST_MEM_FN_ENABLE_CDECL) && !defined(_M_X64)
  70. #define BOOST_MEM_FN_NAME(X) inner_##X##_cdecl
  71. #define BOOST_MEM_FN_CC __cdecl
  72. #include <boost/bind/mem_fn_template.hpp>
  73. #undef BOOST_MEM_FN_CC
  74. #undef BOOST_MEM_FN_NAME
  75. #endif
  76. #ifdef BOOST_MEM_FN_ENABLE_STDCALL
  77. #define BOOST_MEM_FN_NAME(X) inner_##X##_stdcall
  78. #define BOOST_MEM_FN_CC __stdcall
  79. #include <boost/bind/mem_fn_template.hpp>
  80. #undef BOOST_MEM_FN_CC
  81. #undef BOOST_MEM_FN_NAME
  82. #endif
  83. #ifdef BOOST_MEM_FN_ENABLE_FASTCALL
  84. #define BOOST_MEM_FN_NAME(X) inner_##X##_fastcall
  85. #define BOOST_MEM_FN_CC __fastcall
  86. #include <boost/bind/mem_fn_template.hpp>
  87. #undef BOOST_MEM_FN_CC
  88. #undef BOOST_MEM_FN_NAME
  89. #endif
  90. #undef BOOST_MEM_FN_RETURN
  91. }; // struct mf<void>
  92. #undef BOOST_MEM_FN_CLASS_F
  93. #undef BOOST_MEM_FN_TYPEDEF_F
  94. #define BOOST_MEM_FN_NAME(X) X
  95. #define BOOST_MEM_FN_NAME2(X) inner_##X
  96. #define BOOST_MEM_FN_CC
  97. #include <boost/bind/mem_fn_vw.hpp>
  98. #undef BOOST_MEM_FN_NAME
  99. #undef BOOST_MEM_FN_NAME2
  100. #undef BOOST_MEM_FN_CC
  101. #if defined(BOOST_MEM_FN_ENABLE_CDECL) && !defined(_M_X64)
  102. #define BOOST_MEM_FN_NAME(X) X##_cdecl
  103. #define BOOST_MEM_FN_NAME2(X) inner_##X##_cdecl
  104. #define BOOST_MEM_FN_CC __cdecl
  105. #include <boost/bind/mem_fn_vw.hpp>
  106. #undef BOOST_MEM_FN_NAME
  107. #undef BOOST_MEM_FN_NAME2
  108. #undef BOOST_MEM_FN_CC
  109. #endif
  110. #ifdef BOOST_MEM_FN_ENABLE_STDCALL
  111. #define BOOST_MEM_FN_NAME(X) X##_stdcall
  112. #define BOOST_MEM_FN_NAME2(X) inner_##X##_stdcall
  113. #define BOOST_MEM_FN_CC __stdcall
  114. #include <boost/bind/mem_fn_vw.hpp>
  115. #undef BOOST_MEM_FN_NAME
  116. #undef BOOST_MEM_FN_NAME2
  117. #undef BOOST_MEM_FN_CC
  118. #endif
  119. #ifdef BOOST_MEM_FN_ENABLE_FASTCALL
  120. #define BOOST_MEM_FN_NAME(X) X##_fastcall
  121. #define BOOST_MEM_FN_NAME2(X) inner_##X##_fastcall
  122. #define BOOST_MEM_FN_CC __fastcall
  123. #include <boost/bind/mem_fn_vw.hpp>
  124. #undef BOOST_MEM_FN_NAME
  125. #undef BOOST_MEM_FN_NAME2
  126. #undef BOOST_MEM_FN_CC
  127. #endif
  128. } // namespace _mfi
  129. #else // #ifdef BOOST_NO_VOID_RETURNS
  130. #define BOOST_MEM_FN_CLASS_F
  131. #define BOOST_MEM_FN_TYPEDEF(X) typedef X;
  132. namespace _mfi
  133. {
  134. #define BOOST_MEM_FN_RETURN return
  135. #define BOOST_MEM_FN_NAME(X) X
  136. #define BOOST_MEM_FN_CC
  137. #include <boost/bind/mem_fn_template.hpp>
  138. #undef BOOST_MEM_FN_CC
  139. #undef BOOST_MEM_FN_NAME
  140. #if defined(BOOST_MEM_FN_ENABLE_CDECL) && !defined(_M_X64)
  141. #define BOOST_MEM_FN_NAME(X) X##_cdecl
  142. #define BOOST_MEM_FN_CC __cdecl
  143. #include <boost/bind/mem_fn_template.hpp>
  144. #undef BOOST_MEM_FN_CC
  145. #undef BOOST_MEM_FN_NAME
  146. #endif
  147. #if defined(BOOST_MEM_FN_ENABLE_STDCALL) && !defined(_M_X64)
  148. #define BOOST_MEM_FN_NAME(X) X##_stdcall
  149. #define BOOST_MEM_FN_CC __stdcall
  150. #include <boost/bind/mem_fn_template.hpp>
  151. #undef BOOST_MEM_FN_CC
  152. #undef BOOST_MEM_FN_NAME
  153. #endif
  154. #if defined(BOOST_MEM_FN_ENABLE_FASTCALL) && !defined(_M_X64)
  155. #define BOOST_MEM_FN_NAME(X) X##_fastcall
  156. #define BOOST_MEM_FN_CC __fastcall
  157. #include <boost/bind/mem_fn_template.hpp>
  158. #undef BOOST_MEM_FN_CC
  159. #undef BOOST_MEM_FN_NAME
  160. #endif
  161. #undef BOOST_MEM_FN_RETURN
  162. } // namespace _mfi
  163. #undef BOOST_MEM_FN_CLASS_F
  164. #undef BOOST_MEM_FN_TYPEDEF
  165. #endif // #ifdef BOOST_NO_VOID_RETURNS
  166. #define BOOST_MEM_FN_NAME(X) X
  167. #define BOOST_MEM_FN_CC
  168. #define BOOST_MEM_FN_NOEXCEPT
  169. #include <boost/bind/mem_fn_cc.hpp>
  170. #if defined( __cpp_noexcept_function_type ) || defined( _NOEXCEPT_TYPES_SUPPORTED )
  171. # undef BOOST_MEM_FN_NOEXCEPT
  172. # define BOOST_MEM_FN_NOEXCEPT noexcept
  173. # include <boost/bind/mem_fn_cc.hpp>
  174. #endif
  175. #undef BOOST_MEM_FN_NAME
  176. #undef BOOST_MEM_FN_CC
  177. #undef BOOST_MEM_FN_NOEXCEPT
  178. #if defined(BOOST_MEM_FN_ENABLE_CDECL) && !defined(_M_X64)
  179. #define BOOST_MEM_FN_NAME(X) X##_cdecl
  180. #define BOOST_MEM_FN_CC __cdecl
  181. #define BOOST_MEM_FN_NOEXCEPT
  182. #include <boost/bind/mem_fn_cc.hpp>
  183. #undef BOOST_MEM_FN_NAME
  184. #undef BOOST_MEM_FN_CC
  185. #undef BOOST_MEM_FN_NOEXCEPT
  186. #endif
  187. #if defined(BOOST_MEM_FN_ENABLE_STDCALL) && !defined(_M_X64)
  188. #define BOOST_MEM_FN_NAME(X) X##_stdcall
  189. #define BOOST_MEM_FN_CC __stdcall
  190. #define BOOST_MEM_FN_NOEXCEPT
  191. #include <boost/bind/mem_fn_cc.hpp>
  192. #undef BOOST_MEM_FN_NAME
  193. #undef BOOST_MEM_FN_CC
  194. #undef BOOST_MEM_FN_NOEXCEPT
  195. #endif
  196. #if defined(BOOST_MEM_FN_ENABLE_FASTCALL) && !defined(_M_X64)
  197. #define BOOST_MEM_FN_NAME(X) X##_fastcall
  198. #define BOOST_MEM_FN_CC __fastcall
  199. #define BOOST_MEM_FN_NOEXCEPT
  200. #include <boost/bind/mem_fn_cc.hpp>
  201. #undef BOOST_MEM_FN_NAME
  202. #undef BOOST_MEM_FN_CC
  203. #undef BOOST_MEM_FN_NOEXCEPT
  204. #endif
  205. // data member support
  206. namespace _mfi
  207. {
  208. template<class R, class T> class dm
  209. {
  210. public:
  211. typedef R const & result_type;
  212. typedef T const * argument_type;
  213. private:
  214. typedef R (T::*F);
  215. F f_;
  216. template<class U> R const & call(U & u, T const *) const
  217. {
  218. return (u.*f_);
  219. }
  220. template<class U> R const & call(U & u, void const *) const
  221. {
  222. return (get_pointer(u)->*f_);
  223. }
  224. public:
  225. explicit dm(F f): f_(f) {}
  226. R & operator()(T * p) const
  227. {
  228. return (p->*f_);
  229. }
  230. R const & operator()(T const * p) const
  231. {
  232. return (p->*f_);
  233. }
  234. template<class U> R const & operator()(U const & u) const
  235. {
  236. return call(u, &u);
  237. }
  238. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) && !BOOST_WORKAROUND(__MWERKS__, < 0x3200)
  239. R & operator()(T & t) const
  240. {
  241. return (t.*f_);
  242. }
  243. R const & operator()(T const & t) const
  244. {
  245. return (t.*f_);
  246. }
  247. #endif
  248. bool operator==(dm const & rhs) const
  249. {
  250. return f_ == rhs.f_;
  251. }
  252. bool operator!=(dm const & rhs) const
  253. {
  254. return f_ != rhs.f_;
  255. }
  256. };
  257. } // namespace _mfi
  258. template<class R, class T> _mfi::dm<R, T> mem_fn(R T::*f)
  259. {
  260. return _mfi::dm<R, T>(f);
  261. }
  262. } // namespace boost
  263. #endif // #ifndef BOOST_BIND_MEM_FN_HPP_INCLUDED