summaryrefslogtreecommitdiff
path: root/boost/core/pointer_traits.hpp
blob: ff1a5321dbc3f1a4558b2467c4a7be7bf7acc14b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
Copyright 2017 Glen Joseph Fernandes
(glenjofe@gmail.com)

Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_CORE_POINTER_TRAITS_HPP
#define BOOST_CORE_POINTER_TRAITS_HPP

#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_POINTER_TRAITS)
#include <memory>
#else
#include <boost/core/addressof.hpp>
#endif

namespace boost {

template<class T>
struct pointer_traits;

namespace detail {

template<class U>
inline typename boost::pointer_traits<U>::element_type*
ptr_traits_address(const U& v) BOOST_NOEXCEPT
{
    return boost::pointer_traits<U>::to_address(v);
}

} /* detail */

#if !defined(BOOST_NO_CXX11_POINTER_TRAITS)
template<class T>
struct pointer_traits
    : std::pointer_traits<T> {
    template<class U>
    struct rebind_to {
        typedef typename std::pointer_traits<T>::template rebind<U> type;
    };
    static typename std::pointer_traits<T>::element_type*
    to_address(const T& v) BOOST_NOEXCEPT {
        return detail::ptr_traits_address(v.operator->());
    }
};

template<class T>
struct pointer_traits<T*>
    : std::pointer_traits<T*> {
    template<class U>
    struct rebind_to {
        typedef U* type;
    };
    static T* to_address(T* v) BOOST_NOEXCEPT {
        return v;
    }
};
#else
namespace detail {

struct ptr_traits_none { char first, second; };

template<class T>
struct ptr_traits_has_element {
private:
    template<class U>
    static ptr_traits_none call(...);
    template<class U>
    static char call(typename U::element_type* = 0);
public:
    static const bool value = sizeof(call<T>(0)) == 1;
};

template<class T>
struct ptr_traits_first;

#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<template<class, class...> class T, class U, class... Args>
struct ptr_traits_first<T<U, Args...> > {
    typedef U type;
};
#else
template<template<class> class T, class U>
struct ptr_traits_first<T<U> > {
    typedef U type;
};

template<template<class, class> class T, class U1, class U2>
struct ptr_traits_first<T<U1, U2> > {
    typedef U1 type;
};

template<template<class, class, class> class T, class U1, class U2, class U3>
struct ptr_traits_first<T<U1, U2, U3> > {
    typedef U1 type;
};
#endif

template<class T, bool = ptr_traits_has_element<T>::value>
struct ptr_traits_element {
    typedef typename T::element_type type;
};

template<class T>
struct ptr_traits_element<T, false> {
    typedef typename ptr_traits_first<T>::type type;
};

template<class T>
struct ptr_traits_has_difference {
private:
    template<class U>
    static ptr_traits_none call(...);
    template<class U>
    static char call(typename U::difference_type* = 0);
public:
    static const bool value = sizeof(call<T>(0)) == 1;
};

template<class T, bool = ptr_traits_has_difference<T>::value>
struct ptr_traits_difference {
    typedef typename T::difference_type type;
};

template<class T>
struct ptr_traits_difference<T, false> {
    typedef std::ptrdiff_t type;
};

template<class T, class V>
struct ptr_traits_has_rebind {
private:
    template<class U>
    static ptr_traits_none call(...);
    template<class U>
    static char call(typename U::template rebind<V>* = 0);
public:
    static const bool value = sizeof(call<T>(0)) == 1;
};

template<class T, class V>
struct ptr_traits_rebind_to;

#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<template<class, class...> class T, class U, class... Args, class V>
struct ptr_traits_rebind_to<T<U, Args...>, V> {
    typedef T<V, Args...> type;
};
#else
template<template<class> class T, class U, class V>
struct ptr_traits_rebind_to<T<U>, V> {
    typedef T<V> type;
};

template<template<class, class> class T, class U1, class U2, class V>
struct ptr_traits_rebind_to<T<U1, U2>, V> {
    typedef T<V, U2> type;
};

template<template<class, class, class> class T,
    class U1, class U2, class U3, class V>
struct ptr_traits_rebind_to<T<U1, U2, U3>, V> {
    typedef T<V, U2, U3> type;
};
#endif

#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
template<class T, class U, bool = ptr_traits_has_rebind<T, U>::value>
struct ptr_traits_rebind {
    typedef typename T::template rebind<U> type;
};

template<class T, class U>
struct ptr_traits_rebind<T, U, false> {
    typedef typename ptr_traits_rebind_to<T, U>::type type;
};
#else
template<class T, class U>
struct ptr_traits_rebind {
    typedef typename ptr_traits_rebind_to<T, U>::type type;
};
#endif

template<class T>
struct ptr_traits_value {
    typedef T type;
};

template<>
struct ptr_traits_value<void> {
    typedef struct { } type;
};

} /* detail */

template<class T>
struct pointer_traits {
    typedef T pointer;
    typedef typename detail::ptr_traits_element<T>::type element_type;
    typedef typename detail::ptr_traits_difference<T>::type difference_type;
    template<class U>
    struct rebind_to {
        typedef typename detail::ptr_traits_rebind<T, U>::type type;
    };
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
    template<class U>
    using rebind = typename detail::ptr_traits_rebind<T, U>::type;
#endif
    static pointer
    pointer_to(typename detail::ptr_traits_value<element_type>::type& v) {
        return pointer::pointer_to(v);
    }
    static element_type* to_address(const pointer& v) BOOST_NOEXCEPT {
        return detail::ptr_traits_address(v.operator->());
    }
};

template<class T>
struct pointer_traits<T*> {
    typedef T* pointer;
    typedef T element_type;
    typedef std::ptrdiff_t difference_type;
    template<class U>
    struct rebind_to {
        typedef U* type;
    };
#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
    template<class U>
    using rebind = U*;
#endif
    static T*
    pointer_to(typename detail::ptr_traits_value<T>::type& v) BOOST_NOEXCEPT {
        return addressof(v);
    }
    static T* to_address(T* v) BOOST_NOEXCEPT {
        return v;
    }
};
#endif

template<class T>
inline typename pointer_traits<T>::element_type*
to_address(const T& v) BOOST_NOEXCEPT
{
    return pointer_traits<T>::to_address(v);
}

template<class T>
inline T*
to_address(T* v) BOOST_NOEXCEPT
{
    return v;
}

} /* boost */

#endif