1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include "deimos/core/os.h"
#include "deimos/core/api_registry.h"
#include "deimos/core/allocator.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
namespace deimos
{
gsl::cwzstring Utf8ToUtf16Z(StringView src, Span<wchar_t> buffer)
{
Expects(buffer.size() > 0 && buffer.size() > src.size());
int res = ::MultiByteToWideChar(CP_UTF8, 0, src.data(), (int)src.size_bytes(), buffer.data(), (int)buffer.size() - 1);
if (res < 0) { return L"< MBTWC ERROR >"; }
buffer[res] = L'\0';
return buffer.data();
}
class Win32ConsoleApiImpl : public OsConsoleApi
{
HANDLE m_stdout;
HANDLE m_stderr;
constexpr HANDLE Handle(OsConsoleType type) const
{
switch (type)
{
using enum OsConsoleType;
case kStdOut: return m_stdout;
case kStdErr: return m_stderr;
}
}
public:
Win32ConsoleApiImpl() :
m_stdout{::GetStdHandle(STD_OUTPUT_HANDLE)},
m_stderr{::GetStdHandle(STD_ERROR_HANDLE)}
{}
void Write(OsConsoleType type, Span<const std::byte> data) override
{
::WriteConsoleA(Handle(type), data.data(), (DWORD)data.size(), nullptr, nullptr);
}
};
class Win32DllApiImpl : public OsDllApi
{
public:
OsDll* Open(gsl::czstring name) override
{
return std::bit_cast<OsDll*>(::LoadLibraryA(name));
}
void* GetSymbol(OsDll* dll, gsl::czstring name) override
{
return std::bit_cast<void*>(::GetProcAddress(std::bit_cast<HMODULE>(dll), name));
}
};
class Win32VirtualMemoryApiImpl : public OsVirtualMemoryApi
{
public:
gsl::owner<void*> Map(int64_t size) override
{
return ::VirtualAlloc(nullptr, (SIZE_T)size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
}
void Unmap(gsl::owner<void*> ptr) override
{
::VirtualFree(ptr, 0, MEM_RELEASE);
}
gsl::owner<void*> Reserve(int64_t size) override
{
return ::VirtualAlloc(nullptr, (SIZE_T)size, MEM_RESERVE, PAGE_READWRITE);
}
void Commit(void* ptr, int64_t size) override
{
::VirtualAlloc(ptr, (SIZE_T)size, MEM_COMMIT, PAGE_READWRITE);
}
};
class Win32WindowApiImpl : public OsWindowApi
{
static constexpr wchar_t kClassName[] = L"Deimos window class";
bool m_class_registered = false;
void RegisterClass()
{
Expects(!m_class_registered);
WNDCLASSW wnd_class{};
wnd_class.lpfnWndProc = ::DefWindowProcW;
wnd_class.hInstance = ::GetModuleHandleW(nullptr);
wnd_class.lpszClassName = kClassName;
::RegisterClassW(&wnd_class);
m_class_registered = true;
}
public:
StatusOr<gsl::owner<OsWindowHandle*>> Create(gsl::czstring title, int32_t width, int32_t height) override
{
if (!m_class_registered)
{
RegisterClass();
}
Ensures(m_class_registered);
wchar_t title_w_buffer[128]{};
gsl::cwzstring title_w = Utf8ToUtf16Z(title, title_w_buffer);
HWND hwnd = ::CreateWindowExW(
0, kClassName, title_w, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
nullptr, nullptr, ::GetModuleHandle(nullptr), nullptr);
if (hwnd == nullptr)
{
return InternalError("Error while creating Win32 window");
}
::ShowWindow(hwnd, SW_SHOW);
return nullptr;
}
};
class Win32OsApiImpl : public OsApi
{
Win32ConsoleApiImpl m_console_api;
Win32DllApiImpl m_dll_api;
Win32VirtualMemoryApiImpl m_virtual_memory_api;
Win32WindowApiImpl m_window_api;
public:
Win32OsApiImpl()
{
console = &m_console_api;
dll = &m_dll_api;
virtual_memory = &m_virtual_memory_api;
window = &m_window_api;
}
};
void RegisterOsApi(ApiRegistry* api_registry)
{
auto* allocator_api = api_registry->Get<AllocatorApi>();
auto* os_api = allocator_api->system->New<Win32OsApiImpl>();
api_registry->Set(os_api);
}
} // namespace deimos
|