key.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Copyright 2003-2019 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_KEY_HPP
  9. #define BOOST_MULTI_INDEX_KEY_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/multi_index/composite_key.hpp>
  15. #include <boost/multi_index/global_fun.hpp>
  16. #include <boost/multi_index/member.hpp>
  17. #include <boost/multi_index/mem_fun.hpp>
  18. #if __cplusplus>=201703L||\
  19. defined(BOOST_MSVC)&&defined(__cpp_nontype_template_parameter_auto)
  20. #define BOOST_MULTI_INDEX_KEY_SUPPORTED
  21. #include <boost/multi_index/detail/is_function.hpp>
  22. #include <boost/preprocessor/facilities/empty.hpp>
  23. #include <type_traits>
  24. namespace boost{
  25. namespace multi_index{
  26. /* C++17 terse key specification syntax */
  27. namespace detail{
  28. template<typename T,T,typename=void>
  29. struct typed_key_impl;
  30. template<typename Class,typename Type,Type Class::*PtrToMember>
  31. struct typed_key_impl<
  32. Type Class::*,PtrToMember,
  33. typename std::enable_if<!is_function<Type>::value>::type
  34. >
  35. {
  36. using value_type=Class;
  37. using type=member<Class,Type,PtrToMember>;
  38. };
  39. #define BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL(qualifier,extractor) \
  40. template< \
  41. typename Class,typename Type,Type (Class::*PtrToMemberFunction)()qualifier \
  42. > \
  43. struct typed_key_impl<Type (Class::*)()qualifier,PtrToMemberFunction> \
  44. { \
  45. using value_type=Class; \
  46. using type=extractor<Class,Type,PtrToMemberFunction>; \
  47. };
  48. BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL( ,mem_fun)
  49. BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL(const ,const_mem_fun)
  50. BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL(volatile ,volatile_mem_fun)
  51. BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL(const volatile ,cv_mem_fun)
  52. BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL(& ,ref_mem_fun)
  53. BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL(const& ,cref_mem_fun)
  54. BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL(volatile& ,vref_mem_fun)
  55. BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL(const volatile& ,cvref_mem_fun)
  56. #undef BOOST_MULTI_INDEX_KEY_TYPED_KEY_IMPL
  57. template<class Value,typename Type,Type (*PtrToFunction)(Value)>
  58. struct typed_key_impl<Type (*)(Value),PtrToFunction>
  59. {
  60. using value_type=Value;
  61. using type=global_fun<Value,Type,PtrToFunction>;
  62. };
  63. template<typename T>
  64. struct remove_noexcept{using type=T;};
  65. #define BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(qualifier) \
  66. template<typename R,typename C,typename... Args> \
  67. struct remove_noexcept<R(C::*)(Args...)qualifier noexcept> \
  68. {using type=R(C::*)(Args...)qualifier;}; \
  69. \
  70. template<typename R,typename C,typename... Args> \
  71. struct remove_noexcept<R(C::*)(Args...,...)qualifier noexcept> \
  72. {using type=R(C::*)(Args...,...)qualifier;};
  73. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(BOOST_PP_EMPTY())
  74. /* VS warns without dummy arg */
  75. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(const)
  76. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(volatile)
  77. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(const volatile)
  78. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(&)
  79. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(const&)
  80. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(volatile&)
  81. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(const volatile&)
  82. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(&&)
  83. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(const&&)
  84. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(volatile&&)
  85. BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT(const volatile&&)
  86. #undef BOOST_MULTI_INDEX_KEY_REMOVE_MEMFUN_NOEXCEPT
  87. template<typename R,typename... Args>
  88. struct remove_noexcept<R(*)(Args...)noexcept>{using type=R(*)(Args...);};
  89. template<typename R,typename... Args>
  90. struct remove_noexcept<R(*)(Args...,...)noexcept>
  91. {using type=R(*)(Args...,...);};
  92. template<typename T>
  93. using remove_noexcept_t=typename remove_noexcept<T>::type;
  94. template<auto... Keys>
  95. struct key_impl;
  96. template<auto Key>
  97. struct key_impl<Key>:typed_key_impl<remove_noexcept_t<decltype(Key)>,Key>{};
  98. template<typename... Ts>
  99. struct least_generic;
  100. template<typename T0,typename... Ts>
  101. struct least_generic<T0,Ts...>
  102. {
  103. using type=T0;
  104. };
  105. template<typename T0,typename T1,typename... Ts>
  106. struct least_generic<T0,T1,Ts...>
  107. {
  108. static_assert(
  109. std::is_convertible<const T0&,const T1&>::value||
  110. std::is_convertible<const T1&,const T0&>::value,
  111. "one type should be convertible to the other");
  112. using type=typename least_generic<
  113. typename std::conditional<
  114. std::is_convertible<const T0&,const T1&>::value,T0,T1
  115. >::type,
  116. Ts...
  117. >::type;
  118. };
  119. template<auto Key0,auto... Keys>
  120. struct key_impl<Key0,Keys...>
  121. {
  122. using value_type=typename least_generic<
  123. typename std::decay<typename key_impl<Key0>::value_type>::type,
  124. typename std::decay<typename key_impl<Keys>::value_type>::type...
  125. >::type;
  126. using type=composite_key<
  127. value_type,
  128. typename key_impl<Key0>::type,
  129. typename key_impl<Keys>::type...
  130. >;
  131. };
  132. template<typename=composite_key<void,void>>
  133. struct composite_key_size;
  134. template<typename... Args>
  135. struct composite_key_size<composite_key<Args...>>
  136. {
  137. static constexpr auto value=sizeof...(Args)-1;
  138. };
  139. template<auto... Keys>
  140. struct limited_size_key_impl
  141. {
  142. static_assert(
  143. sizeof...(Keys)<=composite_key_size<>::value,
  144. "specified number of keys must meet the limits of "
  145. "boost::multi_index::composite_key");
  146. using type=typename key_impl<Keys...>::type;
  147. };
  148. } /* namespace multi_index::detail */
  149. template<auto... Keys>
  150. using key=typename detail::limited_size_key_impl<Keys...>::type;
  151. } /* namespace multi_index */
  152. } /* namespace boost */
  153. #endif
  154. #endif