Browse Source

Fixed va args count macro with MSVC. Added macros test.

bkaradzic 12 years ago
parent
commit
183189f8f0
6 changed files with 81 additions and 15 deletions
  1. 22 7
      include/bx/macros.h
  2. 2 2
      include/bx/os.h
  3. 4 3
      include/bx/string.h
  4. 3 3
      premake/premake4.lua
  5. 49 0
      tests/macros.cpp
  6. 1 0
      tests/vector_header.cpp

+ 22 - 7
include/bx/macros.h

@@ -8,12 +8,21 @@
 
 #include "bx.h"
 
-#define BX_VA_ARGS_COUNT_DETAIL(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11, _a12, _a13, _a14, _a15, _a16, _last, ...) _last
-#define BX_VA_ARGS_COUNT(...) BX_VA_ARGS_COUNT_DETAIL(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
+#if BX_COMPILER_MSVC
+// Workaround MSVS bug...
+#	define BX_VA_ARGS_PASS(...) BX_VA_ARGS_PASS_1_ __VA_ARGS__ BX_VA_ARGS_PASS_2_
+#	define BX_VA_ARGS_PASS_1_ (
+#	define BX_VA_ARGS_PASS_2_ )
+#else
+#	define BX_VA_ARGS_PASS(...) (__VA_ARGS__)
+#endif // BX_COMPILER_MSVC
+
+#define BX_VA_ARGS_COUNT(...) BX_VA_ARGS_COUNT_ BX_VA_ARGS_PASS(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
+#define BX_VA_ARGS_COUNT_(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11, _a12, _a13, _a14, _a15, _a16, _last, ...) _last
 
-#define BX_MACRO_DISPATCHER_DETAIL2(_func, _argCount) _func ## _argCount
-#define BX_MACRO_DISPATCHER_DETAIL1(_func, _argCount) BX_MACRO_DISPATCHER_DETAIL2(_func, _argCount)
-#define BX_MACRO_DISPATCHER(_func, ...) BX_MACRO_DISPATCHER_DETAIL1(_func, BX_VA_ARGS_COUNT(__VA_ARGS__) )
+#define BX_MACRO_DISPATCHER(_func, ...) BX_MACRO_DISPATCHER_1_(_func, BX_VA_ARGS_COUNT(__VA_ARGS__) )
+#define BX_MACRO_DISPATCHER_1_(_func, _argCount) BX_MACRO_DISPATCHER_2_(_func, _argCount)
+#define BX_MACRO_DISPATCHER_2_(_func, _argCount) BX_CONCATENATE(_func, _argCount)
 
 #define BX_MAKEFOURCC(_a, _b, _c, _d) ( ( (uint32_t)(_a) | ( (uint32_t)(_b) << 8) | ( (uint32_t)(_c) << 16) | ( (uint32_t)(_d) << 24) ) )
 
@@ -41,7 +50,7 @@
 #	define BX_NO_VTABLE
 #	define BX_OVERRIDE
 #	define BX_PRINTF_ARGS(_format, _args) __attribute__ ( (format(__printf__, _format, _args) ) )
-#	define BX_STATIC_ASSERT(_condition, ...) static_assert(_condition, __VA_ARGS__)
+#	define BX_STATIC_ASSERT(_condition, ...) static_assert(_condition, "" __VA_ARGS__)
 #	if BX_COMPILER_CLANG || BX_PLATFORM_IOS
 #		define BX_THREAD /* not supported right now */
 #	else
@@ -82,7 +91,13 @@
 #define BX_UNUSED_10(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10) BX_UNUSED_9(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9); BX_UNUSED_1(_a10)
 #define BX_UNUSED_11(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11) BX_UNUSED_10(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10); BX_UNUSED_1(_a11)
 #define BX_UNUSED_12(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11, _a12) BX_UNUSED_11(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11); BX_UNUSED_1(_a12)
-#define BX_UNUSED(...) BX_MACRO_DISPATCHER(BX_UNUSED_, __VA_ARGS__)(__VA_ARGS__)
+
+#if BX_COMPILER_MSVC
+// Workaround MSVS bug...
+#	define BX_UNUSED(...) BX_MACRO_DISPATCHER(BX_UNUSED_, __VA_ARGS__) BX_VA_ARGS_PASS(__VA_ARGS__)
+#else
+#	define BX_UNUSED(...) BX_MACRO_DISPATCHER(BX_UNUSED_, __VA_ARGS__)(__VA_ARGS__)
+#endif // BX_COMPILER_MSVC
 
 #define BX_CLASS_NO_COPY_NO_ASSIGNMENT(_class) \
 			_class(const _class&); \

+ 2 - 2
include/bx/os.h

@@ -12,9 +12,9 @@
 #	include <windows.h>
 #elif BX_PLATFORM_NACL || BX_PLATFORM_ANDROID || BX_PLATFORM_LINUX || BX_PLATFORM_OSX || BX_PLATFORM_IOS
 #	include <sched.h> // sched_yield
-#	if BX_PLATFORM_OSX
+#	if BX_PLATFORM_OSX || BX_PLATFORM_NACL
 #		include <pthread.h> // mach_port_t
