Templatize SkToXXX.

Makes the checked cast in debug more correct, avoiding new
warnings in vs2015.

BUG=skia:4553

Committed: https://skia.googlesource.com/skia/+/0be9e806af72b3e029e691eef5c891c90d3fd320

Review URL: https://codereview.chromium.org/1814153003
This commit is contained in:
bungeman 2016-03-19 15:06:56 -07:00 committed by Commit bot
parent ba73ad0f3a
commit 68c14d9b32
7 changed files with 118 additions and 148 deletions

View File

@ -415,6 +415,7 @@
'<(skia_include_path)/private/SkTemplates.h',
'<(skia_include_path)/private/SkTArray.h',
'<(skia_include_path)/private/SkTDArray.h',
'<(skia_include_path)/private/SkTFitsIn.h',
'<(skia_include_path)/private/SkTHash.h',
'<(skia_include_path)/private/SkThreadID.h',
'<(skia_include_path)/private/SkTDict.h',

View File

@ -89,7 +89,6 @@
'<(skia_src_path)/utils/SkThreadUtils_pthread.h',
'<(skia_src_path)/utils/SkThreadUtils_win.cpp',
'<(skia_src_path)/utils/SkThreadUtils_win.h',
'<(skia_src_path)/utils/SkTFitsIn.h',
'<(skia_src_path)/utils/SkWhitelistTypefaces.cpp',
#mac

View File

