Commits

gyulalaszlo committed eeae85c Draft

Refactor MSVC-specific code to platform.msvc.h and remove the compiler warnings.

Comments (0)

Files changed (3)

 		return (const void*)((const char *)p - bytes);
 	}
 }
+
+#if _MSC_VER
+	// Override the MAKE_DELETE macro to disable the warning in MSVC
+	#include "platform.msvc.h"
+#endif
+#pragma once
+// # MSVC (2010) specific code
+#ifdef _MSC_VER
+
+#include <stdarg.h>
+#include <stdio.h>
+
+/// Snprintf is not part of C89.
+/// It's standard only in C99. Microsoft has no plan supporting C99.
+#define snprintf c99_snprintf
+
+
+inline int c99_vsnprintf( char* str, size_t size, const char* format, va_list ap )
+{
+    int count = -1;
+    if ( size != 0 )
+    {
+        count = _vsnprintf_s( str, size, _TRUNCATE, format, ap );
+    }
+    if ( count == -1 )
+    {
+        count = _vscprintf( format, ap );
+    }
+    return count;
+}
+
+
+/// MSVC implementation of the C99 snprintf function.
+/// Original source: http://stackoverflow.com/a/8712996/147778
+inline int c99_snprintf( char* str, size_t size, const char* format, ... )
+{
+    int count;
+    va_list ap;
+    va_start( ap, format );
+    count = c99_vsnprintf( str, size, format, ap );
+    va_end( ap );
+    return count;
+}
+
+// Macro Overrides
+// ---------------
+// 
+// Ugly hack to disable warnings for macro overrides.
+__pragma(warning(push))
+__pragma(warning(disable:4005))
+
+/// Disable the compiler warning C4127 for the do {} while(0) loops.
+/// Original source: http://stackoverflow.com/a/1946485/147778
+#define MAKE_DELETE(a, T, p) \
+    __pragma(warning(push)) \
+    __pragma(warning(disable:4127)) \
+    do {if (p) {(p)->~T(); a.deallocate(p);}} while (0) \
+    __pragma(warning(pop))
+
+// Restore the macro override warning.
+__pragma(warning(pop))
+
+#endif // _MSC_VER
 #include <stdio.h>
 
 
-// snprintf is not part of C89. It's standard only in C99. Microsoft has no plan supporting C99.
-// http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
-// 
-// This definition results in a warning on MSVC.
-#if _MSC_VER
-#define snprintf _snprintf
+#if _MSC_VER
+	// MSVC requires snprintf.
+	#include "platform.msvc.h"
 #endif
 
 
+
 namespace foundation
 {
 	/// Functions for operating on an Array<char> as a stream of characters,