wrap_unwrap.hpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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)
  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. template<typename T>
  68. typename unwrapped_type<T>::type&
  69. unwrap(const reference_wrapper<T>& ref) { return ref.get(); }
  70. template<typename T>
  71. typename unwrapped_type<T>::type& unwrap(T& t) { return t; }
  72. template<typename T>
  73. const typename unwrapped_type<T>::type& unwrap(const T& t) { return t; }
  74. } } } // End namespaces detail, iostreams, boost.
  75. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED