mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-04 01:49:05 +00:00
Changed to use concepts instead of static_assert
This commit is contained in:
@@ -158,7 +158,6 @@
|
|||||||
<ClInclude Include="LX-Common.h" />
|
<ClInclude Include="LX-Common.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="src\IO.cpp" />
|
|
||||||
<ClCompile Include="src\Error.cpp" />
|
<ClCompile Include="src\Error.cpp" />
|
||||||
<ClCompile Include="src\Logger.cpp" />
|
<ClCompile Include="src\Logger.cpp" />
|
||||||
<ClCompile Include="src\pch.cpp">
|
<ClCompile Include="src\pch.cpp">
|
||||||
|
|||||||
@@ -34,9 +34,6 @@
|
|||||||
<ClCompile Include="src\Logger.cpp">
|
<ClCompile Include="src\Logger.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="src\IO.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="src\Error.cpp">
|
<ClCompile Include="src\Error.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
|||||||
@@ -63,6 +63,21 @@
|
|||||||
|
|
||||||
#pragma warning(pop) // <- Renables all warnings
|
#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 //
|
// Includes the rest of common //
|
||||||
|
|
||||||
#include <inc/Error.h>
|
#include <inc/Error.h>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
namespace LX
|
namespace LX
|
||||||
{
|
{
|
||||||
// Enum of colors with their Win32 equivalent value //
|
// Enum of colors with their Win32 equivalent value //
|
||||||
enum class Color
|
enum class Color : WORD
|
||||||
{
|
{
|
||||||
BLACK = 0,
|
BLACK = 0,
|
||||||
BLUE = 1,
|
BLUE = 1,
|
||||||
@@ -20,8 +20,25 @@ namespace LX
|
|||||||
WHITE = 15
|
WHITE = 15
|
||||||
};
|
};
|
||||||
|
|
||||||
// Prints a string to std::cout with a certain color using Win32 API //
|
// Variadic template to output multiple arguments to the console //
|
||||||
extern "C" void COMMON_API PrintStringAsColor(const std::string& str, Color c);
|
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")
|
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 //
|
// It acts like a fancy namespace //
|
||||||
Log() = delete;
|
Log() = delete;
|
||||||
|
|
||||||
|
// The different formats the log supports //
|
||||||
enum class Format
|
enum class Format
|
||||||
{
|
{
|
||||||
|
// Default, with a new line character added after every call //
|
||||||
AUTO,
|
AUTO,
|
||||||
|
|
||||||
|
// No new line characters after all the provided arguments //
|
||||||
NONE
|
NONE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Variadic template to allow an undefined ammount of arguments //
|
||||||
template<Format format = Format::AUTO, typename... Args>
|
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)
|
static void out(Args... args)
|
||||||
{
|
{
|
||||||
// Prints out the args ending with a new line unless specified //
|
// Prints out the args ending with a new line unless specified //
|
||||||
@@ -24,11 +32,17 @@ namespace LX
|
|||||||
else { (*s_LogFile << ... << args); }
|
else { (*s_LogFile << ... << args); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Variadic template to allow an undefined ammount of arguments //
|
||||||
template<typename... Args>
|
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)
|
static void LogNewSection(Args... args)
|
||||||
{
|
{
|
||||||
|
// Constant for how a break is represented in the log //
|
||||||
static const char* BREAK = "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-";
|
static const char* BREAK = "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-";
|
||||||
|
|
||||||
|
// Outputs the arguments between two breaks //
|
||||||
*s_LogFile << '\n' << BREAK << '\n';
|
*s_LogFile << '\n' << BREAK << '\n';
|
||||||
(*s_LogFile << ... << args);
|
(*s_LogFile << ... << args);
|
||||||
*s_LogFile << '\n' << BREAK << '\n';
|
*s_LogFile << '\n' << BREAK << '\n';
|
||||||
|
|||||||
@@ -5,16 +5,11 @@ namespace LX
|
|||||||
|
|
||||||
// Util function to throw an error if the condition is met //
|
// Util function to throw an error if the condition is met //
|
||||||
// Given error type must derive from LX::RuntimeError //
|
// 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)
|
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 //
|
// Checks if the condition is met and micro-optimises that errors will not be thrown //
|
||||||
if (condition) [[unlikely]]
|
if (condition) [[unlikely]]
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -34,9 +34,9 @@ namespace LX
|
|||||||
void InvalidFilePath::PrintToConsole() const
|
void InvalidFilePath::PrintToConsole() const
|
||||||
{
|
{
|
||||||
// Tells the user the input file could not be found and how to fix the issue //
|
// 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 << ": ";
|
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";
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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 //
|
// Logs the errors type to the console if built as Debug //
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
std::cout << "LX::RuntimeError thrown of type: ";
|
std::cout << "LX::RuntimeError thrown of type: ";
|
||||||
LX::PrintStringAsColor(e.ErrorType(), LX::Color::WHITE);
|
LX::PrintAsColor<LX::Color::WHITE>(e.ErrorType());
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
#endif // _DEBUG
|
#endif // _DEBUG
|
||||||
|
|
||||||
|
|||||||
@@ -19,15 +19,15 @@ namespace LX
|
|||||||
|
|
||||||
// Prints the error with the relevant information to the console //
|
// Prints the error with the relevant information to the console //
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
PrintAsColor<Color::LIGHT_RED>("Error: ");
|
||||||
std::cout << "Invalid character found in ";
|
std::cout << "Invalid character found in ";
|
||||||
LX::PrintStringAsColor(file, LX::Color::WHITE);
|
PrintAsColor<Color::WHITE>(file);
|
||||||
std::cout << " {";
|
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 << "}:\n";
|
||||||
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << line << " | " << lineContents << "\n";
|
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << line << " | " << lineContents << "\n";
|
||||||
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(col - 1) << "";
|
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(col - 1) << "";
|
||||||
LX::PrintStringAsColor("^", LX::Color::LIGHT_RED);
|
PrintAsColor<Color::LIGHT_RED>("^");
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,23 +32,23 @@ namespace LX
|
|||||||
|
|
||||||
// Prints the error to the console with the relevant info //
|
// Prints the error to the console with the relevant info //
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
LX::PrintStringAsColor("Error: ", LX::Color::LIGHT_RED);
|
PrintAsColor<Color::LIGHT_RED>("Error: ");
|
||||||
std::cout << "Incorrect syntax in ";
|
std::cout << "Incorrect syntax in ";
|
||||||
LX::PrintStringAsColor(file.string(), LX::Color::WHITE);
|
PrintAsColor<Color::WHITE>(file.string());
|
||||||
std::cout << ", found ";
|
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: ";
|
std::cout << " expected: ";
|
||||||
|
|
||||||
// Allows the error to have a custom type that is printed to the console //
|
// 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); }
|
if (expected == LX::Token::UNDEFINED) { PrintAsColor<Color::WHITE>(override.c_str()); }
|
||||||
else { LX::PrintStringAsColor(LX::ToString(expected).c_str(), LX::Color::WHITE); }
|
else { PrintAsColor<Color::WHITE>(ToString(expected).c_str()); }
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
|
|
||||||
// Prints the code with the error to the console //
|
// Prints the code with the error to the console //
|
||||||
std::string errorSquiggle(got.length, '~');
|
std::string errorSquiggle(got.length, '~');
|
||||||
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << got.line << " | " << line << "\n";
|
std::cout << "Line: " << std::setw(lineNumberWidthInConsole) << got.line << " | " << line << "\n";
|
||||||
std::cout << " " << std::setw(lineNumberWidthInConsole) << "" << " | " << std::setw(got.column) << "";
|
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";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user