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");
68 #include <binary_io/file_stream.hpp>
69 #include <spdlog/spdlog.h>
74 using namespace std::literals;
78 template <
class CharT>
87 class = std::enable_if_t<
88 std::is_pointer_v<T>>>
94 class = std::enable_if_t<
95 std::is_pointer_v<T>>>
101 class = std::enable_if_t<
102 std::is_pointer_v<T>>>
107 template <
class CharT, std::
size_t N>
117 static constexpr
auto npos =
static_cast<std::size_t
>(-1);
134 [[nodiscard]] consteval
bool empty() const noexcept {
return this->size() == 0; }
137 [[nodiscard]] consteval
size_type size() const noexcept {
return length(); }
139 template <std::
size_t POS = 0, std::
size_t COUNT = npos>
140 [[nodiscard]] consteval
auto substr() const noexcept
142 return string < CharT, COUNT != npos ? COUNT : N - POS > (this->data() + POS);
148 template <
class CharT, std::
size_t N>
149 string(
const CharT (&)[N]) ->
string<CharT, N - 1>;
158 explicit scope_exit(Fn&& a_fn) 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 std::is_nothrow_copy_constructible_v<EF>)
176 requires(std::is_nothrow_move_constructible_v<EF> ||
177 std::is_copy_constructible_v<EF>)
179 static_assert(!(std::is_nothrow_move_constructible_v<EF> && !std::is_move_constructible_v<EF>));
180 static_assert(!(!std::is_nothrow_move_constructible_v<EF> && !std::is_copy_constructible_v<EF>));
182 if (a_rhs.active()) {
183 if constexpr (std::is_nothrow_move_constructible_v<EF>) {
184 _fn.emplace(std::forward<EF>(*a_rhs._fn));
186 _fn.emplace(a_rhs._fn);
197 if (_fn.has_value()) {
202 void release() noexcept { _fn.reset(); }
205 [[nodiscard]]
bool active()
const noexcept {
return _fn.has_value(); }
207 std::optional<std::remove_reference_t<EF>> _fn;
216 class U = std::underlying_type_t<E>>
226 using super::operator=;
227 using super::operator*;
230 template <
class... Args>
232 std::common_type_t<Args...>,
233 std::underlying_type_t<
234 std::common_type_t<Args...>>>;
247 using super = std::atomic_ref<T>;
252 explicit atomic_ref(
volatile T& a_obj) noexcept(std::is_nothrow_constructible_v<super, value_type&>) :
257 using super::operator=;
284 [[nodiscard]] constexpr
operator std::ptrdiff_t() const noexcept {
return value; }
286 [[nodiscard]] constexpr std::ptrdiff_t
operator()() const noexcept {
return value; }
288 static constexpr
auto value =
static_cast<std::ptrdiff_t
>(
sizeof(T));
294 template <
class T,
class U>
297 auto addr = a_ptr ?
reinterpret_cast<std::uintptr_t
>(a_ptr) + a_adjust : 0;
298 if constexpr (std::is_const_v<U> && std::is_volatile_v<U>) {
299 return reinterpret_cast<std::add_cv_t<T>*
>(addr);
300 }
else if constexpr (std::is_const_v<U>) {
301 return reinterpret_cast<std::add_const_t<T>*
>(addr);
302 }
else if constexpr (std::is_volatile_v<U>) {
303 return reinterpret_cast<std::add_volatile_t<T>*
>(addr);
305 return reinterpret_cast<T*
>(addr);
312 reinterpret_cast<std::uintptr_t*
>(a_ptr)[0] = T::VTABLE[0].address();
316 void memzero(
volatile T* a_ptr, std::size_t a_size =
sizeof(T))
318 const auto begin =
reinterpret_cast<volatile char*
>(a_ptr);
319 constexpr
char val{ 0 };
320 std::fill_n(begin, a_size, val);
323 template <
class... Args>
325 requires(std::same_as<std::remove_cv_t<Args>,
bool>&&...)
327 constexpr
auto ARGC =
sizeof...(Args);
329 std::bitset<ARGC> bits;
331 ((bits[i++] = a_args), ...);
333 if constexpr (ARGC <= std::numeric_limits<unsigned long>::digits) {
334 return bits.to_ulong();
335 }
else if constexpr (ARGC <= std::numeric_limits<unsigned long long>::digits) {
336 return bits.to_ullong();
338 static_assert(
false &&
sizeof...(Args));
343 -> std::optional<std::wstring>
345 const auto cvt = [&](
wchar_t* a_dst, std::size_t a_length) {
350 static_cast<int>(a_in.length()),
352 static_cast<int>(a_length));
355 const auto len = cvt(
nullptr, 0);
360 std::wstring out(len,
'\0');
361 if (cvt(out.data(), out.length()) == 0) {
369 -> std::optional<std::string>
371 const auto cvt = [&](
char* a_dst, std::size_t a_length) {
376 static_cast<int>(a_in.length()),
378 static_cast<int>(a_length),
383 const auto len = cvt(
nullptr, 0);
389 if (cvt(out.data(), out.length()) == 0) {
396 [[noreturn]]
inline void report_and_fail(std::string_view a_msg, std::source_location a_loc = std::source_location::current())
398 const auto body = [&]() {
399 const std::filesystem::path p = a_loc.file_name();
400 auto filename = p.lexically_normal().generic_string();
402 const std::regex r{ R
"((?:^|[\\\/])(?:include|src)[\\\/](.*)$)" };
404 if (std::regex_search(filename, matches, r)) {
405 filename = matches[1].str();
414 .value_or(L
"<character encoding error>"s);
417 const auto caption = []() {
418 std::vector<wchar_t> buf;
421 std::uint32_t result = 0;
423 buf.resize(buf.size() * 2);
427 static_cast<std::uint32_t
>(buf.size()));
430 if (result && result != buf.size()) {
431 std::filesystem::path p(buf.begin(), buf.begin() + result);
432 return p.filename().native();
441 static_cast<int>(a_loc.line()),
442 a_loc.function_name() },
443 spdlog::level::critical,
449 template <
class To,
class From>
452 if constexpr (std::is_same_v<
453 std::remove_cv_t<From>,
454 std::remove_cv_t<To>>) {
458 }
else if constexpr (std::is_reference_v<From>) {
459 return stl::unrestricted_cast<To>(std::addressof(a_from));
462 }
else if constexpr (std::is_reference_v<To>) {
465 std::remove_reference_t<To>>>(a_from);
468 }
else if constexpr (std::is_pointer_v<From> &&
469 std::is_pointer_v<To>) {
470 return static_cast<To
>(
472 static_cast<const volatile void*
>(a_from)));
473 }
else if constexpr ((std::is_pointer_v<From> && std::is_integral_v<To>) ||
474 (std::is_integral_v<From> && std::is_pointer_v<To>)) {
475 return reinterpret_cast<To
>(a_from);
479 std::remove_cv_t<std::remove_reference_t<From>> from;
480 std::remove_cv_t<std::remove_reference_t<To>> to;
483 from = std::forward<From>(a_from);
492 using namespace std::literals;
498 using namespace std::literals;
502 #ifdef SKYRIM_SUPPORT_AE
503 # define RELOCATION_ID(SE, AE) REL::ID(AE)
505 # define RELOCATION_ID(SE, AE) REL::ID(SE)
typename super::value_type value_type
Definition: PCH.h:250
atomic_ref(volatile T &a_obj) noexcept(std::is_nothrow_constructible_v< super, value_type & >)
Definition: PCH.h:252
U underlying_type
Definition: PCH.h:223
E enum_type
Definition: PCH.h:222
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:35
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:103
To unrestricted_cast(From a_from) noexcept
Definition: PCH.h:450
std::basic_string_view< CharT > basic_zstring
Definition: PCH.h:79
void memzero(volatile T *a_ptr, std::size_t a_size=sizeof(T))
Definition: PCH.h:316
void report_and_fail(std::string_view a_msg, std::source_location a_loc=std::source_location::current())
Definition: PCH.h:396
scope_exit(EF) -> scope_exit< EF >
T owner
Definition: PCH.h:89
T observer
Definition: PCH.h:96
auto utf16_to_utf8(std::wstring_view a_in) noexcept -> std::optional< std::string >
Definition: PCH.h:368
void emplace_vtable(T *a_ptr)
Definition: PCH.h:310
auto adjust_pointer(U *a_ptr, std::ptrdiff_t a_adjust) noexcept
Definition: PCH.h:295
auto utf8_to_utf16(std::string_view a_in) noexcept -> std::optional< std::wstring >
Definition: PCH.h:342
requires(std::invocable< std::remove_reference_t< EF >>) class scope_exit
Definition: PCH.h:153
basic_zstring< wchar_t > zwstring
Definition: PCH.h:82
auto pun_bits(Args... a_args) requires(std
Definition: PCH.h:324
basic_zstring< char > zstring
Definition: PCH.h:81
enumeration(Args...) -> enumeration< std::common_type_t< Args... >, std::underlying_type_t< std::common_type_t< Args... >>>
constexpr auto ssizeof_v
Definition: PCH.h:292
consteval bool empty() const noexcept
Definition: PCH.h:134
const char_type & const_reference
Definition: PCH.h:114
consteval auto substr() const noexcept
Definition: PCH.h:140
consteval const_reference operator[](size_type a_pos) const noexcept
Definition: PCH.h:126
const char_type * const_pointer
Definition: PCH.h:112
consteval const_pointer data() const noexcept
Definition: PCH.h:133
std::size_t size_type
Definition: PCH.h:115
consteval const_reference back() const noexcept
Definition: PCH.h:132
consteval string(const_pointer a_string) noexcept
Definition: PCH.h:119
char_type & reference
Definition: PCH.h:113
char_type * pointer
Definition: PCH.h:111
consteval size_type length() const noexcept
Definition: PCH.h:136
consteval size_type size() const noexcept
Definition: PCH.h:137
consteval const_reference front() const noexcept
Definition: PCH.h:135
CharT char_type
Definition: PCH.h:110
constexpr std::ptrdiff_t operator()() const noexcept
Definition: PCH.h:286