Man im so tired This is wrong but I can taste it, im close

This commit is contained in:
NaifBanana 2026-04-10 00:31:11 -05:00
parent a927b96a99
commit b593b7e083
8 changed files with 182 additions and 59 deletions

View File

@ -1,6 +1,6 @@
toAbsolutePath(NB_CORE_SOURCE toAbsolutePath(NB_CORE_SOURCE
./src/Errors.cpp ./src/Errors.cpp
./src/Logger.cpp # ./src/Logger.cpp
./src/Processes.cpp ./src/Processes.cpp
./src/Utils.cpp ./src/Utils.cpp
) )

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <memory>
#ifndef _NB_ERROR #ifndef _NB_ERROR
#define _NB_ERROR #define _NB_ERROR
@ -6,10 +7,11 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include "TypeTraits.hpp"
#include <unordered_map> #include <unordered_map>
#include "Utils.hpp" #include "Utils.hpp"
#ifndef THROW_WITH_INFO /* #ifndef THROW_WITH_INFO
#ifdef CODE_ERROR_LOCATIONS #ifdef CODE_ERROR_LOCATIONS
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__) #define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
#else #else
@ -23,30 +25,43 @@
#else #else
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__) #define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
#endif // LOGGING #endif // LOGGING
#endif // THROW #endif // THROW */
namespace nb { namespace nb {
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap; typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
template <typename T>
using has_type = decltype(T::type);
template<typename StringType, typename ErrorType>
struct NBErrorWhatString;
class ErrorBase_what_str_impl : public std::exception {
template <typename T>
T what_str() const;
template <>
virtual std::wstring what_str() const;
template<>
virtual std::string what_str() const;
};
template<class ErrorType> template<class ErrorType>
class ErrorBase : public std::exception { class ErrorBase : public ErrorBase_what_str_impl {
public: public:
ErrorBase(const ErrorBase&) = default; ErrorBase(const ErrorBase&) = default;
ErrorBase& operator=(const ErrorBase&) = delete; ErrorBase& operator=(const ErrorBase&) = delete;
virtual const char* what() const noexcept override final { template<typename StringType = std::string>
std::string ret = this->msg; inline const StringType what_str() const noexcept {
const std::string replace = nb::NEWLINE + "\t"; return NBErrorWhatString<StringType,ErrorType>::what(*this);
if (trace) {
ret += replace + find_and_replace(
std::string(trace->what()),
nb::NEWLINE,
replace
);
} }
const std::string& ret_ref = ret;
return ret_ref.c_str(); virtual const char* what() const noexcept override final {
const std::string& ret = what_str();
return ret.c_str();
} }
static const std::string type; static const std::string type;
@ -54,7 +69,8 @@ public:
const unsigned int code; const unsigned int code;
const std::string msg; const std::string msg;
const std::exception* trace; const std::shared_ptr<const std::exception> trace;
const bool traceIsNBError;
protected: protected:
ErrorBase() = delete; ErrorBase() = delete;
@ -72,7 +88,8 @@ protected:
) noexcept : ) noexcept :
code(code_), code(code_),
msg(msg_), msg(msg_),
trace{static_cast<std::exception*>(trace_ ? new TraceType(*trace_) : nullptr) } trace{trace_ ? std::make_shared<TraceType>(*trace_) : nullptr},
traceIsNBError( trace_ ? is_detected<has_type, TraceType>::value : false)
{ {
static_assert(std::is_same<const std::unordered_map<unsigned int, const char*>, decltype(ErrorType::ErrorMessages)>::value, static_assert(std::is_same<const std::unordered_map<unsigned int, const char*>, decltype(ErrorType::ErrorMessages)>::value,
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be " "const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
@ -148,19 +165,58 @@ protected:
filename_ filename_
) {} ) {}
std::string (ErrorBase::*_trace_what_str)();
std::wstring (ErrorBase::*_trace_what_wstr)();
}; };
template <typename ErrorType> template <typename ErrorType>
const std::string ErrorBase<ErrorType>::type = ErrorType::type; const std::string ErrorBase<ErrorType>::type = ErrorType::type;
template <typename ErrorType> template <typename ErrorType>
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages; const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
class DefaultError {DefaultError() = default;}; template<typename ErrorType>
struct NBErrorWhatString<std::wstring, ErrorType> {
inline static const std::wstring what(const ErrorBase<ErrorType>& err) noexcept {
std::wstring ret = nb::str_to_wstr(err.msg);
const std::wstring replace = nb::WNEWLINE + L"┗-";
if (err.trace) {
std::wstring trace_msg;
if (err.traceIsNBError) {
trace_msg = std::static_pointer_cast<ErrorBase_what_str_impl>(err.trace)->template what_str<std::wstring>();
} else {
trace_msg = nb::str_to_wstr(std::string(err.trace->what()));
}
trace_msg = find_and_replace<std::wstring>(trace_msg, L"", L"-");
ret += replace + find_and_replace<std::wstring>(
trace_msg,
nb::WNEWLINE,
replace
);
}
return ret;
}
};
template <class ErrorType = DefaultError> template<typename ErrorType>
struct NBErrorWhatString<std::string, ErrorType> {
inline static const std::string what(const ErrorBase<ErrorType>& err) noexcept {
std::string ret = err.msg;
const std::string replace = nb::NEWLINE+" ";
if (err.trace) {
ret += replace + find_and_replace(
std::string(err.trace->what()),
nb::NEWLINE,
replace
);
}
return ret;
}
};
template <class ErrorType = NoneType>
class Error; class Error;
template<> template<>
class Error<DefaultError>; class Error<NoneType>;
template <class ErrorType> template <class ErrorType>
class Error : public ErrorBase<ErrorType> { class Error : public ErrorBase<ErrorType> {
@ -180,8 +236,8 @@ public:
}; };
template<> template<>
class Error<DefaultError> : public ErrorBase<Error<DefaultError>> { class Error<NoneType> : public ErrorBase<Error<NoneType>> {
using Base = ErrorBase<Error<DefaultError>>; using Base = ErrorBase<Error<NoneType>>;
public: public:
using Base::Base; using Base::Base;
Error() : Base(0) {} Error() : Base(0) {}

View File

@ -6,7 +6,43 @@
namespace nb { namespace nb {
template<std::size_t N=0, typename Func, typename... Pack> namespace detail {
template <
class Default,
class AlwaysVoid,
template <class...> class Op,
class... Args
>
struct detector {
using value = std::false_type;
using type = Default;
};
template <
class Default,
template <class...> class Op,
class... Args
>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...> {
using value = std::true_type;
using type = Op<Args...>;
};
} // namespace detail
struct NoneType{};
template <
template <class...> class Op,
class... Args
>
using is_detected = typename detail::detector<NoneType, void, Op, Args...>::value;
template <typename Stream, typename... Args>
void stream(Stream& s, Args&&... args) {
(s << ... << args);
}
/*template<std::size_t N=0, typename Func, typename... Pack>
inline typename std::enable_if<N==sizeof...(Pack), void>::type inline typename std::enable_if<N==sizeof...(Pack), void>::type
ForEach(std::tuple<Pack...>&, Func) {} ForEach(std::tuple<Pack...>&, Func) {}
@ -68,7 +104,7 @@ struct PackIsSameType<T, PackType> {
template <typename T, typename FirstPack, typename... Pack> template <typename T, typename FirstPack, typename... Pack>
struct PackIsSameType<T, FirstPack, Pack...> { struct PackIsSameType<T, FirstPack, Pack...> {
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value; const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
}; };*/
} // namespace nb } // namespace nb

View File

@ -4,40 +4,33 @@
#ifndef _NB_CORE_TYPES #ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES #define _NB_CORE_TYPES
#include <array>
#include <utility>
#include <string> #include <string>
namespace nb { namespace nb {
#ifdef _NB_TARGET_WINDOWS #ifdef _NB_TARGET_WINDOWS
const std::string NEWLINE; const std::string NEWLINE = "\r\n";
const std::wstring WNEWLINE; const std::wstring WNEWLINE = L"\r\n";
#endif // _NB_TARGET_WINDOWS #endif // _NB_TARGET_WINDOWS
#ifdef _NB_TARGET_LINUX #ifdef _NB_TARGET_LINUX
const std::string NEWLINE; const std::string NEWLINE = "\n";
const std::wstring WNEWLINE; const std::wstring WNEWLINE = L"\n";
#endif // _NB_TARGET_LINUX #endif // _NB_TARGET_LINUX
template<typename T> template<typename T>
std::enable_if_t<std::is_convertible_v<T, std::string>, std::wstring> str_to_wstr(const T& str) { std::enable_if_t<std::is_convertible_v<const T&, std::string>, std::wstring> str_to_wstr(const T& in) {
std::mbstate_t state = std::mbstate_t(); std::string str(in);
const char* cstr = std::string(str).c_str(); std::wstring ret(str.begin(), str.end());
std::size_t wlen = 1 + std::mbsrtowcs(nullptr, &cstr, 0, &state);
wchar_t* c_wstr= new wchar_t[wlen];
std::mbsrtowcs(c_wstr, &cstr, wlen, &state);
std::wstring ret(c_wstr, wlen);
delete[] c_wstr;
return ret; return ret;
} }
template<typename T> template<typename T>
std::enable_if_t<std::is_convertible_v<T, std::wstring>, std::string> wstr_to_str(const T& wstr) { std::enable_if_t<std::is_convertible_v<const T&, std::wstring>, std::string> wstr_to_str(const T& in) {
const wchar_t* c_wstr = std::wstring(wstr).c_str(); std::wstring wstr(in);
std::size_t strlen = 1 + std::wcstombs(nullptr, c_wstr, 0); std::size_t wstrlen = wstr.length();
char* c_str= new char[strlen]; char* c_str= new char[wstrlen];
std::wcstombs(c_str, c_wstr, strlen); std::wcstombs(c_str, wstr.c_str(), wstrlen);
std::string ret(c_str, strlen); std::string ret(c_str, wstrlen);
delete[] c_str; delete[] c_str;
return ret; return ret;
} }

View File

@ -1,10 +1,11 @@
#include "Errors.hpp" #include "Errors.hpp"
#include "TypeTraits.hpp"
namespace nb { namespace nb {
const std::string Error<DefaultError>::type = "nb::Error"; const std::string Error<nb::NoneType>::type = "nb::Error";
const ErrorCodeMap Error<DefaultError>::ErrorMessages = { const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
{Error::Codes::GENERAL, "General std::exception."}, {Error::Codes::GENERAL, "General std::exception."},
{Error::Codes::UNDEFINED, "Undefined / general error."}, {Error::Codes::UNDEFINED, "Undefined / general error."},
{Error::Codes::BADERRORCODE, "Unrecognized error code."} {Error::Codes::BADERRORCODE, "Unrecognized error code."}

View File

@ -1,14 +1,8 @@
#include <corecrt.h>
#include <stdint.h>
#include <string> #include <string>
#include "Utils.hpp" #include "Utils.hpp"
#ifdef _NB_TARGET_WINDOWS template<>
const std::string NEWLINE("\r\n"); std::wstring nb::str_to_wstr(const std::string& in) {
const std::wstring WNEWLINE(L"\r\n"); std::wstring ret(in.begin(), in.end());
#endif // _NB_TARGET_WINDOWS return ret;
#ifdef _NB_TARGET_LINUX }
const std::string NEWLINE("\n");
const std::wstring WNEWLINE(L"\n");
#endif // _NB_TARGET_LINUX

View File

@ -1,8 +1,11 @@
#define CODE_ERROR_LOCATIONS #define CODE_ERROR_LOCATIONS
#include "Errors.hpp"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "Errors.hpp"
#include <iostream> #include <iostream>
#include <string>
#include "TypeTraits.hpp"
using namespace nb; using namespace nb;
@ -33,5 +36,12 @@ const ErrorCodeMap TestError::ErrorMessages{
TEST(ErrorTest, Test) { TEST(ErrorTest, Test) {
std::cout << TestError(0, TestError(1, TestError(2))).what(); auto err = TestError(0, TestError(1));
nb::stream(std::cout,
"len string: ", err.what_str().length(), "\n",
"len wstring: ", err.what_str<std::wstring>().length(), "\n"
);
nb::stream(std::wcout,
err.what_str<std::wstring>(), L"\n"
);
} }

View File

@ -4,6 +4,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <string> #include <string>
#include "TypeTraits.hpp"
#include "Utils.hpp" #include "Utils.hpp"
namespace nb { namespace nb {
@ -112,5 +113,37 @@ TEST(UtilsTest, TestStrToWstr) {
); );
} }
struct A { bool x(); };
struct B { bool y(); };
struct C { bool x(); bool y; };
struct D : C {};
template <typename T>
using has_x = decltype(T::x);
template <typename T>
using has_y = decltype(T::y);
TEST(UtilsTest, TestIsDetected) {
auto ret = is_detected<has_x, A>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_y, A>::value;
ASSERT_FALSE(ret);
ret = is_detected<has_y, B>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_x, B>::value;
ASSERT_FALSE(ret);
ret = is_detected<has_x, C>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_y, C>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_x, D>::value;
ASSERT_TRUE(ret);
ret = is_detected<has_y, D>::value;
ASSERT_TRUE(ret);
}
} // namespace nb } // namespace nb