aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkTypes.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/SkTypes.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/SkTypes.h')
-rw-r--r--include/core/SkTypes.h30
1 files changed, 18 insertions, 12 deletions
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h
index b453249ea1..63a2b40591 100644
--- a/include/core/SkTypes.h
+++ b/include/core/SkTypes.h
@@ -79,27 +79,33 @@ inline void operator delete(void* p) {
SK_API void SkDebugf(const char format[], ...);
#endif
-#define SkREQUIRE_SEMICOLON_AFTER(code) do { code } while (false)
-
+// SkASSERT, SkASSERTF and SkASSERT_RELEASE can be used as stand alone assertion expressions, e.g.
+// uint32_t foo(int x) {
+// SkASSERT(x > 4);
+// return x - 4;
+// }
+// and are also written to be compatible with constexpr functions:
+// constexpr uint32_t foo(int x) {
+// return SkASSERT(x > 4),
+// x - 4;
+// }
#define SkASSERT_RELEASE(cond) \
- SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT(#cond); } )
+ static_cast<void>( (cond) ? (void)0 : []{ SK_ABORT("assert(" #cond ")"); }() )
#ifdef SK_DEBUG
- #define SkASSERT(cond) \
- SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT("assert(" #cond ")"); })
- #define SkASSERTF(cond, fmt, ...) \
- SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { \
- SkDebugf(fmt"\n", __VA_ARGS__); \
- SK_ABORT("assert(" #cond ")"); \
- })
+ #define SkASSERT(cond) SkASSERT_RELEASE(cond)
+ #define SkASSERTF(cond, fmt, ...) static_cast<void>( (cond) ? (void)0 : [&]{ \
+ SkDebugf(fmt"\n", __VA_ARGS__); \
+ SK_ABORT("assert(" #cond ")"); \
+ }() )
#define SkDEBUGFAIL(message) SK_ABORT(message)
#define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__)
#define SkDEBUGCODE(...) __VA_ARGS__
#define SkDEBUGF(args ) SkDebugf args
#define SkAssertResult(cond) SkASSERT(cond)
#else
- #define SkASSERT(cond)
- #define SkASSERTF(cond, fmt, ...)
+ #define SkASSERT(cond) static_cast<void>(0)
+ #define SkASSERTF(cond, fmt, ...) static_cast<void>(0)
#define SkDEBUGFAIL(message)
#define SkDEBUGFAILF(fmt, ...)
#define SkDEBUGCODE(...)