This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
propagate_const.h
427 lines (348 loc) · 14.4 KB
/
propagate_const.h
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
Copyright (c) 2014-2018 Jonathan B. Coe
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef JBCOE_PROPAGATE_CONST_INCLUDED
#define JBCOE_PROPAGATE_CONST_INCLUDED
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
#ifndef _MSC_VER
#define PROPAGATE_CONST_CONSTEXPR constexpr
#else
#if _MSC_VER <= 1900 // MSVS 2015 and earlier
#define PROPAGATE_CONST_CONSTEXPR
#define PROPAGATE_CONST_HAS_NO_EXPRESSION_SFINAE
#else
#define PROPAGATE_CONST_CONSTEXPR constexpr
#endif
#endif
namespace std {
namespace experimental {
inline namespace fundamentals_v2 {
template <class T>
class propagate_const {
public:
using element_type = typename std::pointer_traits<T>::element_type;
private:
template <class U>
static element_type* get_pointer(U* u) {
return u;
}
template <class U>
static element_type* get_pointer(U& u) {
return get_pointer(u.get());
}
template <class U>
static const element_type* get_pointer(const U* u) {
return u;
}
template <class U>
static const element_type* get_pointer(const U& u) {
return get_pointer(u.get());
}
template <class U>
struct is_propagate_const : false_type {};
template <class U>
struct is_propagate_const<propagate_const<U>> : true_type {};
public:
// [propagate_const.ctor], constructors
PROPAGATE_CONST_CONSTEXPR propagate_const() = default;
propagate_const(const propagate_const& p) = delete;
PROPAGATE_CONST_CONSTEXPR propagate_const(propagate_const&& p) = default;
#ifdef PROPAGATE_CONST_HAS_NO_EXPRESSION_SFINAE
//
// Make converting constructors explicit as we cannot use SFINAE to check.
//
template <class U, class = enable_if_t<is_constructible<T, U&&>::value>>
explicit PROPAGATE_CONST_CONSTEXPR propagate_const(propagate_const<U>&& pu)
: t_(std::move(pu.t_))
{
}
template <class U,
class = enable_if_t<is_constructible<T, U&&>::value &&
!is_propagate_const<decay_t<U>>::value>>
explicit PROPAGATE_CONST_CONSTEXPR propagate_const(U&& u)
: t_(std::forward<U>(u))
{
}
#else
//
// Use SFINAE to check if converting constructor should be explicit.
//
template <class U, enable_if_t<!is_convertible<U&&, T>::value &&
is_constructible<T, U&&>::value,
bool> = true>
explicit PROPAGATE_CONST_CONSTEXPR propagate_const(propagate_const<U>&& pu)
: t_(std::move(pu.t_)) {}
template <class U, enable_if_t<is_convertible<U&&, T>::value &&
is_constructible<T, U&&>::value,
bool> = false>
PROPAGATE_CONST_CONSTEXPR propagate_const(propagate_const<U>&& pu) : t_(std::move(pu.t_)) {}
template <class U, enable_if_t<!is_convertible<U&&, T>::value &&
is_constructible<T, U&&>::value &&
!is_propagate_const<decay_t<U>>::value,
bool> = true>
explicit PROPAGATE_CONST_CONSTEXPR propagate_const(U&& u) : t_(std::forward<U>(u)) {}
template <class U, enable_if_t<is_convertible<U&&, T>::value &&
is_constructible<T, U&&>::value &&
!is_propagate_const<decay_t<U>>::value,
bool> = false>
PROPAGATE_CONST_CONSTEXPR propagate_const(U&& u) : t_(std::forward<U>(u)) {}
#endif
// [propagate_const.assignment], assignment
propagate_const& operator=(const propagate_const& p) = delete;
PROPAGATE_CONST_CONSTEXPR propagate_const& operator=(propagate_const&& p) = default;
template <class U>
PROPAGATE_CONST_CONSTEXPR propagate_const& operator=(propagate_const<U>&& pu) {
t_ = std::move(pu.t_);
return *this;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
PROPAGATE_CONST_CONSTEXPR propagate_const& operator=(U&& u) {
t_ = std::move(u);
return *this;
}
// [propagate_const.const_observers], const observers
explicit PROPAGATE_CONST_CONSTEXPR operator bool() const { return get() != nullptr; }
PROPAGATE_CONST_CONSTEXPR const element_type* operator->() const { return get(); }
template <class T_ = T, class U = enable_if_t<is_convertible<
const T_, const element_type*>::value>>
PROPAGATE_CONST_CONSTEXPR operator const element_type*() const // Not always defined
{
return get();
}
PROPAGATE_CONST_CONSTEXPR const element_type& operator*() const { return *get(); }
PROPAGATE_CONST_CONSTEXPR const element_type* get() const { return get_pointer(t_); }
// [propagate_const.non_const_observers], non-const observers
PROPAGATE_CONST_CONSTEXPR element_type* operator->() { return get(); }
template <class T_ = T,
class U = enable_if_t<is_convertible<T_, element_type*>::value>>
PROPAGATE_CONST_CONSTEXPR operator element_type*() // Not always defined
{
return get();
}
PROPAGATE_CONST_CONSTEXPR element_type& operator*() { return *get(); }
PROPAGATE_CONST_CONSTEXPR element_type* get() { return get_pointer(t_); }
// [propagate_const.modifiers], modifiers
PROPAGATE_CONST_CONSTEXPR void swap(propagate_const& pt) noexcept(
noexcept(swap(declval<T&>(), declval<T&>()))) {
swap(t_, pt.t_);
}
private:
T t_;
friend struct std::hash<propagate_const<T>>;
friend struct std::equal_to<propagate_const<T>>;
friend struct std::not_equal_to<propagate_const<T>>;
friend struct std::greater<propagate_const<T>>;
friend struct std::less<propagate_const<T>>;
friend struct std::greater_equal<propagate_const<T>>;
friend struct std::less_equal<propagate_const<T>>;
// [propagate_const.relational], relational operators
friend PROPAGATE_CONST_CONSTEXPR bool operator==(const propagate_const& pt, nullptr_t) {
return pt.t_ == nullptr;
}
friend PROPAGATE_CONST_CONSTEXPR bool operator==(nullptr_t, const propagate_const& pu) {
return nullptr == pu.t_;
}
friend PROPAGATE_CONST_CONSTEXPR bool operator!=(const propagate_const& pt, nullptr_t) {
return pt.t_ != nullptr;
}
friend PROPAGATE_CONST_CONSTEXPR bool operator!=(nullptr_t, const propagate_const& pu) {
return nullptr != pu.t_;
}
template <class U>
friend PROPAGATE_CONST_CONSTEXPR bool operator==(const propagate_const& pt,
const propagate_const<U>& pu) {
return pt.t_ == pu.t_;
}
template <class U>
friend PROPAGATE_CONST_CONSTEXPR bool operator!=(const propagate_const& pt,
const propagate_const<U>& pu) {
return pt.t_ != pu.t_;
}
template <class U>
friend PROPAGATE_CONST_CONSTEXPR bool operator<(const propagate_const& pt,
const propagate_const<U>& pu) {
return pt.t_ < pu.t_;
}
template <class U>
friend PROPAGATE_CONST_CONSTEXPR bool operator>(const propagate_const& pt,
const propagate_const<U>& pu) {
return pt.t_ > pu.t_;
}
template <class U>
friend PROPAGATE_CONST_CONSTEXPR bool operator<=(const propagate_const& pt,
const propagate_const<U>& pu) {
return pt.t_ <= pu.t_;
}
template <class U>
friend PROPAGATE_CONST_CONSTEXPR bool operator>=(const propagate_const& pt,
const propagate_const<U>& pu) {
return pt.t_ >= pu.t_;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator==(const propagate_const& pt, const U& u) {
return pt.t_ == u;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator!=(const propagate_const& pt, const U& u) {
return pt.t_ != u;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator<(const propagate_const& pt, const U& u) {
return pt.t_ < u;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator>(const propagate_const& pt, const U& u) {
return pt.t_ > u;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator<=(const propagate_const& pt, const U& u) {
return pt.t_ <= u;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator>=(const propagate_const& pt, const U& u) {
return pt.t_ >= u;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator==(const U& u, const propagate_const& pu) {
return u == pu.t_;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator!=(const U& u, const propagate_const& pu) {
return u != pu.t_;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator<(const U& u, const propagate_const& pu) {
return u < pu.t_;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator>(const U& u, const propagate_const& pu) {
return u > pu.t_;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator<=(const U& u, const propagate_const& pu) {
return u <= pu.t_;
}
template <class U,
class = enable_if_t<!is_propagate_const<decay_t<U>>::value>>
friend PROPAGATE_CONST_CONSTEXPR bool operator>=(const U& u, const propagate_const& pu) {
return u >= pu.t_;
}
};
// [propagate_const.algorithms], specialized algorithms
template <class T>
PROPAGATE_CONST_CONSTEXPR void swap(propagate_const<T>& pt, propagate_const<T>& pu) noexcept(
noexcept(swap(declval<T&>(), declval<T&>())))
{
swap(pt.underlying_ptr(), pu.underlying_ptr());
}
} // end namespace fundamentals_v2
} // end namespace experimental
// [propagate_const.hash], hash support
template <class T>
struct hash<experimental::fundamentals_v2::propagate_const<T>> {
typedef size_t result_type;
typedef experimental::fundamentals_v2::propagate_const<T> argument_type;
bool operator()(
const experimental::fundamentals_v2::propagate_const<T>& pc) const {
return std::hash<T>()(pc.t_);
}
};
// [propagate_const.comparison_function_objects], comparison function objects
template <class T>
struct equal_to<experimental::fundamentals_v2::propagate_const<T>> {
typedef experimental::fundamentals_v2::propagate_const<T> first_argument_type;
typedef experimental::fundamentals_v2::propagate_const<T>
second_argument_type;
bool operator()(
const experimental::fundamentals_v2::propagate_const<T>& pc1,
const experimental::fundamentals_v2::propagate_const<T>& pc2) const {
return std::equal_to<T>()(pc1.t_, pc2.t_);
}
};
template <class T>
struct not_equal_to<experimental::fundamentals_v2::propagate_const<T>> {
typedef experimental::fundamentals_v2::propagate_const<T> first_argument_type;
typedef experimental::fundamentals_v2::propagate_const<T>
second_argument_type;
bool operator()(
const experimental::fundamentals_v2::propagate_const<T>& pc1,
const experimental::fundamentals_v2::propagate_const<T>& pc2) const {
return std::not_equal_to<T>()(pc1.t_, pc2.t_);
}
};
template <class T>
struct less<experimental::fundamentals_v2::propagate_const<T>> {
typedef experimental::fundamentals_v2::propagate_const<T> first_argument_type;
typedef experimental::fundamentals_v2::propagate_const<T>
second_argument_type;
bool operator()(
const experimental::fundamentals_v2::propagate_const<T>& pc1,
const experimental::fundamentals_v2::propagate_const<T>& pc2) const {
return std::less<T>()(pc1.t_, pc2.t_);
}
};
template <class T>
struct greater<experimental::fundamentals_v2::propagate_const<T>> {
typedef experimental::fundamentals_v2::propagate_const<T> first_argument_type;
typedef experimental::fundamentals_v2::propagate_const<T>
second_argument_type;
bool operator()(
const experimental::fundamentals_v2::propagate_const<T>& pc1,
const experimental::fundamentals_v2::propagate_const<T>& pc2) const {
return std::greater<T>()(pc1.t_, pc2.t_);
}
};
template <class T>
struct less_equal<experimental::fundamentals_v2::propagate_const<T>> {
typedef experimental::fundamentals_v2::propagate_const<T> first_argument_type;
typedef experimental::fundamentals_v2::propagate_const<T>
second_argument_type;
bool operator()(
const experimental::fundamentals_v2::propagate_const<T>& pc1,
const experimental::fundamentals_v2::propagate_const<T>& pc2) const {
return std::less_equal<T>()(pc1.t_, pc2.t_);
}
};
template <class T>
struct greater_equal<experimental::fundamentals_v2::propagate_const<T>> {
typedef experimental::fundamentals_v2::propagate_const<T> first_argument_type;
typedef experimental::fundamentals_v2::propagate_const<T>
second_argument_type;
bool operator()(
const experimental::fundamentals_v2::propagate_const<T>& pc1,
const experimental::fundamentals_v2::propagate_const<T>& pc2) const {
return std::greater_equal<T>()(pc1.t_, pc2.t_);
}
};
} // end namespace std
#undef PROPAGATE_CONST_CONSTEXPR
#endif // JBCOE_PROPAGATE_CONST_INCLUDED