summaryrefslogtreecommitdiffstats
path: root/ppapi
diff options
context:
space:
mode:
authordmichael@google.com <dmichael@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-10 21:36:06 +0000
committerdmichael@google.com <dmichael@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-10 21:36:06 +0000
commit7631cf8806adf505db4fed53210e27d2d1dd2cd0 (patch)
tree334a70835e67cbfdf4257b0e9041950b3082d009 /ppapi
parent3ce42e2d90efdb63b2f34d62b48e7ae3901566fb (diff)
downloadchromium_src-7631cf8806adf505db4fed53210e27d2d1dd2cd0.zip
chromium_src-7631cf8806adf505db4fed53210e27d2d1dd2cd0.tar.gz
chromium_src-7631cf8806adf505db4fed53210e27d2d1dd2cd0.tar.bz2
Add a macro (PP_ENUM) that ensures that enums are always 4 bytes. Wherever
possible (Visual Studio C++ and GCC C++0x mode), specify int32_t as the representation type of the enum. For C, where enum gives us no type safety, just typedef an int32_t to the enum name. In all other cases, use a compile-time assertion to ensure that enums are 4 bytes wide. This change only applies the new PP_ENUM macro in 2 commonly used places. This is to make sure that the (possibly contraversial) usage of variadic macro arguments is acceptable before I go change it everywhere. If I can't use variadic macro arguments, I think I will have to separate this in to 2 macros and the enum declarations will look something like this: PP_ENUM_BEGIN(PP_Bool) PP_FALSE=0, PP_TRUE=1 PP_ENUM_END(PP_Bool) BUG=61004 TEST=This should be exercised by all tests that use PP_Bool and PP_Var. Suggestions for improvement are welcome, as always. Review URL: http://codereview.chromium.org/4720005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65704 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r--ppapi/c/pp_bool.h5
-rw-r--r--ppapi/c/pp_macros.h12
-rw-r--r--ppapi/c/pp_var.h2
3 files changed, 19 insertions, 0 deletions
diff --git a/ppapi/c/pp_bool.h b/ppapi/c/pp_bool.h
index 44c360e..bbb5e0ea 100644
--- a/ppapi/c/pp_bool.h
+++ b/ppapi/c/pp_bool.h
@@ -5,6 +5,8 @@
#ifndef PPAPI_C_PP_BOOL_H_
#define PPAPI_C_PP_BOOL_H_
+#include "ppapi/c/pp_macros.h"
+
/**
* @file
* Defines the API ...
@@ -23,6 +25,9 @@ typedef enum {
PP_FALSE = 0,
PP_TRUE = 1
} PP_Bool;
+
+PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_Bool, 4);
+
/**
* @}
* End addtogroup PP
diff --git a/ppapi/c/pp_macros.h b/ppapi/c/pp_macros.h
index e864cc1..ffc48b0 100644
--- a/ppapi/c/pp_macros.h
+++ b/ppapi/c/pp_macros.h
@@ -37,6 +37,18 @@
# endif
#endif
+/* This is a compile-time assertion useful for ensuring that a given type is
+ a given number of bytes wide. The size of the array is designed to be 1
+ (which should always be valid) if the enum's size is SIZE, and otherwise the
+ size of the array will be -1 (which all/most compilers should flag as an
+ error). This is wrapped inside a struct, because if it is a simple global
+ we get multiple definition errors at link time. */
+#define PP_COMPILE_ASSERT_SIZE_IN_BYTES(NAME, SIZE) \
+struct _dummy_struct_for_##NAME { \
+char _COMPILE_ASSERT_FAILED_The_type_named_ \
+## NAME ## _is_not_ ## SIZE ## \
+_bytes_wide[(sizeof(NAME) == SIZE) ? 1 : -1]; }
+
/**
* @}
* End of addtogroup PP
diff --git a/ppapi/c/pp_var.h b/ppapi/c/pp_var.h
index 81596d6..fad5db4 100644
--- a/ppapi/c/pp_var.h
+++ b/ppapi/c/pp_var.h
@@ -27,6 +27,8 @@ typedef enum {
PP_VARTYPE_OBJECT
} PP_VarType;
+PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_VarType, 4);
+
/**
* Do not rely on having a predictable and reproducible
* int/double differentiation.