key.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright 2003-2018 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 <type_traits>
  22. namespace boost{
  23. namespace multi_index{
  24. /* C++17 terse key specification syntax */
  25. namespace detail{
  26. template<typename T,T,typename=void>
  27. struct typed_key_impl;
  28. template<typename Class,typename Type,Type Class::*PtrToMember>
  29. struct typed_key_impl<
  30. Type Class::*,PtrToMember,
  31. typename std::enable_if<!std::is_function<Type>::value>::type
  32. >
  33. {
  34. using value_type=Class;
  35. using type=member<Class,Type,PtrToMember>;
  36. };
  37. template<
  38. typename Class,typename Type,Type (Class::*PtrToMemberFunction)()const
  39. >
  40. struct typed_key_impl<Type (Class::*)()const,PtrToMemberFunction>
  41. {
  42. using value_type=Class;
  43. using type=const_mem_fun<Class,Type,PtrToMemberFunction>;
  44. };
  45. template<typename Class,typename Type,Type (Class::*PtrToMemberFunction)()>
  46. struct typed_key_impl<Type (Class::*)(),PtrToMemberFunction>
  47. {
  48. using value_type=Class;
  49. using type=mem_fun<Class,Type,PtrToMemberFunction>;
  50. };
  51. template<class Value,typename Type,Type (*PtrToFunction)(Value)>
  52. struct typed_key_impl<Type (*)(Value),PtrToFunction>
  53. {
  54. using value_type=Value;
  55. using type=global_fun<Value,Type,PtrToFunction>;
  56. };
  57. template<auto... Keys>
  58. struct key_impl;
  59. template<auto Key>
  60. struct key_impl<Key>:typed_key_impl<decltype(Key),Key>{};
  61. template<typename... Ts>
  62. struct least_generic;
  63. template<typename T0,typename... Ts>
  64. struct least_generic<T0,Ts...>
  65. {
  66. using type=T0;
  67. };
  68. template<typename T0,typename T1,typename... Ts>
  69. struct least_generic<T0,T1,Ts...>
  70. {
  71. static_assert(
  72. std::is_convertible<const T0&,const T1&>::value||
  73. std::is_convertible<const T1&,const T0&>::value,
  74. "one type should be convertible to the other");
  75. using type=typename least_generic<
  76. typename std::conditional<
  77. std::is_convertible<const T0&,const T1&>::value,T0,T1
  78. >::type,
  79. Ts...
  80. >::type;
  81. };
  82. template<auto Key0,auto... Keys>
  83. struct key_impl<Key0,Keys...>
  84. {
  85. using value_type=typename least_generic<
  86. typename std::decay<typename key_impl<Key0>::value_type>::type,
  87. typename std::decay<typename key_impl<Keys>::value_type>::type...
  88. >::type;
  89. using type=composite_key<
  90. value_type,
  91. typename key_impl<Key0>::type,
  92. typename key_impl<Keys>::type...
  93. >;
  94. };
  95. template<typename=composite_key<void,void>>
  96. struct composite_key_size;
  97. template<typename... Args>
  98. struct composite_key_size<composite_key<Args...>>
  99. {
  100. static constexpr auto value=sizeof...(Args)-1;
  101. };
  102. template<auto... Keys>
  103. struct limited_size_key_impl
  104. {
  105. static_assert(
  106. sizeof...(Keys)<=composite_key_size<>::value,
  107. "specified number of keys must meet the limits of "
  108. "boost::multi_index::composite_key");
  109. using type=typename key_impl<Keys...>::type;
  110. };
  111. } /* namespace multi_index::detail */
  112. template<auto... Keys>
  113. using key=typename detail::limited_size_key_impl<Keys...>::type;
  114. } /* namespace multi_index */
  115. } /* namespace boost */
  116. #endif
  117. #endif