2014-04-01 02:33:55 +02:00
|
|
|
#pragma once
|
2015-01-12 19:12:06 +01:00
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
#include <exception>
|
2016-05-13 16:01:48 +02:00
|
|
|
#include <string>
|
2016-02-01 22:55:43 +01:00
|
|
|
|
|
|
|
#include "Platform.h"
|
|
|
|
#include "types.h"
|
2014-04-01 02:33:55 +02:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
namespace fmt
|
|
|
|
{
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename... Args>
|
2016-08-03 22:51:05 +02:00
|
|
|
static std::string format(const char*, const Args&...);
|
|
|
|
}
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T, typename>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_unveil
|
|
|
|
{
|
|
|
|
static_assert(sizeof(T) > 0, "fmt_unveil<>: cannot pass forward-declared object");
|
|
|
|
|
|
|
|
using type = T;
|
|
|
|
|
|
|
|
static inline u64 get(const T& arg)
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
2016-08-03 22:51:05 +02:00
|
|
|
return reinterpret_cast<std::uintptr_t>(&arg);
|
2016-04-25 12:49:12 +02:00
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
return reinterpret_cast<std::uintptr_t>(&arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This overload resolution takes the precedence
|
|
|
|
static inline u64_wrapper get(T&& arg)
|
|
|
|
{
|
|
|
|
return {std::move(arg)};
|
|
|
|
}
|
2016-08-03 22:51:05 +02:00
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_unveil<T, std::enable_if_t<std::is_integral<T>::value && sizeof(T) <= 8 && alignof(T) <= 8>>
|
|
|
|
{
|
|
|
|
using type = T;
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
static inline u64 get(T arg)
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
2016-08-03 22:51:05 +02:00
|
|
|
return static_cast<T>(arg);
|
|
|
|
}
|
|
|
|
};
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_unveil<T, std::enable_if_t<std::is_floating_point<T>::value && sizeof(T) <= 8 && alignof(T) <= 8>>
|
|
|
|
{
|
|
|
|
using type = T;
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
// Convert FP to f64 and reinterpret (TODO?)
|
|
|
|
static inline u64 get(f64 arg)
|
2015-01-18 23:54:56 +01:00
|
|
|
{
|
2016-08-03 22:51:05 +02:00
|
|
|
return reinterpret_cast<u64&>(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_unveil<T, std::enable_if_t<std::is_enum<T>::value>>
|
|
|
|
{
|
|
|
|
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>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_unveil<T*, void>
|
|
|
|
{
|
|
|
|
using type = const T*;
|
|
|
|
|
|
|
|
static inline u64 get(const T* arg)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<std::uintptr_t>(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T, std::size_t N>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_unveil<T[N], void>
|
|
|
|
{
|
|
|
|
using type = const T*;
|
|
|
|
|
|
|
|
static inline u64 get(const T* arg)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<std::uintptr_t>(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_unveil<b8, void>
|
|
|
|
{
|
|
|
|
using type = bool;
|
|
|
|
|
|
|
|
static inline u64 get(const b8& value)
|
|
|
|
{
|
|
|
|
return fmt_unveil<bool>::get(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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>
|
2016-08-03 22:51:05 +02:00
|
|
|
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)
|
|
|
|
static SAFE_BUFFERS FORCE_INLINE const T& get_object(u64 arg)
|
|
|
|
{
|
|
|
|
return *reinterpret_cast<const T*>(static_cast<std::uintptr_t>(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function (safely converts arg to enum value)
|
2016-08-13 15:36:04 +02:00
|
|
|
static SAFE_BUFFERS FORCE_INLINE void format_enum(std::string& out, u64 arg, const char* (*get)(T value))
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
|
|
|
const auto value = static_cast<std::underlying_type_t<T>>(arg);
|
|
|
|
|
|
|
|
// Check narrowing
|
|
|
|
if (static_cast<u64>(value) == arg)
|
|
|
|
{
|
|
|
|
if (const char* str = get(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)
|
2016-08-13 15:36:04 +02:00
|
|
|
static SAFE_BUFFERS FORCE_INLINE 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;
|
|
|
|
|
|
|
|
for (u64 i = 0; i < 64; i++)
|
|
|
|
{
|
|
|
|
const u64 mask = 1ull << i;
|
|
|
|
|
|
|
|
if (arg & mask)
|
|
|
|
{
|
|
|
|
fmt(out, i);
|
|
|
|
|
|
|
|
if (arg > mask)
|
|
|
|
{
|
|
|
|
out += delim;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out += suffix;
|
|
|
|
}
|
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
// 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 <>
|
2016-08-03 22:51:05 +02:00
|
|
|
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>
|
2016-08-03 22:51:05 +02:00
|
|
|
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 <>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_class_string<const char*, void>
|
|
|
|
{
|
|
|
|
static void format(std::string& out, u64 arg);
|
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <>
|
2016-08-03 22:51:05 +02:00
|
|
|
struct fmt_class_string<char*, void> : fmt_class_string<const char*>
|
|
|
|
{
|
|
|
|
// Classify char* as const char*
|
|
|
|
};
|
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
struct fmt_type_info
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
decltype(&fmt_class_string<int>::format) fmt_string;
|
2016-08-03 22:51:05 +02:00
|
|
|
|
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-03 22:51:05 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
return fmt_type_info
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-05 18:49:45 +02:00
|
|
|
&fmt_class_string<T>::format,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename Arg>
|
2016-08-05 18:49:45 +02:00
|
|
|
using fmt_unveil_t = typename fmt_unveil<Arg>::type;
|
|
|
|
|
|
|
|
// Argument array type (each element generated via fmt_unveil<>)
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename... Args>
|
2016-08-05 18:49:45 +02:00
|
|
|
using fmt_args_t = const u64(&&)[sizeof...(Args) + 1];
|
2015-01-18 23:54:56 +01:00
|
|
|
|
2016-08-05 18:49:45 +02:00
|
|
|
namespace fmt
|
|
|
|
{
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename... Args>
|
|
|
|
SAFE_BUFFERS FORCE_INLINE const fmt_type_info* get_type_info()
|
2016-08-08 18:01:06 +02:00
|
|
|
{
|
|
|
|
// Constantly initialized null-terminated list of type-specific information
|
2016-08-13 15:36:04 +02:00
|
|
|
static constexpr fmt_type_info result[sizeof...(Args) + 1]{fmt_type_info::make<Args>()...};
|
2016-08-08 18:01:06 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-08-03 22:51:05 +02:00
|
|
|
// 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;
|
2016-08-03 22:51:05 +02:00
|
|
|
|
|
|
|
// Formatting function
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename... Args>
|
|
|
|
SAFE_BUFFERS FORCE_INLINE void append(std::string& out, const char* fmt, const Args&... args)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
raw_append(out, fmt, fmt::get_type_info<fmt_unveil_t<Args>...>(), fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...});
|
2016-08-03 22:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Formatting function
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename... Args>
|
|
|
|
SAFE_BUFFERS FORCE_INLINE std::string format(const char* fmt, const Args&... args)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2016-08-03 22:51:05 +02:00
|
|
|
std::string result;
|
|
|
|
append<Args...>(result, fmt, args...);
|
2016-02-01 22:55:43 +01:00
|
|
|
return result;
|
|
|
|
}
|
2016-08-03 22:51:05 +02:00
|
|
|
|
2016-08-08 18:01:06 +02:00
|
|
|
// Internal exception message formatting template, must be explicitly specialized or instantiated in cpp to minimize code bloat
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T>
|
2016-08-08 18:01:06 +02:00
|
|
|
[[noreturn]] void raw_throw_exception(const char*, const fmt_type_info*, const u64*);
|
2016-08-03 22:51:05 +02:00
|
|
|
|
2016-08-08 18:01:06 +02:00
|
|
|
// Throw exception with formatting
|
2016-08-13 15:36:04 +02:00
|
|
|
template <typename T = std::runtime_error, typename... Args>
|
2016-08-08 18:01:06 +02:00
|
|
|
[[noreturn]] SAFE_BUFFERS FORCE_INLINE void throw_exception(const char* fmt, const Args&... args)
|
2016-08-03 22:51:05 +02:00
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
raw_throw_exception<T>(fmt, fmt::get_type_info<fmt_unveil_t<Args>...>(), fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...});
|
|
|
|
}
|
2014-04-01 02:33:55 +02:00
|
|
|
}
|