-#	endif
+#	endif // BX_PLATFORM_OSX || BX_PLATFORM_NACL
 #	if BX_PLATFORM_NACL
 #		include <sys/nacl_syscalls.h> // nanosleep
 #	else

+ 4 - 3
include/bx/string.h

@@ -12,7 +12,6 @@
 #include <stdarg.h> // va_list
 #include <stdio.h>  // vsnprintf, vsnwprintf
 #include <string.h>
-#include <string>
 #include <wchar.h>  // wchar_t
 
 namespace bx
@@ -223,7 +222,8 @@ namespace bx
 		return len;
 	}
 
-	inline void stringPrintfVargs(std::string& _out, const char* _format, va_list _argList)
+	template <typename Ty>
+	inline void stringPrintfVargs(Ty& _out, const char* _format, va_list _argList)
 	{
 		char temp[2048];
 
@@ -238,7 +238,8 @@ namespace bx
 		_out.append(out);
 	}
 
-	inline void stringPrintf(std::string& _out, const char* _format, ...)
+	template <typename Ty>
+	inline void stringPrintf(Ty& _out, const char* _format, ...)
 	{
 		va_list argList;
 		va_start(argList, _format);

+ 3 - 3
premake/premake4.lua

@@ -37,7 +37,7 @@ project "bx.test"
 	uuid "8a653da8-23d6-11e3-acb4-887628d43830"
 	kind "ConsoleApp"
 
-	debugdir (BX_DIR .."tests")
+	debugdir (BX_DIR .. "tests")
 
 	includedirs {
 		BX_DIR .. "include",
@@ -49,6 +49,6 @@ project "bx.test"
 	}
 
 	files {
-		BX_DIR .. "test/**.cpp",
-		BX_DIR .. "test/**.H",
+		BX_DIR .. "tests/**.cpp",
+		BX_DIR .. "tests/**.H",
 	}

+ 49 - 0
tests/macros.cpp

@@ -0,0 +1,49 @@
+/*
+ * Copyright 2010-2013 Branimir Karadzic. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#include "test.h"
+#include <bx/bx.h>
+
+BX_STATIC_ASSERT(1 == BX_VA_ARGS_COUNT(1) );
+BX_STATIC_ASSERT(2 == BX_VA_ARGS_COUNT(1, 2) );
+BX_STATIC_ASSERT(3 == BX_VA_ARGS_COUNT(1, 2, 3) );
+BX_STATIC_ASSERT(4 == BX_VA_ARGS_COUNT(1, 2, 3, 4) );
+BX_STATIC_ASSERT(5 == BX_VA_ARGS_COUNT(1, 2, 3, 4, 5) );
+BX_STATIC_ASSERT(6 == BX_VA_ARGS_COUNT(1, 2, 3, 4, 5, 6) );
+
+BX_STATIC_ASSERT(  0 == BX_ALIGN_16(  0) );
+BX_STATIC_ASSERT( 16 == BX_ALIGN_16(  1) );
+BX_STATIC_ASSERT( 16 == BX_ALIGN_16( 15) );
+BX_STATIC_ASSERT( 16 == BX_ALIGN_16( 16) );
+BX_STATIC_ASSERT(256 == BX_ALIGN_16(255) );
+
+BX_STATIC_ASSERT(  0 == BX_ALIGN_256(  0) );
+BX_STATIC_ASSERT(256 == BX_ALIGN_256(  1) );
+BX_STATIC_ASSERT(256 == BX_ALIGN_256( 15) );
+BX_STATIC_ASSERT(256 == BX_ALIGN_256(255) );
+BX_STATIC_ASSERT(256 == BX_ALIGN_256(256) );
+BX_STATIC_ASSERT(256 == BX_ALIGN_256(256) );
+BX_STATIC_ASSERT(512 == BX_ALIGN_256(511) );
+
+TEST(macros)
+{
+	uint32_t unused0;
+	BX_UNUSED(unused0);
+
+	uint32_t unused1;
+	BX_UNUSED(unused0, unused1);
+
+	uint32_t unused2;
+	BX_UNUSED(unused0, unused1, unused2);
+
+	CHECK_EQUAL(1, BX_VA_ARGS_COUNT(1) );
+	CHECK_EQUAL(2, BX_VA_ARGS_COUNT(1, 2) );
+	CHECK_EQUAL(3, BX_VA_ARGS_COUNT(1, 2, 3) );
+	CHECK_EQUAL(4, BX_VA_ARGS_COUNT(1, 2, 3, 4) );
+	CHECK_EQUAL(5, BX_VA_ARGS_COUNT(1, 2, 3, 4, 5) );
+	CHECK_EQUAL(6, BX_VA_ARGS_COUNT(1, 2, 3, 4, 5, 6) );
+
+	CHECK(0 == strcmp(BX_STRINGIZE(TEST 1234 %^&*), "TEST 1234 %^&*") );
+}

+ 1 - 0
tests/vector_header.cpp

@@ -25,4 +25,5 @@
  */
 
 // Test that header is standalone
+#include <tinystl/allocator.h>
 #include <tinystl/vector.h>