mem_fun.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Copyright 2003-2015 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/mpl/if.hpp>
  15. #include <boost/type_traits/remove_reference.hpp>
  16. #include <boost/utility/enable_if.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. * const_mem_fun does the same for const member functions.
  26. * Additionally, mem_fun and const_mem_fun are overloaded to support
  27. * referece_wrappers of T and "chained pointers" to T's. By chained pointer
  28. * to T we mean a type P such that, given a p of Type P
  29. * *...n...*x is convertible to T&, for some n>=1.
  30. * Examples of chained pointers are raw and smart pointers, iterators and
  31. * arbitrary combinations of these (vg. T** or unique_ptr<T*>.)
  32. */
  33. template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()const>
  34. struct const_mem_fun
  35. {
  36. typedef typename remove_reference<Type>::type result_type;
  37. template<typename ChainedPtr>
  38. #if !defined(BOOST_NO_SFINAE)
  39. typename disable_if<
  40. is_convertible<const ChainedPtr&,const Class&>,Type>::type
  41. #else
  42. Type
  43. #endif
  44. operator()(const ChainedPtr& x)const
  45. {
  46. return operator()(*x);
  47. }
  48. Type operator()(const Class& x)const
  49. {
  50. return (x.*PtrToMemberFunction)();
  51. }
  52. Type operator()(const reference_wrapper<const Class>& x)const
  53. {
  54. return operator()(x.get());
  55. }
  56. Type operator()(const reference_wrapper<Class>& x)const
  57. {
  58. return operator()(x.get());
  59. }
  60. };
  61. template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()>
  62. struct mem_fun
  63. {
  64. typedef typename remove_reference<Type>::type result_type;
  65. template<typename ChainedPtr>
  66. #if !defined(BOOST_NO_SFINAE)
  67. typename disable_if<
  68. is_convertible<ChainedPtr&,Class&>,Type>::type
  69. #else
  70. Type
  71. #endif
  72. operator()(const ChainedPtr& x)const
  73. {
  74. return operator()(*x);
  75. }
  76. Type operator()(Class& x)const
  77. {
  78. return (x.*PtrToMemberFunction)();
  79. }
  80. Type operator()(const reference_wrapper<Class>& x)const
  81. {
  82. return operator()(x.get());
  83. }
  84. };
  85. /* MSVC++ 6.0 has problems with const member functions as non-type template
  86. * parameters, somehow it takes them as non-const. const_mem_fun_explicit
  87. * workarounds this deficiency by accepting an extra type parameter that
  88. * specifies the signature of the member function. The workaround was found at:
  89. * Daniel, C.:"Re: weird typedef problem in VC",
  90. * news:microsoft.public.vc.language, 21st nov 2002,
  91. * http://groups.google.com/groups?
  92. * hl=en&lr=&ie=UTF-8&selm=ukwvg3O0BHA.1512%40tkmsftngp05
  93. *
  94. * MSVC++ 6.0 support has been dropped and [const_]mem_fun_explicit is
  95. * deprecated.
  96. */
  97. template<
  98. class Class,typename Type,
  99. typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
  100. struct const_mem_fun_explicit
  101. {
  102. typedef typename remove_reference<Type>::type result_type;
  103. template<typename ChainedPtr>
  104. #if !defined(BOOST_NO_SFINAE)
  105. typename disable_if<
  106. is_convertible<const ChainedPtr&,const Class&>,Type>::type
  107. #else
  108. Type
  109. #endif
  110. operator()(const ChainedPtr& x)const
  111. {
  112. return operator()(*x);
  113. }
  114. Type operator()(const Class& x)const
  115. {
  116. return (x.*PtrToMemberFunction)();
  117. }
  118. Type operator()(const reference_wrapper<const Class>& x)const
  119. {
  120. return operator()(x.get());
  121. }
  122. Type operator()(const reference_wrapper<Class>& x)const
  123. {
  124. return operator()(x.get());
  125. }
  126. };
  127. template<
  128. class Class,typename Type,
  129. typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
  130. struct mem_fun_explicit
  131. {
  132. typedef typename remove_reference<Type>::type result_type;
  133. template<typename ChainedPtr>
  134. #if !defined(BOOST_NO_SFINAE)
  135. typename disable_if<
  136. is_convertible<ChainedPtr&,Class&>,Type>::type
  137. #else
  138. Type
  139. #endif
  140. operator()(const ChainedPtr& x)const
  141. {
  142. return operator()(*x);
  143. }
  144. Type operator()(Class& x)const
  145. {
  146. return (x.*PtrToMemberFunction)();
  147. }
  148. Type operator()(const reference_wrapper<Class>& x)const
  149. {
  150. return operator()(x.get());
  151. }
  152. };
  153. /* BOOST_MULTI_INDEX_CONST_MEM_FUN and BOOST_MULTI_INDEX_MEM_FUN used to
  154. * resolve to [const_]mem_fun_explicit for MSVC++ 6.0 and to
  155. * [const_]mem_fun otherwise. Support for this compiler having been dropped,
  156. * they are now just wrappers over [const_]mem_fun kept for backwards-
  157. * compatibility reasons.
  158. */
  159. #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
  160. ::boost::multi_index::const_mem_fun< Class,Type,&Class::MemberFunName >
  161. #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
  162. ::boost::multi_index::mem_fun< Class,Type,&Class::MemberFunName >
  163. } /* namespace multi_index */
  164. } /* namespace boost */
  165. #endif