diff options
author | Ben Wagner <bungeman@google.com> | 2017-10-17 17:40:56 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-10-17 22:48:51 +0000 |
commit | d71c6d1994f606e3260304589784f8b9ae69abec (patch) | |
tree | a69c5d1e92932355d9b2fe44d55f02ceb3997d19 /include | |
parent | aeaf6e771febbcbaecf9b4ae13091afecdd4656f (diff) |
Make SkTFitsIn and SkTo constexpr.
Because what can be better than marking things that can be constexpr as
constexpr. Also, I have a change where this would be nice to have.
Change-Id: Ie313b19d1930b98ddcd60cc17a320971625f18e3
Reviewed-on: https://skia-review.googlesource.com/60862
Commit-Queue: Ben Wagner <bungeman@google.com>
Reviewed-by: Mike Klein <mtklein@chromium.org>
Diffstat (limited to 'include')
-rw-r--r-- | include/core/SkTypes.h | 6 | ||||
-rw-r--r-- | include/private/SkTFitsIn.h | 10 |
2 files changed, 8 insertions, 8 deletions
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h index 59bdaaa90c..15a03d8fab 100644 --- a/include/core/SkTypes.h +++ b/include/core/SkTypes.h @@ -206,9 +206,9 @@ typedef unsigned U16CPU; typedef uint8_t SkBool8; #include "../private/SkTFitsIn.h" -template <typename D, typename S> D SkTo(S s) { - SkASSERT(SkTFitsIn<D>(s)); - return static_cast<D>(s); +template <typename D, typename S> constexpr D SkTo(S s) { + return SkASSERT(SkTFitsIn<D>(s)), + static_cast<D>(s); } #define SkToS8(x) SkTo<int8_t>(x) #define SkToU8(x) SkTo<uint8_t>(x) diff --git a/include/private/SkTFitsIn.h b/include/private/SkTFitsIn.h index a4ea30b022..a889807360 100644 --- a/include/private/SkTFitsIn.h +++ b/include/private/SkTFitsIn.h @@ -66,7 +66,7 @@ template <typename A, typename B> struct SkTHasMoreDigits * Used when it is statically known that source values are in the range of the Destination. */ template <typename S> struct SkTInRange_True { - static bool fits(S) { + static constexpr bool fits(S) { return true; } }; @@ -75,7 +75,7 @@ template <typename S> struct SkTInRange_True { * This is not valid for uX -> sx and sx -> uX conversions. */ template <typename D, typename S> struct SkTInRange_Cast { - static bool fits(S s) { + static constexpr bool fits(S s) { using S_is_bigger = SkTHasMoreDigits<S, D>; using D_is_bigger = SkTHasMoreDigits<D, S>; @@ -95,7 +95,7 @@ template <typename D, typename S> struct SkTInRange_Cast { * Assumes that Max(S) >= Max(D). */ template <typename D, typename S> struct SkTInRange_LE_MaxD { - static bool fits(S s) { + static constexpr bool fits(S s) { using precondition = SkTHasMoreDigits<S, D>; static_assert(precondition::value, "maxS < maxD"); @@ -106,7 +106,7 @@ template <typename D, typename S> struct SkTInRange_LE_MaxD { /** Tests if the source value >= 0. */ template <typename D, typename S> struct SkTInRange_GE_Zero { - static bool fits(S s) { + static constexpr bool fits(S s) { return static_cast<S>(0) <= s; } }; @@ -197,7 +197,7 @@ template <typename T> struct underlying_type<T, false> { } // namespace sktfitsin /** Returns true if the integer source value 's' will fit in the integer destination type 'D'. */ -template <typename D, typename S> inline bool SkTFitsIn(S s) { +template <typename D, typename S> constexpr inline bool SkTFitsIn(S s) { static_assert(std::is_integral<S>::value || std::is_enum<S>::value, "S must be integral."); static_assert(std::is_integral<D>::value || std::is_enum<D>::value, "D must be integral."); |