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 | #ifndef _PTR_TRAITS_H |
31 | #define _PTR_TRAITS_H 1 |
32 | |
33 | #if __cplusplus >= 201103L |
34 | |
35 | #include <bits/move.h> |
36 | |
37 | namespace std _GLIBCXX_VISIBILITY(default) |
38 | { |
39 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
40 | |
41 | class __undefined; |
42 | |
43 | |
44 | template<typename _Tp> |
45 | struct __get_first_arg |
46 | { using type = __undefined; }; |
47 | |
48 | template<template<typename, typename...> class _Template, typename _Tp, |
49 | typename... _Types> |
50 | struct __get_first_arg<_Template<_Tp, _Types...>> |
51 | { using type = _Tp; }; |
52 | |
53 | template<typename _Tp> |
54 | using __get_first_arg_t = typename __get_first_arg<_Tp>::type; |
55 | |
56 | |
57 | template<typename _Tp, typename _Up> |
58 | struct __replace_first_arg |
59 | { }; |
60 | |
61 | template<template<typename, typename...> class _Template, typename _Up, |
62 | typename _Tp, typename... _Types> |
63 | struct __replace_first_arg<_Template<_Tp, _Types...>, _Up> |
64 | { using type = _Template<_Up, _Types...>; }; |
65 | |
66 | template<typename _Tp, typename _Up> |
67 | using __replace_first_arg_t = typename __replace_first_arg<_Tp, _Up>::type; |
68 | |
69 | template<typename _Tp> |
70 | using __make_not_void |
71 | = typename conditional<is_void<_Tp>::value, __undefined, _Tp>::type; |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | template<typename _Ptr> |
78 | struct pointer_traits |
79 | { |
80 | private: |
81 | template<typename _Tp> |
82 | using __element_type = typename _Tp::element_type; |
83 | |
84 | template<typename _Tp> |
85 | using __difference_type = typename _Tp::difference_type; |
86 | |
87 | template<typename _Tp, typename _Up, typename = void> |
88 | struct __rebind : __replace_first_arg<_Tp, _Up> { }; |
89 | |
90 | template<typename _Tp, typename _Up> |
91 | struct __rebind<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>>> |
92 | { using type = typename _Tp::template rebind<_Up>; }; |
93 | |
94 | public: |
95 | |
96 | using pointer = _Ptr; |
97 | |
98 | |
99 | using element_type |
100 | = __detected_or_t<__get_first_arg_t<_Ptr>, __element_type, _Ptr>; |
101 | |
102 | |
103 | using difference_type |
104 | = __detected_or_t<ptrdiff_t, __difference_type, _Ptr>; |
105 | |
106 | |
107 | template<typename _Up> |
108 | using rebind = typename __rebind<_Ptr, _Up>::type; |
109 | |
110 | static _Ptr |
111 | pointer_to(__make_not_void<element_type>& __e) |
112 | { return _Ptr::pointer_to(__e); } |
113 | |
114 | static_assert(!is_same<element_type, __undefined>::value, |
115 | "pointer type defines element_type or is like SomePointer<T, Args>"); |
116 | }; |
117 | |
118 | |
119 | |
120 | |
121 | |
122 | template<typename _Tp> |
123 | struct pointer_traits<_Tp*> |
124 | { |
125 | |
126 | typedef _Tp* pointer; |
127 | |
128 | typedef _Tp element_type; |
129 | |
130 | typedef ptrdiff_t difference_type; |
131 | |
132 | template<typename _Up> |
133 | using rebind = _Up*; |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | |
140 | static pointer |
141 | pointer_to(__make_not_void<element_type>& __r) noexcept |
142 | { return std::addressof(__r); } |
143 | }; |
144 | |
145 | |
146 | template<typename _Ptr, typename _Tp> |
147 | using __ptr_rebind = typename pointer_traits<_Ptr>::template rebind<_Tp>; |
148 | |
149 | _GLIBCXX_END_NAMESPACE_VERSION |
150 | } |
151 | |
152 | #endif |
153 | |
154 | #endif |
155 | |