Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(522)

Unified Diff: include/core/SkTypes.h

Issue 1123923002: Update our SK_ARRAY_COUNT macro to account for when pointers are passed. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: cq-dry-run Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/core/SkTypes.h
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h
index a1719f89e10e4f86a256c6aa178e94e8651e3c9e..01736566457a07fa7c9b2e1a68baeff874f922db 100644
--- a/include/core/SkTypes.h
+++ b/include/core/SkTypes.h
@@ -296,9 +296,28 @@ static inline bool SkIsU16(long x) {
#define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1)
#endif
+// The SK_ARRAY_COUNT(arr) macro returns the # of elements in an array arr.
+// The expression is a compile-time constant, and therefore can be
+// used in defining new arrays, for example. If you use SK_ARRAY_COUNT on
+// a pointer by mistake, you will get a compile-time error.
+//
+// This template function declaration is used in defining SK_ARRAY_COUNT.
+// Note that the function doesn't need an implementation, as we only
+// use its type.
+template <typename T, size_t N>
+char (&ArraySizeHelper(T (&array)[N]))[N];
+
+// That gcc wants both of these prototypes seems mysterious. VC, for
+// its part, can't decide which to use (another mystery). Matching of
+// template overloads: the final frontier.
+#ifndef _MSC_VER
+template <typename T, size_t N>
+char (&ArraySizeHelper(const T (&array)[N]))[N];
+#endif
+
/** Returns the number of entries in an array (not a pointer)
*/
-#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
+#define SK_ARRAY_COUNT(array) (sizeof(::ArraySizeHelper(array)))
#define SkAlign2(x) (((x) + 1) >> 1 << 1)
#define SkIsAlign2(x) (0 == ((x) & 1))
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698