39#include <source_location>
46#include <system_error>
56 std::is_integral_v<std::time_t> &&
sizeof(std::time_t) ==
sizeof(std::size_t),
57 "wrap std::time_t instead");
60#include <binary_io/file_stream.hpp>
61#include <boost/stl_interfaces/iterator_interface.hpp>
62#include <fmt/format.h>
63#include <spdlog/spdlog.h>
70 using namespace std::literals;
74 template <
class CharT>
83 class = std::enable_if_t<
84 std::is_pointer_v<T>>>
90 class = std::enable_if_t<
91 std::is_pointer_v<T>>>
97 class = std::enable_if_t<
98 std::is_pointer_v<T>>>
103 template <
class CharT, std::
size_t N>
113 static constexpr auto npos =
static_cast<std::size_t
>(-1);
130 [[nodiscard]]
consteval bool empty() const noexcept {
return this->
size() == 0; }
135 template <std::
size_t POS = 0, std::
size_t COUNT = npos>
136 [[nodiscard]]
consteval auto substr() const noexcept
138 return string < CharT, COUNT !=
npos ? COUNT : N - POS > (this->
data() + POS);
144 template <
class CharT, std::
size_t N>
145 string(
const CharT (&)[N]) ->
string<CharT, N - 1>;
149 requires(std::invocable<std::remove_reference_t<EF>>)
156 noexcept(std::is_nothrow_constructible_v<EF, Fn> ||
157 std::is_nothrow_constructible_v<EF, Fn&>)
158 requires(!std::is_same_v<std::remove_cvref_t<Fn>,
scope_exit> &&
159 std::is_constructible_v<EF, Fn>)
161 static_assert(std::invocable<Fn>);
163 if constexpr (!std::is_lvalue_reference_v<Fn> &&
164 std::is_nothrow_constructible_v<EF, Fn>) {
165 _fn.emplace(std::forward<Fn>(a_fn));
173 noexcept(std::is_nothrow_move_constructible_v<EF> ||
174 std::is_nothrow_copy_constructible_v<EF>)
175 requires(std::is_nothrow_move_constructible_v<EF> ||
176 std::is_copy_constructible_v<EF>)
178 static_assert(!(std::is_nothrow_move_constructible_v<EF> && !std::is_move_constructible_v<EF>));
179 static_assert(!(!std::is_nothrow_move_constructible_v<EF> && !std::is_copy_constructible_v<EF>));
181 if (a_rhs.active()) {
182 if constexpr (std::is_nothrow_move_constructible_v<EF>) {
183 _fn.emplace(std::forward<EF>(*a_rhs._fn));
185 _fn.emplace(a_rhs._fn);
196 if (_fn.has_value()) {
204 [[nodiscard]]
bool active() const noexcept {
return _fn.has_value(); }
206 std::optional<std::remove_reference_t<EF>> _fn;
214 class Underlying = std::underlying_type_t<Enum>>
221 static_assert(std::is_enum_v<enum_type>,
"enum_type must be an enum");
222 static_assert(std::is_integral_v<underlying_type>,
"underlying_type must be an integral");
235 template <
class... Args>
237 requires(std::same_as<Args, enum_type>&&...) :
258 [[nodiscard]]
explicit constexpr operator bool() const noexcept {
return _impl !=
static_cast<underlying_type>(0); }
264 template <
class... Args>
266 requires(std::same_as<Args, enum_type>&&...)
272 template <
class... Args>
274 requires(std::same_as<Args, enum_type>&&...)
280 template <
class... Args>
281 [[nodiscard]]
constexpr bool any(Args... a_args)
const noexcept
282 requires(std::same_as<Args, enum_type>&&...)
287 template <
class... Args>
288 [[nodiscard]]
constexpr bool all(Args... a_args)
const noexcept
289 requires(std::same_as<Args, enum_type>&&...)
294 template <
class... Args>
295 [[nodiscard]]
constexpr bool none(Args... a_args)
const noexcept
296 requires(std::same_as<Args, enum_type>&&...)
305 template <
class... Args>
307 std::common_type_t<Args...>,
308 std::underlying_type_t<
309 std::common_type_t<Args...>>>;
313#define SKSE_MAKE_LOGICAL_OP(a_op, a_result) \
314 template <class E, class U1, class U2> \
315 [[nodiscard]] constexpr a_result operator a_op(enumeration<E, U1> a_lhs, enumeration<E, U2> a_rhs) noexcept \
317 return a_lhs.get() a_op a_rhs.get(); \
320 template <class E, class U> \
321 [[nodiscard]] constexpr a_result operator a_op(enumeration<E, U> a_lhs, E a_rhs) noexcept \
323 return a_lhs.get() a_op a_rhs; \
326#define SKSE_MAKE_ARITHMETIC_OP(a_op) \
327 template <class E, class U> \
328 [[nodiscard]] constexpr auto operator a_op(enumeration<E, U> a_enum, U a_shift) noexcept \
329 ->enumeration<E, U> \
331 return static_cast<E>(static_cast<U>(a_enum.get()) a_op a_shift); \
334 template <class E, class U> \
335 constexpr auto operator a_op##=(enumeration<E, U>& a_enum, U a_shift) noexcept \
336 ->enumeration<E, U>& \
338 return a_enum = a_enum a_op a_shift; \
341#define SKSE_MAKE_ENUMERATION_OP(a_op) \
342 template <class E, class U1, class U2> \
343 [[nodiscard]] constexpr auto operator a_op(enumeration<E, U1> a_lhs, enumeration<E, U2> a_rhs) noexcept \
344 ->enumeration<E, std::common_type_t<U1, U2>> \
346 return static_cast<E>(static_cast<U1>(a_lhs.get()) a_op static_cast<U2>(a_rhs.get())); \
349 template <class E, class U> \
350 [[nodiscard]] constexpr auto operator a_op(enumeration<E, U> a_lhs, E a_rhs) noexcept \
351 ->enumeration<E, U> \
353 return static_cast<E>(static_cast<U>(a_lhs.get()) a_op static_cast<U>(a_rhs)); \
356 template <class E, class U> \
357 [[nodiscard]] constexpr auto operator a_op(E a_lhs, enumeration<E, U> a_rhs) noexcept \
358 ->enumeration<E, U> \
360 return static_cast<E>(static_cast<U>(a_lhs) a_op static_cast<U>(a_rhs.get())); \
363 template <class E, class U1, class U2> \
364 constexpr auto operator a_op##=(enumeration<E, U1>& a_lhs, enumeration<E, U2> a_rhs) noexcept \
365 ->enumeration<E, U1>& \
367 return a_lhs = a_lhs a_op a_rhs; \
370 template <class E, class U> \
371 constexpr auto operator a_op##=(enumeration<E, U>& a_lhs, E a_rhs) noexcept \
372 ->enumeration<E, U>& \
374 return a_lhs = a_lhs a_op a_rhs; \
377 template <class E, class U> \
378 constexpr auto operator a_op##=(E& a_lhs, enumeration<E, U> a_rhs) noexcept \
381 return a_lhs = *(a_lhs a_op a_rhs); \
384#define SKSE_MAKE_INCREMENTER_OP(a_op) \
385 template <class E, class U> \
386 constexpr auto operator a_op##a_op(enumeration<E, U>& a_lhs) noexcept \
387 ->enumeration<E, U>& \
389 return a_lhs a_op## = static_cast<E>(1); \
392 template <class E, class U> \
393 [[nodiscard]] constexpr auto operator a_op##a_op(enumeration<E, U>& a_lhs, int) noexcept \
394 ->enumeration<E, U> \
396 const auto tmp = a_lhs; \
411 return static_cast<E
>(~static_cast<U>(a_enum.get()));
434 public std::atomic_ref<T>
437 using super = std::atomic_ref<T>;
442 explicit atomic_ref(
volatile T& a_obj)
noexcept(std::is_nothrow_constructible_v<super, value_type&>) :
447 using super::operator=;
474 [[nodiscard]]
constexpr operator std::ptrdiff_t() const noexcept {
return value; }
476 [[nodiscard]]
constexpr std::ptrdiff_t
operator()() const noexcept {
return value; }
478 static constexpr auto value =
static_cast<std::ptrdiff_t
>(
sizeof(T));
484 template <
class T,
class U>
487 auto addr = a_ptr ?
reinterpret_cast<std::uintptr_t
>(a_ptr) + a_adjust : 0;
488 if constexpr (std::is_const_v<U> && std::is_volatile_v<U>) {
489 return reinterpret_cast<std::add_cv_t<T>*
>(addr);
490 }
else if constexpr (std::is_const_v<U>) {
491 return reinterpret_cast<std::add_const_t<T>*
>(addr);
492 }
else if constexpr (std::is_volatile_v<U>) {
493 return reinterpret_cast<std::add_volatile_t<T>*
>(addr);
495 return reinterpret_cast<T*
>(addr);
500 void memzero(
volatile T* a_ptr, std::size_t a_size =
sizeof(T))
502 const auto begin =
reinterpret_cast<volatile char*
>(a_ptr);
503 constexpr char val{ 0 };
504 std::fill_n(begin, a_size, val);
507 template <
class... Args>
509 requires(std::same_as<std::remove_cv_t<Args>,
bool>&&...)
511 constexpr auto ARGC =
sizeof...(Args);
513 std::bitset<ARGC> bits;
515 ((bits[i++] = a_args), ...);
517 if constexpr (ARGC <= std::numeric_limits<unsigned long>::digits) {
518 return bits.to_ulong();
519 }
else if constexpr (ARGC <= std::numeric_limits<unsigned long long>::digits) {
520 return bits.to_ullong();
522 static_assert(
false &&
sizeof...(Args));
527 -> std::optional<std::wstring>
529 const auto cvt = [&](
wchar_t* a_dst, std::size_t a_length) {
534 static_cast<int>(a_in.length()),
536 static_cast<int>(a_length));
539 const auto len = cvt(
nullptr, 0);
544 std::wstring out(len,
'\0');
545 if (cvt(out.data(), out.length()) == 0) {
553 -> std::optional<std::string>
555 const auto cvt = [&](
char* a_dst, std::size_t a_length) {
560 static_cast<int>(a_in.length()),
562 static_cast<int>(a_length),
567 const auto len = cvt(
nullptr, 0);
572 std::string out(len,
'\0');
573 if (cvt(out.data(), out.length()) == 0) {
580 [[noreturn]]
inline void report_and_fail(std::string_view a_msg, std::source_location a_loc = std::source_location::current())
582 const auto body = [&]() {
583 const std::filesystem::path p = a_loc.file_name();
584 auto filename = p.lexically_normal().generic_string();
586 const std::regex r{ R
"((?:^|[\\\/])(?:include|src)[\\\/](.*)$)" };
588 if (std::regex_search(filename, matches, r)) {
589 filename = matches[1].str();
598 .value_or(L
"<character encoding error>"s);
601 const auto caption = []() {
603 std::vector<wchar_t> buf;
604 buf.reserve(maxPath);
605 buf.resize(maxPath / 2);
606 std::uint32_t result = 0;
608 buf.resize(buf.size() * 2);
612 static_cast<std::uint32_t
>(buf.size()));
613 }
while (result && result == buf.size() && buf.size() <= std::numeric_limits<std::uint32_t>::max());
615 if (result && result != buf.size()) {
616 std::filesystem::path p(buf.begin(), buf.begin() + result);
617 return p.filename().native();
626 static_cast<int>(a_loc.line()),
627 a_loc.function_name() },
628 spdlog::level::critical,
630 WinAPI::MessageBox(
nullptr, body.c_str(), (caption.empty() ?
nullptr : caption.c_str()), 0);
634 template <
class Enum>
636 requires(std::is_enum_v<Enum>)
638 return static_cast<std::underlying_type_t<Enum>
>(a_val);
641 template <
class To,
class From>
644 if constexpr (std::is_same_v<
645 std::remove_cv_t<From>,
646 std::remove_cv_t<To>>) {
650 }
else if constexpr (std::is_reference_v<From>) {
651 return stl::unrestricted_cast<To>(std::addressof(a_from));
654 }
else if constexpr (std::is_reference_v<To>) {
657 std::remove_reference_t<To>>>(a_from);
660 }
else if constexpr (std::is_pointer_v<From> &&
661 std::is_pointer_v<To>) {
662 return static_cast<To
>(
664 static_cast<const volatile void*
>(a_from)));
665 }
else if constexpr ((std::is_pointer_v<From> && std::is_integral_v<To>) ||
666 (std::is_integral_v<From> && std::is_pointer_v<To>)) {
667 return reinterpret_cast<To
>(a_from);
671 std::remove_cv_t<std::remove_reference_t<From>> from;
672 std::remove_cv_t<std::remove_reference_t<To>> to;
675 from = std::forward<From>(a_from);
682#undef SKSE_MAKE_INCREMENTER_OP
683#undef SKSE_MAKE_ENUMERATION_OP
684#undef SKSE_MAKE_ARITHMETIC_OP
685#undef SKSE_MAKE_LOGICAL_OP
689 using namespace std::literals;
696 using namespace std::literals;
#define SKSE_MAKE_ARITHMETIC_OP(a_op)
Definition: PCH.h:326
#define SKSE_MAKE_ENUMERATION_OP(a_op)
Definition: PCH.h:341
#define SKSE_MAKE_INCREMENTER_OP(a_op)
Definition: PCH.h:384
#define SKSE_MAKE_LOGICAL_OP(a_op, a_result)
Definition: PCH.h:313
typename super::value_type value_type
Definition: PCH.h:440
atomic_ref(volatile T &a_obj) noexcept(std::is_nothrow_constructible_v< super, value_type & >)
Definition: PCH.h:442
constexpr bool all(Args... a_args) const noexcept
Definition: PCH.h:288
constexpr enumeration & reset(Args... a_args) noexcept
Definition: PCH.h:273
Underlying underlying_type
Definition: PCH.h:219
constexpr enumeration(Args... a_values) noexcept
Definition: PCH.h:236
constexpr enum_type operator*() const noexcept
Definition: PCH.h:260
constexpr enumeration & set(Args... a_args) noexcept
Definition: PCH.h:265
constexpr bool any(Args... a_args) const noexcept
Definition: PCH.h:281
constexpr bool none(Args... a_args) const noexcept
Definition: PCH.h:295
Enum enum_type
Definition: PCH.h:218
constexpr enumeration & operator=(enum_type a_value) noexcept
Definition: PCH.h:252
~enumeration() noexcept=default
constexpr underlying_type underlying() const noexcept
Definition: PCH.h:262
constexpr enumeration() noexcept=default
constexpr enum_type get() const noexcept
Definition: PCH.h:261
scope_exit(Fn &&a_fn) noexcept(std::is_nothrow_constructible_v< EF, Fn >||std::is_nothrow_constructible_v< EF, Fn & >)
Definition: PCH.h:155
scope_exit(const scope_exit &)=delete
scope_exit(scope_exit &&a_rhs) noexcept(std::is_nothrow_move_constructible_v< EF >||std::is_nothrow_copy_constructible_v< EF >)
Definition: PCH.h:172
~scope_exit() noexcept
Definition: PCH.h:194
void release() noexcept
Definition: PCH.h:201
Definition: AbsorbEffect.h:6
Definition: Relocation.h:60
void * GetCurrentProcess() noexcept
std::size_t GetMaxPath() noexcept
std::uint32_t GetModuleFileName(void *a_module, char *a_filename, std::uint32_t a_size) noexcept
void * GetCurrentModule() noexcept
int MultiByteToWideChar(unsigned int a_codePage, std::uint32_t a_flags, const char *a_multiByteStr, int a_multiByte, wchar_t *a_wideCharStr, int a_wideChar)
std::int32_t MessageBox(void *a_wnd, const char *a_text, const char *a_caption, unsigned int a_type) noexcept
constexpr auto CP_UTF8
Definition: WinAPI.h:5
int WideCharToMultiByte(unsigned int a_codePage, std::uint32_t a_flags, const wchar_t *a_wideCharStr, int a_wideChar, char *a_multiByteStr, int a_multiByte, const char *a_defaultChar, int *a_usedDefaultChar)
void TerminateProcess(void *a_process, unsigned int a_exitCode) noexcept
T not_null
Definition: PCH.h:99
constexpr auto to_underlying(Enum a_val) noexcept
Definition: PCH.h:635
To unrestricted_cast(From a_from) noexcept
Definition: PCH.h:642
std::basic_string_view< CharT > basic_zstring
Definition: PCH.h:75
void memzero(volatile T *a_ptr, std::size_t a_size=sizeof(T))
Definition: PCH.h:500
void report_and_fail(std::string_view a_msg, std::source_location a_loc=std::source_location::current())
Definition: PCH.h:580
T owner
Definition: PCH.h:85
T observer
Definition: PCH.h:92
auto utf16_to_utf8(std::wstring_view a_in) noexcept -> std::optional< std::string >
Definition: PCH.h:552
auto pun_bits(Args... a_args)
Definition: PCH.h:508
auto adjust_pointer(U *a_ptr, std::ptrdiff_t a_adjust) noexcept
Definition: PCH.h:485
auto utf8_to_utf16(std::string_view a_in) noexcept -> std::optional< std::wstring >
Definition: PCH.h:526
basic_zstring< wchar_t > zwstring
Definition: PCH.h:78
constexpr auto operator~(enumeration< E, U > a_enum) noexcept -> enumeration< E, U >
Definition: PCH.h:408
basic_zstring< char > zstring
Definition: PCH.h:77
constexpr auto ssizeof_v
Definition: PCH.h:482
consteval bool empty() const noexcept
Definition: PCH.h:130
const char_type & const_reference
Definition: PCH.h:110
consteval auto substr() const noexcept
Definition: PCH.h:136
consteval const_reference operator[](size_type a_pos) const noexcept
Definition: PCH.h:122
const char_type * const_pointer
Definition: PCH.h:108
consteval const_pointer data() const noexcept
Definition: PCH.h:129
std::size_t size_type
Definition: PCH.h:111
char_type c[N]
Definition: PCH.h:141
consteval const_reference back() const noexcept
Definition: PCH.h:128
consteval string(const_pointer a_string) noexcept
Definition: PCH.h:115
char_type & reference
Definition: PCH.h:109
char_type * pointer
Definition: PCH.h:107
static constexpr auto npos
Definition: PCH.h:113
consteval size_type length() const noexcept
Definition: PCH.h:132
consteval size_type size() const noexcept
Definition: PCH.h:133
consteval const_reference front() const noexcept
Definition: PCH.h:131
CharT char_type
Definition: PCH.h:106
constexpr std::ptrdiff_t operator()() const noexcept
Definition: PCH.h:476