Initial commit

This commit is contained in:
Pasha Bibko
2025-07-19 20:48:54 +01:00
commit ab564e9649
14 changed files with 258 additions and 0 deletions

49
Common/FileRead.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#include <filesystem>
#include <fstream>
namespace LXC::Util
{
struct FileReadError
{
enum Reason
{
FileNotFound,
PermissionDenied,
NotAFile
};
FileReadError(const std::filesystem::path& _path, Reason _reason)
: path(_path), reason(_reason)
{}
std::filesystem::path path;
Reason reason;
};
inline ReturnVal<std::string, FileReadError> ReadFile(const std::filesystem::path& filepath)
{
// Checks the file exists //
if (!std::filesystem::exists(filepath))
return FunctionFail(FileReadError(std::filesystem::absolute(filepath), FileReadError::FileNotFound));
// Checks it is a regular file //
if (!std::filesystem::is_regular_file(filepath))
return FunctionFail(FileReadError(std::filesystem::absolute(filepath), FileReadError::NotAFile));
// Checks it can open the file //
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file)
return FunctionFail(FileReadError(std::filesystem::absolute(filepath), FileReadError::PermissionDenied));
// Copies the file to the output string //
const std::streamsize len = file.tellg();
file.seekg(0, std::ios::beg);
std::string contents(len, '\0');
file.read(&contents[0], len);
return contents;
}
}

11
Common/LXC.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
// Standard libraries //
#include <iostream>
#include <cstdlib>
// LXC util files //
#include <Result.h>
#include <FileRead.h>

73
Common/Result.h Normal file
View File

@@ -0,0 +1,73 @@
#pragma once
#include <type_traits>
#include <cstdlib>
namespace LXC::Util
{
// Custom version of std::unexpected //
template<typename ErrorType> struct FunctionFail
{
explicit FunctionFail(ErrorType _err)
: error(_err) {}
ErrorType error;
};
// Custom version of std::expected //
template<typename ResultType, typename ErrorType>
requires (!std::same_as<ResultType, bool>) // ResultType being bool causes issues with operator overloads
class ReturnVal
{
public:
// Constructor for function sucess //
ReturnVal(ResultType result)
: m_Result(result), m_FunctionFailed(false)
{}
// Constructor for function fail //
ReturnVal(FunctionFail<ErrorType> error)
: m_Error(error.error), m_FunctionFailed(true)
{}
// Destructor //
~ReturnVal() {}
// Different getters of the class //
inline bool Failed() const { return m_FunctionFailed; }
inline bool Suceeded() const { return !m_FunctionFailed; }
inline ResultType& Result()
{
if (Suceeded()) _LIKELY
return m_Result;
std::exit(EXIT_FAILURE);
}
inline ErrorType& Error()
{
if (Failed()) _LIKELY
return m_Error;
std::exit(EXIT_FAILURE);
}
// Operator overloads //
operator bool() const { return !m_FunctionFailed; }
operator ResultType() { return Result(); }
private:
// Union to hold either the result or the error //
union
{
ResultType m_Result;
ErrorType m_Error;
};
// Tracks what item is currently in the union //
bool m_FunctionFailed;
};
}