2018-08-23 00:24:55 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-02-09 13:30:38 +08:00
|
|
|
#include <array>
|
2018-08-23 00:24:55 +08:00
|
|
|
#include <unordered_map>
|
2018-11-05 10:19:00 +08:00
|
|
|
#include <unordered_set>
|
2018-08-23 00:24:55 +08:00
|
|
|
#include <functional>
|
2018-10-31 13:20:44 +08:00
|
|
|
#include <algorithm>
|
2018-11-05 10:19:00 +08:00
|
|
|
#include <regex>
|
2018-10-31 13:20:44 +08:00
|
|
|
#include <memory>
|
2018-10-10 19:03:15 +08:00
|
|
|
#include <optional>
|
2018-08-23 00:24:55 +08:00
|
|
|
#include <thread>
|
2018-08-23 23:53:23 +08:00
|
|
|
#include <mutex>
|
2019-01-24 02:59:34 +08:00
|
|
|
#include <shared_mutex>
|
2019-02-09 13:30:38 +08:00
|
|
|
#include <atomic>
|
2018-09-21 09:59:07 +08:00
|
|
|
#include <cstdint>
|
2018-12-27 12:56:42 +08:00
|
|
|
#include <cassert>
|
2019-02-19 12:12:12 +08:00
|
|
|
|
2019-06-01 02:48:07 +08:00
|
|
|
#ifdef _WIN64
|
|
|
|
constexpr bool x64 = true;
|
|
|
|
#else
|
|
|
|
constexpr bool x64 = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MESSAGE(text) MessageBoxW(NULL, text, L"Textractor", MB_OK)
|
|
|
|
|
2019-02-19 12:12:12 +08:00
|
|
|
template <typename T> using Array = T[];
|
|
|
|
|
|
|
|
template<typename E, typename M = std::mutex>
|
|
|
|
class ThreadSafe
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template <typename... Args>
|
|
|
|
ThreadSafe(Args&&... args) : contents(std::forward<Args>(args)...) {}
|
|
|
|
auto operator->()
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
E* operator->() { return ptr; }
|
|
|
|
std::unique_lock<M> lock;
|
|
|
|
E* ptr;
|
|
|
|
} lockedProxy{ std::unique_lock(mtx), &contents };
|
|
|
|
return lockedProxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
E contents;
|
|
|
|
M mtx;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <auto F>
|
|
|
|
struct Functor
|
|
|
|
{
|
|
|
|
template <typename... Args>
|
|
|
|
auto operator()(Args&&... args) const { return std::invoke(F, std::forward<Args>(args)...); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename HandleCloser = Functor<CloseHandle>>
|
|
|
|
class AutoHandle
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoHandle(HANDLE h) : h(h) {}
|
|
|
|
operator HANDLE() { return h.get(); }
|
|
|
|
PHANDLE operator&() { static_assert(sizeof(*this) == sizeof(HANDLE)); return (PHANDLE)this; }
|
|
|
|
operator bool() { return h.get() != NULL && h.get() != INVALID_HANDLE_VALUE; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct HandleCleaner { void operator()(void* h) { if (h != INVALID_HANDLE_VALUE) HandleCloser()(h); } };
|
|
|
|
std::unique_ptr<void, HandleCleaner> h;
|
|
|
|
};
|
|
|
|
|
2019-06-02 01:59:37 +08:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable: 4996)
|
|
|
|
template <typename... Args>
|
|
|
|
inline std::string FormatString(const char* format, Args... args)
|
|
|
|
{
|
|
|
|
std::string buffer(snprintf(nullptr, 0, format, args...), '\0');
|
|
|
|
sprintf(buffer.data(), format, args...);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
inline std::wstring FormatWideString(const wchar_t* format, Args... args)
|
|
|
|
{
|
|
|
|
std::wstring buffer(_snwprintf(nullptr, 0, format, args...), L'\0');
|
|
|
|
_swprintf(buffer.data(), format, args...);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
2019-02-19 12:12:12 +08:00
|
|
|
#ifdef _DEBUG
|
2019-06-02 01:59:37 +08:00
|
|
|
#define TEST(...) inline auto TEST_RUNNER_DUMMY = CreateThread(nullptr, 0, [](auto) { __VA_ARGS__; return 0UL; }, NULL, 0, nullptr);
|
2019-02-19 12:12:12 +08:00
|
|
|
#else
|
|
|
|
#define TEST(...)
|
|
|
|
#endif
|
2019-02-28 14:40:40 +08:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2019-06-02 01:59:37 +08:00
|
|
|
#define TEST_SYNC(...) inline auto TEST_RUNNER_DUMMY = [] { __VA_ARGS__; return 0UL; }();
|
2019-02-28 14:40:40 +08:00
|
|
|
#else
|
|
|
|
#define TEST_SYNC(...)
|
|
|
|
#endif
|