mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-03 17:39:02 +00:00
Changed to use concepts instead of static_assert
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
namespace LX
|
||||
{
|
||||
// Enum of colors with their Win32 equivalent value //
|
||||
enum class Color
|
||||
enum class Color : WORD
|
||||
{
|
||||
BLACK = 0,
|
||||
BLUE = 1,
|
||||
@@ -20,8 +20,25 @@ namespace LX
|
||||
WHITE = 15
|
||||
};
|
||||
|
||||
// Prints a string to std::cout with a certain color using Win32 API //
|
||||
extern "C" void COMMON_API PrintStringAsColor(const std::string& str, Color c);
|
||||
// Variadic template to output multiple arguments to the console //
|
||||
template<Color color, typename... Args>
|
||||
requires AllLogable<Args...> // <- Checks all types can be outputted to the console
|
||||
|
||||
// Prints arguments to the console with a given color //
|
||||
inline void PrintAsColor(Args... args)
|
||||
{
|
||||
// Gets a handle to the console //
|
||||
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
|
||||
// Sets the color of the console to the desired color //
|
||||
SetConsoleTextAttribute(hConsole, (WORD)color);
|
||||
|
||||
// Outputs the text //
|
||||
(std::cout << ... << args);
|
||||
|
||||
// Resets the color //
|
||||
SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY);
|
||||
}
|
||||
|
||||
inline std::string ReadFileToString(const std::filesystem::path& path, const std::string errorName = "input file path")
|
||||
{
|
||||
|
||||
@@ -8,13 +8,21 @@ namespace LX
|
||||
// It acts like a fancy namespace //
|
||||
Log() = delete;
|
||||
|
||||
// The different formats the log supports //
|
||||
enum class Format
|
||||
{
|
||||
// Default, with a new line character added after every call //
|
||||
AUTO,
|
||||
|
||||
// No new line characters after all the provided arguments //
|
||||
NONE
|
||||
};
|
||||
|
||||
// Variadic template to allow an undefined ammount of arguments //
|
||||
template<Format format = Format::AUTO, typename... Args>
|
||||
requires AllLogable<Args...> // <- Checks all types can be outputted to the console
|
||||
|
||||
// Logs information (if the log is initalised) //
|
||||
static void out(Args... args)
|
||||
{
|
||||
// Prints out the args ending with a new line unless specified //
|
||||
@@ -24,11 +32,17 @@ namespace LX
|
||||
else { (*s_LogFile << ... << args); }
|
||||
}
|
||||
|
||||
// Variadic template to allow an undefined ammount of arguments //
|
||||
template<typename... Args>
|
||||
requires AllLogable<Args...> // <- Checks all types can be outputted to the console
|
||||
|
||||
// Adds a named break between different sections in the log //
|
||||
static void LogNewSection(Args... args)
|
||||
{
|
||||
// Constant for how a break is represented in the log //
|
||||
static const char* BREAK = "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-";
|
||||
|
||||
// Outputs the arguments between two breaks //
|
||||
*s_LogFile << '\n' << BREAK << '\n';
|
||||
(*s_LogFile << ... << args);
|
||||
*s_LogFile << '\n' << BREAK << '\n';
|
||||
|
||||
@@ -5,16 +5,11 @@ namespace LX
|
||||
|
||||
// Util function to throw an error if the condition is met //
|
||||
// Given error type must derive from LX::RuntimeError //
|
||||
template<typename Error, typename... Args>
|
||||
template<typename Error, typename... Args> requires
|
||||
std::is_constructible_v<Error, Args...> && // <- Checks the arguments can be used to construct the object
|
||||
std::is_base_of_v<LX::RuntimeError, Error> // <- Checks the error class is a child of LX::RuntimeError
|
||||
inline void ThrowIf(const bool condition, Args&&... args)
|
||||
{
|
||||
// Checks that the error type will be caught by the error checker //
|
||||
static_assert
|
||||
(
|
||||
std::is_base_of_v<LX::RuntimeError, Error>,
|
||||
"Must throw a type that derives from LX::RuntimeError"
|
||||
);
|
||||
|
||||
// Checks if the condition is met and micro-optimises that errors will not be thrown //
|
||||
if (condition) [[unlikely]]
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user