Compare commits

...

2 Commits

Author SHA1 Message Date
NaifBanana
b593b7e083 Man im so tired This is wrong but I can taste it, im close 2026-04-10 00:31:11 -05:00
NaifBanana
a927b96a99 Added more Util tests and str-wstr related changes to util functions 2026-04-09 17:44:19 -05:00
8 changed files with 297 additions and 62 deletions

View File

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

View File

@ -1,4 +1,5 @@
#pragma once
#include <memory>
#ifndef _NB_ERROR
#define _NB_ERROR
@ -6,10 +7,11 @@
#include <iostream>
#include <string>
#include <type_traits>
#include "TypeTraits.hpp"
#include <unordered_map>
#include "Utils.hpp"
#ifndef THROW_WITH_INFO
/* #ifndef THROW_WITH_INFO
#ifdef CODE_ERROR_LOCATIONS
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
#else
@ -23,30 +25,43 @@
#else
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
#endif // LOGGING
#endif // THROW
#endif // THROW */
namespace nb {
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>
class ErrorBase : public std::exception {
class ErrorBase : public ErrorBase_what_str_impl {
public:
ErrorBase(const ErrorBase&) = default;
ErrorBase& operator=(const ErrorBase&) = delete;
virtual const char* what() const noexcept override final {
std::string ret = this->msg;
const std::string replace = nb::NEWLINE + "\t";
if (trace) {
ret += replace + find_and_replace(
std::string(trace->what()),
nb::NEWLINE,
replace
);
template<typename StringType = std::string>
inline const StringType what_str() const noexcept {
return NBErrorWhatString<StringType,ErrorType>::what(*this);
}
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;
@ -54,7 +69,8 @@ public:
const unsigned int code;
const std::string msg;
const std::exception* trace;
const std::shared_ptr<const std::exception> trace;
const bool traceIsNBError;
protected:
ErrorBase() = delete;
@ -72,7 +88,8 @@ protected:
) noexcept :
code(code_),
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,
"const std::unordered_map<unsigned int, const char*> ErrorMessages must be "
@ -148,19 +165,58 @@ protected:
filename_
) {}
std::string (ErrorBase::*_trace_what_str)();
std::wstring (ErrorBase::*_trace_what_wstr)();
};
template <typename ErrorType>
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
template <typename ErrorType>
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;
template<>
class Error<DefaultError>;
class Error<NoneType>;
template <class ErrorType>
class Error : public ErrorBase<ErrorType> {
@ -180,8 +236,8 @@ public:
};
template<>
class Error<DefaultError> : public ErrorBase<Error<DefaultError>> {
using Base = ErrorBase<Error<DefaultError>>;
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
using Base = ErrorBase<Error<NoneType>>;
public:
using Base::Base;
Error() : Base(0) {}

View File

@ -6,7 +6,43 @@
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
ForEach(std::tuple<Pack...>&, Func) {}
@ -68,7 +104,7 @@ struct PackIsSameType<T, PackType> {
template <typename T, typename FirstPack, typename... Pack>
struct PackIsSameType<T, FirstPack, Pack...> {
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
};
};*/
} // namespace nb

View File

@ -1,28 +1,68 @@
#pragma once
#include <cstdlib>
#include <type_traits>
#ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES
#include <array>
#include <utility>
#include <string>
namespace nb {
#ifdef _NB_TARGET_WINDOWS
const std::string NEWLINE("\r\n");
const std::string NEWLINE = "\r\n";
const std::wstring WNEWLINE = L"\r\n";
#endif // _NB_TARGET_WINDOWS
#ifdef _NB_TARGET_LINUX
const std::string NEWLINE("\n");
const std::string NEWLINE = "\n";
const std::wstring WNEWLINE = L"\n";
#endif // _NB_TARGET_LINUX
template<typename K, typename V, size_t N>
struct ConstexprMap {
template<typename Input>
constexpr ConstexprMap(Input inp) : _data{inp} {}
template<typename T>
std::enable_if_t<std::is_convertible_v<const T&, std::string>, std::wstring> str_to_wstr(const T& in) {
std::string str(in);
std::wstring ret(str.begin(), str.end());
return ret;
}
protected:
const std::array<std::pair<K, V>, N> _data;
};
template<typename T>
std::enable_if_t<std::is_convertible_v<const T&, std::wstring>, std::string> wstr_to_str(const T& in) {
std::wstring wstr(in);
std::size_t wstrlen = wstr.length();
char* c_str= new char[wstrlen];
std::wcstombs(c_str, wstr.c_str(), wstrlen);
std::string ret(c_str, wstrlen);
delete[] c_str;
return ret;
}
template <typename T = std::string, typename A, typename B, typename C>
T find_and_replace(
const A& original,
const B& find,
const C& replace
) {
const T& original_t = T(original);
const T& find_t = T(find);
const T& replace_t = T(replace);
T ret = original_t;
std::size_t find_len = find_t.length();
std::size_t replace_len = replace_t.length();
std::size_t currpos = 0;
std::size_t lastpos = 0;
while(true) {
lastpos = currpos;
currpos = original_t.find(find_t, lastpos);
if (currpos == T::npos) {
break;
}
ret = ret.erase(currpos, find_len);
ret = ret.insert(currpos, replace_t);
currpos += replace_len;
}
return ret;
}
/* template<typename T>
T swap_endian(const T& val) {
@ -37,7 +77,6 @@ T swap_endian(const T& val) {
return ret;
} */
std::string find_and_replace(const std::string& original, const std::string& find, const std::string& replace);
// using ByteVector = std::vector<uint8_t>;

View File

@ -1,10 +1,11 @@
#include "Errors.hpp"
#include "TypeTraits.hpp"
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::UNDEFINED, "Undefined / general error."},
{Error::Codes::BADERRORCODE, "Unrecognized error code."}

View File

@ -1,23 +1,8 @@
#include <string>
#include "Utils.hpp"
std::string nb::find_and_replace(const std::string& original, const std::string& find, const std::string& replace) {
std::string ret = original;
std::size_t find_len = find.length();
std::size_t replace_len = replace.length();
std::size_t currpos = 0;
std::size_t lastpos = 0;
while(true) {
lastpos = currpos;
currpos = original.find(find, lastpos);
if (currpos == std::string::npos) {
break;
}
ret = ret.erase(currpos, find_len);
ret = ret.insert(currpos, replace);
currpos += replace_len;
}
template<>
std::wstring nb::str_to_wstr(const std::string& in) {
std::wstring ret(in.begin(), in.end());
return ret;
}

View File

@ -1,8 +1,11 @@
#define CODE_ERROR_LOCATIONS
#include "Errors.hpp"
#include <gtest/gtest.h>
#include "Errors.hpp"
#include <iostream>
#include <string>
#include "TypeTraits.hpp"
using namespace nb;
@ -33,5 +36,12 @@ const ErrorCodeMap TestError::ErrorMessages{
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

@ -1,17 +1,21 @@
#include <iterator>
#define CODE_ERROR_LOCATIONS
#include "Utils.hpp"
#include <gtest/gtest.h>
#include <string>
#include "TypeTraits.hpp"
#include "Utils.hpp"
namespace nb {
TEST(UtilsTest, Test) {
TEST(UtilsTest, TestFindAndReplace) {
ASSERT_STREQ(
find_and_replace("Jeff", "e", "efe").c_str(),
"Jefeff"
);
auto tmp = find_and_replace("Naif", "a", "afa");
std::string tmp = find_and_replace("Naif", "a", "afa");
ASSERT_STREQ(
find_and_replace(tmp, "i", "ifi").c_str(),
"Nafaifif"
@ -38,4 +42,108 @@ TEST(UtilsTest, Test) {
);
}
TEST(UtilsTest, TestFindAndReplaceWstr) {
ASSERT_STREQ(
find_and_replace<std::wstring>(L"Jeff", L"e", L"efe").c_str(),
L"Jefeff"
);
std::wstring tmp = find_and_replace<std::wstring>(L"Naif", L"a", L"afa");
ASSERT_STREQ(
find_and_replace<std::wstring>(tmp, L"i", L"ifi").c_str(),
L"Nafaifif"
);
tmp = find_and_replace<std::wstring>(L"aeiou", L"a", L"afa");
tmp = find_and_replace<std::wstring>(tmp, L"e", L"efe");
tmp = find_and_replace<std::wstring>(tmp, L"i", L"ifi");
tmp = find_and_replace<std::wstring>(tmp, L"o", L"ofo");
tmp = find_and_replace<std::wstring>(tmp, L"u", L"ufu");
ASSERT_STREQ(
tmp.c_str(),
L"afaefeifiofoufu"
);
tmp = find_and_replace<std::wstring>(tmp, L"afa", L"a");
tmp = find_and_replace<std::wstring>(tmp, L"efe", L"e");
tmp = find_and_replace<std::wstring>(tmp, L"ifi", L"i");
tmp = find_and_replace<std::wstring>(tmp, L"ofo", L"o");
tmp = find_and_replace<std::wstring>(tmp, L"ufu", L"u");
ASSERT_STREQ(
tmp.c_str(),
L"aeiou"
);
}
TEST(UtilsTest, TestWstrToStr) {
ASSERT_STREQ(
nb::wstr_to_str(L"Hi!").c_str(),
"Hi!"
);
ASSERT_STREQ(
nb::wstr_to_str(L"Naif").c_str(),
"Naif"
);
ASSERT_STREQ(
nb::wstr_to_str(L"\r\r\r\n").c_str(),
"\r\r\r\n"
);
ASSERT_STREQ(
nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(),
"Naif\ttalks\r\ra\t\nlot"
);
}
TEST(UtilsTest, TestStrToWstr) {
ASSERT_STREQ(
nb::str_to_wstr("Hi!").c_str(),
L"Hi!"
);
ASSERT_STREQ(
nb::str_to_wstr("Naif").c_str(),
L"Naif"
);
ASSERT_STREQ(
nb::str_to_wstr("\r\r\r\n").c_str(),
L"\r\r\r\n"
);
ASSERT_STREQ(
nb::str_to_wstr("Naif\ttalks\r\ra\t\nlot").c_str(),
L"Naif\ttalks\r\ra\t\nlot"
);
}
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