aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkColor.h
diff options
context:
space:
mode:
authorGravatar Mike Klein <mtklein@chromium.org>2017-09-28 09:47:45 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-09-29 13:49:04 +0000
commit37bbfe3ea4745170df3f5ef2f4c1e1332af49cea (patch)
tree3f2c0a6496cf023485578339ef0b32a29a9e7c09 /include/core/SkColor.h
parentfdd2cb52b781dd68f515a1eb9fe6d054a0ebc226 (diff)
Make SkASSERT and co. constexpr compatible.
One of the nice bits of constexpr is that an expression is constexpr if there exists any set of argument values that make it constant. It doesn't have to be constant for _all_ argument values. This means that this expression is constexpr: condition ? constexpr_value : []{ arbitrary non-constexpr code; }(); ... it's constant when condition is true. We can use this to rewrite SkASSERT(condition) as ( (condition) ? (void)0 : []{ SK_ABORT(#condition); }() ) Both sides of the ?: are void, and when condition is true at compile time the right hand side disappears completely. In C++11 constexpr functions we just have to use the comma operator to jam SkASSERT() into the order of evaluation: constexpr uint32_t foo(int x, int y) { return SkASSERT(x > y), x - y; } Change-Id: I21878d14fb2af76d93591d2ae229460ee825cfde Reviewed-on: https://skia-review.googlesource.com/52663 Reviewed-by: Ben Wagner <bungeman@google.com> Reviewed-by: Trent Apted <tapted@chromium.org> Commit-Queue: Mike Klein <mtklein@chromium.org>
Diffstat (limited to 'include/core/SkColor.h')
-rw-r--r--include/core/SkColor.h28
1 files changed, 6 insertions, 22 deletions
diff --git a/include/core/SkColor.h b/include/core/SkColor.h
index c8c6a6acf3..b1b83e3acf 100644
--- a/include/core/SkColor.h
+++ b/include/core/SkColor.h
@@ -28,29 +28,13 @@ typedef uint32_t SkColor;
/** Return a SkColor value from 8 bit component values
*/
-static inline SkColor SkColorSetARGBInline(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
-{
- SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255);
-
- return (a << 24) | (r << 16) | (g << 8) | (b << 0);
+static constexpr inline SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
+ return SkASSERT(a <= 255 && r <= 255 && g <= 255 && b <= 255),
+ (a << 24) | (r << 16) | (g << 8) | (b << 0);
}
-
-#define SkColorSetARGBMacro(a, r, g, b) \
- static_cast<SkColor>( \
- (static_cast<U8CPU>(a) << 24) | \
- (static_cast<U8CPU>(r) << 16) | \
- (static_cast<U8CPU>(g) << 8) | \
- (static_cast<U8CPU>(b) << 0))
-
-/** gcc will generate static initializers for code of this form:
- * static const SkColor kMyColor = SkColorSetARGB(0xFF, 0x01, 0x02, 0x03)
- * if SkColorSetARGB() is a static inline, but not if it's a macro.
- */
-#if defined(NDEBUG)
-#define SkColorSetARGB(a, r, g, b) SkColorSetARGBMacro(a, r, g, b)
-#else
-#define SkColorSetARGB(a, r, g, b) SkColorSetARGBInline(a, r, g, b)
-#endif
+// Legacy aliases.
+#define SkColorSetARGBInline SkColorSetARGB
+#define SkColorSetARGBMacro SkColorSetARGB
/** Return a SkColor value from 8 bit component values, with an implied value
of 0xFF for alpha (fully opaque)