at_key.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2006 Dan Marsden
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(BOOST_FUSION_AT_KEY_20060304_1755)
  8. #define BOOST_FUSION_AT_KEY_20060304_1755
  9. #include <boost/type_traits/is_const.hpp>
  10. #include <boost/fusion/sequence/intrinsic_fwd.hpp>
  11. #include <boost/fusion/algorithm/query/find.hpp>
  12. #include <boost/fusion/iterator/deref_data.hpp>
  13. #include <boost/fusion/support/tag_of.hpp>
  14. #include <boost/fusion/support/detail/access.hpp>
  15. namespace boost { namespace fusion
  16. {
  17. // Special tags:
  18. struct sequence_facade_tag;
  19. struct boost_array_tag; // boost::array tag
  20. struct mpl_sequence_tag; // mpl sequence tag
  21. struct std_pair_tag; // std::pair tag
  22. namespace extension
  23. {
  24. template <typename Tag>
  25. struct at_key_impl
  26. {
  27. template <typename Seq, typename Key>
  28. struct apply
  29. {
  30. typedef typename
  31. result_of::deref_data<
  32. typename result_of::find<Seq, Key>::type
  33. >::type
  34. type;
  35. static type
  36. call(Seq& seq)
  37. {
  38. return fusion::deref_data(fusion::find<Key>(seq));
  39. }
  40. };
  41. };
  42. template <>
  43. struct at_key_impl<sequence_facade_tag>
  44. {
  45. template <typename Sequence, typename Key>
  46. struct apply : Sequence::template at_key_impl<Sequence, Key> {};
  47. };
  48. template <>
  49. struct at_key_impl<boost_array_tag>;
  50. template <>
  51. struct at_key_impl<mpl_sequence_tag>;
  52. template <>
  53. struct at_key_impl<std_pair_tag>;
  54. }
  55. namespace result_of
  56. {
  57. template <typename Sequence, typename Key>
  58. struct at_key
  59. : extension::at_key_impl<typename detail::tag_of<Sequence>::type>::
  60. template apply<Sequence, Key>
  61. {};
  62. }
  63. template <typename Key, typename Sequence>
  64. inline typename
  65. lazy_disable_if<
  66. is_const<Sequence>
  67. , result_of::at_key<Sequence, Key>
  68. >::type
  69. at_key(Sequence& seq)
  70. {
  71. return result_of::at_key<Sequence, Key>::call(seq);
  72. }
  73. template <typename Key, typename Sequence>
  74. inline typename result_of::at_key<Sequence const, Key>::type
  75. at_key(Sequence const& seq)
  76. {
  77. return result_of::at_key<Sequence const, Key>::call(seq);
  78. }
  79. }}
  80. #endif