Skip to content

Commit b7a35fe

Browse files
author
James King
committed
Initial commit
0 parents  commit b7a35fe

9 files changed

+369
-0
lines changed

.vscode/c_cpp_properties.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Win32",
5+
"includePath": [
6+
"${workspaceFolder}/**",
7+
"${workspaceFolder}"
8+
],
9+
"defines": [
10+
"_DEBUG",
11+
"UNICODE",
12+
"_UNICODE"
13+
],
14+
"windowsSdkVersion": "10.0.18362.0",
15+
"compilerPath": "D:/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.25.28610/bin/Hostx64/x64/cl.exe",
16+
"cStandard": "c11",
17+
"intelliSenseMode": "msvc-x64"
18+
}
19+
],
20+
"version": 4
21+
}

data.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
template<typename element_type>
4+
struct array {
5+
size_t length;
6+
element_type* data;
7+
};
8+
9+
template<typename value_type>
10+
value_type copy(value_type const& value) {
11+
return value;
12+
}
13+
14+
template<typename element_type, size_t length>
15+
inline array<element_type> array_from_static(element_type (&a)[length]) {
16+
return array<element_type> { length, a };
17+
}
18+
19+
template<typename element_type, size_t length>
20+
struct value_array {
21+
element_type value[length];
22+
};
23+
24+
template<typename storage_type>
25+
inline storage_type const* const_ptr(storage_type const& value) {
26+
return &value;
27+
}

defer.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <tuple>
4+
5+
#define __defer_helper(code, count, tup) \
6+
struct __defer_struct_##count { \
7+
decltype(tup) const& params; \
8+
__defer_struct_##count(decltype(tup) const& t) : params(t) { } \
9+
~__defer_struct_##count() { using namespace std; code; } \
10+
}; \
11+
__defer_struct_##count __defer_variable_##count (tup)
12+
13+
#define __defer_ind(code, count, tup) __defer_helper(code, count, tup)
14+
15+
#define defer(code, ...) __defer_ind(code, __COUNTER__, std::make_tuple(__VA_ARGS__))

directx_util.cpp

Whitespace-only changes.

directx_util.h

Whitespace-only changes.

format.h

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <cstring>
5+
6+
inline void putstr(std::string const& str) {
7+
std::puts(str.c_str());
8+
}
9+
10+
inline void wputstr(std::wstring const& str) {
11+
_putws(str.c_str());
12+
}
13+
14+
namespace std {
15+
inline string to_string(char const* c_str) {
16+
return c_str;
17+
}
18+
19+
inline wstring to_wstring(wchar_t const* c_str) {
20+
return c_str;
21+
}
22+
}
23+
24+
template<typename... arg_types>
25+
inline std::string formatv(std::string const& fstr, arg_types... args) {
26+
std::string result;
27+
size_t arg_index = 0;
28+
29+
if constexpr(sizeof...(arg_types) > 0) {
30+
std::string converted[] = { std::to_string(args)... };
31+
for(char const& c : fstr) {
32+
if(c == '%') {
33+
result += converted[arg_index++];
34+
} else {
35+
result += c;
36+
}
37+
}
38+
} else {
39+
result = fstr;
40+
}
41+
42+
return result;
43+
}
44+
45+
template<typename... arg_types>
46+
inline std::wstring wformatv(std::wstring const& fstr, arg_types... args) {
47+
std::wstring result;
48+
size_t arg_index = 0;
49+
50+
if constexpr(sizeof...(arg_types) > 0) {
51+
std::wstring converted[] = { std::to_wstring(args)... };
52+
for(wchar_t const& c : fstr) {
53+
if(c == '%') {
54+
result += converted[arg_index++];
55+
} else {
56+
result += c;
57+
}
58+
}
59+
} else {
60+
result = fstr;
61+
}
62+
63+
return result;
64+
}
65+
66+
template<typename... arg_type>
67+
inline void printv(std::string const& fstr, arg_type... args) {
68+
std::string str = formatv(fstr, args...);
69+
putstr(str);
70+
}
71+
72+
template<typename... arg_type>
73+
inline void wprintv(std::wstring const& fstr, arg_type... args) {
74+
std::wstring str = wformatv(fstr, args...);
75+
wputstr(str);
76+
}
77+
78+
template<typename... arg_types>
79+
inline void panicv(std::string const& fstr, arg_types... args) {
80+
printv(fstr, args...);
81+
exit(-1);
82+
}
83+
84+
template<typename... arg_types>
85+
inline void wpanicv(std::wstring const& fstr, arg_types... args) {
86+
wprintv(fstr, args...);
87+
exit(-1);
88+
}

