Added sleep_ms() and check_debugger() functions + moved various auxiliary macros into the MUtils library.

This commit is contained in:
LoRd_MuldeR 2014-11-30 21:33:03 +01:00
parent 4d41d882da
commit 3676070821
5 changed files with 74 additions and 18 deletions

View 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)

View File

@ -101,25 +101,12 @@ namespace MUtils
///////////////////////////////////////////////////////////////////////////////
#define MUTILS_DELETE(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)
#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()))

View 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);
}

View File

@ -609,6 +609,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
///////////////////////////////////////////////////////////////////////////////

View 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