mem_fun.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright 2003-2008 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)&&(_MSC_VER>=1200)
  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 auto_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. template<
  95. class Class,typename Type,
  96. typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
  97. struct const_mem_fun_explicit
  98. {
  99. typedef typename remove_reference<Type>::type result_type;
  100. template<typename ChainedPtr>
  101. #if !defined(BOOST_NO_SFINAE)
  102. typename disable_if<
  103. is_convertible<const ChainedPtr&,const Class&>,Type>::type
  104. #else
  105. Type
  106. #endif
  107. operator()(const ChainedPtr& x)const
  108. {
  109. return operator()(*x);
  110. }
  111. Type operator()(const Class& x)const
  112. {
  113. return (x.*PtrToMemberFunction)();
  114. }
  115. Type operator()(const reference_wrapper<const Class>& x)const
  116. {
  117. return operator()(x.get());
  118. }
  119. Type operator()(const reference_wrapper<Class>& x)const
  120. {
  121. return operator()(x.get());
  122. }
  123. };
  124. template<
  125. class Class,typename Type,
  126. typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction>
  127. struct mem_fun_explicit
  128. {
  129. typedef typename remove_reference<Type>::type result_type;
  130. template<typename ChainedPtr>
  131. #if !defined(BOOST_NO_SFINAE)
  132. typename disable_if<
  133. is_convertible<ChainedPtr&,Class&>,Type>::type
  134. #else
  135. Type
  136. #endif
  137. operator()(const ChainedPtr& x)const
  138. {
  139. return operator()(*x);
  140. }
  141. Type operator()(Class& x)const
  142. {
  143. return (x.*PtrToMemberFunction)();
  144. }
  145. Type operator()(const reference_wrapper<Class>& x)const
  146. {
  147. return operator()(x.get());
  148. }
  149. };
  150. /* BOOST_MULTI_INDEX_CONST_MEM_FUN and BOOST_MULTI_INDEX_MEM_FUN resolve to
  151. * [const_]mem_fun_explicit for MSVC++ 6.0 and to [const_]mem_fun otherwise.
  152. */
  153. #if defined(BOOST_MSVC)&&(BOOST_MSVC<1300)
  154. #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
  155. ::boost::multi_index::const_mem_fun_explicit<\
  156. Class,Type,Type (Class::*)()const,&Class::MemberFunName >
  157. #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
  158. ::boost::multi_index::mem_fun_explicit<\
  159. Class,Type,Type (Class::*)(),&Class::MemberFunName >
  160. #else
  161. #define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
  162. ::boost::multi_index::const_mem_fun< Class,Type,&Class::MemberFunName >
  163. #define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
  164. ::boost::multi_index::mem_fun< Class,Type,&Class::MemberFunName >
  165. #endif
  166. } /* namespace multi_index */
  167. } /* namespace boost */
  168. #endif