utility.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file utility.hpp
  3. /// Proto callables for things found in the std \<utility\> header
  4. //
  5. // Copyright 2010 Eric Niebler. Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_PROTO_FUNCTIONAL_STD_UTILITY_HPP_EAN_11_27_2010
  9. #define BOOST_PROTO_FUNCTIONAL_STD_UTILITY_HPP_EAN_11_27_2010
  10. #include <utility>
  11. #include <boost/type_traits/remove_const.hpp>
  12. #include <boost/type_traits/remove_reference.hpp>
  13. #include <boost/proto/proto_fwd.hpp>
  14. namespace boost { namespace proto { namespace functional
  15. {
  16. /// \brief A PolymorphicFunctionObject type that invokes the
  17. /// \c std::make_pair() algorithm on its arguments.
  18. ///
  19. /// A PolymorphicFunctionObject type that invokes the
  20. /// \c std::make_pair() algorithm on its arguments.
  21. struct make_pair
  22. {
  23. BOOST_PROTO_CALLABLE()
  24. template<typename Sig>
  25. struct result;
  26. template<typename This, typename First, typename Second>
  27. struct result<This(First, Second)>
  28. {
  29. typedef
  30. std::pair<
  31. typename remove_const<typename remove_reference<First>::type>::type
  32. , typename remove_const<typename remove_reference<Second>::type>::type
  33. >
  34. type;
  35. };
  36. template<typename First, typename Second>
  37. std::pair<First, Second> operator()(First const &first, Second const &second) const
  38. {
  39. return std::make_pair(first, second);
  40. }
  41. };
  42. /// \brief A PolymorphicFunctionObject type that returns
  43. /// the first element of a std::pair.
  44. ///
  45. /// A PolymorphicFunctionObject type that returns
  46. /// the first element of a std::pair..
  47. struct first
  48. {
  49. BOOST_PROTO_CALLABLE()
  50. template<typename Sig>
  51. struct result;
  52. template<typename This, typename Pair>
  53. struct result<This(Pair)>
  54. {
  55. typedef typename Pair::first_type type;
  56. };
  57. template<typename This, typename Pair>
  58. struct result<This(Pair &)>
  59. {
  60. typedef typename Pair::first_type &type;
  61. };
  62. template<typename This, typename Pair>
  63. struct result<This(Pair const &)>
  64. {
  65. typedef typename Pair::first_type const &type;
  66. };
  67. template<typename Pair>
  68. typename Pair::first_type &operator()(Pair &pair) const
  69. {
  70. return pair.first;
  71. }
  72. template<typename Pair>
  73. typename Pair::first_type const &operator()(Pair const &pair) const
  74. {
  75. return pair.first;
  76. }
  77. };
  78. /// \brief A PolymorphicFunctionObject type that returns
  79. /// the second element of a std::pair.
  80. ///
  81. /// A PolymorphicFunctionObject type that returns
  82. /// the second element of a std::pair..
  83. struct second
  84. {
  85. BOOST_PROTO_CALLABLE()
  86. template<typename Sig>
  87. struct result;
  88. template<typename This, typename Pair>
  89. struct result<This(Pair)>
  90. {
  91. typedef typename Pair::second_type type;
  92. };
  93. template<typename This, typename Pair>
  94. struct result<This(Pair &)>
  95. {
  96. typedef typename Pair::second_type &type;
  97. };
  98. template<typename This, typename Pair>
  99. struct result<This(Pair const &)>
  100. {
  101. typedef typename Pair::second_type const &type;
  102. };
  103. template<typename Pair>
  104. typename Pair::second_type &operator()(Pair &pair) const
  105. {
  106. return pair.second;
  107. }
  108. template<typename Pair>
  109. typename Pair::second_type const &operator()(Pair const &pair) const
  110. {
  111. return pair.second;
  112. }
  113. };
  114. }}}
  115. #endif