Changed to use concepts instead of static_assert

This commit is contained in:
Pasha Bibko
2025-05-07 19:43:09 +01:00
parent 98fc4589ab
commit c472cb5fc5
11 changed files with 65 additions and 47 deletions

View File

@@ -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">

View File

@@ -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>

View File

@@ -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>

View File

@@ -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")
{

View File

@@ -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';

View File

@@ -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]]
{

View File

@@ -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";
}

View File

@@ -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);
}
}

View File

@@ -43,7 +43,7 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_
// Logs the errors type to the console if built as Debug //
#ifdef _DEBUG
std::cout << "LX::RuntimeError thrown of type: ";
LX::PrintStringAsColor(e.ErrorType(), LX::Color::WHITE);
LX::PrintAsColor<LX::Color::WHITE>(e.ErrorType());
std::cout << "\n";
#endif // _DEBUG

View File

@@ -19,15 +19,15 @@ namespace LX
// Prints the error with the relevant information to the console //
std::cout << "\n";
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
PrintAsColor<Color::LIGHT_RED>("Error: ");
std::cout << "Invalid character found in ";
LX::PrintStringAsColor(file, LX::Color::WHITE);
PrintAsColor<Color::WHITE>(file);
std::cout << " {";
LX::PrintStringAsColor(std::string(1, invalid), LX::Color::LIGHT_RED);
PrintAsColor<Color::LIGHT_RED>(std::string(1, invalid));
std::cout << "}:\n";
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << line << " | " << lineContents << "\n";
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(col - 1) << "";
LX::PrintStringAsColor("^", LX::Color::LIGHT_RED);
PrintAsColor<Color::LIGHT_RED>("^");
std::cout << "\n";
}

View File

@@ -32,23 +32,23 @@ namespace LX
// Prints the error to the console with the relevant info //
std::cout << "\n";
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
PrintAsColor<Color::LIGHT_RED>("Error: ");
std::cout << "Incorrect syntax in ";
LX::PrintStringAsColor(file.string(), LX::Color::WHITE);
PrintAsColor<Color::WHITE>(file.string());
std::cout << ", found ";
LX::PrintStringAsColor(LX::ToString(got.type).c_str(), LX::Color::WHITE);
PrintAsColor<Color::WHITE>(LX::ToString(got.type).c_str());
std::cout << " expected: ";
// Allows the error to have a custom type that is printed to the console //
if (expected == LX::Token::UNDEFINED) { LX::PrintStringAsColor(override.c_str(), LX::Color::WHITE); }
else { LX::PrintStringAsColor(LX::ToString(expected).c_str(), LX::Color::WHITE); }
if (expected == LX::Token::UNDEFINED) { PrintAsColor<Color::WHITE>(override.c_str()); }
else { PrintAsColor<Color::WHITE>(ToString(expected).c_str()); }
std::cout << "\n";
// Prints the code with the error to the console //
std::string errorSquiggle(got.length, '~');
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << got.line << " | " << line << "\n";
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(got.column) << "";
LX::PrintStringAsColor(errorSquiggle.c_str(), LX::Color::LIGHT_RED);
PrintAsColor<Color::LIGHT_RED>(errorSquiggle.c_str());
std::cout << "\n";
}