]> git.lyx.org Git - lyx.git/blob - boost/boost/type_traits/cv_traits.hpp
update from boost cvs
[lyx.git] / boost / boost / type_traits / cv_traits.hpp
1 //  (C) Copyright Steve Cleary, Beman Dawes, Howard Hinnant & John Maddock 2000.
2 //  Permission to copy, use, modify, sell and
3 //  distribute this software is granted provided this copyright notice appears
4 //  in all copies. This software is provided "as is" without express or implied
5 //  warranty, and with no claim as to its suitability for any purpose.
6 //
7 //  See http://www.boost.org for most recent version including documentation.
8 //
9 //  defines traits classes for cv-qualified types:
10 //  is_const, is_volatile, remove_const, remove_volatile, remove_cv.
11 //
12 //  Revision History:
13 //  24th March 2001:
14 //    Fixed is_const/is_volatile so that they work with reference types
15
16 #ifndef BOOST_CV_TYPE_TRAITS_HPP
17 #define BOOST_CV_TYPE_TRAITS_HPP
18
19 #ifndef BOOST_ICE_TYPE_TRAITS_HPP
20 #include <boost/type_traits/ice.hpp>
21 #endif
22 #ifndef BOOST_FWD_TYPE_TRAITS_HPP
23 #include <boost/type_traits/fwd.hpp>
24 #endif
25
26 namespace boost{
27
28 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
29 namespace detail{
30 //
31 // implementation helper:
32 //
33 template <class T>
34 struct cv_traits_imp{};
35
36 template <class T>
37 struct cv_traits_imp<T*>
38 {
39    BOOST_STATIC_CONSTANT(bool, is_const = false);
40    BOOST_STATIC_CONSTANT(bool, is_volatile = false);
41    typedef T unqualified_type;
42 };
43
44 template <class T>
45 struct cv_traits_imp<const T*>
46 {
47    BOOST_STATIC_CONSTANT(bool, is_const = true);
48    BOOST_STATIC_CONSTANT(bool, is_volatile = false);
49    typedef T unqualified_type;
50 };
51
52 template <class T>
53 struct cv_traits_imp<volatile T*>
54 {
55    BOOST_STATIC_CONSTANT(bool, is_const = false);
56    BOOST_STATIC_CONSTANT(bool, is_volatile = true);
57    typedef T unqualified_type;
58 };
59
60 template <class T>
61 struct cv_traits_imp<const volatile T*>
62 {
63    BOOST_STATIC_CONSTANT(bool, is_const = true);
64    BOOST_STATIC_CONSTANT(bool, is_volatile = true);
65    typedef T unqualified_type;
66 };
67
68 template <class T, bool is_vol>
69 struct remove_const_helper
70 {
71    typedef T type;
72 };
73 template <class T>
74 struct remove_const_helper<T, true>
75 {
76    typedef volatile T type;
77 };
78
79 template <class T, bool is_vol>
80 struct remove_volatile_helper
81 {
82    typedef T type;
83 };
84 template <class T>
85 struct remove_volatile_helper<T, true>
86 {
87    typedef const T type;
88 };
89
90 } // namespace detail
91
92 // * convert a type T to a non-volatile type - remove_volatile<T>
93 template <typename T>
94 struct remove_volatile
95 {
96    typedef typename detail::cv_traits_imp<T*>::unqualified_type uq_type;
97    typedef typename detail::remove_volatile_helper<uq_type, ::boost::is_const<T>::value>::type type;
98 };
99 template <typename T> struct remove_volatile<T&>{ typedef T& type; };
100 template <typename T, std::size_t N> struct remove_volatile<volatile T[N]>{ typedef T type[N]; };
101 template <typename T, std::size_t N> struct remove_volatile<const volatile T[N]>{ typedef const T type[N]; };
102
103 // * convert a type T to non-const type - remove_const<T>
104 template <typename T>
105 struct remove_const
106 {
107    typedef typename detail::cv_traits_imp<T*>::unqualified_type uq_type;
108    typedef typename detail::remove_const_helper<uq_type, ::boost::is_volatile<T>::value>::type type;
109 };
110 template <typename T> struct remove_const<T&>{ typedef T& type; };
111 template <typename T, std::size_t N> struct remove_const<const T[N]>{ typedef T type[N]; };
112 template <typename T, std::size_t N> struct remove_const<const volatile T[N]>{ typedef volatile T type[N]; };
113
114 //  convert a type T to a non-cv-qualified type - remove_cv<T>
115 template <typename T>
116 struct remove_cv
117 {
118    typedef typename detail::cv_traits_imp<T*>::unqualified_type type;
119 };
120 template <typename T> struct remove_cv<T&>{ typedef T& type; };
121 template <typename T, std::size_t N> struct remove_cv<const T[N]>{ typedef T type[N]; };
122 template <typename T, std::size_t N> struct remove_cv<volatile T[N]>{ typedef T type[N]; };
123 template <typename T, std::size_t N> struct remove_cv<const volatile T[N]>{ typedef T type[N]; };
124
125 //* is a type T  declared const - is_const<T>
126 template <typename T>
127 struct is_const
128 {
129    BOOST_STATIC_CONSTANT(bool, value = detail::cv_traits_imp<T*>::is_const);
130 };
131 template <typename T> struct is_const<T&>
132 { BOOST_STATIC_CONSTANT(bool, value = false); };
133 #if defined(__BORLANDC__)
134 // these are illegal specialisations; cv-qualifies applied to
135 // references have no effect according to [8.3.2p1],
136 // C++ Builder requires them though as it treats cv-qualified
137 // references as distinct types...
138 template <typename T> struct is_const<T&const>
139 { BOOST_STATIC_CONSTANT(bool, value = false); };
140 template <typename T> struct is_const<T&volatile>
141 { BOOST_STATIC_CONSTANT(bool, value = false); };
142 template <typename T> struct is_const<T&const volatile>
143 { BOOST_STATIC_CONSTANT(bool, value = false); };
144 #endif
145
146 //* is a type T declared volatile - is_volatile<T>
147 template <typename T>
148 struct is_volatile
149 {
150    BOOST_STATIC_CONSTANT(bool, value = detail::cv_traits_imp<T*>::is_volatile);
151 };
152 template <typename T> struct is_volatile<T&>
153 { BOOST_STATIC_CONSTANT(bool, value = false); };
154 #if defined(__BORLANDC__)
155 // these are illegal specialisations; cv-qualifies applied to
156 // references have no effect according to [8.3.2p1],
157 // C++ Builder requires them though as it treats cv-qualified
158 // references as distinct types...
159 template <typename T> struct is_volatile<T&const>
160 { BOOST_STATIC_CONSTANT(bool, value = false); };
161 template <typename T> struct is_volatile<T&volatile>
162 { BOOST_STATIC_CONSTANT(bool, value = false); };
163 template <typename T> struct is_volatile<T&const volatile>
164 { BOOST_STATIC_CONSTANT(bool, value = false); };
165 #endif
166
167 #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
168 // The following three don't work:
169 template <typename T> struct remove_volatile{ typedef T type; };
170 template <typename T> struct remove_const{ typedef T type; };
171 template <typename T> struct remove_cv{ typedef T type; };
172
173 namespace detail{
174    using ::boost::type_traits::yes_type;
175    using ::boost::type_traits::no_type;
176    yes_type is_const_helper(const volatile void*);
177    no_type is_const_helper(volatile void *);
178    yes_type is_volatile_helper(const volatile void*);
179    no_type is_volatile_helper(const void *);
180 }
181
182 template <typename T>
183 struct is_const
184
185 private:
186    static T t;
187 public:
188    BOOST_STATIC_CONSTANT(bool, value = (sizeof(detail::yes_type) == sizeof(detail::is_const_helper(&t))));
189 };
190
191 template <>
192 struct is_const<void>
193 {
194    BOOST_STATIC_CONSTANT(bool, value = false);
195 };
196 #ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS
197 template <>
198 struct is_const<const void>
199 {
200    BOOST_STATIC_CONSTANT(bool, value = true);
201 };
202 template <>
203 struct is_const<volatile void>
204 {
205    BOOST_STATIC_CONSTANT(bool, value = false);
206 };
207 template <>
208 struct is_const<const volatile void>
209 {
210    BOOST_STATIC_CONSTANT(bool, value = true);
211 };
212 #endif
213
214 template <typename T>
215 struct is_volatile
216 {
217 private:
218    static T t;
219 public:
220    BOOST_STATIC_CONSTANT(bool, value = (sizeof(detail::yes_type) == sizeof(detail::is_volatile_helper(&t))));
221 };
222
223 template <>
224 struct is_volatile<void>
225 {
226    BOOST_STATIC_CONSTANT(bool, value = false);
227 };
228 #ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS
229 template <>
230 struct is_volatile<const void>
231 {
232    BOOST_STATIC_CONSTANT(bool, value = false);
233 };
234 template <>
235 struct is_volatile<volatile void>
236 {
237    BOOST_STATIC_CONSTANT(bool, value = true);
238 };
239 template <>
240 struct is_volatile<const volatile void>
241 {
242    BOOST_STATIC_CONSTANT(bool, value = true);
243 };
244 #endif
245
246 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
247
248 // * convert a type T to const type - add_const<T>
249 // this is not required since the result is always
250 // the same as "T const", but it does suppress warnings
251 // from some compilers:
252 template <typename T>
253 struct add_const
254 {
255    typedef T const type;
256 };
257 // * convert a type T to volatile type - add_volatile<T>
258 // this is not required since the result is always
259 // the same as "T volatile", but it does suppress warnings
260 // from some compilers:
261 template <typename T>
262 struct add_volatile
263 {
264    typedef T volatile type;
265 };
266 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
267 template <class T>
268 struct add_const<T&>{ typedef T& type; };
269 template <class T>
270 struct add_volatile<T&>{ typedef T& type; };
271 #endif
272
273 } // namespace boost
274
275
276 #endif // BOOST_CV_TYPE_TRAITS_HPP
277
278
279