@ -36,13 +36,13 @@ int SkStrStartsWithOneOf(const char string[], const char prefixes[]);
static int SkStrFind(const char string[], const char substring[]) {
const char *first = strstr(string, substring);
if (NULL == first) return -1;
return SkToS32(first - &string[0]);
return SkToInt(first - &string[0]);
}
static int SkStrFindLastOf(const char string[], const char subchar) {
const char* last = strrchr(string, subchar);
if (NULL == last) return -1;
return SkToS32(last - &string[0]);
return SkToInt(last - &string[0]);
}
static bool SkStrContains(const char string[], const char substring[]) {

View File

@ -256,27 +256,20 @@ typedef unsigned U16CPU;
*/
typedef uint8_t SkBool8;
#ifdef SK_DEBUG
SK_API int8_t SkToS8(intmax_t);
SK_API uint8_t SkToU8(uintmax_t);
SK_API int16_t SkToS16(intmax_t);
SK_API uint16_t SkToU16(uintmax_t);
SK_API int32_t SkToS32(intmax_t);
SK_API uint32_t SkToU32(uintmax_t);
SK_API int SkToInt(intmax_t);
SK_API unsigned SkToUInt(uintmax_t);
SK_API size_t SkToSizeT(uintmax_t);
#else
#define SkToS8(x) ((int8_t)(x))
#define SkToU8(x) ((uint8_t)(x))
#define SkToS16(x) ((int16_t)(x))
#define SkToU16(x) ((uint16_t)(x))
#define SkToS32(x) ((int32_t)(x))
#define SkToU32(x) ((uint32_t)(x))
#define SkToInt(x) ((int)(x))
#define SkToUInt(x) ((unsigned)(x))
#define SkToSizeT(x) ((size_t)(x))
#endif
#include "../private/SkTFitsIn.h"
template <typename D, typename S> D SkTo(S s) {
SkASSERT(SkTFitsIn<D>(s));
return static_cast<D>(s);
}
#define SkToS8(x) SkTo<int8_t>(x)
#define SkToU8(x) SkTo<uint8_t>(x)
#define SkToS16(x) SkTo<int16_t>(x)
#define SkToU16(x) SkTo<uint16_t>(x)
#define SkToS32(x) SkTo<int32_t>(x)
#define SkToU32(x) SkTo<uint32_t>(x)
#define SkToInt(x) SkTo<int>(x)
#define SkToUInt(x) SkTo<unsigned>(x)
#define SkToSizeT(x) SkTo<size_t>(x)
/** Returns 0 or 1 based on the condition
*/

View File

@ -8,8 +8,7 @@
#ifndef SkTFitsIn_DEFINED
#define SkTFitsIn_DEFINED
#include "SkTypes.h"
#include "SkTLogic.h"
#include "../private/SkTLogic.h"
#include <limits>
#include <type_traits>
@ -24,7 +23,7 @@ struct SkTMux {
};
/** SkTHasMoreDigits = (digits(A) >= digits(B)) ? true_type : false_type. */
template<typename A, typename B> struct SkTHasMoreDigits
template <typename A, typename B> struct SkTHasMoreDigits
: skstd::bool_constant<std::numeric_limits<A>::digits >= std::numeric_limits<B>::digits>
{ };
@ -32,9 +31,9 @@ template<typename A, typename B> struct SkTHasMoreDigits
* that source values are in the range of the Destination.
*/
template <typename S> struct SkTOutOfRange_False {
typedef std::false_type can_be_true;
typedef S source_type;
static bool apply(S s) {
using can_be_true = std::false_type;
using source_type = S;
static bool apply(S) {
return false;
}
};
@ -43,11 +42,11 @@ template <typename S> struct SkTOutOfRange_False {
* Assumes that Min(S) <= Min(D).
*/
template <typename D, typename S> struct SkTOutOfRange_LT_MinD {
typedef std::true_type can_be_true;
typedef S source_type;
using can_be_true = std::true_type;
using source_type = S;
static bool apply(S s) {
typedef SkTHasMoreDigits<S, D> precondition;
static_assert(precondition::value, "SkTOutOfRange_LT_MinD__minS_gt_minD");
using precondition = SkTHasMoreDigits<S, D>;
static_assert(precondition::value, "minS > minD");
return s < static_cast<S>((std::numeric_limits<D>::min)());
}
@ -55,8 +54,8 @@ template <typename D, typename S> struct SkTOutOfRange_LT_MinD {
/** A low side predicate which tests if the source value is less than 0. */
template <typename D, typename S> struct SkTOutOfRange_LT_Zero {
typedef std::true_type can_be_true;
typedef S source_type;
using can_be_true = std::true_type;
using source_type = S;
static bool apply(S s) {
return s < static_cast<S>(0);
}
@ -66,11 +65,11 @@ template <typename D, typename S> struct SkTOutOfRange_LT_Zero {
* Assumes that Max(S) >= Max(D).
*/
template <typename D, typename S> struct SkTOutOfRange_GT_MaxD {
typedef std::true_type can_be_true;
typedef S source_type;
using can_be_true = std::true_type;
using source_type = S;
static bool apply(S s) {
typedef SkTHasMoreDigits<S, D> precondition;
static_assert(precondition::value, "SkTOutOfRange_GT_MaxD__maxS_lt_maxD");
using precondition = SkTHasMoreDigits<S, D>;
static_assert(precondition::value, "maxS < maxD");
return s > static_cast<S>((std::numeric_limits<D>::max)());
}
@ -79,9 +78,9 @@ template <typename D, typename S> struct SkTOutOfRange_GT_MaxD {
/** Composes two SkTOutOfRange predicates.
* First checks OutOfRange_Low then, if in range, OutOfRange_High.
*/
template<class OutOfRange_Low, class OutOfRange_High> struct SkTOutOfRange_Either {
typedef std::true_type can_be_true;
typedef typename OutOfRange_Low::source_type source_type;
template <typename OutOfRange_Low, typename OutOfRange_High> 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) {
@ -94,22 +93,22 @@ template<class OutOfRange_Low, class OutOfRange_High> struct SkTOutOfRange_Eithe
/** SkTCombineOutOfRange::type is an SkTOutOfRange_XXX type which is the
* optimal combination of OutOfRange_Low and OutOfRange_High.
*/
template<class OutOfRange_Low, class OutOfRange_High> struct SkTCombineOutOfRange {
typedef SkTOutOfRange_Either<OutOfRange_Low, OutOfRange_High> Both;
typedef SkTOutOfRange_False<typename OutOfRange_Low::source_type> Neither;
template <typename OutOfRange_Low, typename OutOfRange_High> struct SkTCombineOutOfRange {
using Both = SkTOutOfRange_Either<OutOfRange_Low, OutOfRange_High>;
using Neither = SkTOutOfRange_False<typename OutOfRange_Low::source_type>;
typedef typename OutOfRange_Low::can_be_true apply_low;
typedef typename OutOfRange_High::can_be_true apply_high;
using apply_low = typename OutOfRange_Low::can_be_true;
using apply_high = typename OutOfRange_High::can_be_true;
typedef typename SkTMux<apply_low::value, apply_high::value,
Both, OutOfRange_Low, OutOfRange_High, Neither>::type type;
using type = typename SkTMux<apply_low::value, apply_high::value,
Both, OutOfRange_Low, OutOfRange_High, Neither>::type;
};
template<typename D, typename S, class OutOfRange_Low, class OutOfRange_High>
template <typename D, typename S, typename OutOfRange_Low, typename OutOfRange_High>
struct SkTRangeChecker {
/** This is the method which is called at runtime to do the range check. */
static bool OutOfRange(S s) {
typedef typename SkTCombineOutOfRange<OutOfRange_Low, OutOfRange_High>::type Combined;
using Combined = typename SkTCombineOutOfRange<OutOfRange_Low, OutOfRange_High>::type;
return Combined::apply(s);
}
};
@ -118,90 +117,97 @@ struct SkTRangeChecker {
* the implementation of which is tailored for the source and destination types.
* Assumes that S and D are unsigned integer types.
*/
template<typename D, typename S> struct SkTFitsIn_Unsigned2Unsiged {
typedef SkTOutOfRange_False<S> OutOfRange_Low;
typedef SkTOutOfRange_GT_MaxD<D, S> OutOfRange_High;
template <typename D, typename S> struct SkTFitsIn_Unsigned2Unsiged {
using OutOfRange_Low = SkTOutOfRange_False<S>;
using OutOfRange_High = SkTOutOfRange_GT_MaxD<D, S>;
typedef SkTRangeChecker<D, S, OutOfRange_Low, OutOfRange_High> HighSideOnlyCheck;
typedef SkTRangeChecker<D, S, SkTOutOfRange_False<S>, SkTOutOfRange_False<S> > NoCheck;
using HighSideOnlyCheck = SkTRangeChecker<D, S, OutOfRange_Low, OutOfRange_High>;
using NoCheck = SkTRangeChecker<D, S, SkTOutOfRange_False<S>, SkTOutOfRange_False<S>>;
// If std::numeric_limits<D>::digits >= std::numeric_limits<S>::digits, nothing to check.
// This also protects the precondition of SkTOutOfRange_GT_MaxD.
typedef SkTHasMoreDigits<D, S> sourceFitsInDesitination;
typedef skstd::conditional_t<sourceFitsInDesitination::value, NoCheck, HighSideOnlyCheck> type;
using sourceFitsInDesitination = SkTHasMoreDigits<D, S>;
using type = skstd::conditional_t<sourceFitsInDesitination::value, NoCheck, HighSideOnlyCheck>;
};
/** 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<typename D, typename S> struct SkTFitsIn_Signed2Signed {
typedef SkTOutOfRange_LT_MinD<D, S> OutOfRange_Low;
typedef SkTOutOfRange_GT_MaxD<D, S> OutOfRange_High;
template <typename D, typename S> struct SkTFitsIn_Signed2Signed {
using OutOfRange_Low = SkTOutOfRange_LT_MinD<D, S>;
using OutOfRange_High = SkTOutOfRange_GT_MaxD<D, S>;
typedef SkTRangeChecker<D, S, OutOfRange_Low, OutOfRange_High> FullCheck;
typedef SkTRangeChecker<D, S, SkTOutOfRange_False<S>, SkTOutOfRange_False<S> > NoCheck;
using FullCheck = SkTRangeChecker<D, S, OutOfRange_Low, OutOfRange_High>;
using NoCheck = SkTRangeChecker<D, S, SkTOutOfRange_False<S>, SkTOutOfRange_False<S>>;
// If std::numeric_limits<D>::digits >= std::numeric_limits<S>::digits, nothing to check.
// This also protects the precondition of SkTOutOfRange_LT_MinD and SkTOutOfRange_GT_MaxD.
typedef SkTHasMoreDigits<D, S> sourceFitsInDesitination;
typedef skstd::conditional_t<sourceFitsInDesitination::value, NoCheck, FullCheck> type;
using sourceFitsInDesitination = SkTHasMoreDigits<D, S>;
using type = skstd::conditional_t<sourceFitsInDesitination::value, NoCheck, FullCheck>;
};
/** 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<typename D, typename S> struct SkTFitsIn_Signed2Unsigned {
typedef SkTOutOfRange_LT_Zero<D, S> OutOfRange_Low;
typedef SkTOutOfRange_GT_MaxD<D, S> OutOfRange_High;
template <typename D, typename S> struct SkTFitsIn_Signed2Unsigned {
using OutOfRange_Low = SkTOutOfRange_LT_Zero<D, S>;
using OutOfRange_High = SkTOutOfRange_GT_MaxD<D, S>;
typedef SkTRangeChecker<D, S, OutOfRange_Low, OutOfRange_High> FullCheck;
typedef SkTRangeChecker<D, S, OutOfRange_Low, SkTOutOfRange_False<S> > LowSideOnlyCheck;
using FullCheck = SkTRangeChecker<D, S, OutOfRange_Low, OutOfRange_High>;
using LowSideOnlyCheck = SkTRangeChecker<D, S, OutOfRange_Low, SkTOutOfRange_False<S>>;
// If std::numeric_limits<D>::max() >= std::numeric_limits<S>::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.
typedef SkTHasMoreDigits<D, S> sourceCannotExceedDest;
typedef skstd::conditional_t<sourceCannotExceedDest::value, LowSideOnlyCheck, FullCheck> type;
using sourceCannotExceedDest = SkTHasMoreDigits<D, S>;
using type = skstd::conditional_t<sourceCannotExceedDest::value, LowSideOnlyCheck, FullCheck>;
};
/** 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<typename D, typename S> struct SkTFitsIn_Unsigned2Signed {
typedef SkTOutOfRange_False<S> OutOfRange_Low;
typedef SkTOutOfRange_GT_MaxD<D, S> OutOfRange_High;
template <typename D, typename S> struct SkTFitsIn_Unsigned2Signed {
using OutOfRange_Low = SkTOutOfRange_False<S>;
using OutOfRange_High = SkTOutOfRange_GT_MaxD<D, S>;
typedef SkTRangeChecker<D, S, OutOfRange_Low, OutOfRange_High> HighSideOnlyCheck;
typedef SkTRangeChecker<D, S, SkTOutOfRange_False<S>, SkTOutOfRange_False<S> > NoCheck;
using HighSideOnlyCheck = SkTRangeChecker<D, S, OutOfRange_Low, OutOfRange_High>;
using NoCheck = SkTRangeChecker<D, S, SkTOutOfRange_False<S>, SkTOutOfRange_False<S>>;
// If std::numeric_limits<D>::max() >= std::numeric_limits<S>::max(), nothing to check.
// (Until C++11, assume more digits means greater max.)
// This also protects the precondition of SkTOutOfRange_GT_MaxD.
typedef SkTHasMoreDigits<D, S> sourceCannotExceedDest;
typedef skstd::conditional_t<sourceCannotExceedDest::value, NoCheck, HighSideOnlyCheck> type;
using sourceCannotExceedDest = SkTHasMoreDigits<D, S>;
using type = skstd::conditional_t<sourceCannotExceedDest::value, NoCheck, HighSideOnlyCheck>;
};
/** 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<typename D, typename S> struct SkTFitsIn {
template <typename D, typename S> struct SkTFitsIn {
// One of the following will be the 'selector' type.
typedef SkTFitsIn_Signed2Signed<D, S> S2S;
typedef SkTFitsIn_Signed2Unsigned<D, S> S2U;
typedef SkTFitsIn_Unsigned2Signed<D, S> U2S;
typedef SkTFitsIn_Unsigned2Unsiged<D, S> U2U;
using S2S = SkTFitsIn_Signed2Signed<D, S>;
using S2U = SkTFitsIn_Signed2Unsigned<D, S>;
using U2S = SkTFitsIn_Unsigned2Signed<D, S>;
using U2U = SkTFitsIn_Unsigned2Unsiged<D, S>;
typedef skstd::bool_constant<std::numeric_limits<S>::is_signed> S_is_signed;
typedef skstd::bool_constant<std::numeric_limits<D>::is_signed> D_is_signed;
using S_is_signed = skstd::bool_constant<std::numeric_limits<S>::is_signed>;
using D_is_signed = skstd::bool_constant<std::numeric_limits<D>::is_signed>;
typedef typename SkTMux<S_is_signed::value, D_is_signed::value,
S2S, S2U, U2S, U2U>::type selector;
using selector = typename SkTMux<S_is_signed::value, D_is_signed::value,
S2S, S2U, U2S, U2U>::type;
// This type is an SkTRangeChecker.
typedef typename selector::type type;
using type = typename selector::type;
};
template <typename T, bool = std::is_enum<T>::value> struct underlying_type {
using type = skstd::underlying_type_t<T>;
};
template <typename T> struct underlying_type<T, false> {
using type = T;
};
} // namespace Private
@ -209,10 +215,13 @@ template<typename D, typename S> struct 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) {
static_assert(std::numeric_limits<S>::is_integer, "SkTFitsIn_source_must_be_integer");
static_assert(std::numeric_limits<D>::is_integer, "SkTFitsIn_destination_must_be_integer");
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.");
return !sktfitsin::Private::SkTFitsIn<D, S>::type::OutOfRange(s);
using RealS = typename sktfitsin::Private::underlying_type<S>::type;
using RealD = typename sktfitsin::Private::underlying_type<D>::type;
return !sktfitsin::Private::SkTFitsIn<RealD, RealS>::type::OutOfRange(s);
}
#endif

View File

@ -14,8 +14,6 @@
#ifndef SkTLogic_DEFINED
#define SkTLogic_DEFINED
#include "SkTypes.h"
#include <stddef.h>
#include <stdint.h>
#include <type_traits>
@ -42,7 +40,7 @@ template <typename T> using remove_extent_t = typename std::remove_extent<T>::ty
// On all platforms, variadic functions only exist in the c calling convention.
// mcvc 2013 introduced __vectorcall, but it wan't until 2015 that it was added to is_function.
template <typename> struct is_function : std::false_type {};
#if !defined(SK_BUILD_FOR_WIN)
#if !defined(WIN32)
template <typename R, typename... Args> struct is_function<R(Args...)> : std::true_type {};
#else
template <typename R, typename... Args> struct is_function<R __cdecl (Args...)> : std::true_type {};
@ -50,7 +48,7 @@ template <typename R, typename... Args> struct is_function<R __cdecl (Args...)>
template <typename R, typename... Args> struct is_function<R __stdcall (Args...)> : std::true_type {};
template <typename R, typename... Args> struct is_function<R __fastcall (Args...)> : std::true_type {};
#endif
#if defined(_MSC_VER) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
#if defined(_MSC_VER) && (_M_IX86_FP >= 2 || defined(_M_AMD64) || defined(_M_X64))
template <typename R, typename... Args> struct is_function<R __vectorcall (Args...)> : std::true_type {};
#endif
#endif
@ -64,6 +62,27 @@ template <typename T> using add_lvalue_reference_t = typename std::add_lvalue_re
template <typename... T> using common_type_t = typename std::common_type<T...>::type;
// Chromium currently requires gcc 4.8.2 or a recent clang compiler, but uses libstdc++4.6.4.
// Note that Precise actually uses libstdc++4.6.3.
// Unfortunately, libstdc++ STL before libstdc++4.7 do not define std::underlying_type.
// Newer gcc and clang compilers have __underlying_type which does not depend on runtime support.
// See https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html for __GLIBCXX__ values.
// Unfortunately __GLIBCXX__ is a date, but no updates to versions before 4.7 are now anticipated.
#define SK_GLIBCXX_4_7_0 20120322
// Updates to versions before 4.7 but released after 4.7 was released.
#define SK_GLIBCXX_4_5_4 20120702
#define SK_GLIBCXX_4_6_4 20121127
#if defined(__GLIBCXX__) && (__GLIBCXX__ < SK_GLIBCXX_4_7_0 || \
__GLIBCXX__ == SK_GLIBCXX_4_5_4 || \
__GLIBCXX__ == SK_GLIBCXX_4_6_4)
template <typename T> struct underlying_type {
using type = __underlying_type(T);
};
#else
template <typename T> using underlying_type = std::underlying_type<T>;
#endif
template <typename T> using underlying_type_t = typename skstd::underlying_type<T>::type;
template <typename S, typename D,
bool=std::is_void<S>::value || is_function<D>::value || std::is_array<D>::value>
struct is_convertible_detector {

View File

@ -1,4 +1,3 @@
/*
* Copyright 2006 The Android Open Source Project
*
@ -6,58 +5,8 @@
* found in the LICENSE file.
*/
#include "SkTypes.h"
#ifdef SK_DEBUG
int8_t SkToS8(intmax_t x) {
SkASSERT((int8_t)x == x);
return (int8_t)x;
}
uint8_t SkToU8(uintmax_t x) {
SkASSERT((uint8_t)x == x);
return (uint8_t)x;
}
int16_t SkToS16(intmax_t x) {
SkASSERT((int16_t)x == x);
return (int16_t)x;
}
uint16_t SkToU16(uintmax_t x) {
SkASSERT((uint16_t)x == x);
return (uint16_t)x;
}
int32_t SkToS32(intmax_t x) {
SkASSERT((int32_t)x == x);
return (int32_t)x;
}
uint32_t SkToU32(uintmax_t x) {
SkASSERT((uint32_t)x == x);
return (uint32_t)x;
}
int SkToInt(intmax_t x) {
SkASSERT((int)x == x);
return (int)x;
}
unsigned SkToUInt(uintmax_t x) {
SkASSERT((unsigned)x == x);
return (unsigned)x;
}
size_t SkToSizeT(uintmax_t x) {
SkASSERT((size_t)x == x);
return (size_t)x;
}
#endif
#if defined(GOOGLE3)
void SkDebugfForDumpStackTrace(const char* data, void* unused) {
SkDebugf("%s", data);