pair.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2006 Tobias Schwinger
  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(FUSION_PAIR_07222005_1203)
  8. #define FUSION_PAIR_07222005_1203
  9. #include <iosfwd>
  10. #include <boost/fusion/support/detail/access.hpp>
  11. #include <boost/fusion/support/detail/as_fusion_element.hpp>
  12. #include <boost/config.hpp>
  13. #include <boost/utility/enable_if.hpp>
  14. #include <boost/type_traits/is_convertible.hpp>
  15. #if defined (BOOST_MSVC)
  16. # pragma warning(push)
  17. # pragma warning (disable: 4512) // assignment operator could not be generated.
  18. #endif
  19. namespace boost { namespace fusion
  20. {
  21. // A half runtime pair where the first type does not have data
  22. template <typename First, typename Second>
  23. struct pair
  24. {
  25. pair()
  26. : second() {}
  27. pair(pair const& rhs)
  28. : second(rhs.second) {}
  29. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  30. pair(pair&& rhs)
  31. : second(std::forward<Second>(rhs.second)) {}
  32. #endif
  33. pair(typename detail::call_param<Second>::type val)
  34. : second(val) {}
  35. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  36. template <typename Second2>
  37. pair(Second2&& val
  38. , typename boost::enable_if<is_convertible<Second2, Second> >::type* /*dummy*/ = 0
  39. ) : second(std::forward<Second2>(val)) {}
  40. #endif
  41. template <typename Second2>
  42. pair(pair<First, Second2> const& rhs)
  43. : second(rhs.second) {}
  44. template <typename Second2>
  45. pair& operator=(pair<First, Second2> const& rhs)
  46. {
  47. second = rhs.second;
  48. return *this;
  49. }
  50. pair& operator=(pair const& rhs)
  51. {
  52. second = rhs.second;
  53. return *this;
  54. }
  55. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  56. pair& operator=(pair&& rhs)
  57. {
  58. second = std::forward<Second>(rhs.second);
  59. return *this;
  60. }
  61. #endif
  62. typedef First first_type;
  63. typedef Second second_type;
  64. Second second;
  65. };
  66. namespace result_of
  67. {
  68. template<typename First, typename Second>
  69. struct make_pair
  70. {
  71. typedef fusion::pair<First,
  72. typename detail::as_fusion_element<Second>::type> type;
  73. };
  74. template<class Pair>
  75. struct first
  76. {
  77. typedef typename Pair::first_type type;
  78. };
  79. template<class Pair>
  80. struct second
  81. {
  82. typedef typename Pair::second_type type;
  83. };
  84. }
  85. template <typename First, typename Second>
  86. inline typename result_of::make_pair<First,Second>::type
  87. make_pair(Second const& val)
  88. {
  89. return pair<First, typename detail::as_fusion_element<Second>::type>(val);
  90. }
  91. template <typename First, typename Second>
  92. inline std::ostream&
  93. operator<<(std::ostream& os, pair<First, Second> const& p)
  94. {
  95. os << p.second;
  96. return os;
  97. }
  98. template <typename First, typename Second>
  99. inline std::istream&
  100. operator>>(std::istream& is, pair<First, Second>& p)
  101. {
  102. is >> p.second;
  103. return is;
  104. }
  105. template <typename First, typename SecondL, typename SecondR>
  106. inline bool
  107. operator==(pair<First, SecondL> const& l, pair<First, SecondR> const& r)
  108. {
  109. return l.second == r.second;
  110. }
  111. template <typename First, typename SecondL, typename SecondR>
  112. inline bool
  113. operator!=(pair<First, SecondL> const& l, pair<First, SecondR> const& r)
  114. {
  115. return l.second != r.second;
  116. }
  117. template <typename First, typename SecondL, typename SecondR>
  118. inline bool
  119. operator<(pair<First, SecondL> const& l, pair<First, SecondR> const& r)
  120. {
  121. return l.second < r.second;
  122. }
  123. }}
  124. #if defined (BOOST_MSVC)
  125. # pragma warning(pop)
  126. #endif
  127. #endif