-
Notifications
You must be signed in to change notification settings - Fork 12
/
strong_typedef.hpp
752 lines (701 loc) · 36.9 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
#ifndef JSS_STRONG_TYPEDEF_HPP
#define JSS_STRONG_TYPEDEF_HPP
#include <type_traits>
#include <utility>
#include <functional>
#include <ostream>
namespace jss {
/// Internal implementation namespace
namespace detail {
/// A small type used for operation tests
using small_result= char;
/// A large type with a different size to the small type, used for
/// operation tests
struct large_result {
small_result dummy[2];
};
} // namespace detail
/// The strong_typedef template used to create unique types with
/// specific properties.
///
/// Tag is a tag type used for uniqueness. It can be incomplete as
/// it is never used for anything except creating unique template
/// instantiations
///
/// ValueType is the type this Strong typedef is based on: it is the type of
/// the underlying value
///
/// Properties are types that provide mixins that enable certain operations
/// on values of this type.
template <typename Tag, typename ValueType, typename... Properties>
class strong_typedef
: public Properties::template mixin<
strong_typedef<Tag, ValueType, Properties...>, ValueType>... {
public:
/// The underlying value type
using underlying_value_type= ValueType;
/// A default constructed strong_typedef has a default-constructed value
constexpr strong_typedef() noexcept : value() {}
/// Construct a strong_typedef holding the specified value
explicit constexpr strong_typedef(ValueType value_) noexcept(
std::is_nothrow_move_constructible<ValueType>::value) :
value(std::move(value_)) {}
/// Explicit conversion operator to read the underlying value
explicit constexpr operator ValueType const &() const noexcept {
return value;
}
/// Get a const reference to the underlying value
constexpr ValueType const &underlying_value() const noexcept {
return value;
}
/// Get a reference to the underlying value
constexpr ValueType &underlying_value() noexcept {
return value;
}
/// Get a reference to the underlying value
friend constexpr ValueType &underlying_value(strong_typedef &t) {
return t.underlying_value();
}
/// Get a const reference to the underlying value
friend constexpr ValueType const &
underlying_value(strong_typedef const &t) {
return t.underlying_value();
}
/// Get a const rvalue reference to the underlying value
friend constexpr ValueType &&underlying_value(strong_typedef &&t) {
return std::move(t.underlying_value());
}
/// Get an rvalue reference to the underlying value
friend constexpr ValueType const &&
underlying_value(strong_typedef const &&t) {
return std::move(t.underlying_value());
}
private:
/// The underlying value
ValueType value;
};
/// The underlying value of a value that is not a strong_typedef is just
/// that value
template <typename T> constexpr T &&underlying_value(T &&t) {
return std::forward<T>(t);
}
/// Namespace to wrap the property types
namespace strong_typedef_properties {
/// Add operator== and operator!= to the strong_typedef
struct equality_comparable {
template <typename Derived, typename ValueType> struct mixin {
friend constexpr bool
operator==(Derived const &lhs, Derived const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() ==
std::declval<ValueType const &>())) {
return lhs.underlying_value() == rhs.underlying_value();
}
friend constexpr bool
operator!=(Derived const &lhs, Derived const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() !=
std::declval<ValueType const &>())) {
return lhs.underlying_value() != rhs.underlying_value();
}
};
};
/// Add the preincrement operator to the strong_typedef
struct pre_incrementable {
template <typename Derived, typename ValueType> struct mixin {
friend Derived &operator++(Derived &self) noexcept(
noexcept(++std::declval<ValueType &>())) {
++self.underlying_value();
return self;
}
};
};
/// Add the post-increment operator to the strong_typedef
struct post_incrementable {
template <typename Derived, typename ValueType> struct mixin {
friend Derived operator++(Derived &self, int) noexcept(
noexcept(std::declval<ValueType &>()++)) {
return Derived{self.underlying_value()++};
}
};
};
/// Add both pre- and post-increment operators to the strong_typedef
struct incrementable {
template <typename Derived, typename ValueType>
struct mixin : pre_incrementable::mixin<Derived, ValueType>,
post_incrementable::mixin<Derived, ValueType> {};
};
/// Add the pre-decrement operator to the strong_typedef
struct pre_decrementable {
template <typename Derived, typename ValueType> struct mixin {
friend Derived &operator--(Derived &self) noexcept(
noexcept(--std::declval<ValueType &>())) {
--self.underlying_value();
return self;
}
};
};
/// Add the post-decrement operator to the strong_typedef
struct post_decrementable {
template <typename Derived, typename ValueType> struct mixin {
friend Derived operator--(Derived &self, int) noexcept(
noexcept(std::declval<ValueType &>()--)) {
return Derived{self.underlying_value()--};
}
};
};
/// Add both pre- and post-decrement operators to the strong_typedef
struct decrementable {
template <typename Derived, typename ValueType>
struct mixin : pre_decrementable::mixin<Derived, ValueType>,
post_decrementable::mixin<Derived, ValueType> {};
};
/// Add operator+ that supports adding the strong_typedef to anything
/// the underlying value can be added to
struct generic_mixed_addable {
template <typename Derived, typename ValueType> struct mixin {
template <typename Rhs>
friend typename std::enable_if<
!std::is_same<Rhs, Derived>::value &&
std::is_convertible<
decltype(
std::declval<ValueType const &>() +
underlying_value(std::declval<Rhs const &>())),
ValueType>::value,
Derived>::type
operator+(Derived const &lhs, Rhs const &rhs) noexcept(noexcept(
std::declval<ValueType const &>() +
underlying_value(std::declval<Rhs const &>()))) {
return Derived{lhs.underlying_value() +
underlying_value(rhs)};
}
template <typename Lhs>
friend typename std::enable_if<
!std::is_same<Lhs, Derived>::value &&
std::is_convertible<
decltype(
underlying_value(std::declval<Lhs const &>()) +
std::declval<ValueType const &>()),
ValueType>::value,
Derived>::type
operator+(Lhs const &lhs, Derived const &rhs) noexcept(noexcept(
underlying_value(std::declval<Lhs const &>()) +
std::declval<ValueType const &>())) {
return Derived{underlying_value(lhs) +
rhs.underlying_value()};
}
};
};
#define JSS_COMPOUND_ASSIGN(op_symbol) op_symbol##=
#define JSS_DEFINE_OP_MIXINS(name, op_symbol) \
/** Add operator op_symbol to the strong_typedef **/ \
template <typename Other> struct mixed_##name { \
template < \
typename Derived, typename ValueType, \
bool= std::is_literal_type<ValueType>::value> \
struct mixin { \
friend constexpr Derived \
operator op_symbol(Derived const &lhs, Other const &rhs) noexcept( \
noexcept(std::declval<ValueType const &>() \
op_symbol underlying_value( \
std::declval<Other const &>()))) { \
return Derived{lhs.underlying_value() \
op_symbol underlying_value(rhs)}; \
} \
\
friend constexpr Derived \
operator op_symbol(Other const &lhs, Derived const &rhs) noexcept( \
noexcept(underlying_value(std::declval<Other const &>()) \
op_symbol std::declval<ValueType const &>())) { \
return Derived{underlying_value(lhs) \
op_symbol rhs.underlying_value()}; \
} \
\
friend Derived &operator JSS_COMPOUND_ASSIGN(op_symbol)( \
Derived &lhs, \
Other const \
&rhs) noexcept(noexcept(std::declval<ValueType &>() \
JSS_COMPOUND_ASSIGN(op_symbol) \
underlying_value( \
std::declval< \
Other const \
&>()))) { \
lhs.underlying_value() JSS_COMPOUND_ASSIGN(op_symbol) \
underlying_value(rhs); \
return lhs; \
} \
}; \
}; \
template <typename Other> \
template <typename Derived, typename ValueType> \
struct mixed_##name<Other>::mixin<Derived, ValueType, false> { \
friend Derived \
operator op_symbol(Derived const &lhs, Other const &rhs) noexcept( \
noexcept(std::declval<ValueType const &>() \
op_symbol underlying_value( \
std::declval<Other const &>()))) { \
return Derived{lhs.underlying_value() \
op_symbol underlying_value(rhs)}; \
} \
\
friend Derived \
operator op_symbol(Other const &lhs, Derived const &rhs) noexcept( \
noexcept(underlying_value(std::declval<Other const &>()) \
op_symbol std::declval<ValueType const &>())) { \
return Derived{underlying_value(lhs) \
op_symbol rhs.underlying_value()}; \
} \
\
friend Derived &operator JSS_COMPOUND_ASSIGN(op_symbol)( \
Derived &lhs, \
Other const \
&rhs) noexcept(noexcept(std::declval<ValueType &>() \
JSS_COMPOUND_ASSIGN(op_symbol) \
underlying_value( \
std::declval< \
Other const &>()))) { \
lhs.underlying_value() JSS_COMPOUND_ASSIGN(op_symbol) \
underlying_value(rhs); \
return lhs; \
} \
}; \
struct self_##name { \
template < \
typename Derived, typename ValueType, \
bool= std::is_literal_type<ValueType>::value> \
struct mixin { \
friend constexpr Derived operator op_symbol( \
Derived const &lhs, \
Derived const \
&rhs) noexcept(noexcept(std::declval<ValueType const &>() \
op_symbol std::declval< \
ValueType const &>())) { \
return Derived{lhs.underlying_value() \
op_symbol rhs.underlying_value()}; \
} \
friend Derived &operator JSS_COMPOUND_ASSIGN(op_symbol)( \
Derived &lhs, \
Derived const \
&rhs) noexcept(noexcept(std::declval<ValueType &>() \
JSS_COMPOUND_ASSIGN(op_symbol) \
std::declval< \
ValueType const \
&>())) { \
lhs.underlying_value() JSS_COMPOUND_ASSIGN(op_symbol) \
rhs.underlying_value(); \
return lhs; \
} \
}; \
}; \
\
template <typename Derived, typename ValueType> \
struct self_##name::mixin<Derived, ValueType, false> { \
friend Derived \
operator op_symbol(Derived const &lhs, Derived const &rhs) noexcept( \
noexcept(std::declval<ValueType const &>() \
op_symbol std::declval<ValueType const &>())) { \
return Derived{lhs.underlying_value() \
op_symbol rhs.underlying_value()}; \
} \
friend Derived &operator JSS_COMPOUND_ASSIGN(op_symbol)( \
Derived &lhs, \
Derived const \
&rhs) noexcept(noexcept(std::declval<ValueType &>() \
JSS_COMPOUND_ASSIGN(op_symbol) \
std::declval< \
ValueType const &>())) { \
lhs.underlying_value() JSS_COMPOUND_ASSIGN(op_symbol) \
rhs.underlying_value(); \
return lhs; \
} \
}; \
struct name { \
template <typename Derived, typename ValueType> \
struct mixin \
: self_##name::mixin<Derived, ValueType>, \
mixed_##name<ValueType>::template mixin<Derived, ValueType> {}; \
};
/// Define mixins for the built-in operators
///
/// self_xxx provides the operation where both LHS and RHS are
/// the strong_typedef
///
/// mixed_xxx<Other> provides the operation where only the LHS or RHS
/// is the strong_typedef and the other operand is of type Other
///
/// xxx combines self_xxx and mixed_xxx<underlying_value_type>
///
JSS_DEFINE_OP_MIXINS(addable, +)
JSS_DEFINE_OP_MIXINS(subtractable, -)
JSS_DEFINE_OP_MIXINS(multiplicable, *)
JSS_DEFINE_OP_MIXINS(divisible, /)
JSS_DEFINE_OP_MIXINS(modulus, %)
JSS_DEFINE_OP_MIXINS(bitwise_or, |)
JSS_DEFINE_OP_MIXINS(bitwise_and, &)
JSS_DEFINE_OP_MIXINS(bitwise_xor, ^)
/// Allow subtraction with any type that can be subtracted from the
/// underlying value, or the underlying value can be subtracted from
struct generic_mixed_subtractable {
template <typename Derived, typename ValueType> struct mixin {
template <typename Rhs>
friend typename std::enable_if<
!std::is_same<Rhs, Derived>::value &&
std::is_convertible<
decltype(
std::declval<ValueType const &>() -
underlying_value(std::declval<Rhs const &>())),
ValueType>::value,
Derived>::type
operator-(Derived const &lhs, Rhs const &rhs) noexcept(noexcept(
std::declval<ValueType const &>() -
underlying_value(std::declval<Rhs const &>()))) {
return Derived{lhs.underlying_value() -
underlying_value(rhs)};
}
template <typename Lhs>
friend typename std::enable_if<
!std::is_same<Lhs, Derived>::value &&
std::is_convertible<
decltype(
underlying_value(std::declval<Lhs const &>()) -
std::declval<ValueType const &>()),
ValueType>::value,
Derived>::type
operator-(Lhs const &lhs, Derived const &rhs) noexcept(noexcept(
underlying_value(std::declval<Lhs const &>()) -
std::declval<ValueType const &>())) {
return Derived{underlying_value(lhs) -
rhs.underlying_value()};
}
};
};
/// Allow subtracting two strong_typedef instances to produce
/// a DifferenceType value tha represents the difference
template <typename DifferenceType> struct difference {
template <typename Derived, typename ValueType> struct mixin {
friend DifferenceType
operator-(Derived const &lhs, Derived const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() -
std::declval<ValueType const &>())) {
return DifferenceType{lhs.underlying_value() -
rhs.underlying_value()};
}
};
};
/// Add ordering comparison operators to the strong_typedef
struct ordered {
template <typename Derived, typename ValueType> struct mixin {
friend constexpr bool
operator<(Derived const &lhs, Derived const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() <
std::declval<ValueType const &>())) {
return lhs.underlying_value() < rhs.underlying_value();
}
friend constexpr bool
operator>(Derived const &lhs, Derived const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() >
std::declval<ValueType const &>())) {
return lhs.underlying_value() > rhs.underlying_value();
}
friend constexpr bool
operator<=(Derived const &lhs, Derived const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() <=
std::declval<ValueType const &>())) {
return lhs.underlying_value() <= rhs.underlying_value();
}
friend constexpr bool
operator>=(Derived const &lhs, Derived const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() >=
std::declval<ValueType const &>())) {
return lhs.underlying_value() >= rhs.underlying_value();
}
};
};
/// Add ordering comparisons to the strong_typedef where the
/// other operand is of type Other
template <typename Other> struct mixed_ordered {
template <typename Derived, typename ValueType> struct mixin {
friend typename std::enable_if<
!std::is_same<Other, Derived>::value &&
std::is_convertible<
decltype(
std::declval<ValueType const &>() <
underlying_value(
std::declval<Other const &>())),
bool>::value,
bool>::type
operator<(Derived const &lhs, Other const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() <
underlying_value(std::declval<Other const &>()))) {
return lhs.underlying_value() < underlying_value(rhs);
}
friend typename std::enable_if<
!std::is_same<Other, Derived>::value &&
std::is_convertible<
decltype(
underlying_value(
std::declval<Other const &>()) <
std::declval<ValueType const &>()),
bool>::value,
bool>::type
operator<(Other const &lhs, Derived const &rhs) noexcept(
noexcept(
underlying_value(std::declval<Other const &>()) <
std::declval<ValueType const &>())) {
return underlying_value(lhs) < rhs.underlying_value();
}
friend typename std::enable_if<
!std::is_same<Other, Derived>::value &&
std::is_convertible<
decltype(
std::declval<ValueType const &>() >
underlying_value(
std::declval<Other const &>())),
bool>::value,
bool>::type
operator>(Derived const &lhs, Other const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() >
underlying_value(std::declval<Other const &>()))) {
return lhs.underlying_value() > underlying_value(rhs);
}
friend typename std::enable_if<
!std::is_same<Other, Derived>::value &&
std::is_convertible<
decltype(
underlying_value(
std::declval<Other const &>()) >
std::declval<ValueType const &>()),
bool>::value,
bool>::type
operator>(Other const &lhs, Derived const &rhs) noexcept(
noexcept(
underlying_value(std::declval<Other const &>()) >
std::declval<ValueType const &>())) {
return underlying_value(lhs) > rhs.underlying_value();
}
friend typename std::enable_if<
!std::is_same<Other, Derived>::value &&
std::is_convertible<
decltype(
std::declval<ValueType const &>() >=
underlying_value(
std::declval<Other const &>())),
bool>::value,
bool>::type
operator>=(Derived const &lhs, Other const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() >=
std::declval<Other const &>())) {
return lhs.underlying_value() >= underlying_value(rhs);
}
friend typename std::enable_if<
!std::is_same<Other, Derived>::value &&
std::is_convertible<
decltype(
underlying_value(
std::declval<Other const &>()) >=
std::declval<ValueType const &>()),
bool>::value,
bool>::type
operator>=(Other const &lhs, Derived const &rhs) noexcept(
noexcept(
underlying_value(std::declval<Other const &>()) >=
std::declval<ValueType const &>())) {
return underlying_value(lhs) >= rhs.underlying_value();
}
friend typename std::enable_if<
!std::is_same<Other, Derived>::value &&
std::is_convertible<
decltype(
std::declval<ValueType const &>() <=
underlying_value(
std::declval<Other const &>())),
bool>::value,
bool>::type
operator<=(Derived const &lhs, Other const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() <=
underlying_value(std::declval<Other const &>()))) {
return lhs.underlying_value() <= underlying_value(rhs);
}
friend typename std::enable_if<
!std::is_same<Other, Derived>::value &&
std::is_convertible<
decltype(
underlying_value(
std::declval<Other const &>()) <=
std::declval<ValueType const &>()),
bool>::value,
bool>::type
operator<=(Other const &lhs, Derived const &rhs) noexcept(
noexcept(
underlying_value(std::declval<Other const &>()) <=
std::declval<ValueType const &>())) {
return underlying_value(lhs) <= rhs.underlying_value();
}
};
};
/// Allow this strong_typedef to be used with std::hash
struct hashable {
struct base {};
template <typename Derived, typename ValueType>
struct mixin : base {};
};
/// Add a stream operator to write the strong_typedef to a std::ostream
struct streamable {
template <typename Derived, typename ValueType> struct mixin {
friend std::ostream &
operator<<(std::ostream &os, Derived const &st) {
return os << st.underlying_value();
}
};
};
/// Combine ordered and equality_comparable
struct comparable {
template <typename Derived, typename ValueType>
struct mixin
: ordered::template mixin<Derived, ValueType>,
equality_comparable::template mixin<Derived, ValueType> {};
};
/// Add a division operation to the strong_typedef that
/// produces a RatioType instances representing the result
template <typename RatioType> struct ratio {
template <typename Derived, typename ValueType> struct mixin {
friend RatioType
operator/(Derived const &lhs, Derived const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() /
std::declval<ValueType const &>())) {
return RatioType{lhs.underlying_value() /
rhs.underlying_value()};
}
};
};
/// Add the bitwise not operator to the strong_typedef
struct bitwise_not {
template <
typename Derived, typename ValueType,
bool= std::is_literal_type<ValueType>::value>
struct mixin {
friend constexpr Derived operator~(Derived const &lhs) noexcept(
noexcept(~std::declval<ValueType const &>())) {
return Derived{~lhs.underlying_value()};
}
};
};
template <typename Derived, typename ValueType>
struct bitwise_not::mixin<Derived, ValueType, false> {
friend Derived operator~(Derived const &lhs) noexcept(
noexcept(~std::declval<ValueType const &>())) {
return Derived{~lhs.underlying_value()};
}
};
/// Add the bitwise left-shift operator to the strong_typedef
template <typename Other> struct bitwise_left_shift {
template <
typename Derived, typename ValueType,
bool= std::is_literal_type<ValueType>::value>
struct mixin {
friend constexpr Derived
operator<<(Derived const &lhs, Other const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>()
<< underlying_value(std::declval<Other const &>()))) {
return Derived{lhs.underlying_value()
<< underlying_value(rhs)};
}
friend Derived &
operator<<=(Derived &lhs, Other const &rhs) noexcept(noexcept(
std::declval<ValueType &>()<<=
underlying_value(std::declval<Other const &>()))) {
lhs.underlying_value()<<= underlying_value(rhs);
return lhs;
}
};
};
template <typename Other>
template <typename Derived, typename ValueType>
struct bitwise_left_shift<Other>::mixin<Derived, ValueType, false> {
friend Derived
operator<<(Derived const &lhs, Other const &rhs) noexcept(noexcept(
std::declval<ValueType const &>()
<< underlying_value(std::declval<Other const &>()))) {
return Derived{lhs.underlying_value() << underlying_value(rhs)};
}
friend Derived &
operator<<=(Derived &lhs, Other const &rhs) noexcept(noexcept(
std::declval<ValueType &>()<<=
underlying_value(std::declval<Other const &>()))) {
lhs.underlying_value()<<= underlying_value(rhs);
return lhs;
}
};
/// Add the bitwise right-shift operator to the strong_typedef
template <typename Other> struct bitwise_right_shift {
template <
typename Derived, typename ValueType,
bool= std::is_literal_type<ValueType>::value>
struct mixin {
friend constexpr Derived
operator>>(Derived const &lhs, Other const &rhs) noexcept(
noexcept(
std::declval<ValueType const &>() >>
underlying_value(std::declval<Other const &>()))) {
return Derived{lhs.underlying_value() >>
underlying_value(rhs)};
}
friend Derived &
operator>>=(Derived &lhs, Other const &rhs) noexcept(noexcept(
std::declval<ValueType &>()>>=
underlying_value(std::declval<Other const &>()))) {
lhs.underlying_value()>>= underlying_value(rhs);
return lhs;
}
};
};
template <typename Other>
template <typename Derived, typename ValueType>
struct bitwise_right_shift<Other>::mixin<Derived, ValueType, false> {
friend Derived
operator>>(Derived const &lhs, Other const &rhs) noexcept(noexcept(
std::declval<ValueType const &>() >>
underlying_value(std::declval<Other const &>()))) {
return Derived{lhs.underlying_value() >> underlying_value(rhs)};
}
friend Derived &
operator>>=(Derived &lhs, Other const &rhs) noexcept(noexcept(
std::declval<ValueType &>()>>=
underlying_value(std::declval<Other const &>()))) {
lhs.underlying_value()>>= underlying_value(rhs);
return lhs;
}
};
} // namespace strong_typedef_properties
} // namespace jss
namespace std {
/// A specialization of std::hash for those instances of strong_typedef that
/// have the hashable property
template <typename Tag, typename ValueType, typename... Properties>
struct hash<jss::strong_typedef<Tag, ValueType, Properties...>> {
template <typename Arg>
typename std::enable_if<
std::is_same<
Arg,
jss::strong_typedef<Tag, ValueType, Properties...>>::value &&
std::is_base_of<
jss::strong_typedef_properties::hashable::base, Arg>::value,
size_t>::type
operator()(Arg const &arg) const noexcept(noexcept(
std::hash<ValueType>()(std::declval<ValueType const &>()))) {
return std::hash<ValueType>()(arg.underlying_value());
}
};
} // namespace std
#endif