wrap_unwrap.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2003-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. #ifndef BOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED
  8. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  9. # pragma once
  10. #endif
  11. #include <boost/config.hpp> // SFINAE, MSVC.
  12. #include <boost/detail/workaround.hpp>
  13. #include <boost/iostreams/detail/enable_if_stream.hpp>
  14. #include <boost/iostreams/traits_fwd.hpp> // is_std_io.
  15. #include <boost/mpl/bool.hpp>
  16. #include <boost/mpl/identity.hpp>
  17. #include <boost/mpl/eval_if.hpp>
  18. #include <boost/mpl/if.hpp>
  19. #include <boost/ref.hpp>
  20. namespace boost { namespace iostreams { namespace detail {
  21. //------------------Definition of wrap/unwrap traits--------------------------//
  22. template<typename T>
  23. struct wrapped_type
  24. : mpl::if_<is_std_io<T>, reference_wrapper<T>, T>
  25. { };
  26. template<typename T>
  27. struct unwrapped_type
  28. : unwrap_reference<T>
  29. { };
  30. template<typename T>
  31. struct unwrap_ios
  32. : mpl::eval_if<
  33. is_std_io<T>,
  34. unwrap_reference<T>,
  35. mpl::identity<T>
  36. >
  37. { };
  38. //------------------Definition of wrap----------------------------------------//
  39. #ifndef BOOST_NO_SFINAE //----------------------------------------------------//
  40. template<typename T>
  41. inline T wrap(const T& t BOOST_IOSTREAMS_DISABLE_IF_STREAM(T))
  42. { return t; }
  43. template<typename T>
  44. inline typename wrapped_type<T>::type
  45. wrap(T& t BOOST_IOSTREAMS_ENABLE_IF_STREAM(T)) { return boost::ref(t); }
  46. #else // #ifndef BOOST_NO_SFINAE //-------------------------------------------//
  47. template<typename T>
  48. inline typename wrapped_type<T>::type // BCC 5.x needs namespace qualification.
  49. wrap_impl(const T& t, mpl::true_) { return boost::ref(const_cast<T&>(t)); }
  50. template<typename T>
  51. inline typename wrapped_type<T>::type // BCC 5.x needs namespace qualification.
  52. wrap_impl(T& t, mpl::true_) { return boost::ref(t); }
  53. template<typename T>
  54. inline typename wrapped_type<T>::type
  55. wrap_impl(const T& t, mpl::false_) { return t; }
  56. template<typename T>
  57. inline typename wrapped_type<T>::type
  58. wrap_impl(T& t, mpl::false_) { return t; }
  59. template<typename T>
  60. inline typename wrapped_type<T>::type
  61. wrap(const T& t) { return wrap_impl(t, is_std_io<T>()); }
  62. template<typename T>
  63. inline typename wrapped_type<T>::type
  64. wrap(T& t) { return wrap_impl(t, is_std_io<T>()); }
  65. #endif // #ifndef BOOST_NO_SFINAE //------------------------------------------//
  66. //------------------Definition of unwrap--------------------------------------//
  67. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1310) //----------------------------------//
  68. template<typename T>
  69. typename unwrapped_type<T>::type&
  70. unwrap(const reference_wrapper<T>& ref) { return ref.get(); }
  71. template<typename T>
  72. typename unwrapped_type<T>::type& unwrap(T& t) { return t; }
  73. template<typename T>
  74. const typename unwrapped_type<T>::type& unwrap(const T& t) { return t; }
  75. #else // #if !BOOST_WORKAROUND(BOOST_MSVC, < 1310) //-------------------------//
  76. // Since unwrap is a potential bottleneck, we avoid runtime tag dispatch.
  77. template<bool IsRefWrap>
  78. struct unwrap_impl;
  79. template<>
  80. struct unwrap_impl<true> {
  81. template<typename T>
  82. static typename unwrapped_type<T>::type& unwrap(const T& t)
  83. { return t.get(); }
  84. };
  85. template<>
  86. struct unwrap_impl<false> {
  87. template<typename T>
  88. static typename unwrapped_type<T>::type& unwrap(const T& t)
  89. { return const_cast<T&>(t); }
  90. };
  91. template<typename T>
  92. typename unwrapped_type<T>::type&
  93. unwrap(const T& t)
  94. { return unwrap_impl<is_reference_wrapper<T>::value>::unwrap(t); }
  95. #endif // #if !BOOST_WORKAROUND(BOOST_MSVC, < 1310) //------------------------//
  96. } } } // End namespaces detail, iostreams, boost.
  97. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED