/* * Copyright 2013 Google Inc. * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef SkTFitsIn_DEFINED #define SkTFitsIn_DEFINED #include "../private/SkTLogic.h" #include #include namespace sktfitsin { namespace Private { /** SkTMux::type = (a && b) ? Both : (a) ? A : (b) ? B : Neither; */ template struct SkTMux { using type = skstd::conditional_t, skstd::conditional_t>; }; /** SkTHasMoreDigits = (digits(A) >= digits(B)) ? true_type : false_type. */ template struct SkTHasMoreDigits : skstd::bool_constant::digits >= std::numeric_limits::digits> { }; /** A high or low side predicate which is used when it is statically known * that source values are in the range of the Destination. */ template struct SkTOutOfRange_False { using can_be_true = std::false_type; using source_type = S; static bool apply(S) { return false; } }; /** A low side predicate which tests if the source value < Min(D). * Assumes that Min(S) <= Min(D). */ template struct SkTOutOfRange_LT_MinD { using can_be_true = std::true_type; using source_type = S; static bool apply(S s) { using precondition = SkTHasMoreDigits; static_assert(precondition::value, "minS > minD"); return s < static_cast((std::numeric_limits::min)()); } }; /** A low side predicate which tests if the source value is less than 0. */ template struct SkTOutOfRange_LT_Zero { using can_be_true = std::true_type; using source_type = S; static bool apply(S s) { return s < static_cast(0); } }; /** A high side predicate which tests if the source value > Max(D). * Assumes that Max(S) >= Max(D). */ template struct SkTOutOfRange_GT_MaxD { using can_be_true = std::true_type; using source_type = S; static bool apply(S s) { using precondition = SkTHasMoreDigits; static_assert(precondition::value, "maxS < maxD"); return s > static_cast((std::numeric_limits::max)()); } }; /** Composes two SkTOutOfRange predicates. * First checks OutOfRange_Low then, if in range, OutOfRange_High. */ template struct SkTOutOfRange_Either { using can_be_true = std::true_type; using source_type = typename OutOfRange_Low::source_type; static bool apply(source_type s) { bool outOfRange = OutOfRange_Low::apply(s); if (!outOfRange) { outOfRange = OutOfRange_High::apply(s); } return outOfRange; } }; /** SkTCombineOutOfRange::type is an SkTOutOfRange_XXX type which is the * optimal combination of OutOfRange_Low and OutOfRange_High. */ template struct SkTCombineOutOfRange { using Both = SkTOutOfRange_Either; using Neither = SkTOutOfRange_False; using apply_low = typename OutOfRange_Low::can_be_true; using apply_high = typename OutOfRange_High::can_be_true; using type = typename SkTMux::type; }; template struct SkTRangeChecker { /** This is the method which is called at runtime to do the range check. */ static bool OutOfRange(S s) { using Combined = typename SkTCombineOutOfRange::type; return Combined::apply(s); } }; /** SkTFitsIn_Unsigned2Unsiged::type is an SkTRangeChecker with an OutOfRange(S s) method * the implementation of which is tailored for the source and destination types. * Assumes that S and D are unsigned integer types. */ template struct SkTFitsIn_Unsigned2Unsiged { using OutOfRange_Low = SkTOutOfRange_False; using OutOfRange_High = SkTOutOfRange_GT_MaxD; using HighSideOnlyCheck = SkTRangeChecker; using NoCheck = SkTRangeChecker, SkTOutOfRange_False>; // If std::numeric_limits::digits >= std::numeric_limits::digits, nothing to check. // This also protects the precondition of SkTOutOfRange_GT_MaxD. using sourceFitsInDesitination = SkTHasMoreDigits; using type = skstd::conditional_t; }; /** SkTFitsIn_Signed2Signed::type is an SkTRangeChecker with an OutOfRange(S s) method * the implementation of which is tailored for the source and destination types. * Assumes that S and D are signed integer types. */ template struct SkTFitsIn_Signed2Signed { using OutOfRange_Low = SkTOutOfRange_LT_MinD; using OutOfRange_High = SkTOutOfRange_GT_MaxD; using FullCheck = SkTRangeChecker; using NoCheck = SkTRangeChecker, SkTOutOfRange_False>; // If std::numeric_limits::digits >= std::numeric_limits::digits, nothing to check. // This also protects the precondition of SkTOutOfRange_LT_MinD and SkTOutOfRange_GT_MaxD. using sourceFitsInDesitination = SkTHasMoreDigits; using type = skstd::conditional_t; }; /** SkTFitsIn_Signed2Unsigned::type is an SkTRangeChecker with an OutOfRange(S s) method * the implementation of which is tailored for the source and destination types. * Assumes that S is a signed integer type and D is an unsigned integer type. */ template struct SkTFitsIn_Signed2Unsigned { using OutOfRange_Low = SkTOutOfRange_LT_Zero; using OutOfRange_High = SkTOutOfRange_GT_MaxD; using FullCheck = SkTRangeChecker; using LowSideOnlyCheck = SkTRangeChecker>; // If std::numeric_limits::max() >= std::numeric_limits::max(), // no need to check the high side. (Until C++11, assume more digits means greater max.) // This also protects the precondition of SkTOutOfRange_GT_MaxD. using sourceCannotExceedDest = SkTHasMoreDigits; using type = skstd::conditional_t; }; /** SkTFitsIn_Unsigned2Signed::type is an SkTRangeChecker with an OutOfRange(S s) method * the implementation of which is tailored for the source and destination types. * Assumes that S is an usigned integer type and D is a signed integer type. */ template struct SkTFitsIn_Unsigned2Signed { using OutOfRange_Low = SkTOutOfRange_False; using OutOfRange_High = SkTOutOfRange_GT_MaxD; using HighSideOnlyCheck = SkTRangeChecker; using NoCheck = SkTRangeChecker, SkTOutOfRange_False>; // If std::numeric_limits::max() >= std::numeric_limits::max(), nothing to check. // (Until C++11, assume more digits means greater max.) // This also protects the precondition of SkTOutOfRange_GT_MaxD. using sourceCannotExceedDest = SkTHasMoreDigits; using type = skstd::conditional_t; }; /** SkTFitsIn::type is an SkTRangeChecker with an OutOfRange(S s) method * the implementation of which is tailored for the source and destination types. * Assumes that S and D are integer types. */ template struct SkTFitsIn { // One of the following will be the 'selector' type. using S2S = SkTFitsIn_Signed2Signed; using S2U = SkTFitsIn_Signed2Unsigned; using U2S = SkTFitsIn_Unsigned2Signed; using U2U = SkTFitsIn_Unsigned2Unsiged; using S_is_signed = skstd::bool_constant::is_signed>; using D_is_signed = skstd::bool_constant::is_signed>; using selector = typename SkTMux::type; // This type is an SkTRangeChecker. using type = typename selector::type; }; template ::value> struct underlying_type { using type = skstd::underlying_type_t; }; template struct underlying_type { using type = T; }; } // namespace Private } // namespace sktfitsin /** Returns true if the integer source value 's' will fit in the integer destination type 'D'. */ template inline bool SkTFitsIn(S s) { static_assert(std::is_integral::value || std::is_enum::value, "S must be integral."); static_assert(std::is_integral::value || std::is_enum::value, "D must be integral."); using RealS = typename sktfitsin::Private::underlying_type::type; using RealD = typename sktfitsin::Private::underlying_type::type; return !sktfitsin::Private::SkTFitsIn::type::OutOfRange(s); } #endif