1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-21 18:22:33 +01:00
rpcs3/Utilities/StrFmt.h

459 lines
10 KiB
C
Raw Normal View History

#pragma once // No BOM and only basic ASCII in this header, or a neko will die
2020-12-12 13:01:29 +01:00
#include "util/types.hpp"
2016-08-14 02:22:19 +02:00
2016-05-13 16:01:48 +02:00
#include <string>
2016-02-01 22:55:43 +01:00
namespace fmt
{
2020-12-18 08:39:54 +01:00
template <typename CharT, usz N, typename... Args>
2020-02-04 19:37:00 +01:00
static std::string format(const CharT(&)[N], const Args&...);
2022-03-16 19:59:20 +01:00
#ifdef _WIN32
2023-03-04 11:05:32 +01:00
struct win_error
{
unsigned long error{};
void* module_handle{};
};
2022-03-16 20:44:47 +01:00
// Get a string for a windows error (DWORD). Optionally a module HANDLE can be passed.
std::string win_error_to_string(unsigned long error, void* module_handle = nullptr);
2023-03-04 11:05:32 +01:00
std::string win_error_to_string(const win_error& error);
2022-03-16 19:59:20 +01:00
#endif
}
template <typename T, typename = void>
struct fmt_unveil
{
static_assert(sizeof(T) > 0, "fmt_unveil<> error: incomplete type");
using type = T;
static inline u64 get(const T& arg)
{
2020-12-12 14:29:55 +01:00
return reinterpret_cast<uptr>(&arg);
}
2016-08-07 15:59:46 +02:00
// Temporary value container (can possibly be created by other fmt_unveil<> specializations)
struct u64_wrapper
{
T arg;
// Allow implicit conversion
operator u64() const
{
2020-12-12 14:29:55 +01:00
return reinterpret_cast<uptr>(&arg);
2016-08-07 15:59:46 +02:00
}
};
// This overload resolution takes the precedence
static inline u64_wrapper get(T&& arg)
{
return u64_wrapper{std::move(arg)};
2016-08-07 15:59:46 +02:00
}
};
2016-08-13 15:36:04 +02:00
template <typename T>
struct fmt_unveil<T, std::enable_if_t<std::is_integral_v<T> && sizeof(T) <= 8 && alignof(T) <= 8>>
{
using type = T;
static inline u64 get(T arg)
{
return static_cast<T>(arg);
}
};
2016-08-13 15:36:04 +02:00
template <typename T>
struct fmt_unveil<T, std::enable_if_t<std::is_floating_point_v<T> && sizeof(T) <= 8 && alignof(T) <= 8>>
{
using type = T;
// Convert FP to f64 and reinterpret as u64
static inline u64 get(const f64& arg)
{
return std::bit_cast<u64>(arg);
}
};
2016-08-13 15:36:04 +02:00
template <typename T>
struct fmt_unveil<T, std::enable_if_t<std::is_enum_v<T>>>
{
using type = T;
static inline u64 get(T arg)
{
return static_cast<std::underlying_type_t<T>>(arg);
}
};
2016-08-13 15:36:04 +02:00
template <typename T>
struct fmt_unveil<T*, void>
{
2018-04-21 22:55:01 +02:00
using type = std::add_const_t<T>*;
2018-04-21 22:55:01 +02:00
static inline u64 get(type arg)
{
2020-12-12 14:29:55 +01:00
return reinterpret_cast<uptr>(arg);
}
};
namespace fmt
{
template <typename T>
concept CharT = (std::is_same_v<const T, const char> || std::is_same_v<const T, const char8_t>);
}
template <fmt::CharT T, usz N>
struct fmt_unveil<T[N], void>
{
2018-04-21 22:55:01 +02:00
using type = std::add_const_t<T>*;
2018-04-21 22:55:01 +02:00
static inline u64 get(type arg)
{
2020-12-12 14:29:55 +01:00
return reinterpret_cast<uptr>(arg);
}
};
2020-12-18 08:39:54 +01:00
template <typename T, bool Se, usz Align>
struct fmt_unveil<se_t<T, Se, Align>, void>
{
using type = typename fmt_unveil<T>::type;
static inline auto get(const se_t<T, Se, Align>& arg)
{
return fmt_unveil<T>::get(arg);
}
};
// String type format provider, also type classifier (format() called if an argument is formatted as "%s")
2016-08-13 15:36:04 +02:00
template <typename T, typename = void>
struct fmt_class_string
{
// Formatting function (must be explicitly specialized)
static void format(std::string& out, u64 arg);
// Helper typedef (visible in format())
using type = T;
// Helper function (converts arg to object reference)
2021-02-09 10:33:50 +01:00
static FORCE_INLINE SAFE_BUFFERS(const T&) get_object(u64 arg)
{
2020-12-12 14:29:55 +01:00
return *reinterpret_cast<const T*>(static_cast<uptr>(arg));
}
// Enum -> string function type
using convert_t = const char*(*)(T value);
// Helper function (safely converts arg to enum value)
2021-02-09 10:33:50 +01:00
static FORCE_INLINE SAFE_BUFFERS(void) format_enum(std::string& out, u64 arg, convert_t convert)
{
const auto value = static_cast<std::underlying_type_t<T>>(arg);
// Check narrowing
if (static_cast<u64>(value) == arg)
{
if (const char* str = convert(static_cast<T>(value)))
{
out += str;
return;
}
}
// Fallback to underlying type formatting
fmt_class_string<std::underlying_type_t<T>>::format(out, static_cast<u64>(value));
}
2016-08-07 21:01:27 +02:00
// Helper function (bitset formatting)
2021-02-09 10:33:50 +01:00
static FORCE_INLINE SAFE_BUFFERS(void) format_bitset(std::string& out, u64 arg, const char* prefix, const char* delim, const char* suffix, void (*fmt)(std::string&, u64))
2016-08-07 21:01:27 +02:00
{
// Start from raw value
fmt_class_string<u64>::format(out, arg);
out += prefix;
2017-02-15 16:07:42 +01:00
for (u64 i = 0; i < 63; i++)
2016-08-07 21:01:27 +02:00
{
const u64 mask = 1ull << i;
if (arg & mask)
{
fmt(out, i);
2017-02-15 16:07:42 +01:00
if (arg >> (i + 1))
2016-08-07 21:01:27 +02:00
{
out += delim;
}
}
}
2017-02-15 16:07:42 +01:00
if (arg & (1ull << 63))
{
fmt(out, 63);
}
2016-08-07 21:01:27 +02:00
out += suffix;
}
// Helper constant (may be used in format_enum as lambda return value)
static constexpr const char* unknown = nullptr;
};
2016-08-13 15:36:04 +02:00
template <>
struct fmt_class_string<const void*, void>
{
static void format(std::string& out, u64 arg);
};
2016-08-13 15:36:04 +02:00
template <typename T>
struct fmt_class_string<T*, void> : fmt_class_string<const void*, void>
{
// Classify all pointers as const void*
};
2016-08-13 15:36:04 +02:00
template <>
struct fmt_class_string<const char*, void>
{
static void format(std::string& out, u64 arg);
};
2016-08-13 15:36:04 +02:00
template <>
struct fmt_class_string<char*, void> : fmt_class_string<const char*>
{
// Classify char* as const char*
};
2021-07-10 15:03:43 +02:00
template <>
struct fmt_class_string<const char8_t*, void> : fmt_class_string<const char*>
{
};
template <>
struct fmt_class_string<char8_t*, void> : fmt_class_string<const char8_t*>
{
};
template <>
struct fmt_class_string<const wchar_t*, void>
{
static void format(std::string& out, u64 arg);
};
template <>
struct fmt_class_string<wchar_t*, void> : fmt_class_string<const wchar_t*>
{
};
2023-01-07 20:51:02 +01:00
namespace fmt
{
template <typename T>
concept StringConvertible = requires (T & t)
{
{ t.to_string() } -> std::convertible_to<std::string>;
};
}
template <fmt::StringConvertible T>
struct fmt_class_string<T, void>
{
static FORCE_INLINE SAFE_BUFFERS(const T&) get_object(u64 arg)
{
return *reinterpret_cast<const T*>(static_cast<uptr>(arg));
}
static void format(std::string& out, u64 arg)
{
out += get_object(arg).to_string();
}
};
namespace fmt
{
// Both uchar and std::byte are allowed
template <typename T>
2021-07-17 19:30:21 +02:00
concept ByteArray = requires (T& t) { const_cast<std::conditional_t<std::is_same_v<decltype(std::as_const(t[0])), const std::byte&>, std::byte, uchar>&>(std::data(t)[0]); };
}
template <fmt::ByteArray T>
struct fmt_class_string<T, void>
{
static FORCE_INLINE SAFE_BUFFERS(const T&) get_object(u64 arg)
{
return *reinterpret_cast<const T*>(static_cast<uptr>(arg));
}
static void format(std::string& out, u64 arg)
{
const auto& obj = get_object(arg);
void format_byte_array(std::string&, const uchar*, usz);
format_byte_array(out, reinterpret_cast<const uchar*>(std::data(obj)), std::size(obj));
}
};
2016-08-05 18:49:45 +02:00
struct fmt_type_info
{
2016-08-05 18:49:45 +02:00
decltype(&fmt_class_string<int>::format) fmt_string;
2016-08-13 15:36:04 +02:00
template <typename T>
2016-08-05 18:49:45 +02:00
static constexpr fmt_type_info make()
{
2016-08-05 18:49:45 +02:00
return fmt_type_info
{
2016-08-05 18:49:45 +02:00
&fmt_class_string<T>::format,
};
}
};
2018-08-25 11:06:57 +02:00
// Argument array type (each element generated via fmt_unveil<>)
template <typename... Args>
using fmt_args_t = const u64(&&)[sizeof...(Args) + 1];
template <typename Arg>
using fmt_unveil_t = typename fmt_unveil<Arg>::type;
2016-08-05 18:49:45 +02:00
namespace fmt
{
2018-03-22 20:48:38 +01:00
// Base-57 format helper
struct base57
{
const uchar* data;
2020-12-18 08:39:54 +01:00
usz size;
2018-03-22 20:48:38 +01:00
2024-07-27 08:49:22 +02:00
base57() = default;
2018-03-22 20:48:38 +01:00
template <typename T>
2024-07-27 08:49:22 +02:00
base57(const T& arg) noexcept
: data(reinterpret_cast<const uchar*>(std::addressof(arg)))
2018-03-22 20:48:38 +01:00
, size(sizeof(T))
{
}
2024-07-27 08:49:22 +02:00
base57(const uchar* data, usz size) noexcept
2018-03-22 20:48:38 +01:00
: data(data)
, size(size)
{
}
};
2024-07-27 08:49:22 +02:00
struct base57_result : public base57
{
std::unique_ptr<uchar[]> memory;
base57_result() noexcept = default;
base57_result(base57_result&&) = default;
base57_result& operator=(base57_result&&) = default;
explicit base57_result(usz size) noexcept
: base57(size ? new uchar[size] : nullptr, size)
, memory(const_cast<uchar*>(this->data))
{
}
static base57_result from_string(std::string_view str);
};
template <typename... Args>
constexpr const fmt_type_info type_info_v[sizeof...(Args) + 1]{fmt_type_info::make<fmt_unveil_t<Args>>()...};
// Internal formatting function
2016-08-05 18:49:45 +02:00
void raw_append(std::string& out, const char*, const fmt_type_info*, const u64*) noexcept;
// Formatting function
2020-12-18 08:39:54 +01:00
template <typename CharT, usz N, typename... Args>
2021-02-09 10:33:50 +01:00
FORCE_INLINE SAFE_BUFFERS(void) append(std::string& out, const CharT(&fmt)[N], const Args&... args)
{
raw_append(out, reinterpret_cast<const char*>(fmt), type_info_v<Args...>, fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...});
}
// Formatting function
2020-12-18 08:39:54 +01:00
template <typename CharT, usz N, typename... Args>
2021-02-09 10:33:50 +01:00
FORCE_INLINE SAFE_BUFFERS(std::string) format(const CharT(&fmt)[N], const Args&... args)
2016-02-01 22:55:43 +01:00
{
std::string result;
2020-02-04 19:37:00 +01:00
append(result, fmt, args...);
2016-02-01 22:55:43 +01:00
return result;
}
// Internal exception message formatting template, must be explicitly specialized or instantiated in cpp to minimize code bloat
[[noreturn]] void raw_throw_exception(std::source_location, const char*, const fmt_type_info*, const u64*);
// Throw exception with formatting
2020-12-18 08:39:54 +01:00
template <typename CharT, usz N, typename... Args>
struct throw_exception
{
struct args_break_t {};
2021-02-09 10:33:50 +01:00
[[noreturn]] FORCE_INLINE SAFE_BUFFERS() throw_exception(const CharT(&fmt)[N], const Args&... args,
args_break_t = args_break_t{}, std::source_location src_loc = std::source_location::current())
{
raw_throw_exception(src_loc, reinterpret_cast<const char*>(fmt), type_info_v<Args...>, fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...});
}
#ifndef _MSC_VER
[[noreturn]] ~throw_exception();
#endif
};
2020-12-18 08:39:54 +01:00
template <typename CharT, usz N, typename... Args>
throw_exception(const CharT(&)[N], const Args&...) -> throw_exception<CharT, N, Args...>;
// Helper template: pack format variables
template <typename Arg = void, typename... Args>
struct tie
{
// Universal reference
std::add_rvalue_reference_t<Arg> arg;
tie<Args...> next;
// Store only references, unveil op is postponed
tie(Arg&& arg, Args&&... args) noexcept
: arg(std::forward<Arg>(arg))
, next(std::forward<Args>(args)...)
{
}
using type = std::remove_cvref_t<Arg>;
// Storage for fmt_unveil (deferred initialization)
decltype(fmt_unveil<type>::get(std::declval<Arg>())) value;
void init(u64 to[])
{
value = fmt_unveil<type>::get(arg);
to[0] = value;
next.init(to + 1);
}
};
template <>
struct tie<void>
{
void init(u64 to[]) const
{
// Isn't really null terminated, this value has no meaning
to[0] = 0;
}
};
template <typename... Args>
tie(Args&&... args) -> tie<Args...>;
// Ensure with formatting
template <typename T, typename CharT, usz N, typename... Args>
decltype(auto) ensure(T&& arg, const CharT(&fmt)[N], tie<Args...> args, std::source_location src_loc = std::source_location::current()) noexcept
{
if (std::forward<T>(arg)) [[likely]]
{
return std::forward<T>(arg);
}
// Prepare u64 array
u64 data[sizeof...(Args) + 1];
args.init(data);
raw_throw_exception(src_loc, reinterpret_cast<const char*>(fmt), type_info_v<std::remove_cvref_t<Args>...>, +data);
}
}