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 
6 // See http://www.boost.org/libs/iostreams for documentation.
7 
8 #ifndef BOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED
10 
11 #if defined(_MSC_VER)
12 # pragma once
13 #endif
14 
15 #include <boost/config.hpp>                             // SFINAE, MSVC.
16 #include <boost/detail/workaround.hpp>
17 #include <boost/iostreams/detail/enable_if_stream.hpp>
18 #include <boost/iostreams/traits_fwd.hpp>               // is_std_io.
19 #include <boost/mpl/bool.hpp>
20 #include <boost/mpl/identity.hpp>
21 #include <boost/mpl/eval_if.hpp>
22 #include <boost/mpl/if.hpp>
23 #include <boost/ref.hpp>
24 
25 namespace boost { namespace iostreams { namespace detail {
26 
27 //------------------Definition of wrap/unwrap traits--------------------------//
28 
29 template<typename T>
30 struct wrapped_type
31     : mpl::if_<is_std_io<T>, reference_wrapper<T>, T>
32     { };
33 
34 template<typename T>
35 struct unwrapped_type
36     : unwrap_reference<T>
37     { };
38 
39 template<typename T>
40 struct unwrap_ios
41     : mpl::eval_if<
42           is_std_io<T>,
43           unwrap_reference<T>,
44           mpl::identity<T>
45       >
46     { };
47 
48 //------------------Definition of wrap----------------------------------------//
49 
50 #ifndef BOOST_NO_SFINAE //----------------------------------------------------//
51     template<typename T>
wrap(const T & t BOOST_IOSTREAMS_DISABLE_IF_STREAM (T))52     inline T wrap(const T& t BOOST_IOSTREAMS_DISABLE_IF_STREAM(T))
53     { return t; }
54 
55     template<typename T>
56     inline typename wrapped_type<T>::type
wrap(T & t BOOST_IOSTREAMS_ENABLE_IF_STREAM (T))57     wrap(T& t BOOST_IOSTREAMS_ENABLE_IF_STREAM(T)) { return boost::ref(t); }
58 #else // #ifndef BOOST_NO_SFINAE //-------------------------------------------//
59     template<typename T>
60     inline typename wrapped_type<T>::type // BCC 5.x needs namespace qualification.
wrap_impl(const T & t,mpl::true_)61     wrap_impl(const T& t, mpl::true_) { return boost::ref(const_cast<T&>(t)); }
62 
63     template<typename T>
64     inline typename wrapped_type<T>::type // BCC 5.x needs namespace qualification.
wrap_impl(T & t,mpl::true_)65     wrap_impl(T& t, mpl::true_) { return boost::ref(t); }
66 
67     template<typename T>
68     inline typename wrapped_type<T>::type
wrap_impl(const T & t,mpl::false_)69     wrap_impl(const T& t, mpl::false_) { return t; }
70 
71     template<typename T>
72     inline typename wrapped_type<T>::type
wrap_impl(T & t,mpl::false_)73     wrap_impl(T& t, mpl::false_) { return t; }
74 
75     template<typename T>
76     inline typename wrapped_type<T>::type
wrap(const T & t)77     wrap(const T& t) { return wrap_impl(t, is_std_io<T>()); }
78 
79     template<typename T>
80     inline typename wrapped_type<T>::type
wrap(T & t)81     wrap(T& t) { return wrap_impl(t, is_std_io<T>()); }
82 #endif // #ifndef BOOST_NO_SFINAE //------------------------------------------//
83 
84 //------------------Definition of unwrap--------------------------------------//
85 
86 template<typename T>
87 typename unwrapped_type<T>::type&
unwrap(const reference_wrapper<T> & ref)88 unwrap(const reference_wrapper<T>& ref) { return ref.get(); }
89 
90 template<typename T>
unwrap(T & t)91 typename unwrapped_type<T>::type& unwrap(T& t) { return t; }
92 
93 template<typename T>
unwrap(const T & t)94 const typename unwrapped_type<T>::type& unwrap(const T& t) { return t; }
95 
96 } } } // End namespaces detail, iostreams, boost.
97 
98 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED
99