at_key.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*!
  2. @file
  3. Defines `boost::hana::at_key`.
  4. @copyright Louis Dionne 2013-2017
  5. @copyright Jason Rice 2017
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  8. */
  9. #ifndef BOOST_HANA_AT_KEY_HPP
  10. #define BOOST_HANA_AT_KEY_HPP
  11. #include <boost/hana/fwd/at_key.hpp>
  12. #include <boost/hana/accessors.hpp>
  13. #include <boost/hana/at.hpp>
  14. #include <boost/hana/concept/searchable.hpp>
  15. #include <boost/hana/concept/struct.hpp>
  16. #include <boost/hana/config.hpp>
  17. #include <boost/hana/core/dispatch.hpp>
  18. #include <boost/hana/detail/decay.hpp>
  19. #include <boost/hana/equal.hpp>
  20. #include <boost/hana/find.hpp>
  21. #include <boost/hana/find_if.hpp>
  22. #include <boost/hana/first.hpp>
  23. #include <boost/hana/functional/on.hpp>
  24. #include <boost/hana/index_if.hpp>
  25. #include <boost/hana/length.hpp>
  26. #include <boost/hana/optional.hpp>
  27. #include <boost/hana/second.hpp>
  28. #include <cstddef>
  29. #include <utility>
  30. BOOST_HANA_NAMESPACE_BEGIN
  31. //! @cond
  32. template <typename Xs, typename Key>
  33. constexpr decltype(auto) at_key_t::operator()(Xs&& xs, Key const& key) const {
  34. using S = typename hana::tag_of<Xs>::type;
  35. using AtKey = BOOST_HANA_DISPATCH_IF(at_key_impl<S>,
  36. hana::Searchable<S>::value
  37. );
  38. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  39. static_assert(hana::Searchable<S>::value,
  40. "hana::at_key(xs, key) requires 'xs' to be Searchable");
  41. #endif
  42. return AtKey::apply(static_cast<Xs&&>(xs), key);
  43. }
  44. //! @endcond
  45. template <typename S, bool condition>
  46. struct at_key_impl<S, when<condition>> : default_ {
  47. template <typename Xs, typename Key>
  48. static constexpr auto apply(Xs&& xs, Key const& key) {
  49. return hana::find(static_cast<Xs&&>(xs), key).value();
  50. }
  51. };
  52. namespace at_key_detail {
  53. template <typename T>
  54. struct equal_to {
  55. T const& t;
  56. template <typename U>
  57. constexpr auto operator()(U const& u) const {
  58. return hana::equal(t, u);
  59. }
  60. };
  61. }
  62. template <typename S>
  63. struct at_key_impl<S, when<hana::Sequence<S>::value>> {
  64. template <typename Xs, typename Key>
  65. static constexpr decltype(auto) apply(Xs&& xs, Key const& key) {
  66. using Result = decltype(hana::index_if(
  67. static_cast<Xs&&>(xs), at_key_detail::equal_to<Key>{key}));
  68. return hana::at(static_cast<Xs&&>(xs), Result{}.value());
  69. }
  70. };
  71. template <typename S>
  72. struct at_key_impl<S, when<hana::Struct<S>::value>> {
  73. template <typename X, typename Key>
  74. static constexpr decltype(auto) apply(X&& x, Key const& key) {
  75. auto accessor = hana::second(*hana::find_if(hana::accessors<S>(),
  76. hana::equal.to(key) ^hana::on^ hana::first
  77. ));
  78. return accessor(static_cast<X&&>(x));
  79. }
  80. };
  81. BOOST_HANA_NAMESPACE_END
  82. #endif // !BOOST_HANA_AT_KEY_HPP