mem_fun.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Copyright 2003-2022 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_MEM_FUN_HPP
  9. #define BOOST_MULTI_INDEX_MEM_FUN_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/core/enable_if.hpp>
  15. #include <boost/mpl/if.hpp>
  16. #include <boost/type_traits/remove_reference.hpp>
  17. #if !defined(BOOST_NO_SFINAE)
  18. #include <boost/type_traits/is_convertible.hpp>
  19. #endif
  20. namespace boost{
  21. template<class T> class reference_wrapper; /* fwd decl. */
  22. namespace multi_index{
  23. /* mem_fun implements a read-only key extractor based on a given non-const
  24. * member function of a class.
  25. * Also, the following variations are provided:
  26. * const_mem_fun: const member functions
  27. * volatile_mem_fun: volatile member functions
  28. * cv_mem_fun: const volatile member functions
  29. * ref_mem_fun: ref-qualifed member functions (C++11)
  30. * cref_mem_fun: const ref-qualifed member functions (C++11)
  31. * vref_mem_fun: volatile ref-qualifed member functions (C++11)
  32. * cvref_mem_fun: const volatile ref-qualifed member functions (C++11)
  33. *
  34. * All of these classes are overloaded to support boost::referece_wrappers
  35. * of T and "chained pointers" to T's. By chained pointer to T we mean a type
  36. * P such that, given a p of Type P
  37. * *...n...*x is convertible to T&, for some n>=1.
  38. * Examples of chained pointers are raw and smart pointers, iterators and
  39. * arbitrary combinations of these (vg. T** or unique_ptr<T*>.)
  40. */
  41. namespace detail{
  42. template<
  43. class Class,typename Type,
  44. typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction
  45. >
  46. struct const_mem_fun_impl
  47. {
  48. typedef typename remove_reference<Type>::type result_type;
  49. template<typename ChainedPtr>
  50. #if !defined(BOOST_NO_SFINAE)
  51. typename disable_if<
  52. is_convertible<const ChainedPtr&,const Class&>,Type>::type
  53. #else
  54. Type
  55. #endif
  56. operator()(const ChainedPtr& x)const
  57. {
  58. return operator()(*x);
  59. }
  60. Type operator()(const Class& x)const
  61. {
  62. return (x.*PtrToMemberFunction)();
  63. }
  64. Type operator()(const reference_wrapper<const Class>& x)const
  65. {
  66. return operator()(x.get());
  67. }
  68. Type operator()(const reference_wrapper<Class>& x)const
  69. {
  70. return operator()(x.get());
  71. }
  72. };
  73. template<
  74. class Class,typename Type,
  75. typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction
  76. >
  77. struct mem_fun_impl
  78. {
  79. typedef typename remove_reference<Type>::type result_type;
  80. template<typename ChainedPtr>
  81. #if !defined(BOOST_NO_SFINAE)
  82. typename disable_if<
  83. is_convertible<ChainedPtr&,Class&>,Type>::type
  84. #else
  85. Type
  86. #endif
  87. operator()(const ChainedPtr& x)const
  88. {
  89. return operator()(*x);
  90. }
  91. Type operator()(Class& x)const
  92. {
  93. return (x.*PtrToMemberFunction)();
  94. }
  95. Type operator()(const reference_wrapper<Class>& x)const
  96. {
  97. return operator()(x.get());
  98. }
  99. };
  100. } /* namespace multi_index::detail */
  101. template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()const>
  102. struct const_mem_fun:detail::const_mem_fun_impl<
  103. Class,Type,Type (Class::*)()const,PtrToMemberFunction
  104. >{};
  105. template<
  106. class Class,typename Type,
  107. Type (Class::*PtrToMemberFunction)()const volatile
  108. >
  109. struct cv_mem_fun:detail::const_mem_fun_impl<
  110. Class,Type,Type (Class::*)()const volatile,PtrToMemberFunction
  111. >{};
  112. template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()>
  113. struct mem_fun:
  114. detail::mem_fun_impl<Class,Type,Type (Class::*)(),PtrToMemberFunction>{};
  115. template<
  116. class Class,typename Type,Type (Class::*PtrToMemberFunction)()volatile
  117. >
  118. struct volatile_mem_fun:detail::mem_fun_impl<
  119. Class,Type,Type (Class::*)()volatile,PtrToMemberFunction
  120. >{};
  121. #if !defined(BOOST_NO_CXX11_REF_QUALIFIERS)
  122. template<
  123. class Class,typename Type,Type (Class::*PtrToMemberFunction)()const&
  124. >
  125. struct cref_mem_fun:detail::const_mem_fun_impl<
  126. Class,Type,Type (Class::*)()const&,PtrToMemberFunction
  127. >{};
  128. template<
  129. class Class,typename Type,
  130. Type (Class::*PtrToMemberFunction)()const volatile&
  131. >
  132. struct cvref_mem_fun:detail::const_mem_fun_impl<
  133. Class,Type,Type (Class::*)()const volatile&,PtrToMemberFunction
  134. >{};
  135. template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()&>
  136. struct ref_mem_fun:
  137. detail::mem_fun_impl<Class,Type,Type (Class::*)()&,PtrToMemberFunction>{};
  138. template<
  139. class Class,typename Type,Type (Class::*PtrToMemberFunction)()volatile&
  140. >
  141. struct vref_mem_fun:detail::mem_fun_impl<
  142. Class,Type,Type (Class::*)()volatile&,PtrToMemberFunction
  143. >{};
  144. #endif
  145. /* MSVC++ 6.0 has problems with const member functions as non-type template
  146. * parameters, somehow it takes them as non-const. const_mem_fun_explicit
  147. * workarounds this deficiency by accepting an extra type parameter that
  148. * specifies the signature of the member function. The workaround was found at:
  149. * Daniel, C.:"Re: weird typedef problem in VC",
  150. * news:microsoft.public.vc.language, 21st nov 2002,
  151. * http://groups.google.com/groups?
  152. * hl=en&lr=&ie=UTF-8&selm=ukwvg3O0BHA.1512%40tkmsftngp05
  153. *
  154. * MSVC++ 6.0 support has been dropped and [const_]mem_fun_explicit is
  155. * deprecated.
  156. */
  157. template<
  158. class Class,typename Type,
  159. typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction
  160. >
  161. struct const_mem_fun_explicit
  162. {
  163. typedef typename remove_reference<Type>::type result_type;
  164. template<typename ChainedPtr>
  165. #if !defined(BOOST_NO_SFINAE)
  166. typename disable_if<
  167. is_convertible<const ChainedPtr&,const Class&>,Type>::type
  168. #else
  169. Type
  170. #endif
  171. operator()(const ChainedPtr& x)const
  172. {
  173. return operator()(*x);
  174. }
  175. Type operator()(const Class& x)const
  176. {
  177. return (x.*PtrToMemberFunction)();
  178. }
  179. Type operator()(const reference_wrapper<const Class>& x)const
  180. {
  181. return operator()(x.get());
  182. }
  183. Type operator()(const reference_wrapper<Class>& x)const
  184. {
  185. return operator()(x.get());
  186. }
  187. };
  188. template<
  189. class Class,typename Type,
  190. typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction
  191. >
  192. struct mem_fun_explicit
  193. {
  194. typedef typename remove_reference<Type>::type result_type;
  195. template<typename ChainedPtr>
  196. #if !defined(BOOST_NO_SFINAE)
  197. typename disable_if<
  198. is_convertible<ChainedPtr&,Class&>,Type>::type
  199. #else
  200. Type
  201. #endif
  202. operator()(const ChainedPtr& x)const
  203. {
  204. return operator()(*x);
  205. }
  206. Type operator()(Class& x)const
  207. {
  208. return (x.*PtrToMemberFunction)();
  209. }
  210. Type operator()(const reference_wrapper<Class>& x)const
  211. {
  212. return operator()(x.get());
  213. }
  214. };
  215. /* BOOST_MULTI_INDEX_CONST_MEM_FUN and BOOST_MULTI_INDEX_MEM_FUN used to
  216. * resolve to [const_]mem_fun_explicit for MSVC++ 6.0 and to
  217. * [const_]mem_fun otherwise. Support for this compiler having been dropped,
  218. * they are now just wrappers over [const_]mem_fun kept for backwards-
  219. * compatibility reasons.
  220. */
  221. #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
  222. ::boost::multi_index::const_mem_fun< Class,Type,&Class::MemberFunName >
  223. #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
  224. ::boost::multi_index::mem_fun< Class,Type,&Class::MemberFunName >
  225. } /* namespace multi_index */
  226. } /* namespace boost */
  227. #endif