type.h

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#pragma once
2+
3+
// type_eq //
4+
template<typename lhs, typename rhs>
5+
constexpr bool type_eq = false;
6+
7+
template<typename type>
8+
constexpr bool type_eq<type, type> = true;
9+
10+
// is_ptr //
11+
template<typename type>
12+
constexpr bool is_ptr = false;
13+
14+
template<typename type>
15+
constexpr bool is_ptr<type*> = true;
16+
17+
template<typename type>
18+
constexpr bool is_ptr<type* const> = true;
19+
20+
// is_ref //
21+
template<typename type>
22+
constexpr bool is_ref = false;
23+
24+
template<typename type>
25+
constexpr bool is_ref<type&> = true;
26+
27+
// is_by_copy //
28+
template<typename type>
29+
constexpr bool is_by_copy = !is_ptr<type> && !is_ref<type>;
30+
31+
// is_const //
32+
template<typename type>
33+
constexpr bool is_const = false;
34+
35+
template<typename type>
36+
constexpr bool is_const<type const> = true;
37+
38+
// is_instantiation //
39+
template<template<typename> typename temp, typename type>
40+
constexpr bool is_instantiation = false;
41+
42+
template<template<typename> typename temp, typename type>
43+
constexpr bool is_instantiation<temp, temp<type>> = true;
44+
45+
// remove_ref //
46+
namespace {
47+
template<typename t>
48+
struct remove_ref_struct { using type = t; };
49+
50+
template<typename t>
51+
struct remove_ref_struct<t&> { using type = t; };
52+
}
53+
54+
template<typename t>
55+
using remove_ref = typename remove_ref_struct<t>::type;
56+
57+
// remove_ptr //
58+
namespace {
59+
template<typename t>
60+
struct remove_ptr_struct { using type = t; };
61+
62+
template<typename t>
63+
struct remove_ptr_struct<t*> { using type = t; };
64+
}
65+
66+
template<typename t>
67+
using remove_ptr = typename remove_ptr_struct<t>::type;
68+
69+
// remove_const //
70+
namespace {
71+
template<typename t>
72+
struct remove_const_struct { using type = t; };
73+
74+
template<typename t>
75+
struct remove_const_struct<t const> { using type = t; };
76+
77+
template<typename t>
78+
struct remove_const_struct<t* const> { using type = t; };
79+
80+
template<typename t>
81+
struct remove_const_struct<t const* const> { using type = t; };
82+
}
83+
84+
template<typename t>
85+
using remove_const = typename remove_const_struct<t>::type;
86+
87+
// remove_const_from_ptr //
88+
template<typename t>
89+
using remove_const_from_ptr = remove_const<remove_ptr<t>>*;
90+
91+
// remove_const_from_ref //
92+
template<typename t>
93+
using remove_const_from_ref = remove_const<remove_ref<t>>&;
94+
95+
// branch //
96+
namespace {
97+
template<bool b, typename t, typename f>
98+
struct branch_struct;
99+
100+
template<typename t, typename f>
101+
struct branch_struct<true, t, f> { using type = t; };
102+
103+
template<typename t, typename f>
104+
struct branch_struct<false, t, f> { using type = f; };
105+
}
106+
107+
template<bool b, typename t, typename f>
108+
using branch = typename branch_struct<b, t, f>::type;
109+
110+
// concepts //
111+
template<typename type>
112+
concept by_copy = is_by_copy<type>;
113+
114+
template<typename type>
115+
concept non_const_by_copy = is_by_copy<type> && !is_const<type>;
116+
117+
template<typename type>
118+
concept const_by_copy = is_by_copy<type> && is_const<type>;
119+
120+
template<typename type>
121+
concept ptr = is_ptr<type>;
122+
123+
template<typename type>
124+
concept non_const_ptr = is_ptr<type> && !is_const<remove_ptr<type>>;
125+
126+
template<typename type>
127+
concept const_ptr = is_ptr<type> && is_const<remove_ptr<type>>;
128+
129+
template<typename type>
130+
concept ref = is_ref<type>;
131+
132+
template<typename type>
133+
concept non_const_ref = is_ref<type> && !is_const<remove_ref<type>>;
134+
135+
template<typename type>
136+
concept const_ref = is_ref<type> && is_const<remove_ref<type>>;

