utility.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // (C) Copyright John Maddock 2005.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_TR1_UTILITY_HPP_INCLUDED
  6. # define BOOST_TR1_UTILITY_HPP_INCLUDED
  7. # include <boost/tr1/detail/config.hpp>
  8. #ifdef BOOST_HAS_TR1_UTILITY
  9. # if defined(BOOST_HAS_INCLUDE_NEXT) && !defined(BOOST_TR1_DISABLE_INCLUDE_NEXT)
  10. # include_next BOOST_TR1_HEADER(utility)
  11. # else
  12. # include <boost/tr1/detail/config_all.hpp>
  13. # include BOOST_TR1_STD_HEADER(BOOST_TR1_PATH(utility))
  14. # endif
  15. #else
  16. #if defined(BOOST_TR1_USE_OLD_TUPLE)
  17. #include <boost/type_traits/integral_constant.hpp>
  18. #include <boost/type_traits/add_const.hpp>
  19. #include <boost/type_traits/add_reference.hpp>
  20. #include <boost/mpl/if.hpp>
  21. namespace std{ namespace tr1{
  22. template <class T> struct tuple_size; // forward declaration
  23. template < int I, class T> struct tuple_element; // forward declaration
  24. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  25. template <class T1, class T2>
  26. struct tuple_size< ::std::pair<T1, T2> >
  27. : public ::boost::integral_constant< ::std::size_t, 2>
  28. {
  29. };
  30. template <class T1, class T2>
  31. struct tuple_element<0, ::std::pair<T1, T2> >
  32. {
  33. typedef typename std::pair<T1, T2>::first_type type;
  34. };
  35. template <class T1, class T2>
  36. struct tuple_element<1, std::pair<T1, T2> >
  37. {
  38. typedef typename std::pair<T1, T2>::second_type type;
  39. };
  40. #endif
  41. namespace tuple_detail{
  42. template <int I, class T1, class T2>
  43. struct tuple_get_result
  44. {
  45. typedef typename boost::mpl::if_c<I==0, T1, T2>::type t1;
  46. typedef typename boost::add_reference<t1>::type type;
  47. };
  48. template <int I, class T1, class T2>
  49. struct const_tuple_get_result
  50. {
  51. typedef typename boost::mpl::if_c<I==0, T1, T2>::type t1;
  52. # if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT( 0x582))
  53. // I have absolutely no idea why add_const is not working here for Borland!
  54. // It passes all other free-standing tests, some strange interaction going on
  55. typedef typename boost::add_reference< const t1 >::type type;
  56. # else
  57. typedef typename boost::add_const<t1>::type t2;
  58. typedef typename boost::add_reference<t2>::type type;
  59. # endif
  60. };
  61. template<int I, class T1, class T2>
  62. inline typename tuple_detail::tuple_get_result<I,T1,T2>::type get(std::pair<T1, T2>& p, const ::boost::true_type&)
  63. {
  64. return p.first;
  65. }
  66. template<int I, class T1, class T2>
  67. inline typename tuple_detail::const_tuple_get_result<I,T1,T2>::type get(const std::pair<T1, T2>& p, const ::boost::true_type&)
  68. {
  69. return p.first;
  70. }
  71. template<int I, class T1, class T2>
  72. inline typename tuple_detail::tuple_get_result<I,T1,T2>::type get(std::pair<T1, T2>& p, const ::boost::false_type&)
  73. {
  74. return p.second;
  75. }
  76. template<int I, class T1, class T2>
  77. inline typename tuple_detail::const_tuple_get_result<I,T1,T2>::type get(const std::pair<T1, T2>& p, const ::boost::false_type&)
  78. {
  79. return p.second;
  80. }
  81. }
  82. template<int I, class T1, class T2>
  83. inline typename tuple_detail::tuple_get_result<I,T1,T2>::type get(std::pair<T1, T2>& p)
  84. {
  85. return tuple_detail::get<I>(p, boost::integral_constant<bool, I==0>());
  86. }
  87. template<int I, class T1, class T2>
  88. inline typename tuple_detail::const_tuple_get_result<I,T1,T2>::type get(const std::pair<T1, T2>& p)
  89. {
  90. return tuple_detail::get<I>(p, boost::integral_constant<bool, I==0>());
  91. }
  92. } } // namespaces
  93. #else
  94. #include <boost/tr1/tuple.hpp>
  95. #endif
  96. #endif
  97. #endif