-
Notifications
You must be signed in to change notification settings - Fork 124
/
strong_typedef.hpp
806 lines (722 loc) · 39.4 KB
/
strong_typedef.hpp
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// Copyright (C) 2016-2020 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef TYPE_SAFE_STRONG_TYPEDEF_HPP_INCLUDED
#define TYPE_SAFE_STRONG_TYPEDEF_HPP_INCLUDED
#if defined(TYPE_SAFE_IMPORT_STD_MODULE)
import std;
#else
#include <iosfwd>
#include <iterator>
#include <type_traits>
#include <utility>
#endif
#include <type_safe/config.hpp>
#include <type_safe/detail/all_of.hpp>
#ifdef _MSC_VER
# define TYPE_SAFE_MSC_EMPTY_BASES __declspec(empty_bases)
#else
# define TYPE_SAFE_MSC_EMPTY_BASES
#endif
namespace type_safe
{
/// A strong typedef emulation.
///
/// Unlike regular typedefs, this does create a new type and only allows explicit conversion from
/// the underlying one. The `Tag` is used to differentiate between different strong typedefs to the
/// same type. It is designed to be used as a base class and does not provide any operations by
/// itself. Use the types in the `strong_typedef_op` namespace to generate operations and/or your
/// own member functions.
///
/// Example:
/// ```cpp
/// struct my_handle
/// : strong_typedef<my_handle, void*>,
/// strong_typedef_op::equality_comparison<my_handle>
/// {
/// using strong_typedef::strong_typedef;
/// };
///
/// struct my_int
/// : strong_typedef<my_int, int>,
/// strong_typedef_op::integer_arithmetic<my_int>,
/// strong_typedef_op::equality_comparison<my_int>,
/// strong_typedef_op::relational_comparison<my_int>
/// {
/// using strong_typedef::strong_typedef;
/// };
/// ```
template <class Tag, typename T>
class strong_typedef
{
public:
/// \effects Value initializes the underlying value.
constexpr strong_typedef() : value_() {}
/// \effects Copy (1)/moves (2) the underlying value.
/// \group value_ctor
explicit constexpr strong_typedef(const T& value) : value_(value) {}
/// \group value_ctor
explicit constexpr strong_typedef(T&& value) noexcept(
std::is_nothrow_move_constructible<T>::value)
: value_(static_cast<T&&>(value)) // std::move() might not be constexpr
{}
/// \returns A reference to the stored underlying value.
/// \group value_conv
explicit TYPE_SAFE_CONSTEXPR14 operator T&() TYPE_SAFE_LVALUE_REF noexcept
{
return value_;
}
/// \group value_conv
explicit constexpr operator const T&() const TYPE_SAFE_LVALUE_REF noexcept
{
return value_;
}
#if TYPE_SAFE_USE_REF_QUALIFIERS
/// \group value_conv
explicit TYPE_SAFE_CONSTEXPR14 operator T &&() && noexcept
{
return std::move(value_);
}
/// \group value_conv
explicit constexpr operator const T &&() const&& noexcept
{
return std::move(value_);
}
#endif
friend void swap(strong_typedef& a, strong_typedef& b) noexcept
{
using std::swap;
swap(static_cast<T&>(a), static_cast<T&>(b));
}
T value_;
};
/// \exclude
namespace detail
{
template <class Tag, typename T>
T underlying_type(strong_typedef<Tag, T>);
template <typename... Ts>
struct make_void
{
typedef void type;
};
template <typename... Ts>
using void_t = typename make_void<Ts...>::type;
} // namespace detail
/// The underlying type of the [ts::strong_typedef]().
/// \exclude target
template <class StrongTypedef>
using underlying_type
= decltype(detail::underlying_type(std::declval<typename std::decay<StrongTypedef>::type>()));
/// \group is_strong_typedef
/// Whether a type `T` is a [ts::strong_type]()
template <class T, typename = detail::void_t<>>
struct is_strong_typedef : std::false_type
{};
/// \group is_strong_typedef
template <class T>
struct is_strong_typedef<T, detail::void_t<decltype(detail::underlying_type(std::declval<T>()))>>
: std::true_type
{};
/// Accesses the underlying value.
/// \returns A reference to the underlying value.
/// \group strong_typedef_get
template <class Tag, typename T>
TYPE_SAFE_CONSTEXPR14 T& get(strong_typedef<Tag, T>& type) noexcept
{
return static_cast<T&>(type);
}
/// \group strong_typedef_get
template <class Tag, typename T>
constexpr const T& get(const strong_typedef<Tag, T>& type) noexcept
{
return static_cast<const T&>(type);
}
/// \group strong_typedef_get
template <class Tag, typename T>
TYPE_SAFE_CONSTEXPR14 T&& get(strong_typedef<Tag, T>&& type) noexcept
{
return static_cast<T&&>(static_cast<T&>(type));
}
/// \group strong_typedef_get
template <class Tag, typename T>
constexpr const T&& get(const strong_typedef<Tag, T>&& type) noexcept
{
return static_cast<const T&&>(static_cast<const T&>(type));
}
/// Some operations for [ts::strong_typedef]().
///
/// They all generate operators forwarding to the underlying type.
/// Inherit from them in the typedef definition.
namespace strong_typedef_op
{
/// \exclude
namespace detail
{
template <typename From, typename To>
using enable_if_convertible = typename std::enable_if<
!std::is_same<typename std::decay<From>::type, To>::value
&& !std::is_base_of<To, typename std::decay<From>::type>::value
&& std::is_convertible<typename std::decay<From>::type, To>::value>::type;
template <typename From, typename To>
using enable_if_convertible_same = typename std::enable_if<
std::is_convertible<typename std::decay<From>::type, To>::value>::type;
template <class StrongTypedef>
constexpr const underlying_type<StrongTypedef>& get_underlying(const StrongTypedef& type)
{
return get(type);
}
template <class StrongTypedef>
TYPE_SAFE_CONSTEXPR14 underlying_type<StrongTypedef>& get_underlying(StrongTypedef& type)
{
return get(static_cast<StrongTypedef&>(type));
}
template <class StrongTypedef>
TYPE_SAFE_CONSTEXPR14 underlying_type<StrongTypedef>&& get_underlying(StrongTypedef&& type)
{
return get(static_cast<StrongTypedef&&>(type));
}
// ensure constexpr
template <class T>
constexpr T&& forward(typename std::remove_reference<T>::type& t) noexcept
{
return static_cast<T&&>(t);
}
template <class T>
constexpr T&& forward(typename std::remove_reference<T>::type&& t) noexcept
{
static_assert(!std::is_lvalue_reference<T>::value,
"Can not forward an rvalue as an lvalue.");
return static_cast<T&&>(t);
}
template <class StrongTypedef,
typename = typename std::enable_if<is_strong_typedef<StrongTypedef>::value>::type>
constexpr auto forward_or_underlying(StrongTypedef&& type) noexcept
-> decltype(get(forward<StrongTypedef>(type)))
{
return get(forward<StrongTypedef>(type));
}
template <class T>
constexpr typename std::enable_if<!is_strong_typedef<T>::value, T&&>::type
forward_or_underlying(T&& type) noexcept
{
return detail::forward<T>(type);
}
} // namespace detail
/// \exclude
#define TYPE_SAFE_DETAIL_MAKE_OP(Op, Name, Result) \
/** \exclude */ \
template <class StrongTypedef> \
constexpr Result operator Op(const Name<StrongTypedef>& lhs, const Name<StrongTypedef>& rhs) \
{ \
return Result( \
detail::get_underlying<StrongTypedef>(static_cast<const StrongTypedef&>(lhs)) \
Op detail::get_underlying<StrongTypedef>(static_cast<const StrongTypedef&>(rhs))); \
} \
/** \exclude */ \
template <class StrongTypedef> \
constexpr Result operator Op(Name<StrongTypedef>&& lhs, const Name<StrongTypedef>& rhs) \
{ \
return Result( \
detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(lhs)) \
Op detail::get_underlying<StrongTypedef>(static_cast<const StrongTypedef&>(rhs))); \
} \
/** \exclude */ \
template <class StrongTypedef> \
constexpr Result operator Op(const Name<StrongTypedef>& lhs, Name<StrongTypedef>&& rhs) \
{ \
return Result(detail::get_underlying<StrongTypedef>(static_cast<const StrongTypedef&>( \
lhs)) Op detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(rhs))); \
} \
/** \exclude */ \
template <class StrongTypedef> \
constexpr Result operator Op(Name<StrongTypedef>&& lhs, Name<StrongTypedef>&& rhs) \
{ \
return Result(detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>( \
lhs)) Op detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(rhs))); \
} \
/* mixed */ \
/** \exclude */ \
template <class StrongTypedef, typename Other, \
typename = detail::enable_if_convertible<Other&&, StrongTypedef>> \
constexpr Result operator Op(const Name<StrongTypedef>& lhs, Other&& rhs) \
{ \
return Result(detail::get_underlying<StrongTypedef>(static_cast<const StrongTypedef&>( \
lhs)) Op detail::get_underlying<StrongTypedef>(detail::forward<Other>(rhs))); \
} \
/** \exclude */ \
template <class StrongTypedef, typename Other, \
typename = detail::enable_if_convertible<Other&&, StrongTypedef>> \
constexpr Result operator Op(Name<StrongTypedef>&& lhs, Other&& rhs) \
{ \
return Result(detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(lhs)) \
Op detail::get_underlying<StrongTypedef>(detail::forward<Other>(rhs))); \
} \
/** \exclude */ \
template <class StrongTypedef, typename Other, \
typename = detail::enable_if_convertible<Other&&, StrongTypedef>> \
constexpr Result operator Op(Other&& lhs, const Name<StrongTypedef>& rhs) \
{ \
return Result( \
detail::get_underlying<StrongTypedef>(detail::forward<Other>(lhs)) \
Op detail::get_underlying<StrongTypedef>(static_cast<const StrongTypedef&>(rhs))); \
} \
/** \exclude */ \
template <class StrongTypedef, typename Other, \
typename = detail::enable_if_convertible<Other&&, StrongTypedef>> \
constexpr Result operator Op(Other&& lhs, Name<StrongTypedef>&& rhs) \
{ \
return Result(detail::get_underlying<StrongTypedef>(detail::forward<Other>( \
lhs)) Op detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(rhs))); \
}
/// \exclude
#define TYPE_SAFE_DETAIL_MAKE_OP_STRONGTYPEDEF_OTHER(Op, Name, Result) \
/** \exclude */ \
template <class StrongTypedef, typename OtherArg, typename Other, \
typename = detail::enable_if_convertible_same<Other&&, OtherArg>> \
constexpr Result operator Op(const Name<StrongTypedef, OtherArg>& lhs, Other&& rhs) \
{ \
return Result(get(static_cast<const StrongTypedef&>(lhs)) \
Op detail::forward_or_underlying(detail::forward<Other>(rhs))); \
} \
/** \exclude */ \
template <class StrongTypedef, typename OtherArg, typename Other, \
typename = detail::enable_if_convertible_same<Other&&, OtherArg>> \
constexpr Result operator Op(Name<StrongTypedef, OtherArg>&& lhs, Other&& rhs) \
{ \
return Result(get(static_cast<StrongTypedef&&>(lhs)) \
Op detail::forward_or_underlying(detail::forward<Other>(rhs))); \
}
#define TYPE_SAFE_DETAIL_MAKE_OP_OTHER_STRONGTYPEDEF(Op, Name, Result) \
/** \exclude */ \
template <class StrongTypedef, typename OtherArg, typename Other, \
typename = detail::enable_if_convertible_same<Other&&, OtherArg>> \
constexpr Result operator Op(Other&& lhs, const Name<StrongTypedef, OtherArg>& rhs) \
{ \
return Result(detail::forward_or_underlying(detail::forward<Other>(lhs)) \
Op get(static_cast<const StrongTypedef&>(rhs))); \
} \
/** \exclude */ \
template <class StrongTypedef, typename OtherArg, typename Other, \
typename = detail::enable_if_convertible_same<Other&&, OtherArg>> \
constexpr Result operator Op(Other&& lhs, Name<StrongTypedef, OtherArg>&& rhs) \
{ \
return Result(detail::forward_or_underlying(detail::forward<Other>(lhs)) \
Op get(static_cast<StrongTypedef&&>(rhs))); \
}
#define TYPE_SAFE_DETAIL_MAKE_OP_MIXED(Op, Name, Result) \
TYPE_SAFE_DETAIL_MAKE_OP_STRONGTYPEDEF_OTHER(Op, Name, Result) \
TYPE_SAFE_DETAIL_MAKE_OP_OTHER_STRONGTYPEDEF(Op, Name, Result)
/// \exclude
#define TYPE_SAFE_DETAIL_MAKE_OP_COMPOUND(Op, Name) \
/** \exclude */ \
template <class StrongTypedef> \
TYPE_SAFE_CONSTEXPR14 StrongTypedef& operator Op(Name<StrongTypedef>& lhs, \
const Name<StrongTypedef>& rhs) \
{ \
detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&>(lhs)) \
Op detail::get_underlying<StrongTypedef>(static_cast<const StrongTypedef&>(rhs)); \
return static_cast<StrongTypedef&>(lhs); \
} /** \exclude */ \
template <class StrongTypedef> \
TYPE_SAFE_CONSTEXPR14 StrongTypedef& operator Op(Name<StrongTypedef>& lhs, \
Name<StrongTypedef>&& rhs) \
{ \
detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&>(lhs)) \
Op detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(rhs)); \
return static_cast<StrongTypedef&>(lhs); \
} \
/** \exclude */ \
template <class StrongTypedef> \
TYPE_SAFE_CONSTEXPR14 StrongTypedef&& operator Op(Name<StrongTypedef>&& lhs, \
const Name<StrongTypedef>& rhs) \
{ \
detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(lhs)) \
Op detail::get_underlying<StrongTypedef>(static_cast<const StrongTypedef&>(rhs)); \
return static_cast<StrongTypedef&&>(lhs); \
} /** \exclude */ \
template <class StrongTypedef> \
TYPE_SAFE_CONSTEXPR14 StrongTypedef&& operator Op(Name<StrongTypedef>&& lhs, \
Name<StrongTypedef>&& rhs) \
{ \
detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(lhs)) \
Op detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(rhs)); \
return static_cast<StrongTypedef&&>(lhs); \
} \
/* mixed */ \
/** \exclude */ \
template <class StrongTypedef, typename Other, \
typename = detail::enable_if_convertible<Other&&, StrongTypedef>> \
TYPE_SAFE_CONSTEXPR14 StrongTypedef& operator Op(Name<StrongTypedef>& lhs, Other&& rhs) \
{ \
detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&>(lhs)) \
Op detail::get_underlying<StrongTypedef>(detail::forward<Other>(rhs)); \
return static_cast<StrongTypedef&>(lhs); \
} \
/** \exclude */ \
template <class StrongTypedef, typename Other, \
typename = detail::enable_if_convertible<Other&&, StrongTypedef>> \
TYPE_SAFE_CONSTEXPR14 StrongTypedef&& operator Op(Name<StrongTypedef>&& lhs, Other&& rhs) \
{ \
detail::get_underlying<StrongTypedef>(static_cast<StrongTypedef&&>(lhs)) \
Op detail::get_underlying<StrongTypedef>(detail::forward<Other>(rhs)); \
return static_cast<StrongTypedef&&>(lhs); \
}
/// \exclude
#define TYPE_SAFE_DETAIL_MAKE_OP_COMPOUND_MIXED(Op, Name) \
/** \exclude */ \
template <class StrongTypedef, typename OtherArg, typename Other, \
typename = detail::enable_if_convertible_same<Other&&, OtherArg>> \
TYPE_SAFE_CONSTEXPR14 StrongTypedef& operator Op(Name<StrongTypedef, OtherArg>& lhs, \
Other&& rhs) \
{ \
get(static_cast<StrongTypedef&>(lhs)) \
Op detail::forward_or_underlying(detail::forward<Other>(rhs)); \
return static_cast<StrongTypedef&>(lhs); \
} \
/** \exclude */ \
template <class StrongTypedef, typename OtherArg, typename Other, \
typename = detail::enable_if_convertible_same<Other&&, OtherArg>> \
TYPE_SAFE_CONSTEXPR14 StrongTypedef&& operator Op(Name<StrongTypedef, OtherArg>&& lhs, \
Other&& rhs) \
{ \
get(static_cast<StrongTypedef&&>(lhs)) \
Op detail::forward_or_underlying(detail::forward<Other>(rhs)); \
return static_cast<StrongTypedef&&>(lhs); \
}
/// \exclude
#define TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP(Name, Op) \
template <class StrongTypedef> \
struct TYPE_SAFE_MSC_EMPTY_BASES Name \
{}; \
TYPE_SAFE_DETAIL_MAKE_OP(Op, Name, StrongTypedef) \
TYPE_SAFE_DETAIL_MAKE_OP_COMPOUND(Op## =, Name) \
template <class StrongTypedef, typename Other> \
struct TYPE_SAFE_MSC_EMPTY_BASES mixed_##Name \
{}; \
TYPE_SAFE_DETAIL_MAKE_OP_MIXED(Op, mixed_##Name, StrongTypedef) \
TYPE_SAFE_DETAIL_MAKE_OP_COMPOUND_MIXED(Op## =, mixed_##Name) \
template <class StrongTypedef, typename Other> \
struct TYPE_SAFE_MSC_EMPTY_BASES mixed_##Name##_noncommutative \
{}; \
TYPE_SAFE_DETAIL_MAKE_OP_STRONGTYPEDEF_OTHER(Op, mixed_##Name##_noncommutative, StrongTypedef) \
TYPE_SAFE_DETAIL_MAKE_OP_COMPOUND_MIXED(Op## =, mixed_##Name##_noncommutative)
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES equality_comparison
{};
TYPE_SAFE_DETAIL_MAKE_OP(==, equality_comparison, bool)
TYPE_SAFE_DETAIL_MAKE_OP(!=, equality_comparison, bool)
template <class StrongTypedef, typename Other>
struct TYPE_SAFE_MSC_EMPTY_BASES mixed_equality_comparison
{};
TYPE_SAFE_DETAIL_MAKE_OP_MIXED(==, mixed_equality_comparison, bool)
TYPE_SAFE_DETAIL_MAKE_OP_MIXED(!=, mixed_equality_comparison, bool)
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES relational_comparison
{};
TYPE_SAFE_DETAIL_MAKE_OP(<, relational_comparison, bool)
TYPE_SAFE_DETAIL_MAKE_OP(<=, relational_comparison, bool)
TYPE_SAFE_DETAIL_MAKE_OP(>, relational_comparison, bool)
TYPE_SAFE_DETAIL_MAKE_OP(>=, relational_comparison, bool)
template <class StrongTypedef, typename Other>
struct TYPE_SAFE_MSC_EMPTY_BASES mixed_relational_comparison
{};
TYPE_SAFE_DETAIL_MAKE_OP_MIXED(<, mixed_relational_comparison, bool)
TYPE_SAFE_DETAIL_MAKE_OP_MIXED(<=, mixed_relational_comparison, bool)
TYPE_SAFE_DETAIL_MAKE_OP_MIXED(>, mixed_relational_comparison, bool)
TYPE_SAFE_DETAIL_MAKE_OP_MIXED(>=, mixed_relational_comparison, bool)
TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP(addition, +)
TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP(subtraction, -)
TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP(multiplication, *)
TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP(division, /)
TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP(modulo, %)
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES explicit_bool
{
/// \exclude
explicit constexpr operator bool() const
{
return static_cast<bool>(
detail::get_underlying<StrongTypedef>(static_cast<const StrongTypedef&>(*this)));
}
};
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES increment
{
/// \exclude
TYPE_SAFE_CONSTEXPR14 StrongTypedef& operator++()
{
using type = underlying_type<StrongTypedef>;
++static_cast<type&>(static_cast<StrongTypedef&>(*this));
return static_cast<StrongTypedef&>(*this);
}
/// \exclude
TYPE_SAFE_CONSTEXPR14 StrongTypedef operator++(int)
{
auto result = static_cast<StrongTypedef&>(*this);
++*this;
return result;
}
};
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES decrement
{
/// \exclude
TYPE_SAFE_CONSTEXPR14 StrongTypedef& operator--()
{
using type = underlying_type<StrongTypedef>;
--static_cast<type&>(static_cast<StrongTypedef&>(*this));
return static_cast<StrongTypedef&>(*this);
}
/// \exclude
TYPE_SAFE_CONSTEXPR14 StrongTypedef operator--(int)
{
auto result = static_cast<StrongTypedef&>(*this);
--*this;
return result;
}
};
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES unary_plus
{};
/// \exclude
template <class StrongTypedef>
constexpr StrongTypedef operator+(const unary_plus<StrongTypedef>& lhs)
{
return StrongTypedef(+get(static_cast<const StrongTypedef&>(lhs)));
}
/// \exclude
template <class StrongTypedef>
constexpr StrongTypedef operator+(unary_plus<StrongTypedef>&& lhs)
{
return StrongTypedef(+get(static_cast<StrongTypedef&&>(lhs)));
}
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES unary_minus
{};
/// \exclude
template <class StrongTypedef>
constexpr StrongTypedef operator-(const unary_minus<StrongTypedef>& lhs)
{
return StrongTypedef(-get(static_cast<const StrongTypedef&>(lhs)));
}
/// \exclude
template <class StrongTypedef>
constexpr StrongTypedef operator-(unary_minus<StrongTypedef>&& lhs)
{
return StrongTypedef(-get(static_cast<StrongTypedef&&>(lhs)));
}
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES integer_arithmetic : unary_plus<StrongTypedef>,
unary_minus<StrongTypedef>,
addition<StrongTypedef>,
subtraction<StrongTypedef>,
multiplication<StrongTypedef>,
division<StrongTypedef>,
modulo<StrongTypedef>,
increment<StrongTypedef>,
decrement<StrongTypedef>
{};
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES floating_point_arithmetic : unary_plus<StrongTypedef>,
unary_minus<StrongTypedef>,
addition<StrongTypedef>,
subtraction<StrongTypedef>,
multiplication<StrongTypedef>,
division<StrongTypedef>
{};
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES complement
{};
/// \exclude
template <class StrongTypedef>
constexpr StrongTypedef operator~(const complement<StrongTypedef>& lhs)
{
return StrongTypedef(~get(static_cast<const StrongTypedef&>(lhs)));
}
/// \exclude
template <class StrongTypedef>
constexpr StrongTypedef operator~(complement<StrongTypedef>&& lhs)
{
return StrongTypedef(~get(static_cast<StrongTypedef&&>(lhs)));
}
TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP(bitwise_or, |)
TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP(bitwise_xor, ^)
TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP(bitwise_and, &)
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES bitmask : complement<StrongTypedef>,
bitwise_or<StrongTypedef>,
bitwise_xor<StrongTypedef>,
bitwise_and<StrongTypedef>
{};
template <class StrongTypedef, typename IntT>
struct TYPE_SAFE_MSC_EMPTY_BASES bitshift
{};
TYPE_SAFE_DETAIL_MAKE_OP_MIXED(<<, bitshift, StrongTypedef)
TYPE_SAFE_DETAIL_MAKE_OP_MIXED(>>, bitshift, StrongTypedef)
TYPE_SAFE_DETAIL_MAKE_OP_COMPOUND_MIXED(<<=, bitshift)
TYPE_SAFE_DETAIL_MAKE_OP_COMPOUND_MIXED(>>=, bitshift)
template <class StrongTypedef, typename Result, typename ResultPtr = Result*,
typename ResultConstPtr = const Result*>
struct TYPE_SAFE_MSC_EMPTY_BASES dereference
{
/// \exclude
Result& operator*()
{
using type = underlying_type<StrongTypedef>;
return *static_cast<type&>(static_cast<StrongTypedef&>(*this));
}
/// \exclude
const Result& operator*() const
{
using type = underlying_type<StrongTypedef>;
return *static_cast<const type&>(static_cast<const StrongTypedef&>(*this));
}
/// \exclude
ResultPtr operator->()
{
using type = underlying_type<StrongTypedef>;
return static_cast<type&>(static_cast<StrongTypedef&>(*this));
}
/// \exclude
ResultConstPtr operator->() const
{
using type = underlying_type<StrongTypedef>;
return static_cast<const type&>(static_cast<const StrongTypedef&>(*this));
}
};
template <class StrongTypedef, typename Result, typename Index = std::size_t>
struct TYPE_SAFE_MSC_EMPTY_BASES array_subscript
{
/// \exclude
Result& operator[](const Index& i)
{
using type = underlying_type<StrongTypedef>;
return static_cast<type&>(static_cast<StrongTypedef&>(*this))[i];
}
/// \exclude
const Result& operator[](const Index& i) const
{
using type = underlying_type<StrongTypedef>;
return static_cast<const type&>(static_cast<const StrongTypedef&>(*this))[i];
}
};
template <class StrongTypedef, class Category, typename T, typename Distance = std::ptrdiff_t>
struct TYPE_SAFE_MSC_EMPTY_BASES iterator : dereference<StrongTypedef, T, T*, const T*>,
increment<StrongTypedef>
{
using iterator_category = Category;
using value_type = typename std::remove_cv<T>::type;
using difference_type = Distance;
using pointer = T*;
using reference = T&;
};
template <class StrongTypedef, typename T, typename Distance = std::ptrdiff_t>
struct TYPE_SAFE_MSC_EMPTY_BASES input_iterator : iterator<StrongTypedef, std::input_iterator_tag, T, Distance>,
equality_comparison<StrongTypedef>
{};
template <class StrongTypedef, typename T, typename Distance = std::ptrdiff_t>
struct TYPE_SAFE_MSC_EMPTY_BASES output_iterator : iterator<StrongTypedef, std::output_iterator_tag, T, Distance>
{};
template <class StrongTypedef, typename T, typename Distance = std::ptrdiff_t>
struct TYPE_SAFE_MSC_EMPTY_BASES forward_iterator : input_iterator<StrongTypedef, T, Distance>
{
using iterator_category = std::forward_iterator_tag;
};
template <class StrongTypedef, typename T, typename Distance = std::ptrdiff_t>
struct TYPE_SAFE_MSC_EMPTY_BASES bidirectional_iterator : forward_iterator<StrongTypedef, T, Distance>,
decrement<StrongTypedef>
{
using iterator_category = std::bidirectional_iterator_tag;
};
template <class StrongTypedef, typename T, typename Distance = std::ptrdiff_t>
struct TYPE_SAFE_MSC_EMPTY_BASES random_access_iterator : bidirectional_iterator<StrongTypedef, T, Distance>,
array_subscript<StrongTypedef, T, Distance>,
relational_comparison<StrongTypedef>
{
using iterator_category = std::random_access_iterator_tag;
/// \exclude
StrongTypedef& operator+=(const Distance& d)
{
using type = underlying_type<StrongTypedef>;
static_cast<type&>(static_cast<StrongTypedef&>(*this)) += d;
return static_cast<StrongTypedef&>(*this);
}
/// \exclude
StrongTypedef& operator-=(const Distance& d)
{
using type = underlying_type<StrongTypedef>;
static_cast<type&>(static_cast<StrongTypedef&>(*this)) -= d;
return static_cast<StrongTypedef&>(*this);
}
/// \exclude
friend StrongTypedef operator+(const StrongTypedef& iter, const Distance& n)
{
using type = underlying_type<StrongTypedef>;
return StrongTypedef(static_cast<const type&>(iter) + n);
}
/// \exclude
friend StrongTypedef operator+(const Distance& n, const StrongTypedef& iter)
{
return iter + n;
}
/// \exclude
friend StrongTypedef operator-(const StrongTypedef& iter, const Distance& n)
{
using type = underlying_type<StrongTypedef>;
return StrongTypedef(static_cast<const type&>(iter) - n);
}
/// \exclude
friend Distance operator-(const StrongTypedef& lhs, const StrongTypedef& rhs)
{
using type = underlying_type<StrongTypedef>;
return static_cast<const type&>(lhs) - static_cast<const type&>(rhs);
}
};
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES input_operator
{
/// \exclude
template <typename Char, class CharTraits>
friend std::basic_istream<Char, CharTraits>& operator>>(
std::basic_istream<Char, CharTraits>& in, StrongTypedef& val)
{
using type = underlying_type<StrongTypedef>;
return in >> static_cast<type&>(val);
}
};
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES output_operator
{
/// \exclude
template <typename Char, class CharTraits>
friend std::basic_ostream<Char, CharTraits>& operator<<(
std::basic_ostream<Char, CharTraits>& out, const StrongTypedef& val)
{
using type = underlying_type<StrongTypedef>;
return out << static_cast<const type&>(val);
}
};
#undef TYPE_SAFE_DETAIL_MAKE_OP
#undef TYPE_SAFE_DETAIL_MAKE_OP_MIXED
#undef TYPE_SAFE_DETAIL_MAKE_OP_STRONGTYPEDEF_OTHER
#undef TYPE_SAFE_DETAIL_MAKE_OP_OTHER_STRONGTYPEDEF
#undef TYPE_SAFE_DETAIL_MAKE_OP_COMPOUND
#undef TYPE_SAFE_DETAIL_MAKE_STRONG_TYPEDEF_OP
} // namespace strong_typedef_op
/// Inherit from it in the `std::hash<StrongTypedef>` specialization to make
/// it hashable like the underlying type. See example/strong_typedef.cpp.
template <class StrongTypedef>
struct TYPE_SAFE_MSC_EMPTY_BASES hashable : std::hash<type_safe::underlying_type<StrongTypedef>>
{
using underlying_type = type_safe::underlying_type<StrongTypedef>;
using underlying_hash = std::hash<underlying_type>;
std::size_t operator()(const StrongTypedef& lhs) const
noexcept(noexcept(underlying_hash{}(std::declval<underlying_type>())))
{
return underlying_hash{}(static_cast<const underlying_type&>(lhs));
}
};
} // namespace type_safe
#undef TYPE_SAFE_MSC_EMPTY_BASES
#endif // TYPE_SAFE_STRONG_TYPEDEF_HPP_INCLUDED