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

Unified Diff: third_party/WebKit/Source/wtf/SizeAssertions.h

Issue 2231953003: Added ASSERT_SIZE macro for checking size of size-sensitive classes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: g cl try Created 4 years, 3 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 | « third_party/WebKit/Source/wtf/BUILD.gn ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/SizeAssertions.h
diff --git a/third_party/WebKit/Source/wtf/SizeAssertions.h b/third_party/WebKit/Source/wtf/SizeAssertions.h
new file mode 100644
index 0000000000000000000000000000000000000000..d321392ba1bdfd09b217ce66018c9af51bd40c49
--- /dev/null
+++ b/third_party/WebKit/Source/wtf/SizeAssertions.h
@@ -0,0 +1,25 @@
+#ifndef WTF_SizeAssertions_h
esprehn 2016/09/08 22:25:28 missing the short copyright header
sashab 2016/09/09 01:23:47 Thanks! Also removed unused #include below. Still
+#define WTF_SizeAssertions_h
+
+#include "wtf/CPU.h"
+
+namespace WTF {
+
+// The ASSERT_SIZE macro can be used to check that a given struct is the same
+// size as a class. This is useful to visualize where the space is being used in
+// a class, as well as give a useful compile error message when the size doesn't
+// match the expected value.
+template<class T, class U> struct assert_size {
+ template<int ActualSize, int ExpectedSize> struct assertSizeEqual {
+ static_assert(ActualSize == ExpectedSize, "Class should stay small");
+ static const bool innerValue = true;
+ };
+ static const bool value = assertSizeEqual<sizeof(T), sizeof(U)>::innerValue;
+};
+
+} // namespace WTF
+
+#define ASSERT_SIZE(className, sameSizeAsClassName) \
+ static_assert(WTF::assert_size<className, sameSizeAsClassName>::value, "");
+
+#endif // WTF_SizeAssertions_h
« no previous file with comments | « third_party/WebKit/Source/wtf/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698