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:
@@ -158,7 +158,6 @@
|
||||
<ClInclude Include="LX-Common.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\IO.cpp" />
|
||||
<ClCompile Include="src\Error.cpp" />
|
||||
<ClCompile Include="src\Logger.cpp" />
|
||||
<ClCompile Include="src\pch.cpp">
|
||||
|
||||
@@ -34,9 +34,6 @@
|
||||
<ClCompile Include="src\Logger.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\IO.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Error.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
|
||||
@@ -63,6 +63,21 @@
|
||||
|
||||
#pragma warning(pop) // <- Renables all warnings
|
||||
|
||||
// Concepts used for template verification //
|
||||
|
||||
namespace LX
|
||||
{
|
||||
// Checks if a type can be outputted to std::ostream //
|
||||
template<typename T> concept Logable = requires(std::ostream & os, T t)
|
||||
{
|
||||
// I have no idea what this part does at all //
|
||||
{ os << t } -> std::same_as<std::ostream&>;
|
||||
};
|
||||
|
||||
// Checks if a list of types can be outputted to std::ostream //
|
||||
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
|
||||
}
|
||||
|
||||
// Includes the rest of common //
|
||||
|
||||
#include <inc/Error.h>
|
||||
|
||||
@@ -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]]
|
||||
{
|
||||
|
||||
@@ -34,9 +34,9 @@ namespace LX
|
||||
void InvalidFilePath::PrintToConsole() const
|
||||
{
|
||||
// Tells the user the input file could not be found and how to fix the issue //
|
||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
||||
LX::PrintAsColor<Color::LIGHT_RED>("Error: ");
|
||||
std::cout << "Invalid " << name << ": ";
|
||||
LX::PrintStringAsColor(fileLocation, LX::Color::WHITE);
|
||||
LX::PrintAsColor<Color::WHITE>(fileLocation);
|
||||
std::cout << "\n\nMake sure the file exists and the process has the correct path to the file\n";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
#include <LX-Common.h>
|
||||
|
||||
namespace LX
|
||||
{
|
||||
void PrintStringAsColor(const std::string& str, Color c)
|
||||
{
|
||||
// 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)c);
|
||||
|
||||
// Outputs the text //
|
||||
std::cout << str;
|
||||
|
||||
// Resets the color //
|
||||
SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user