]> git.lyx.org Git - lyx.git/blob - boost/boost/ref.hpp
Sync 13x and 14x trees.
[lyx.git] / boost / boost / ref.hpp
1 #ifndef BOOST_REF_HPP_INCLUDED
2 #define BOOST_REF_HPP_INCLUDED
3
4 // MS compatible compilers support #pragma once
5
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
9
10 #include <boost/config.hpp>
11 #include <boost/utility/addressof.hpp>
12 #include <boost/mpl/bool.hpp>
13
14 //
15 //  ref.hpp - ref/cref, useful helper functions
16 //
17 //  Copyright (C) 1999, 2000 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
18 //  Copyright (C) 2001, 2002 Peter Dimov
19 //  Copyright (C) 2002 David Abrahams
20 //
21 // Distributed under the Boost Software License, Version 1.0. (See
22 // accompanying file LICENSE_1_0.txt or copy at
23 // http://www.boost.org/LICENSE_1_0.txt)
24 //
25 //  See http://www.boost.org/libs/bind/ref.html for documentation.
26 //
27
28 namespace boost
29 {
30
31 template<class T> class reference_wrapper
32
33 public:
34     typedef T type;
35
36 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
37
38     explicit reference_wrapper(T& t): t_(&t) {}
39
40 #else
41
42     explicit reference_wrapper(T& t): t_(boost::addressof(t)) {}
43
44 #endif
45
46     operator T& () const { return *t_; }
47
48     T& get() const { return *t_; }
49
50     T* get_pointer() const { return t_; }
51
52 private:
53
54     T* t_;
55 };
56
57 # if defined(__BORLANDC__) && (__BORLANDC__ <= 0x570)
58 #  define BOOST_REF_CONST
59 # else
60 #  define BOOST_REF_CONST const
61 # endif
62
63 template<class T> inline reference_wrapper<T> BOOST_REF_CONST ref(T & t)
64
65     return reference_wrapper<T>(t);
66 }
67
68 template<class T> inline reference_wrapper<T const> BOOST_REF_CONST cref(T const & t)
69 {
70     return reference_wrapper<T const>(t);
71 }
72
73 # undef BOOST_REF_CONST
74
75 # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
76
77 template<typename T>
78 class is_reference_wrapper
79     : public mpl::false_
80 {
81 };
82
83 template<typename T>
84 class unwrap_reference
85 {
86  public:
87     typedef T type;
88 };
89
90 #  define AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(X) \
91 template<typename T> \
92 class is_reference_wrapper< X > \
93     : public mpl::true_ \
94 { \
95 }; \
96 \
97 template<typename T> \
98 class unwrap_reference< X > \
99 { \
100  public: \
101     typedef T type; \
102 }; \
103 /**/
104
105 AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(reference_wrapper<T>)
106 #if !defined(BOOST_NO_CV_SPECIALIZATIONS)
107 AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(reference_wrapper<T> const)
108 AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(reference_wrapper<T> volatile)
109 AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF(reference_wrapper<T> const volatile)
110 #endif
111
112 #  undef AUX_REFERENCE_WRAPPER_METAFUNCTIONS_DEF
113
114 # else // no partial specialization
115
116 } // namespace boost
117
118 #include <boost/type.hpp>
119
120 namespace boost
121 {
122
123 namespace detail
124 {
125   typedef char (&yes_reference_wrapper_t)[1];
126   typedef char (&no_reference_wrapper_t)[2];
127       
128   no_reference_wrapper_t is_reference_wrapper_test(...);
129
130   template<typename T>
131   yes_reference_wrapper_t is_reference_wrapper_test(type< reference_wrapper<T> >);
132
133   template<bool wrapped>
134   struct reference_unwrapper
135   {
136       template <class T>
137       struct apply
138       {
139           typedef T type;
140       };
141   };
142
143   template<>
144   struct reference_unwrapper<true>
145   {
146       template <class T>
147       struct apply
148       {
149           typedef typename T::type type;
150       };
151   };
152 }
153
154 template<typename T>
155 class is_reference_wrapper
156 {
157  public:
158     BOOST_STATIC_CONSTANT(
159         bool, value = (
160              sizeof(detail::is_reference_wrapper_test(type<T>()))
161             == sizeof(detail::yes_reference_wrapper_t)));
162     
163     typedef ::boost::mpl::bool_<value> type;
164 };
165
166 template <typename T>
167 class unwrap_reference
168     : public detail::reference_unwrapper<
169         is_reference_wrapper<T>::value
170       >::template apply<T>
171 {};
172
173 # endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
174
175 } // namespace boost
176
177 #endif // #ifndef BOOST_REF_HPP_INCLUDED