From 68c14d9b3244ef9d58727ff11f2742be9236f46e Mon Sep 17 00:00:00 2001 From: bungeman Date: Sat, 19 Mar 2016 15:06:56 -0700 Subject: 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 --- include/core/SkTypes.h | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) (limited to 'include/core/SkTypes.h') diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h index 1051f08cea..d7a791163b 100644 --- a/include/core/SkTypes.h +++ b/include/core/SkTypes.h @@ -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 D SkTo(S s) { + SkASSERT(SkTFitsIn(s)); + return static_cast(s); +} +#define SkToS8(x) SkTo(x) +#define SkToU8(x) SkTo(x) +#define SkToS16(x) SkTo(x) +#define SkToU16(x) SkTo(x) +#define SkToS32(x) SkTo(x) +#define SkToU32(x) SkTo(x) +#define SkToInt(x) SkTo(x) +#define SkToUInt(x) SkTo(x) +#define SkToSizeT(x) SkTo(x) /** Returns 0 or 1 based on the condition */ -- cgit v1.2.3