Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: base/numerics/safe_conversions_impl.h

Issue 2931323002: Split out code to be shared between CheckedNumeric and ClampedNumeric (Closed)
Patch Set: iwyu Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ 5 #ifndef BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
6 #define BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 struct ArithmeticOrUnderlyingEnum<T, false> { 531 struct ArithmeticOrUnderlyingEnum<T, false> {
532 using type = T; 532 using type = T;
533 static const bool value = std::is_arithmetic<type>::value; 533 static const bool value = std::is_arithmetic<type>::value;
534 }; 534 };
535 535
536 // The following are helper templates used in the CheckedNumeric class. 536 // The following are helper templates used in the CheckedNumeric class.
537 template <typename T> 537 template <typename T>
538 class CheckedNumeric; 538 class CheckedNumeric;
539 539
540 template <typename T> 540 template <typename T>
541 class ClampedNumeric;
542
543 template <typename T>
541 class StrictNumeric; 544 class StrictNumeric;
542 545
543 // Used to treat CheckedNumeric and arithmetic underlying types the same. 546 // Used to treat CheckedNumeric and arithmetic underlying types the same.
544 template <typename T> 547 template <typename T>
545 struct UnderlyingType { 548 struct UnderlyingType {
546 using type = typename ArithmeticOrUnderlyingEnum<T>::type; 549 using type = typename ArithmeticOrUnderlyingEnum<T>::type;
547 static const bool is_numeric = std::is_arithmetic<type>::value; 550 static const bool is_numeric = std::is_arithmetic<type>::value;
548 static const bool is_checked = false; 551 static const bool is_checked = false;
549 static const bool is_strict = false; 552 static const bool is_strict = false;
550 }; 553 };
551 554
552 template <typename T> 555 template <typename T>
553 struct UnderlyingType<CheckedNumeric<T>> { 556 struct UnderlyingType<CheckedNumeric<T>> {
554 using type = T; 557 using type = T;
555 static const bool is_numeric = true; 558 static const bool is_numeric = true;
556 static const bool is_checked = true; 559 static const bool is_checked = true;
560 static const bool is_clamped = false;
557 static const bool is_strict = false; 561 static const bool is_strict = false;
558 }; 562 };
559 563
564 template <typename T>
565 struct UnderlyingType<ClampedNumeric<T>> {
566 using type = T;
567 static const bool is_numeric = true;
568 static const bool is_checked = false;
569 static const bool is_clamped = true;
570 static const bool is_strict = false;
571 };
572
560 template <typename T> 573 template <typename T>
561 struct UnderlyingType<StrictNumeric<T>> { 574 struct UnderlyingType<StrictNumeric<T>> {
562 using type = T; 575 using type = T;
563 static const bool is_numeric = true; 576 static const bool is_numeric = true;
564 static const bool is_checked = false; 577 static const bool is_checked = false;
578 static const bool is_clamped = false;
565 static const bool is_strict = true; 579 static const bool is_strict = true;
566 }; 580 };
567 581
568 template <typename L, typename R> 582 template <typename L, typename R>
569 struct IsCheckedOp { 583 struct IsCheckedOp {
570 static const bool value = 584 static const bool value =
571 UnderlyingType<L>::is_numeric && UnderlyingType<R>::is_numeric && 585 UnderlyingType<L>::is_numeric && UnderlyingType<R>::is_numeric &&
572 (UnderlyingType<L>::is_checked || UnderlyingType<R>::is_checked); 586 (UnderlyingType<L>::is_checked || UnderlyingType<R>::is_checked);
573 }; 587 };
574 588
575 template <typename L, typename R> 589 template <typename L, typename R>
590 struct IsClampedOp {
591 static const bool value =
592 UnderlyingType<L>::is_numeric && UnderlyingType<R>::is_numeric &&
593 (UnderlyingType<L>::is_clamped || UnderlyingType<R>::is_clamped);
594 };
595
596 template <typename L, typename R>
576 struct IsStrictOp { 597 struct IsStrictOp {
577 static const bool value = 598 static const bool value =
578 UnderlyingType<L>::is_numeric && UnderlyingType<R>::is_numeric && 599 UnderlyingType<L>::is_numeric && UnderlyingType<R>::is_numeric &&
579 (UnderlyingType<L>::is_strict || UnderlyingType<R>::is_strict); 600 (UnderlyingType<L>::is_strict || UnderlyingType<R>::is_strict);
580 }; 601 };
581 602
582 template <typename L, typename R> 603 template <typename L, typename R>
583 constexpr bool IsLessImpl(const L lhs, 604 constexpr bool IsLessImpl(const L lhs,
584 const R rhs, 605 const R rhs,
585 const RangeCheck l_range, 606 const RangeCheck l_range,
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 "Types must be numeric."); 716 "Types must be numeric.");
696 using Promotion = BigEnoughPromotion<L, R>; 717 using Promotion = BigEnoughPromotion<L, R>;
697 using BigType = typename Promotion::type; 718 using BigType = typename Promotion::type;
698 return Promotion::is_contained 719 return Promotion::is_contained
699 // Force to a larger type for speed if both are contained. 720 // Force to a larger type for speed if both are contained.
700 ? C<BigType, BigType>::Test( 721 ? C<BigType, BigType>::Test(
701 static_cast<BigType>(static_cast<L>(lhs)), 722 static_cast<BigType>(static_cast<L>(lhs)),
702 static_cast<BigType>(static_cast<R>(rhs))) 723 static_cast<BigType>(static_cast<R>(rhs)))
703 // Let the template functions figure it out for mixed types. 724 // Let the template functions figure it out for mixed types.
704 : C<L, R>::Test(lhs, rhs); 725 : C<L, R>::Test(lhs, rhs);
705 }; 726 }
706 727
707 } // namespace internal 728 } // namespace internal
708 } // namespace base 729 } // namespace base
709 730
710 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ 731 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698