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