Compare commits

..

No commits in common. "b593b7e083a8812f4a86bb81d17455c85c2c85a4" and "126862748e840d760c96df2b8fdb97697b249500" have entirely different histories.

8 changed files with 62 additions and 297 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,5 +1,4 @@
#pragma once #pragma once
#include <memory>
#ifndef _NB_ERROR #ifndef _NB_ERROR
#define _NB_ERROR #define _NB_ERROR
@ -7,11 +6,10 @@
#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
@ -25,43 +23,30 @@
#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 ErrorBase_what_str_impl { class ErrorBase : public std::exception {
public: public:
ErrorBase(const ErrorBase&) = default; ErrorBase(const ErrorBase&) = default;
ErrorBase& operator=(const ErrorBase&) = delete; ErrorBase& operator=(const ErrorBase&) = delete;
template<typename StringType = std::string>
inline const StringType what_str() const noexcept {
return NBErrorWhatString<StringType,ErrorType>::what(*this);
}
virtual const char* what() const noexcept override final { virtual const char* what() const noexcept override final {
const std::string& ret = what_str(); std::string ret = this->msg;
return ret.c_str(); const std::string replace = nb::NEWLINE + "\t";
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();
} }
static const std::string type; static const std::string type;
@ -69,8 +54,7 @@ public:
const unsigned int code; const unsigned int code;
const std::string msg; const std::string msg;
const std::shared_ptr<const std::exception> trace; const std::exception* trace;
const bool traceIsNBError;
protected: protected:
ErrorBase() = delete; ErrorBase() = delete;
@ -88,8 +72,7 @@ protected:
) noexcept : ) noexcept :
code(code_), code(code_),
msg(msg_), msg(msg_),
trace{trace_ ? std::make_shared<TraceType>(*trace_) : nullptr}, trace{static_cast<std::exception*>(trace_ ? new 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 "
@ -165,58 +148,19 @@ 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;
template<typename ErrorType> class DefaultError {DefaultError() = default;};
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<typename ErrorType> template <class ErrorType = DefaultError>
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<NoneType>; class Error<DefaultError>;
template <class ErrorType> template <class ErrorType>
class Error : public ErrorBase<ErrorType> { class Error : public ErrorBase<ErrorType> {
@ -236,8 +180,8 @@ public:
}; };
template<> template<>
class Error<NoneType> : public ErrorBase<Error<NoneType>> { class Error<DefaultError> : public ErrorBase<Error<DefaultError>> {
using Base = ErrorBase<Error<NoneType>>; using Base = ErrorBase<Error<DefaultError>>;
public: public:
using Base::Base; using Base::Base;
Error() : Base(0) {} Error() : Base(0) {}

View File

@ -6,43 +6,7 @@
namespace nb { namespace nb {
namespace detail { template<std::size_t N=0, typename Func, typename... Pack>
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) {}
@ -104,7 +68,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

@ -1,68 +1,28 @@
#pragma once #pragma once
#include <cstdlib>
#include <type_traits>
#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 = "\r\n"; const std::string NEWLINE("\r\n");
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 = "\n"; const std::string NEWLINE("\n");
const std::wstring WNEWLINE = L"\n";
#endif // _NB_TARGET_LINUX #endif // _NB_TARGET_LINUX
template<typename T> template<typename K, typename V, size_t N>
std::enable_if_t<std::is_convertible_v<const T&, std::string>, std::wstring> str_to_wstr(const T& in) { struct ConstexprMap {
std::string str(in); template<typename Input>
std::wstring ret(str.begin(), str.end()); constexpr ConstexprMap(Input inp) : _data{inp} {}
return ret;
}
template<typename T> protected:
std::enable_if_t<std::is_convertible_v<const T&, std::wstring>, std::string> wstr_to_str(const T& in) { const std::array<std::pair<K, V>, N> _data;
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> /* template<typename T>
T swap_endian(const T& val) { T swap_endian(const T& val) {
@ -77,6 +37,7 @@ T swap_endian(const T& val) {
return ret; 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>; // using ByteVector = std::vector<uint8_t>;

View File

@ -1,11 +1,10 @@
#include "Errors.hpp" #include "Errors.hpp"
#include "TypeTraits.hpp"
namespace nb { namespace nb {
const std::string Error<nb::NoneType>::type = "nb::Error"; const std::string Error<DefaultError>::type = "nb::Error";
const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = { const ErrorCodeMap Error<DefaultError>::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,8 +1,23 @@
#include <string> #include <string>
#include "Utils.hpp" #include "Utils.hpp"
template<>
std::wstring nb::str_to_wstr(const std::string& in) { std::string nb::find_and_replace(const std::string& original, const std::string& find, const std::string& replace) {
std::wstring ret(in.begin(), in.end()); 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;
}
return ret; return ret;
} }

View File

@ -1,11 +1,8 @@
#define CODE_ERROR_LOCATIONS #define CODE_ERROR_LOCATIONS
#include <gtest/gtest.h>
#include "Errors.hpp" #include "Errors.hpp"
#include <gtest/gtest.h>
#include <iostream> #include <iostream>
#include <string>
#include "TypeTraits.hpp"
using namespace nb; using namespace nb;
@ -36,12 +33,5 @@ const ErrorCodeMap TestError::ErrorMessages{
TEST(ErrorTest, Test) { TEST(ErrorTest, Test) {
auto err = TestError(0, TestError(1)); std::cout << TestError(0, TestError(1, TestError(2))).what();
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,21 +1,17 @@
#include <iterator>
#define CODE_ERROR_LOCATIONS #define CODE_ERROR_LOCATIONS
#include <gtest/gtest.h>
#include <string>
#include "TypeTraits.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include <gtest/gtest.h>
namespace nb { namespace nb {
TEST(UtilsTest, TestFindAndReplace) { TEST(UtilsTest, Test) {
ASSERT_STREQ( ASSERT_STREQ(
find_and_replace("Jeff", "e", "efe").c_str(), find_and_replace("Jeff", "e", "efe").c_str(),
"Jefeff" "Jefeff"
); );
std::string tmp = find_and_replace("Naif", "a", "afa"); auto tmp = find_and_replace("Naif", "a", "afa");
ASSERT_STREQ( ASSERT_STREQ(
find_and_replace(tmp, "i", "ifi").c_str(), find_and_replace(tmp, "i", "ifi").c_str(),
"Nafaifif" "Nafaifif"
@ -42,108 +38,4 @@ TEST(UtilsTest, TestFindAndReplace) {
); );
} }
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 } // namespace nb