hash_index_args.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2003-2013 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_DETAIL_HASH_INDEX_ARGS_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_HASH_INDEX_ARGS_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/functional/hash.hpp>
  15. #include <boost/mpl/aux_/na.hpp>
  16. #include <boost/mpl/eval_if.hpp>
  17. #include <boost/mpl/identity.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #include <boost/multi_index/tag.hpp>
  20. #include <boost/static_assert.hpp>
  21. #include <boost/type_traits/is_same.hpp>
  22. #include <functional>
  23. namespace boost{
  24. namespace multi_index{
  25. namespace detail{
  26. /* Hashed index specifiers can be instantiated in two forms:
  27. *
  28. * (hashed_unique|hashed_non_unique)<
  29. * KeyFromValue,
  30. * Hash=boost::hash<KeyFromValue::result_type>,
  31. * Pred=std::equal_to<KeyFromValue::result_type> >
  32. * (hashed_unique|hashed_non_unique)<
  33. * TagList,
  34. * KeyFromValue,
  35. * Hash=boost::hash<KeyFromValue::result_type>,
  36. * Pred=std::equal_to<KeyFromValue::result_type> >
  37. *
  38. * hashed_index_args implements the machinery to accept this
  39. * argument-dependent polymorphism.
  40. */
  41. template<typename KeyFromValue>
  42. struct index_args_default_hash
  43. {
  44. typedef ::boost::hash<typename KeyFromValue::result_type> type;
  45. };
  46. template<typename KeyFromValue>
  47. struct index_args_default_pred
  48. {
  49. typedef std::equal_to<typename KeyFromValue::result_type> type;
  50. };
  51. template<typename Arg1,typename Arg2,typename Arg3,typename Arg4>
  52. struct hashed_index_args
  53. {
  54. typedef is_tag<Arg1> full_form;
  55. typedef typename mpl::if_<
  56. full_form,
  57. Arg1,
  58. tag< > >::type tag_list_type;
  59. typedef typename mpl::if_<
  60. full_form,
  61. Arg2,
  62. Arg1>::type key_from_value_type;
  63. typedef typename mpl::if_<
  64. full_form,
  65. Arg3,
  66. Arg2>::type supplied_hash_type;
  67. typedef typename mpl::eval_if<
  68. mpl::is_na<supplied_hash_type>,
  69. index_args_default_hash<key_from_value_type>,
  70. mpl::identity<supplied_hash_type>
  71. >::type hash_type;
  72. typedef typename mpl::if_<
  73. full_form,
  74. Arg4,
  75. Arg3>::type supplied_pred_type;
  76. typedef typename mpl::eval_if<
  77. mpl::is_na<supplied_pred_type>,
  78. index_args_default_pred<key_from_value_type>,
  79. mpl::identity<supplied_pred_type>
  80. >::type pred_type;
  81. BOOST_STATIC_ASSERT(is_tag<tag_list_type>::value);
  82. BOOST_STATIC_ASSERT(!mpl::is_na<key_from_value_type>::value);
  83. BOOST_STATIC_ASSERT(!mpl::is_na<hash_type>::value);
  84. BOOST_STATIC_ASSERT(!mpl::is_na<pred_type>::value);
  85. };
  86. } /* namespace multi_index::detail */
  87. } /* namespace multi_index */
  88. } /* namespace boost */
  89. #endif