#pragma once
#include "Platform.h"
#include "Lock.h"

/**
 * Note: This file can not be named DbgHelp due to collision with the dbghelp.h file in the Windows
 * API.
 */


#ifdef WINDOWS

// We want wchars in the lib!
#define DBGHELP_TRANSLATE_TCHAR
#include <Dbghelp.h>
#pragma comment (lib, "Dbghelp.lib")

/**
 * Helpers for using DbgHelp on Windows.
 */
class DbgHelp : NoCopy {
public:
	// Handle to our process.
	const HANDLE process;

	// Initialized?
	const bool initialized;

	// Lock for dbghelp.
	util::Lock lock;

	// Dtor.
	~DbgHelp();

	// Get the global, static instance of the library.
	// Note: this function is not defined here, since globals do not work
	// as expected when the same code is linked into multiple DLLs. See
	// the macro DEFINE_DBG_HELPER_INSTANCE below.
	static DbgHelp &instance();

private:
	// For use in the main Storm binary.
	friend class DbgHelpManager;

	// Create.
	DbgHelp();
};

// Define the instance method above, in a way that is suitable to use in a single-threaded library.
#define DEFINE_DBG_HELPER_INSTANCE()			\
	DbgHelp &DbgHelp::instance() {				\
		static DbgHelp i;						\
		return i;								\
	}

// Get a pointer to the instance method.
#define DBG_HELPER_INSTANCE_PTR &::DbgHelp::instance

#else

/**
 * Dummy implementation to make it possible to use the type on other systems.
 */
class DbgHelp : NoCopy {};

#define DEFINE_DBG_HELPER_INSTANCE()
#define DBG_HELPER_INSTANCE_PTR static_cast<DbgHelp &(*)()>(0)

#endif
