blob: 93291df6ef077f7478e81d261d0ca02ead391283 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001// (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 NDNBOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED
9#define NDNBOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED
10
11#if defined(_MSC_VER) && (_MSC_VER >= 1020)
12# pragma once
13#endif
14
15#include <ndnboost/config.hpp> // SFINAE, MSVC.
16#include <ndnboost/detail/workaround.hpp>
17#include <ndnboost/iostreams/detail/enable_if_stream.hpp>
18#include <ndnboost/iostreams/traits_fwd.hpp> // is_std_io.
19#include <ndnboost/mpl/bool.hpp>
20#include <ndnboost/mpl/identity.hpp>
21#include <ndnboost/mpl/eval_if.hpp>
22#include <ndnboost/mpl/if.hpp>
23#include <ndnboost/ref.hpp>
24
25namespace ndnboost { namespace iostreams { namespace detail {
26
27//------------------Definition of wrap/unwrap traits--------------------------//
28
29template<typename T>
30struct wrapped_type
31 : mpl::if_<is_std_io<T>, reference_wrapper<T>, T>
32 { };
33
34template<typename T>
35struct unwrapped_type
36 : unwrap_reference<T>
37 { };
38
39template<typename T>
40struct 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 NDNBOOST_NO_SFINAE //----------------------------------------------------//
51 template<typename T>
52 inline T wrap(const T& t NDNBOOST_IOSTREAMS_DISABLE_IF_STREAM(T))
53 { return t; }
54
55 template<typename T>
56 inline typename wrapped_type<T>::type
57 wrap(T& t NDNBOOST_IOSTREAMS_ENABLE_IF_STREAM(T)) { return ndnboost::ref(t); }
58#else // #ifndef NDNBOOST_NO_SFINAE //-------------------------------------------//
59 template<typename T>
60 inline typename wrapped_type<T>::type // BCC 5.x needs namespace qualification.
61 wrap_impl(const T& t, mpl::true_) { return ndnboost::ref(const_cast<T&>(t)); }
62
63 template<typename T>
64 inline typename wrapped_type<T>::type // BCC 5.x needs namespace qualification.
65 wrap_impl(T& t, mpl::true_) { return ndnboost::ref(t); }
66
67 template<typename T>
68 inline typename wrapped_type<T>::type
69 wrap_impl(const T& t, mpl::false_) { return t; }
70
71 template<typename T>
72 inline typename wrapped_type<T>::type
73 wrap_impl(T& t, mpl::false_) { return t; }
74
75 template<typename T>
76 inline typename wrapped_type<T>::type
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
81 wrap(T& t) { return wrap_impl(t, is_std_io<T>()); }
82#endif // #ifndef NDNBOOST_NO_SFINAE //------------------------------------------//
83
84//------------------Definition of unwrap--------------------------------------//
85
86#if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1310) //----------------------------------//
87
88template<typename T>
89typename unwrapped_type<T>::type&
90unwrap(const reference_wrapper<T>& ref) { return ref.get(); }
91
92template<typename T>
93typename unwrapped_type<T>::type& unwrap(T& t) { return t; }
94
95template<typename T>
96const typename unwrapped_type<T>::type& unwrap(const T& t) { return t; }
97
98#else // #if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1310) //-------------------------//
99
100// Since unwrap is a potential bottleneck, we avoid runtime tag dispatch.
101template<bool IsRefWrap>
102struct unwrap_impl;
103
104template<>
105struct unwrap_impl<true> {
106 template<typename T>
107 static typename unwrapped_type<T>::type& unwrap(const T& t)
108 { return t.get(); }
109};
110
111template<>
112struct unwrap_impl<false> {
113 template<typename T>
114 static typename unwrapped_type<T>::type& unwrap(const T& t)
115 { return const_cast<T&>(t); }
116};
117
118template<typename T>
119typename unwrapped_type<T>::type&
120unwrap(const T& t)
121{ return unwrap_impl<is_reference_wrapper<T>::value>::unwrap(t); }
122
123#endif // #if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, < 1310) //------------------------//
124
125} } } // End namespaces detail, iostreams, boost.
126
127#endif // #ifndef NDNBOOST_IOSTREAMS_DETAIL_WRAP_UNWRAP_HPP_INCLUDED