42 #include <source_location>
48 #include <string_view>
49 #include <system_error>
52 #include <type_traits>
59 std::is_integral_v<std::time_t> &&
sizeof(std::time_t) ==
sizeof(std::size_t),
60 "wrap std::time_t instead");
63 #include <binary_io/file_stream.hpp>
64 #include <spdlog/spdlog.h>
72 using namespace std::literals;
76 template <
class CharT>
85 class = std::enable_if_t<
86 std::is_pointer_v<T>>>
92 class = std::enable_if_t<
93 std::is_pointer_v<T>>>
99 class = std::enable_if_t<
100 std::is_pointer_v<T>>>
105 template <
class CharT, std::
size_t N>
115 static constexpr
auto npos =
static_cast<std::size_t
>(-1);
132 [[nodiscard]] consteval
bool empty() const noexcept {
return this->size() == 0; }
135 [[nodiscard]] consteval
size_type size() const noexcept {
return length(); }
137 template <std::
size_t POS = 0, std::
size_t COUNT = npos>
138 [[nodiscard]] consteval
auto substr() const noexcept
140 return string < CharT, COUNT != npos ? COUNT : N - POS > (this->data() + POS);
146 template <
class CharT, std::
size_t N>
147 string(
const CharT (&)[N]) ->
string<CharT, N - 1>;
151 requires(std::invocable<std::remove_reference_t<EF>>)
158 noexcept(std::is_nothrow_constructible_v<EF, Fn> ||
159 std::is_nothrow_constructible_v<EF, Fn&>)
161 std::is_constructible_v<EF, Fn>)
163 static_assert(std::invocable<Fn>);
165 if constexpr (!std::is_lvalue_reference_v<Fn> &&
166 std::is_nothrow_constructible_v<EF, Fn>) {
167 _fn.emplace(std::forward<Fn>(a_fn));
175 noexcept(std::is_nothrow_move_constructible_v<EF> ||
176 std::is_nothrow_copy_constructible_v<EF>)
177 requires(std::is_nothrow_move_constructible_v<EF> ||
178 std::is_copy_constructible_v<EF>)
180 static_assert(!(std::is_nothrow_move_constructible_v<EF> && !std::is_move_constructible_v<EF>));
181 static_assert(!(!std::is_nothrow_move_constructible_v<EF> && !std::is_copy_constructible_v<EF>));
183 if (a_rhs.active()) {
184 if constexpr (std::is_nothrow_move_constructible_v<EF>) {
185 _fn.emplace(std::forward<EF>(*a_rhs._fn));
187 _fn.emplace(a_rhs._fn);
198 if (_fn.has_value()) {
203 void release() noexcept { _fn.reset(); }
206 [[nodiscard]]
bool active()
const noexcept {
return _fn.has_value(); }
208 std::optional<std::remove_reference_t<EF>> _fn;
216 class Underlying = std::underlying_type_t<Enum>>
223 static_assert(std::is_enum_v<enum_type>,
"enum_type must be an enum");
224 static_assert(std::is_integral_v<underlying_type>,
"underlying_type must be an integral");
237 template <
class... Args>
239 requires(std::same_as<Args, enum_type>&&...) :
260 [[nodiscard]]
explicit constexpr
operator bool() const noexcept {
return _impl !=
static_cast<underlying_type>(0); }
266 template <
class... Args>
268 requires(std::same_as<Args, enum_type>&&...)
274 template <
class... Args>
276 requires(std::same_as<Args, enum_type>&&...)
282 template <
class... Args>
283 [[nodiscard]] constexpr
bool any(Args... a_args)
const noexcept
284 requires(std::same_as<Args, enum_type>&&...)
289 template <
class... Args>
290 [[nodiscard]] constexpr
bool all(Args... a_args)
const noexcept
291 requires(std::same_as<Args, enum_type>&&...)
296 template <
class... Args>
297 [[nodiscard]] constexpr
bool none(Args... a_args)
const noexcept
298 requires(std::same_as<Args, enum_type>&&...)
304 underlying_type _impl{ 0 };
307 template <
class... Args>
309 std::common_type_t<Args...>,
310 std::underlying_type_t<
311 std::common_type_t<Args...>>>;
315 #define SKSE_MAKE_LOGICAL_OP(a_op, a_result) \
316 template <class E, class U1, class U2> \
317 [[nodiscard]] constexpr a_result operator a_op(enumeration<E, U1> a_lhs, enumeration<E, U2> a_rhs) noexcept \
319 return a_lhs.get() a_op a_rhs.get(); \
322 template <class E, class U> \
323 [[nodiscard]] constexpr a_result operator a_op(enumeration<E, U> a_lhs, E a_rhs) noexcept \
325 return a_lhs.get() a_op a_rhs; \
328 #define SKSE_MAKE_ARITHMETIC_OP(a_op) \
329 template <class E, class U> \
330 [[nodiscard]] constexpr auto operator a_op(enumeration<E, U> a_enum, U a_shift) noexcept \
331 ->enumeration<E, U> \
333 return static_cast<E>(static_cast<U>(a_enum.get()) a_op a_shift); \
336 template <class E, class U> \
337 constexpr auto operator a_op##=(enumeration<E, U>& a_enum, U a_shift) noexcept \
338 ->enumeration<E, U>& \
340 return a_enum = a_enum a_op a_shift; \
343 #define SKSE_MAKE_ENUMERATION_OP(a_op) \
344 template <class E, class U1, class U2> \
345 [[nodiscard]] constexpr auto operator a_op(enumeration<E, U1> a_lhs, enumeration<E, U2> a_rhs) noexcept \
346 ->enumeration<E, std::common_type_t<U1, U2>> \
348 return static_cast<E>(static_cast<U1>(a_lhs.get()) a_op static_cast<U2>(a_rhs.get())); \
351 template <class E, class U> \
352 [[nodiscard]] constexpr auto operator a_op(enumeration<E, U> a_lhs, E a_rhs) noexcept \
353 ->enumeration<E, U> \
355 return static_cast<E>(static_cast<U>(a_lhs.get()) a_op static_cast<U>(a_rhs)); \
358 template <class E, class U> \
359 [[nodiscard]] constexpr auto operator a_op(E a_lhs, enumeration<E, U> a_rhs) noexcept \
360 ->enumeration<E, U> \
362 return static_cast<E>(static_cast<U>(a_lhs) a_op static_cast<U>(a_rhs.get())); \
365 template <class E, class U1, class U2> \
366 constexpr auto operator a_op##=(enumeration<E, U1>& a_lhs, enumeration<E, U2> a_rhs) noexcept \
367 ->enumeration<E, U1>& \
369 return a_lhs = a_lhs a_op a_rhs; \
372 template <class E, class U> \
373 constexpr auto operator a_op##=(enumeration<E, U>& a_lhs, E a_rhs) noexcept \
374 ->enumeration<E, U>& \
376 return a_lhs = a_lhs a_op a_rhs; \
379 template <class E, class U> \
380 constexpr auto operator a_op##=(E& a_lhs, enumeration<E, U> a_rhs) noexcept \
383 return a_lhs = *(a_lhs a_op a_rhs); \
386 #define SKSE_MAKE_INCREMENTER_OP(a_op) \
387 template <class E, class U> \
388 constexpr auto operator a_op##a_op(enumeration<E, U>& a_lhs) noexcept \
389 ->enumeration<E, U>& \
391 return a_lhs a_op## = static_cast<E>(1); \
394 template <class E, class U> \
395 [[nodiscard]] constexpr auto operator a_op##a_op(enumeration<E, U>& a_lhs, int) noexcept \
396 ->enumeration<E, U> \
398 const auto tmp = a_lhs; \
413 return static_cast<E
>(~static_cast<U>(a_enum.get()));
439 using super = std::atomic_ref<T>;
444 explicit atomic_ref(
volatile T& a_obj) noexcept(std::is_nothrow_constructible_v<super, value_type&>) :
449 using super::operator=;
476 [[nodiscard]] constexpr
operator std::ptrdiff_t() const noexcept {
return value; }
478 [[nodiscard]] constexpr std::ptrdiff_t
operator()() const noexcept {
return value; }
480 static constexpr
auto value =
static_cast<std::ptrdiff_t
>(
sizeof(T));
486 template <
class T,
class U>
489 auto addr = a_ptr ?
reinterpret_cast<std::uintptr_t
>(a_ptr) + a_adjust : 0;
490 if constexpr (std::is_const_v<U> && std::is_volatile_v<U>) {
491 return reinterpret_cast<std::add_cv_t<T>*
>(addr);
492 }
else if constexpr (std::is_const_v<U>) {
493 return reinterpret_cast<std::add_const_t<T>*
>(addr);
494 }
else if constexpr (std::is_volatile_v<U>) {
495 return reinterpret_cast<std::add_volatile_t<T>*
>(addr);
497 return reinterpret_cast<T*
>(addr);
504 reinterpret_cast<std::uintptr_t*
>(a_ptr)[0] = T::VTABLE[0].address();
508 void memzero(
volatile T* a_ptr, std::size_t a_size =
sizeof(T))
510 const auto begin =
reinterpret_cast<volatile char*
>(a_ptr);
511 constexpr
char val{ 0 };
512 std::fill_n(begin, a_size, val);
515 template <
class... Args>
517 requires(std::same_as<std::remove_cv_t<Args>,
bool>&&...)
519 constexpr
auto ARGC =
sizeof...(Args);
521 std::bitset<ARGC> bits;
523 ((bits[i++] = a_args), ...);
525 if constexpr (ARGC <= std::numeric_limits<unsigned long>::digits) {
526 return bits.to_ulong();
527 }
else if constexpr (ARGC <= std::numeric_limits<unsigned long long>::digits) {
528 return bits.to_ullong();
530 static_assert(
false &&
sizeof...(Args));
535 -> std::optional<std::wstring>
537 const auto cvt = [&](
wchar_t* a_dst, std::size_t a_length) {
542 static_cast<int>(a_in.length()),
544 static_cast<int>(a_length));
547 const auto len = cvt(
nullptr, 0);
552 std::wstring out(len,
'\0');
553 if (cvt(out.data(), out.length()) == 0) {
561 -> std::optional<std::string>
563 const auto cvt = [&](
char* a_dst, std::size_t a_length) {
568 static_cast<int>(a_in.length()),
570 static_cast<int>(a_length),
575 const auto len = cvt(
nullptr, 0);
581 if (cvt(out.data(), out.length()) == 0) {
588 [[noreturn]]
inline void report_and_fail(std::string_view a_msg, std::source_location a_loc = std::source_location::current())
590 const auto body = [&]() {
591 const std::filesystem::path p = a_loc.file_name();
592 auto filename = p.lexically_normal().generic_string();
594 const std::regex r{ R
"((?:^|[\\\/])(?:include|src)[\\\/](.*)$)" };
596 if (std::regex_search(filename, matches, r)) {
597 filename = matches[1].str();
606 .value_or(L
"<character encoding error>"s);
609 const auto caption = []() {
610 std::vector<wchar_t> buf;
613 std::uint32_t result = 0;
615 buf.resize(buf.size() * 2);
619 static_cast<std::uint32_t
>(buf.size()));
622 if (result && result != buf.size()) {
623 std::filesystem::path p(buf.begin(), buf.begin() + result);
624 return p.filename().native();
633 static_cast<int>(a_loc.line()),
634 a_loc.function_name() },
635 spdlog::level::critical,
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;
695 using namespace std::literals;
699 #ifdef SKYRIM_SUPPORT_AE
700 # define RELOCATION_ID(SE, AE) REL::ID(AE)
702 # define RELOCATION_ID(SE, AE) REL::ID(SE)
709 # define KEEP_FOR_RE() \
713 # define KEEP_FOR_RE()
typename super::value_type value_type
Definition: PCH.h:442
atomic_ref(volatile T &a_obj) noexcept(std::is_nothrow_constructible_v< super, value_type & >)
Definition: PCH.h:444
Underlying underlying_type
Definition: PCH.h:221
constexpr enum_type operator*() const noexcept
Definition: PCH.h:262
constexpr bool none(Args... a_args) const noexcept requires(std
Definition: PCH.h:297
constexpr enumeration(Args... a_values) noexcept requires(std
Definition: PCH.h:238
Enum enum_type
Definition: PCH.h:220
constexpr enumeration & operator=(enum_type a_value) noexcept
Definition: PCH.h:254
constexpr bool all(Args... a_args) const noexcept requires(std
Definition: PCH.h:290
~enumeration() noexcept=default
constexpr enumeration & reset(Args... a_args) noexcept requires(std
Definition: PCH.h:275
constexpr enumeration & set(Args... a_args) noexcept requires(std
Definition: PCH.h:267
constexpr underlying_type underlying() const noexcept
Definition: PCH.h:264
constexpr enumeration() noexcept=default
constexpr bool any(Args... a_args) const noexcept requires(std
Definition: PCH.h:283
constexpr enum_type get() const noexcept
Definition: PCH.h:263
std::int32_t WideCharToMultiByte(std::uint32_t a_codePage, std::uint32_t a_flags, const wchar_t *a_src, std::int32_t a_srcLen, char *a_dst, std::int32_t a_dstLen, const char *a_default, std::int32_t *a_defaultLen)
bool TerminateProcess(HANDLE a_process, std::uint32_t a_exitCode) noexcept
constexpr auto CP_UTF8
Definition: KERNEL32.h:12
std::int32_t MessageBoxW(HWND a_wnd, const wchar_t *a_text, const wchar_t *a_caption, std::uint32_t a_type) noexcept
HMODULE GetCurrentModule() noexcept
std::int32_t MultiByteToWideChar(std::uint32_t a_codePage, std::uint32_t a_flags, const char *a_src, std::int32_t a_srcLen, wchar_t *a_dst, std::int32_t a_dstLen) noexcept
constexpr auto MAX_PATH
Definition: BASE.h:34
std::uint32_t GetModuleFileNameW(HMODULE a_module, wchar_t *a_name, std::uint32_t a_nameLen) noexcept
HANDLE GetCurrentProcess() noexcept
NiColor max(const NiColor &a_lhs, const NiColor &a_rhs)
Definition: ColorUtil.h:71
Definition: AbsorbEffect.h:6
string(const CharT(&)[N]) -> string< CharT, N - 1 >
atomic_ref(volatile T &) -> atomic_ref< T >
T not_null
Definition: PCH.h:101
SKSE_MAKE_ENUMERATION_OP(<<)
To unrestricted_cast(From a_from) noexcept
Definition: PCH.h:642
std::basic_string_view< CharT > basic_zstring
Definition: PCH.h:77
void memzero(volatile T *a_ptr, std::size_t a_size=sizeof(T))
Definition: PCH.h:508
void report_and_fail(std::string_view a_msg, std::source_location a_loc=std::source_location::current())
Definition: PCH.h:588
scope_exit(EF) -> scope_exit< EF >
T owner
Definition: PCH.h:87
T observer
Definition: PCH.h:94
auto utf16_to_utf8(std::wstring_view a_in) noexcept -> std::optional< std::string >
Definition: PCH.h:560
void emplace_vtable(T *a_ptr)
Definition: PCH.h:502
SKSE_MAKE_INCREMENTER_OP(+)
auto adjust_pointer(U *a_ptr, std::ptrdiff_t a_adjust) noexcept
Definition: PCH.h:487
auto utf8_to_utf16(std::string_view a_in) noexcept -> std::optional< std::wstring >
Definition: PCH.h:534
requires(std::invocable< std::remove_reference_t< EF >>) class scope_exit
Definition: PCH.h:151
SKSE_MAKE_LOGICAL_OP(==, bool)
basic_zstring< wchar_t > zwstring
Definition: PCH.h:80
constexpr auto operator~(enumeration< E, U > a_enum) noexcept -> enumeration< E, U >
Definition: PCH.h:410
auto pun_bits(Args... a_args) requires(std
Definition: PCH.h:516
SKSE_MAKE_ARITHMETIC_OP(<<)
basic_zstring< char > zstring
Definition: PCH.h:79
enumeration(Args...) -> enumeration< std::common_type_t< Args... >, std::underlying_type_t< std::common_type_t< Args... >>>
constexpr auto ssizeof_v
Definition: PCH.h:484
consteval bool empty() const noexcept
Definition: PCH.h:132
const char_type & const_reference
Definition: PCH.h:112
consteval auto substr() const noexcept
Definition: PCH.h:138
consteval const_reference operator[](size_type a_pos) const noexcept
Definition: PCH.h:124
const char_type * const_pointer
Definition: PCH.h:110
consteval const_pointer data() const noexcept
Definition: PCH.h:131
std::size_t size_type
Definition: PCH.h:113
consteval const_reference back() const noexcept
Definition: PCH.h:130
consteval string(const_pointer a_string) noexcept
Definition: PCH.h:117
char_type & reference
Definition: PCH.h:111
char_type * pointer
Definition: PCH.h:109
consteval size_type length() const noexcept
Definition: PCH.h:134
consteval size_type size() const noexcept
Definition: PCH.h:135
consteval const_reference front() const noexcept
Definition: PCH.h:133
CharT char_type
Definition: PCH.h:108
constexpr std::ptrdiff_t operator()() const noexcept
Definition: PCH.h:478