OSDN Git Service

Added sleep_ms() and check_debugger() functions + moved various auxiliary macros...
authorLoRd_MuldeR <mulder2@gmx.de>
Sun, 30 Nov 2014 20:33:03 +0000 (21:33 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Sun, 30 Nov 2014 20:33:03 +0000 (21:33 +0100)
include/MUtils/Exception.h
include/MUtils/Global.h
include/MUtils/OSSupport.h
src/OSSupport_Win32.cpp
src/Startup.cpp

index a9b256d..1ece717 100644 (file)
@@ -55,3 +55,11 @@ while(0)
        throw std::runtime_error((MESSAGE)); \
 } \
 while(0)
+
+#define MUTILS_THROW_FMT(MESSAGE, ...) do \
+{ \
+       char _message[256]; \
+       _snprintf_s(_message, 256, _TRUNCATE, (MESSAGE), __VA_ARGS__); \
+       throw std::runtime_error(_message); \
+} \
+while(0)
index f44cec3..16029ca 100644 (file)
@@ -101,25 +101,12 @@ namespace MUtils
 
 ///////////////////////////////////////////////////////////////////////////////
 
-#define MUTILS_DELETE(PTR) do \
-{ \
-       if((PTR)) \
-       { \
-               delete (PTR); \
-               (PTR) = NULL; \
-       } \
-} \
-while(0)
-
-#define MUTILS_DELETE_ARRAY(PTR) do \
-{ \
-       if((PTR)) \
-       { \
-               delete [] (PTR); \
-               (PTR) = NULL; \
-       } \
-} \
-while(0)
+//Delete helper
+#define MUTILS_DELETE(PTR) do { if((PTR)) { delete (PTR); (PTR) = NULL; } } while(0)
+#define MUTILS_DELETE_ARRAY(PTR) do { if((PTR)) { delete [] (PTR); (PTR) = NULL; } } while(0)
+
+//Zero memory
+#define MUTILS_ZERO_MEMORY(PTR) memset(&(PTR), 0, sizeof((PTR)))
 
 //String conversion macros
 #define MUTILS_WCHR(STR) (reinterpret_cast<const wchar_t*>((STR).utf16()))
index 1cd5498..f6c8a8c 100644 (file)
@@ -123,6 +123,12 @@ namespace MUtils
                //Message handler
                MUTILS_API bool handle_os_message(const void *const message, long *result);
 
+               //Sleep
+               MUTILS_API void sleep_ms(const size_t &duration);
+
+               //Check if debugger is present
+               MUTILS_API void check_debugger(void);
+
                //Error handling
                MUTILS_API void fatal_exit(const wchar_t* const errorMessage);
        }
index 46dd086..b31adda 100644 (file)
@@ -610,6 +610,59 @@ bool MUtils::OS::handle_os_message(const void *const message, long *result)
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// SLEEP
+///////////////////////////////////////////////////////////////////////////////
+
+void MUtils::OS::sleep_ms(const size_t &duration)
+{
+       Sleep((DWORD) duration);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// DEBUGGER CHECK
+///////////////////////////////////////////////////////////////////////////////
+
+#if (!(MUTILS_DEBUG))
+static __forceinline bool is_debugger_present(void)
+{
+       __try
+       {
+               CloseHandle((HANDLE)((DWORD_PTR)-3));
+       }
+       __except(1)
+       {
+               return true;
+       }
+
+       BOOL bHaveDebugger = FALSE;
+       if(CheckRemoteDebuggerPresent(GetCurrentProcess(), &bHaveDebugger))
+       {
+               if(bHaveDebugger) return true;
+       }
+
+       return IsDebuggerPresent();
+}
+static __forceinline bool check_debugger_helper(void)
+{
+       if(is_debugger_present())
+       {
+               MUtils::OS::fatal_exit(L"Not a debug build. Please unload debugger and try again!");
+               return true;
+       }
+       return false;
+}
+#else
+#define check_debugger_helper() (false)
+#endif
+
+void MUtils::OS::check_debugger(void)
+{
+       check_debugger_helper();
+}
+
+static volatile bool g_debug_check = check_debugger_helper();
+
+///////////////////////////////////////////////////////////////////////////////
 // FATAL EXIT
 ///////////////////////////////////////////////////////////////////////////////
 
index 5d64585..16da7a4 100644 (file)
@@ -104,6 +104,7 @@ int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entr
        __try
        {
                MUtils::ErrorHandler::initialize();
+               MUtils::OS::check_debugger();
                iResult = startup_helper(argc, argv, entry_point);
        }
        __except(1)
@@ -113,6 +114,7 @@ int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entr
        }
 #else //_MSCVER
        MUtils::ErrorHandler::initialize();
+       MUtils::OS::check_debugger();
        iResult = startup_helper(argc, argv, entry_point);
 #endif //_MSCVER
 #endif //MUTILS_DEBUG