Textractor_test/include/common.h

118 lines
2.8 KiB
C
Raw Normal View History

2018-08-23 00:24:55 +08:00
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <string>
#include <vector>
#include <deque>
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>
#include <algorithm>
2018-11-05 10:19:00 +08:00
#include <regex>
#include <memory>
#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
2019-02-19 12:12:12 +08:00
template <typename T> using Array = T[];
2019-08-07 17:18:04 +08:00
template <auto F> using Functor = std::integral_constant<std::decay_t<decltype(F)>, F>;
template <typename V>
struct Identity { V operator()(V v) const { return v; } };
struct PermissivePointer
{
template <typename T>
operator T*() { return (T*)p; }
void* p;
};
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)); assert(!h); 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()(PermissivePointer{ h }); } };
std::unique_ptr<void, HandleCleaner> h;
};
2019-06-10 13:49:11 +08:00
template<typename T, typename M = std::mutex>
2019-06-05 11:12:45 +08:00
class Synchronized
2019-02-19 12:12:12 +08:00
{
public:
template <typename... Args>
2019-06-05 11:12:45 +08:00
Synchronized(Args&&... args) : contents(std::forward<Args>(args)...) {}
struct Locker
2019-02-19 12:12:12 +08:00
{
2019-06-10 13:49:11 +08:00
T* operator->() { return &contents; }
2019-06-05 11:12:45 +08:00
std::unique_lock<M> lock;
2019-06-10 13:49:11 +08:00
T& contents;
2019-06-05 11:12:45 +08:00
};
2019-06-10 13:49:11 +08:00
Locker Acquire() { return { std::unique_lock(m), contents }; }
2019-06-05 11:12:45 +08:00
Locker operator->() { return Acquire(); }
2019-02-19 12:12:12 +08:00
private:
2019-06-10 13:49:11 +08:00
T contents;
M m;
2019-02-19 12:12:12 +08:00
};
2019-06-09 19:33:26 +08:00
static struct
2019-06-04 05:58:30 +08:00
{
BYTE DUMMY[100];
template <typename T>
operator T*() { static_assert(sizeof(T) < sizeof(DUMMY)); return (T*)DUMMY; }
} DUMMY;
2019-06-13 16:01:29 +08:00
template <typename T>
inline auto FormatArg(T arg) { return arg; }
template <typename C>
inline auto FormatArg(const std::basic_string<C>& arg) { return arg.c_str(); }
2019-06-02 01:59:37 +08:00
#pragma warning(push)
#pragma warning(disable: 4996)
template <typename... Args>
2019-06-29 18:13:26 +08:00
inline std::string FormatString(const char* format, const Args&... args)
2019-06-02 01:59:37 +08:00
{
2019-06-13 16:01:29 +08:00
std::string buffer(snprintf(nullptr, 0, format, FormatArg(args)...), '\0');
sprintf(buffer.data(), format, FormatArg(args)...);
2019-06-02 01:59:37 +08:00
return buffer;
}
template <typename... Args>
2019-06-29 18:13:26 +08:00
inline std::wstring FormatString(const wchar_t* format, const Args&... args)
2019-06-02 01:59:37 +08:00
{
2019-06-13 16:01:29 +08:00
std::wstring buffer(_snwprintf(nullptr, 0, format, FormatArg(args)...), L'\0');
_swprintf(buffer.data(), format, FormatArg(args)...);
2019-06-02 01:59:37 +08:00
return buffer;
}
#pragma warning(pop)
2019-09-11 06:53:55 +08:00
#define MESSAGE(text) MessageBoxW(NULL, FormatArg(text), L"Textractor", MB_OK)
2019-02-19 12:12:12 +08:00
#ifdef _DEBUG
2019-06-13 16:01:29 +08:00
#define TEST(...) static auto _ = CreateThread(nullptr, 0, [](auto) { __VA_ARGS__; return 0UL; }, NULL, 0, nullptr);
2019-02-19 12:12:12 +08:00
#else
#define TEST(...)
#endif