aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkTypes.h
diff options
context:
space:
mode:
authorGravatar bungeman <bungeman@google.com>2016-03-19 15:06:56 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-03-19 15:06:56 -0700
commit68c14d9b3244ef9d58727ff11f2742be9236f46e (patch)
treefe165d20a4eb33380ce150c215cb64c1edf1dfff /include/core/SkTypes.h
parentba73ad0f3a6d9d505cae08e4f3cc5d2d6b9b1501 (diff)
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
Diffstat (limited to 'include/core/SkTypes.h')
-rw-r--r--include/core/SkTypes.h35
1 files changed, 14 insertions, 21 deletions
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 <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
*/