Compare commits
3
Commits
nbdata
...
b593b7e083
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b593b7e083 | ||
|
|
a927b96a99 | ||
|
|
126862748e |
+1
-2
@@ -48,13 +48,12 @@ if(NB_BUILD_TESTS)
|
|||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
FetchContent_Declare(
|
FetchContent_Declare(
|
||||||
gtest
|
gtest
|
||||||
URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip
|
URL https://github.com/google/googletest/archive/refs/heads/main.zip
|
||||||
)
|
)
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||||
FetchContent_MakeAvailable(gtest)
|
FetchContent_MakeAvailable(gtest)
|
||||||
endif()
|
endif()
|
||||||
include(GoogleTest)
|
|
||||||
set(GTEST_COLOR ON)
|
set(GTEST_COLOR ON)
|
||||||
set(GLFW_BUILD_TESTS ON CACHE BOOL "" FORCE)
|
set(GLFW_BUILD_TESTS ON CACHE BOOL "" FORCE)
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
@@ -2,16 +2,10 @@
|
|||||||
#ifndef _NB_ANSI_TERM
|
#ifndef _NB_ANSI_TERM
|
||||||
#define _NB_ANSI_TERM
|
#define _NB_ANSI_TERM
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <cmath>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
#include "TypeTraits.hpp"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
----------- TECH DEBT ------------
|
----------- TECH DEBT ------------
|
||||||
Idk wtf to do here. This was originally to allow me to print
|
Idk wtf to do here. This was originally to allow me to print
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
include_directories(./.)
|
|
||||||
|
|
||||||
toAbsolutePath(NB_CORE_SOURCE
|
toAbsolutePath(NB_CORE_SOURCE
|
||||||
./src/Errors.cpp
|
./src/Errors.cpp
|
||||||
|
# ./src/Logger.cpp
|
||||||
./src/Processes.cpp
|
./src/Processes.cpp
|
||||||
./src/Logger.cpp
|
./src/Utils.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
toAbsolutePath(NB_CORE_INCLUDE
|
toAbsolutePath(NB_CORE_INCLUDE
|
||||||
@@ -15,6 +14,7 @@ toAbsolutePath(NB_CORE_INCLUDE
|
|||||||
./ThreadsafeQueue.hpp
|
./ThreadsafeQueue.hpp
|
||||||
./Types.hpp
|
./Types.hpp
|
||||||
./TypeTraits.hpp
|
./TypeTraits.hpp
|
||||||
|
./Utils.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
||||||
@@ -22,6 +22,8 @@ set(NB_CORE_INCLUDE ${NB_CORE_INCLUDE} PARENT_SCOPE)
|
|||||||
|
|
||||||
add_library(NBCore ${NB_CORE_SOURCE})
|
add_library(NBCore ${NB_CORE_SOURCE})
|
||||||
|
|
||||||
|
target_include_directories(NBCore PUBLIC ./.)
|
||||||
|
|
||||||
if (NB_BUILD_TESTS)
|
if (NB_BUILD_TESTS)
|
||||||
add_subdirectory(./tests)
|
add_subdirectory(./tests)
|
||||||
endif()
|
endif()
|
||||||
+203
-82
@@ -1,14 +1,17 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <memory>
|
||||||
#ifndef _NB_ERROR
|
#ifndef _NB_ERROR
|
||||||
#define _NB_ERROR
|
#define _NB_ERROR
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <memory>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#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
|
||||||
@@ -22,118 +25,236 @@
|
|||||||
#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<class ErrorType>
|
template <typename T>
|
||||||
class ErrorBase : public std::exception {
|
using has_type = decltype(T::type);
|
||||||
public:
|
|
||||||
ErrorBase(
|
|
||||||
const unsigned int code,
|
|
||||||
unsigned int line=0,
|
|
||||||
std::string filename=""
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
code,
|
|
||||||
ErrorBase<ErrorType>::lookup(code),
|
|
||||||
line,
|
|
||||||
filename
|
|
||||||
) {}
|
|
||||||
ErrorBase(
|
|
||||||
const unsigned int code,
|
|
||||||
const std::exception& trace,
|
|
||||||
unsigned int line=0,
|
|
||||||
std::string filename=""
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
code,
|
|
||||||
ErrorBase<ErrorType>::lookup(code),
|
|
||||||
trace,
|
|
||||||
line,
|
|
||||||
filename
|
|
||||||
) {}
|
|
||||||
|
|
||||||
static std::string lookup(unsigned int);
|
template<typename StringType, typename ErrorType>
|
||||||
unsigned int code() const noexcept {
|
struct NBErrorWhatString;
|
||||||
return static_cast<const ErrorType*>(this)->_code;
|
|
||||||
|
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;
|
||||||
};
|
};
|
||||||
virtual const char* what() const noexcept override final { return _msg.c_str(); };
|
|
||||||
|
template<class ErrorType>
|
||||||
|
class ErrorBase : public ErrorBase_what_str_impl {
|
||||||
|
public:
|
||||||
|
ErrorBase(const ErrorBase&) = default;
|
||||||
|
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 {
|
||||||
|
const std::string& ret = what_str();
|
||||||
|
return ret.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
|
||||||
|
const unsigned int code;
|
||||||
|
const std::string msg;
|
||||||
|
const std::shared_ptr<const std::exception> trace;
|
||||||
|
const bool traceIsNBError;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ErrorBase() = default;
|
ErrorBase() = delete;
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename TraceType=std::exception,
|
||||||
|
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||||
|
>
|
||||||
ErrorBase(
|
ErrorBase(
|
||||||
const unsigned int code,
|
const unsigned int code_,
|
||||||
std::string msg,
|
std::string msg_,
|
||||||
unsigned int line=0,
|
const TraceType* trace_=nullptr,
|
||||||
std::string filename=""
|
unsigned int line_=0,
|
||||||
) noexcept : _code{code}, _msg{""} {
|
std::string filename_=""
|
||||||
|
) noexcept :
|
||||||
|
code(code_),
|
||||||
|
msg(msg_),
|
||||||
|
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 "
|
||||||
"a class member."
|
"a class member."
|
||||||
);
|
);
|
||||||
static_assert(std::is_enum_v<typename ErrorType::ErrorCodes>, "enum ErrorCodes must be a class member.");
|
static_assert(std::is_enum_v<typename ErrorType::Codes>, "enum Codes must be a class member.");
|
||||||
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::ErrorCodes>, unsigned int>::value,
|
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::Codes>, unsigned int>::value,
|
||||||
"enum ErrorCodes must be of underlying type unsigned int."
|
"enum Codes must be of underlying type unsigned int."
|
||||||
);
|
);
|
||||||
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
|
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
|
||||||
"const std::string type must be a class member."
|
"const std::string type must be a class member."
|
||||||
);
|
);
|
||||||
|
static_assert(std::is_base_of<std::exception, TraceType>::value,
|
||||||
|
"`trace_` must be a pointer to a child object of std::exception."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
_msg += std::string(ErrorType::type);
|
template <
|
||||||
_msg += "[" + std::to_string(_code) + "]";
|
typename TraceType,
|
||||||
if (line && filename.size()) {
|
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||||
_msg += " in \'" + filename + "\':" + std::to_string(line);
|
>
|
||||||
}
|
|
||||||
_msg += "\n " + msg;
|
|
||||||
}
|
|
||||||
ErrorBase(
|
ErrorBase(
|
||||||
const unsigned int code,
|
const unsigned int& code_,
|
||||||
std::string msg,
|
const TraceType& trace_,
|
||||||
const std::exception& trace,
|
unsigned int line_=0,
|
||||||
unsigned int line=0,
|
std::string filename_=""
|
||||||
std::string filename=""
|
) noexcept : ErrorBase(
|
||||||
) noexcept : ErrorBase(code, msg, line, filename) {
|
code_,
|
||||||
std::string what_msg(trace.what());
|
ErrorType::ErrorMessages.at(code_),
|
||||||
std::string::size_type newline_pos=-1;
|
&trace_,
|
||||||
while((newline_pos=what_msg.find("\n", newline_pos+1))!= std::string::npos) {
|
line_,
|
||||||
what_msg.replace(newline_pos, 1, "\n ");
|
filename_
|
||||||
}
|
) {}
|
||||||
_msg += "\n Trace - " + what_msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int _code;
|
template <
|
||||||
std::string _msg;
|
typename TraceType,
|
||||||
|
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||||
|
>
|
||||||
|
ErrorBase(
|
||||||
|
const std::string& msg_,
|
||||||
|
const TraceType& trace_,
|
||||||
|
unsigned int line_=0,
|
||||||
|
std::string filename_=""
|
||||||
|
) noexcept : ErrorBase(
|
||||||
|
0,
|
||||||
|
msg_,
|
||||||
|
&trace_,
|
||||||
|
line_,
|
||||||
|
filename_
|
||||||
|
) {}
|
||||||
|
|
||||||
|
ErrorBase(
|
||||||
|
const unsigned int& code_,
|
||||||
|
unsigned int line_=0,
|
||||||
|
std::string filename_=""
|
||||||
|
) noexcept : ErrorBase(
|
||||||
|
code_,
|
||||||
|
ErrorType::ErrorMessages.at(code_),
|
||||||
|
static_cast<std::exception*>(nullptr),
|
||||||
|
line_,
|
||||||
|
filename_
|
||||||
|
) {}
|
||||||
|
|
||||||
|
ErrorBase(
|
||||||
|
const std::string& msg_,
|
||||||
|
unsigned int line_=0,
|
||||||
|
std::string filename_=""
|
||||||
|
) noexcept : ErrorBase(
|
||||||
|
0,
|
||||||
|
msg_,
|
||||||
|
nullptr,
|
||||||
|
line_,
|
||||||
|
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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Error : public ErrorBase<Error> {
|
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<NoneType>;
|
||||||
|
|
||||||
|
template <class ErrorType>
|
||||||
|
class Error : public ErrorBase<ErrorType> {
|
||||||
|
using Base = ErrorBase<ErrorType>;
|
||||||
public:
|
public:
|
||||||
enum ErrorCodes : unsigned int {
|
template <typename... Args>
|
||||||
|
Error(Args... T) : Base(T...) {}
|
||||||
|
|
||||||
|
using Base::what;
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
using Base::type;
|
||||||
|
using Base::ErrorMessages;
|
||||||
|
|
||||||
|
friend ErrorBase<Error>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
|
||||||
|
using Base = ErrorBase<Error<NoneType>>;
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
Error() : Base(0) {}
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
GENERAL, UNDEFINED, BADERRORCODE
|
GENERAL, UNDEFINED, BADERRORCODE
|
||||||
};
|
};
|
||||||
|
|
||||||
using ErrorBase<Error>::ErrorBase;
|
using Base::what;
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
|
||||||
friend ErrorBase<Error>;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
static const std::string type;
|
static const std::string type;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class ErrorType>
|
|
||||||
std::string ErrorBase<ErrorType>::lookup(unsigned int code) {
|
|
||||||
for (auto kv : ErrorType::ErrorMessages) {
|
|
||||||
if (kv.first==code) {
|
|
||||||
return std::string(kv.second);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw Error(Error::ErrorCodes::BADERRORCODE);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|
||||||
|
|
||||||
#endif // _NB_ERROR
|
#endif // _NB_ERROR
|
||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_CORE_TYPES
|
|
||||||
#define _NB_CORE_TYPES
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
T swapEndian(const T& val) {
|
|
||||||
T ret;
|
|
||||||
const int size = sizeof(T);
|
|
||||||
auto retLoc = static_cast<void*>(&ret);
|
|
||||||
auto valLoc = static_cast<const void*>(&val);
|
|
||||||
|
|
||||||
for (int i = 0; i < size; ++i) {
|
|
||||||
memcpy(retLoc+i, valLoc+(size-i-1), 1);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
using ByteVector = std::vector<uint8_t>;
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
#endif // _NB_CORE_TYPES
|
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <type_traits>
|
||||||
|
#ifndef _NB_CORE_TYPES
|
||||||
|
#define _NB_CORE_TYPES
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
#ifdef _NB_TARGET_WINDOWS
|
||||||
|
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::wstring WNEWLINE = L"\n";
|
||||||
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
T ret;
|
||||||
|
const int size = sizeof(T);
|
||||||
|
auto retLoc = static_cast<void*>(&ret);
|
||||||
|
auto valLoc = static_cast<const void*>(&val);
|
||||||
|
|
||||||
|
for (int i = 0; i < size; ++i) {
|
||||||
|
memcpy(retLoc+i, valLoc+(size-i-1), 1);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
} */
|
||||||
|
|
||||||
|
|
||||||
|
// using ByteVector = std::vector<uint8_t>;
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_CORE_TYPES
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
#include "Errors.hpp"
|
#include "Errors.hpp"
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
const std::string Error::type = "Error";
|
const std::string Error<nb::NoneType>::type = "nb::Error";
|
||||||
const ErrorCodeMap Error::ErrorMessages = {
|
|
||||||
{ErrorCodes::GENERAL, "General std::exception."},
|
const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
|
||||||
{ErrorCodes::UNDEFINED, "Undefined / general error."},
|
{Error::Codes::GENERAL, "General std::exception."},
|
||||||
{ErrorCodes::BADERRORCODE, "Unrecognized error code."}
|
{Error::Codes::UNDEFINED, "Undefined / general error."},
|
||||||
|
{Error::Codes::BADERRORCODE, "Unrecognized error code."}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
#endif // _NB_TARGET_LINUX
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
#include "Processes.hpp"
|
#include "Processes.hpp"
|
||||||
#include "Types.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include <string>
|
||||||
|
#include "Utils.hpp"
|
||||||
|
|
||||||
|
template<>
|
||||||
|
std::wstring nb::str_to_wstr(const std::string& in) {
|
||||||
|
std::wstring ret(in.begin(), in.end());
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
@@ -4,8 +4,9 @@ if (NB_BUILD_TESTS)
|
|||||||
enable_testing()
|
enable_testing()
|
||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
add_executable(TestCore
|
add_executable(TestCore
|
||||||
testErrors.cpp
|
./testErrors.cpp
|
||||||
testProcesses.cpp
|
#./testProcesses.cpp
|
||||||
|
./testUtils.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(TestCore
|
target_link_libraries(TestCore
|
||||||
NBCore
|
NBCore
|
||||||
|
|||||||
@@ -1,38 +1,47 @@
|
|||||||
#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 <sstream>
|
#include <string>
|
||||||
#include "Logger.hpp"
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
using namespace nb;
|
using namespace nb;
|
||||||
|
|
||||||
class TestError : public ErrorBase<TestError> {
|
class TestError : public Error<TestError> {
|
||||||
|
using Base = Error<TestError>;
|
||||||
public:
|
public:
|
||||||
using ErrorBase<TestError>::ErrorBase;
|
using Base::Base;
|
||||||
|
using Base::what;
|
||||||
|
using Base::code;
|
||||||
|
using Base::msg;
|
||||||
|
using Base::trace;
|
||||||
|
|
||||||
enum ErrorCodes : unsigned int {
|
enum Codes : unsigned int {
|
||||||
A, B, C, D
|
A, B, C, D
|
||||||
};
|
};
|
||||||
|
|
||||||
static const std::string type;
|
static const std::string type;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
static const nb::ErrorCodeMap ErrorMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
const std::string TestError::type="TestError";
|
const std::string TestError::type="TestError";
|
||||||
const ErrorCodeMap TestError::ErrorMessages{
|
const ErrorCodeMap TestError::ErrorMessages{
|
||||||
{TestError::ErrorCodes::A, "Hey!"},
|
{TestError::Codes::A, "Hey!"},
|
||||||
{TestError::ErrorCodes::B, "How"},
|
{TestError::Codes::B, "How"},
|
||||||
{TestError::ErrorCodes::C, "You"},
|
{TestError::Codes::C, "You"},
|
||||||
{TestError::ErrorCodes::D, "Doin"}
|
{TestError::Codes::D, "Doin"}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
TEST(ErrorTest, Test) {
|
TEST(ErrorTest, Test) {
|
||||||
EXPECT_EQ(1, 1);
|
auto err = TestError(0, TestError(1));
|
||||||
|
nb::stream(std::cout,
|
||||||
std::stringstream sstream;
|
"len string: ", err.what_str().length(), "\n",
|
||||||
|
"len wstring: ", err.what_str<std::wstring>().length(), "\n"
|
||||||
ASSERT_TRUE(nb::logger.isRunning());
|
);
|
||||||
nb::logger.log("Hey!");
|
nb::stream(std::wcout,
|
||||||
|
err.what_str<std::wstring>(), L"\n"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
#define CODE_ERROR_LOCATIONS
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include "Processes.hpp"
|
#include "Processes.hpp"
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,149 @@
|
|||||||
|
#include <iterator>
|
||||||
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
|
#include "Utils.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
TEST(UtilsTest, TestFindAndReplace) {
|
||||||
|
ASSERT_STREQ(
|
||||||
|
find_and_replace("Jeff", "e", "efe").c_str(),
|
||||||
|
"Jefeff"
|
||||||
|
);
|
||||||
|
|
||||||
|
std::string tmp = find_and_replace("Naif", "a", "afa");
|
||||||
|
ASSERT_STREQ(
|
||||||
|
find_and_replace(tmp, "i", "ifi").c_str(),
|
||||||
|
"Nafaifif"
|
||||||
|
);
|
||||||
|
|
||||||
|
tmp = find_and_replace("aeiou", "a", "afa");
|
||||||
|
tmp = find_and_replace(tmp, "e", "efe");
|
||||||
|
tmp = find_and_replace(tmp, "i", "ifi");
|
||||||
|
tmp = find_and_replace(tmp, "o", "ofo");
|
||||||
|
tmp = find_and_replace(tmp, "u", "ufu");
|
||||||
|
ASSERT_STREQ(
|
||||||
|
tmp.c_str(),
|
||||||
|
"afaefeifiofoufu"
|
||||||
|
);
|
||||||
|
|
||||||
|
tmp = find_and_replace(tmp, "afa", "a");
|
||||||
|
tmp = find_and_replace(tmp, "efe", "e");
|
||||||
|
tmp = find_and_replace(tmp, "ifi", "i");
|
||||||
|
tmp = find_and_replace(tmp, "ofo", "o");
|
||||||
|
tmp = find_and_replace(tmp, "ufu", "u");
|
||||||
|
ASSERT_STREQ(
|
||||||
|
tmp.c_str(),
|
||||||
|
"aeiou"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
Reference in New Issue
Block a user