Made common a project

This commit is contained in:
Pasha Bibko
2025-05-05 16:45:34 +01:00
parent 616ed1ca21
commit c64a2c692c
27 changed files with 337 additions and 146 deletions

52
Common/inc/Console.h Normal file
View File

@@ -0,0 +1,52 @@
namespace LX
{
enum class Color
{
BLACK = 0,
BLUE = 1,
GREEN = 2,
AQUA = 3,
RED = 4,
PURPLE = 5,
YELLOW = 6,
LIGHT_GRAY = 7,
LIGHT_BLUE = 9,
LIGHT_GREEN = 10,
LIGHT_AQUA = 11,
LIGHT_RED = 12,
LIGHT_PURPLE = 13,
LIGHT_YELLOW = 14,
WHITE = 15
};
inline 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);
}
// Util function for getting a line of the source at a given index (used for errors) //
inline std::string GetLineAtIndexOf(const std::string src, const std::streamsize index)
{
// Finds the start of the line //
size_t start = src.rfind('\n', index);
if (start == std::string::npos) { start = 0; } // None means first line
else { start = start + 1; } // Skips new line char
// Finds the end of the line //
size_t end = src.find('\n', index);
if (end == std::string::npos) { end = src.size(); } // None means last line
// Returns the string between start and end //
return src.substr(start, end - start);
}
}

22
Common/inc/Error.h Normal file
View File

@@ -0,0 +1,22 @@
namespace LX
{
// Base error class for all LX thrown errors //
// Holds nothing apart from the v-table //
struct RuntimeError
{
// Prints the error to the console //
// Include Common/Console.h for printing util functions //
virtual void PrintToConsole() const = 0;
// Returns a C-String of the type that was thrown //
virtual const char* ErrorType() const = 0;
};
}
// Helper macro to autogenerate a basic error type in a .h file //
// Still requires function definitions in a .cpp file //
#define CREATE_EMPTY_LX_ERROR_TYPE(name)\
struct name : public LX::RuntimeError{ GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS };
// Helper macro to autogenerate function declarations of functions required by LX::RuntimeError //
#define GENERATE_LX_ERROR_REQUIRED_FUNCTION_DECLARATIONS void PrintToConsole() const; const char* ErrorType() const;

23
Common/inc/Logger.h Normal file
View File

@@ -0,0 +1,23 @@
namespace LX
{
template<typename... Args>
inline void SafeLog(std::ofstream* log, Args... args)
{
if (log != nullptr)
{
(*log << ... << args);
(*log << '\n');
}
}
inline void SafeFlush(std::ofstream* log)
{
if (log != nullptr)
{
log->flush();
}
}
// Gives a standard way to mark a change between different sections within the log output //
constexpr const char* LOG_BREAK = "\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
}

26
Common/inc/ThrowIf.h Normal file
View File

@@ -0,0 +1,26 @@
namespace LX
{
// Foward declares LX::RuntimeError so it can be used to see if a class derives from //
struct RuntimeError;
// Util function to throw an error if the condition is met //
// Given error type must derive from LX::RuntimeError //
template<typename Error, typename... 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 //
if (condition) [[unlikely]]
{
// Throws a COPY of the error and not itself //
// How C++ works, no way around it //
throw Error(std::forward<Args>(args)...);
}
}
}