SkTypes: templates are usually better than macros
Change-Id: If55797642e839154ca06d09c991257031e1d319e
Reviewed-on: https://skia-review.googlesource.com/134331
Commit-Queue: Hal Canary <halcanary@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
Auto-Submit: Hal Canary <halcanary@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h
index 6586750..ef27a7f 100644
--- a/include/core/SkTypes.h
+++ b/include/core/SkTypes.h
@@ -78,6 +78,8 @@
#define SkAssertResult(cond) if (cond) {} do {} while(false)
#endif
+////////////////////////////////////////////////////////////////////////////////
+
// some clients (e.g. third_party/WebKit/Source/platform/fonts/FontCustomPlatformData.h)
// depend on SkString forward declaration below. Remove this once dependencies are fixed.
class SkString;
@@ -124,11 +126,11 @@
static constexpr int64_t SK_MaxS64 = 0x7FFFFFFFFFFFFFFF;
static constexpr int64_t SK_MinS64 = -SK_MaxS64;
-static inline int32_t SkLeftShift(int32_t value, int32_t shift) {
+inline constexpr int32_t SkLeftShift(int32_t value, int32_t shift) {
return (int32_t) ((uint32_t) value << shift);
}
-static inline int64_t SkLeftShift(int64_t value, int32_t shift) {
+inline constexpr int64_t SkLeftShift(int64_t value, int32_t shift) {
return (int64_t) ((uint64_t) value << shift);
}
@@ -148,20 +150,29 @@
#define SK_END_REQUIRE_DENSE
#endif
-#define SkAlign2(x) (((x) + 1) >> 1 << 1)
-#define SkIsAlign2(x) (0 == ((x) & 1))
+////////////////////////////////////////////////////////////////////////////////
-#define SkAlign4(x) (((x) + 3) >> 2 << 2)
-#define SkIsAlign4(x) (0 == ((x) & 3))
+template <typename T> inline constexpr T SkAlign2(T x) { return (x + 1) >> 1 << 1; }
+template <typename T> inline constexpr T SkAlign4(T x) { return (x + 3) >> 2 << 2; }
+template <typename T> inline constexpr T SkAlign8(T x) { return (x + 7) >> 3 << 3; }
-#define SkAlign8(x) (((x) + 7) >> 3 << 3)
-#define SkIsAlign8(x) (0 == ((x) & 7))
+template <typename T> inline constexpr bool SkIsAlign2(T x) { return 0 == (x & 1); }
+template <typename T> inline constexpr bool SkIsAlign4(T x) { return 0 == (x & 3); }
+template <typename T> inline constexpr bool SkIsAlign8(T x) { return 0 == (x & 7); }
-#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
-#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
+template <typename T> inline constexpr T SkAlignPtr(T x) {
+ return sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x);
+}
+template <typename T> inline constexpr bool SkIsAlignPtr(T x) {
+ return sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x);
+}
typedef uint32_t SkFourByteTag;
-#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
+inline constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d) {
+ return (((uint8_t)a << 24) | ((uint8_t)b << 16) | ((uint8_t)c << 8) | (uint8_t)d);
+}
+
+////////////////////////////////////////////////////////////////////////////////
/** 32 bit integer to hold a unicode value
*/