1
From: Joe Nahmias <joe@nahmias.net>
2
Date: Sun, 2 Mar 2014 13:44:53 -0500
3
Subject: Use C++11 standard static assertion functionality
5
Use static_assert() provided by MSVC (>=2010) [released on 2010-04-12]
6
and gcc (>=4.3) [released on 2008-03-05]. See also:
7
<http://www.pixelbeat.org/programming/gcc/static_assert.html>
9
Change all callers to provide an error message if the assertion fails as
10
required by the standard.
13
src/lua-engine.cpp | 4 ++--
14
src/types-des.h | 4 ----
15
src/types.h | 13 +++++++++++--
16
src/utils/endian.h | 4 ++--
17
5 files changed, 19 insertions(+), 10 deletions(-)
19
diff --git a/SConstruct b/SConstruct
20
index 4d5b446..e834d71 100644
23
@@ -77,6 +77,10 @@ if env['PLATFORM'] == 'cygwin':
24
env.Append(LINKFLAGS = " -mno-cygwin")
25
env['LIBS'] = ['wsock32'];
27
+# tell g++ to support c++0x, used for static_assert()
28
+if env['CXX'] == 'g++':
29
+ env.Append(CXXFLAGS = "-std=c++0x")
31
if env['PLATFORM'] == 'win32':
32
env.Append(CPPPATH = [".", "drivers/win/", "drivers/common/", "drivers/", "drivers/win/zlib", "drivers/win/directx", "drivers/win/lua/include"])
33
env.Append(CPPDEFINES = ["PSS_STYLE=2", "WIN32", "_USE_SHARED_MEMORY_", "NETWORK", "FCEUDEF_DEBUGGER", "NOMINMAX", "NEED_MINGW_HACKS", "_WIN32_IE=0x0600"])
34
diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp
35
index 53774f7..0d0fd0e 100644
36
--- a/src/lua-engine.cpp
37
+++ b/src/lua-engine.cpp
38
@@ -241,7 +241,7 @@ static const char* luaCallIDStrings [] =
41
//make sure we have the right number of strings
42
-CTASSERT(sizeof(luaCallIDStrings)/sizeof(*luaCallIDStrings) == LUACALL_COUNT)
43
+CTASSERT(sizeof(luaCallIDStrings)/sizeof(*luaCallIDStrings) == LUACALL_COUNT, "luaCallIDStrings doesn't have the correct number of strings");
45
static const char* luaMemHookTypeStrings [] =
47
@@ -255,7 +255,7 @@ static const char* luaMemHookTypeStrings [] =
50
//make sure we have the right number of strings
51
-CTASSERT(sizeof(luaMemHookTypeStrings)/sizeof(*luaMemHookTypeStrings) == LUAMEMHOOK_COUNT)
52
+CTASSERT(sizeof(luaMemHookTypeStrings)/sizeof(*luaMemHookTypeStrings) == LUAMEMHOOK_COUNT, "luaMemHookTypeStrings doesn't have the correct number of strings");
54
static char* rawToCString(lua_State* L, int idx=0);
55
static const char* toCString(lua_State* L, int idx=0);
56
diff --git a/src/types-des.h b/src/types-des.h
57
index e94536b..76cd2d9 100644
60
@@ -442,10 +442,6 @@ char (*BLAHBLAHBLAH( UNALIGNED T (&)[N] ))[N];
61
//B32(10000000,11111111,10101010,01010101) = 2164238933
62
//---------------------------
65
-#define CTASSERT(x) typedef char __assert ## y[(x) ? 1 : -1]
68
static const char hexValid[23] = {"0123456789ABCDEFabcdef"};
71
diff --git a/src/types.h b/src/types.h
72
index fca8d8f..d44730c 100644
75
@@ -128,8 +128,17 @@ typedef uint32_t uint32;
76
typedef void (*writefunc)(uint32 A, uint8 V);
77
typedef uint8 (*readfunc)(uint32 A);
80
-#define CTASSERT(x) typedef char __assert ## y[(x) ? 1 : -1];
81
+// Use C++11 static_assert(), if available
82
+// See: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1720.html
84
+ // Visual Studio >= 2010
85
+# define CTASSERT(expr, msg) static_assert(expr, msg)
86
+#elif ( 4 < __GNUC__ || (4 == __GNUC__ && 3 <= __GNUC_MINOR__) ) \
87
+ && defined __GXX_EXPERIMENTAL_CXX0X__
88
+ // g++ >= 4.3 (with -std=c++0x)
89
+# define CTASSERT(expr, msg) static_assert(expr, msg)
91
+# define CTASSERT(expr,msg) typedef static char __assert_ ## msg[(expr) ? 1 : -1];
94
#include "utils/endian.h"
95
diff --git a/src/utils/endian.h b/src/utils/endian.h
96
index 5bdbcde..6928d1c 100644
97
--- a/src/utils/endian.h
98
+++ b/src/utils/endian.h
99
@@ -82,7 +82,7 @@ inline int read_double_le(double *Bufo, EMUFILE*is) { uint64 temp; int ret = rea
101
int readle(T *Bufo, EMUFILE*is)
103
- CTASSERT(sizeof(T)==1||sizeof(T)==2||sizeof(T)==4||sizeof(T)==8);
104
+ CTASSERT(sizeof(T)==1||sizeof(T)==2||sizeof(T)==4||sizeof(T)==8, "readle() called with bad Bufo size");
106
case 1: return read8le((uint8*)Bufo,is);
107
case 2: return read16le((uint16*)Bufo,is);
108
@@ -96,7 +96,7 @@ int readle(T *Bufo, EMUFILE*is)
110
int writele(T *Bufo, EMUFILE*os)
112
- CTASSERT(sizeof(T)==1||sizeof(T)==2||sizeof(T)==4||sizeof(T)==8);
113
+ CTASSERT(sizeof(T)==1||sizeof(T)==2||sizeof(T)==4||sizeof(T)==8, "writele() called with bad Bufo size");
115
case 1: return write8le(*(uint8*)Bufo,os);
116
case 2: return write16le(*(uint16*)Bufo,os);