win_error.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "win_error.h"
2+
#include "format.h"
3+
#include "defer.h"
4+
5+
#include <optional>
6+
7+
constexpr size_t min_error_message_length = 1024;
8+
WCHAR* win_error_message_buffer;
9+
10+
[[noreturn]] void panic_win(int line, WCHAR const* file) {
11+
auto error_code = GetLastError();
12+
13+
if(!error_code) {
14+
wprintv(L"[% line %]\nGot null, but no error code found.", file, line);
15+
exit(-1);
16+
}
17+
18+
DWORD char_count = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, nullptr, error_code, 0, (LPWSTR) (void*) &win_error_message_buffer, min_error_message_length, nullptr);
19+
defer(if(win_error_message_buffer) LocalFree(win_error_message_buffer), win_error_message_buffer);
20+
21+
if(char_count == 0) {
22+
// The error code did not exist in the system errors.
23+
// Try Ntdsbmsg.dll for the error code.
24+
25+
HINSTANCE lib = LoadLibraryA("Ntdsbmsg.dll");
26+
27+
if(lib != NULL) {
28+
char_count = FormatMessageW(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, lib, error_code, 0, (LPWSTR) (void*) &win_error_message_buffer, min_error_message_length, nullptr);
29+
FreeLibrary(lib);
30+
}
31+
}
32+
33+
if(char_count == 0) {
34+
wprintv(L"[% line %]\nUnknown Windows error: %", file, line, error_code);
35+
exit(-1);
36+
}
37+
38+
wprintv(L"[% line %]\n%", file, line, win_error_message_buffer);
39+
exit(-1);
40+
}
41+
42+
void _panic_if_win_err_impl(DWORD error_code, int line, WCHAR const* file) {
43+
if(error_code == S_OK) return;
44+
SetLastError(error_code);
45+
panic_win(line, file);
46+
}

win_error.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <Windows.h>
4+
5+
#define _WD(x) L##x
6+
#define WD(x) _WD(x)
7+
8+
#define panic_if_win_err(code, ...) _panic_if_win_err(code, __LINE__, WD(__FILE__))
9+
#define panic_if_win_null(value) _panic_if_win_null(value, __LINE__, WD(__FILE__))
10+
11+
[[noreturn]] void panic_win(int line, WCHAR const* file);
12+
13+
void _panic_if_win_err_impl(DWORD error_code, int line, WCHAR const* file);
14+
15+
template<typename value_type>
16+
inline value_type _panic_if_win_null(value_type value, int line, WCHAR const* file) {
17+
if(value) {
18+
return value;
19+
}
20+
21+
panic_win(line, file);
22+
}
23+
24+
template<typename... arg_types>
25+
inline auto _panic_if_win_err(auto value, int line, WCHAR const* file, arg_types... args) {
26+
if constexpr(sizeof...(args) == 0) {
27+
_panic_if_win_err_impl((DWORD) (LONGLONG) value, line, file);
28+
return value;
29+
} else {
30+
if(((value == args) || ... || false)) {
31+
return value;
32+
}
33+
34+
_panic_if_win_err_impl((DWORD) (LONGLONG) value, line, file);
35+
}
36+
}

0 commit comments

Comments
 (0)