Compare commits
7
Commits
nbdata
...
85241b4aa5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
85241b4aa5 | ||
|
|
a5dc639958 | ||
|
|
1b4a891a58 | ||
|
|
6c71bd46af | ||
|
|
b593b7e083 | ||
|
|
a927b96a99 | ||
|
|
126862748e |
+2
-2
@@ -48,13 +48,12 @@ if(NB_BUILD_TESTS)
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
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)
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
FetchContent_MakeAvailable(gtest)
|
||||
endif()
|
||||
include(GoogleTest)
|
||||
set(GTEST_COLOR ON)
|
||||
set(GLFW_BUILD_TESTS ON CACHE BOOL "" FORCE)
|
||||
endif()
|
||||
@@ -62,6 +61,7 @@ endif()
|
||||
if (NB_LOGGING)
|
||||
message(STATUS "Building with automatic logging")
|
||||
add_compile_definitions(_NB_AUTOLOG)
|
||||
add_compile_definitions(_NB_CODE_ERROR_LOCATIONS)
|
||||
endif()
|
||||
|
||||
if (NB_TARGET_WINDOWS)
|
||||
|
||||
@@ -2,16 +2,10 @@
|
||||
#ifndef _NB_ANSI_TERM
|
||||
#define _NB_ANSI_TERM
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
/*
|
||||
----------- TECH DEBT ------------
|
||||
Idk wtf to do here. This was originally to allow me to print
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
include_directories(./.)
|
||||
|
||||
toAbsolutePath(NB_CORE_SOURCE
|
||||
./src/Errors.cpp
|
||||
./src/Processes.cpp
|
||||
./src/Logger.cpp
|
||||
./src/Processes.cpp
|
||||
./src/StringUtils.cpp
|
||||
./src/Utils.cpp
|
||||
)
|
||||
|
||||
toAbsolutePath(NB_CORE_INCLUDE
|
||||
@@ -12,9 +12,11 @@ toAbsolutePath(NB_CORE_INCLUDE
|
||||
./Errors.hpp
|
||||
./Logger.hpp
|
||||
./Processes.hpp
|
||||
./StringUtils.hpp
|
||||
./ThreadsafeQueue.hpp
|
||||
./Types.hpp
|
||||
./TypeTraits.hpp
|
||||
./Utils.hpp
|
||||
)
|
||||
|
||||
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
||||
@@ -22,6 +24,8 @@ set(NB_CORE_INCLUDE ${NB_CORE_INCLUDE} PARENT_SCOPE)
|
||||
|
||||
add_library(NBCore ${NB_CORE_SOURCE})
|
||||
|
||||
target_include_directories(NBCore PUBLIC ./.)
|
||||
|
||||
if (NB_BUILD_TESTS)
|
||||
add_subdirectory(./tests)
|
||||
endif()
|
||||
@@ -2,6 +2,7 @@
|
||||
#ifndef _NB_DATASINK
|
||||
#define _NB_DATASINK
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
@@ -34,9 +35,9 @@ class BufferedDataProcessor : public DataSink<DataType> {
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
bool stop() noexcept { return type_ptr->stop(); }
|
||||
bool run() { return type_ptr->run(); }
|
||||
bool in(const DataType& val) { return type_ptr->in(val); }
|
||||
virtual bool stop() noexcept override { return type_ptr->stop(); }
|
||||
virtual bool run() override { return type_ptr->run(); }
|
||||
virtual bool in(const DataType& val) override { return type_ptr->in(val); }
|
||||
|
||||
protected:
|
||||
unsigned int count() const {
|
||||
@@ -77,7 +78,7 @@ public:
|
||||
bool isRunning() const noexcept override {
|
||||
return this->_running && (_runningThread!=nullptr);
|
||||
}
|
||||
bool run() {
|
||||
bool run() override {
|
||||
if (!type_ptr->isRunning()) {
|
||||
this->_running = true;
|
||||
_runningThread = std::make_shared<std::thread>([&]{
|
||||
|
||||
+202
-98
@@ -5,135 +5,239 @@
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include "TypeTraits.hpp"
|
||||
#include <unordered_map>
|
||||
|
||||
#ifndef THROW_WITH_INFO
|
||||
#ifdef CODE_ERROR_LOCATIONS
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__)
|
||||
#endif // CODE_ERROR_LOCATIONS
|
||||
#endif // THROW_WITH_INFO
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef LOGGING
|
||||
#define THROW_WTIH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
|
||||
#endif // LOGGING
|
||||
#endif // THROW
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
||||
|
||||
template<class ErrorType>
|
||||
class ErrorBase : public std::exception {
|
||||
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
|
||||
) {}
|
||||
template <typename T>
|
||||
using has_type = decltype(T::type);
|
||||
|
||||
static std::string lookup(unsigned int);
|
||||
unsigned int code() const noexcept {
|
||||
return static_cast<const ErrorType*>(this)->_code;
|
||||
};
|
||||
virtual const char* what() const noexcept override final { return _msg.c_str(); };
|
||||
template<class ErrorType = NoneType>
|
||||
class ErrorBase;
|
||||
|
||||
template<>
|
||||
class ErrorBase<NoneType> : public std::exception {
|
||||
public:
|
||||
const unsigned int code;
|
||||
const std::string msg;
|
||||
const std::shared_ptr<const std::exception> trace;
|
||||
const bool traceIsNBError;
|
||||
|
||||
virtual inline std::string str() const noexcept = 0;
|
||||
|
||||
virtual const char* what() const noexcept override final {
|
||||
const std::string& ret = str();
|
||||
return ret.c_str();
|
||||
}
|
||||
|
||||
protected:
|
||||
ErrorBase() = default;
|
||||
template <
|
||||
typename TraceType = std::exception,
|
||||
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||
>
|
||||
ErrorBase(
|
||||
const unsigned int code,
|
||||
std::string msg,
|
||||
unsigned int line=0,
|
||||
std::string filename=""
|
||||
) noexcept : _code{code}, _msg{""} {
|
||||
const unsigned int& code_,
|
||||
const std::string& msg_,
|
||||
const std::shared_ptr<TraceType> trace_,
|
||||
const unsigned int& line_,
|
||||
std::string filename_
|
||||
) noexcept :
|
||||
code(code_),
|
||||
msg(msg_),
|
||||
trace{std::dynamic_pointer_cast<std::exception>(trace_)},
|
||||
traceIsNBError( trace_ ? is_detected<has_type, TraceType>::value : false)
|
||||
{
|
||||
static_assert(std::is_base_of<std::exception, TraceType>::value,
|
||||
"`trace_` must be a pointer to a child object of std::exception."
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template <class ErrorType>
|
||||
class ErrorBase : public ErrorBase<NoneType> {
|
||||
using Base = ErrorBase<NoneType>;
|
||||
public:
|
||||
ErrorBase(const ErrorBase&) = default;
|
||||
ErrorBase& operator=(const ErrorBase&) = delete;
|
||||
|
||||
virtual inline std::string str() const noexcept override {
|
||||
std::string ret = msg + nb::NEWLINE;
|
||||
if (trace) {
|
||||
const std::string tabover = " ";
|
||||
std::string trace_msg;
|
||||
if (traceIsNBError) {
|
||||
trace_msg = std::static_pointer_cast<const ErrorBase>(trace)->str();
|
||||
trace_msg = std::string(std::string_view(trace_msg).substr(
|
||||
0, trace_msg.length()-nb::NEWLINE.length()
|
||||
));
|
||||
} else {
|
||||
trace_msg = std::string(trace->what());
|
||||
}
|
||||
ret += indent_strblock(trace_msg, tabover, tabover+"Trace: ")+nb::NEWLINE;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
using Base::traceIsNBError;
|
||||
|
||||
protected:
|
||||
using Base::Base;
|
||||
|
||||
template <
|
||||
typename TraceType = std::exception,
|
||||
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||
>
|
||||
ErrorBase(
|
||||
const unsigned int code_,
|
||||
std::string msg_,
|
||||
const TraceType* trace_=nullptr,
|
||||
unsigned int line_=0,
|
||||
std::string filename_=""
|
||||
) : Base(
|
||||
code_,
|
||||
msg_,
|
||||
trace_ ? std::make_shared<TraceType>(*trace_) : nullptr,
|
||||
line_,
|
||||
filename_
|
||||
) {
|
||||
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 "
|
||||
"a class member."
|
||||
);
|
||||
static_assert(std::is_enum_v<typename ErrorType::ErrorCodes>, "enum ErrorCodes must be a class member.");
|
||||
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::ErrorCodes>, unsigned int>::value,
|
||||
"enum ErrorCodes must be of underlying type unsigned int."
|
||||
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::Codes>, unsigned int>::value,
|
||||
"enum Codes must be of underlying type unsigned int."
|
||||
);
|
||||
static_assert(std::is_same<const std::string, decltype(ErrorType::type)>::value,
|
||||
"const std::string type must be a class member."
|
||||
);
|
||||
|
||||
_msg += std::string(ErrorType::type);
|
||||
_msg += "[" + std::to_string(_code) + "]";
|
||||
if (line && filename.size()) {
|
||||
_msg += " in \'" + filename + "\':" + std::to_string(line);
|
||||
}
|
||||
_msg += "\n " + msg;
|
||||
}
|
||||
ErrorBase(
|
||||
const unsigned int code,
|
||||
std::string msg,
|
||||
const std::exception& trace,
|
||||
unsigned int line=0,
|
||||
std::string filename=""
|
||||
) noexcept : ErrorBase(code, msg, line, filename) {
|
||||
std::string what_msg(trace.what());
|
||||
std::string::size_type newline_pos=-1;
|
||||
while((newline_pos=what_msg.find("\n", newline_pos+1))!= std::string::npos) {
|
||||
what_msg.replace(newline_pos, 1, "\n ");
|
||||
}
|
||||
_msg += "\n Trace - " + what_msg;
|
||||
}
|
||||
|
||||
unsigned int _code;
|
||||
std::string _msg;
|
||||
|
||||
template <
|
||||
typename TraceType,
|
||||
std::enable_if_t<std::is_base_of_v<std::exception, TraceType>, bool> = true
|
||||
>
|
||||
ErrorBase(
|
||||
const unsigned int& code_,
|
||||
const TraceType& trace_,
|
||||
unsigned int line_=0,
|
||||
std::string filename_=""
|
||||
) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages.at(code_),
|
||||
&trace_,
|
||||
line_,
|
||||
filename_
|
||||
) {}
|
||||
|
||||
template <
|
||||
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_,
|
||||
static_cast<std::exception*>(nullptr),
|
||||
line_,
|
||||
filename_
|
||||
) {}
|
||||
};
|
||||
template <typename ErrorType>
|
||||
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
|
||||
template <typename ErrorType>
|
||||
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
|
||||
|
||||
template <class ErrorType = NoneType>
|
||||
class Error;
|
||||
|
||||
template<>
|
||||
class Error<NoneType>;
|
||||
|
||||
template <class ErrorType>
|
||||
class Error : public ErrorBase<ErrorType> {
|
||||
using Base = ErrorBase<ErrorType>;
|
||||
public:
|
||||
template <typename... Args>
|
||||
Error(Args... args) : Base(args...) {}
|
||||
|
||||
using Base::str;
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
using Base::traceIsNBError;
|
||||
using Base::type;
|
||||
using Base::ErrorMessages;
|
||||
|
||||
friend ErrorBase<Error>;
|
||||
};
|
||||
|
||||
class Error : public ErrorBase<Error> {
|
||||
template<>
|
||||
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
|
||||
using Base = ErrorBase<Error<NoneType>>;
|
||||
public:
|
||||
enum ErrorCodes : unsigned int {
|
||||
using Base::Base;
|
||||
Error() : Base(0) {}
|
||||
|
||||
template <typename... Args>
|
||||
Error(Args... args) : Base(args...) {}
|
||||
|
||||
enum Codes : unsigned int {
|
||||
GENERAL, UNDEFINED, BADERRORCODE
|
||||
};
|
||||
|
||||
using ErrorBase<Error>::ErrorBase;
|
||||
using Base::str;
|
||||
using Base::what;
|
||||
|
||||
friend ErrorBase<Error>;
|
||||
|
||||
protected:
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
using Base::traceIsNBError;
|
||||
static const std::string type;
|
||||
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
|
||||
|
||||
|
||||
#endif // _NB_ERROR
|
||||
+31
-21
@@ -2,12 +2,9 @@
|
||||
#ifndef _NB_LOGGER
|
||||
#define _NB_LOGGER
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <ostream>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
@@ -23,14 +20,6 @@ typedef std::chrono::time_point<
|
||||
std::chrono::nanoseconds
|
||||
> LoggerTimePoint;
|
||||
|
||||
struct LogEvent{
|
||||
const LoggerTimePoint time;
|
||||
const unsigned char lvl;
|
||||
const std::string msg;
|
||||
const std::thread::id tid;
|
||||
const uint64_t pid;
|
||||
};
|
||||
|
||||
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
|
||||
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
|
||||
|
||||
@@ -41,7 +30,7 @@ class LoggerBase
|
||||
using LoggerType = Logger;
|
||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||
public:
|
||||
bool run() {
|
||||
bool run() override {
|
||||
if (!type_ptr->isRunning()) {
|
||||
this->_running = true;
|
||||
this->_runningThread = std::make_shared<std::thread>([&]{
|
||||
@@ -62,6 +51,14 @@ private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
};
|
||||
|
||||
struct LogEvent{
|
||||
const LoggerTimePoint time;
|
||||
const unsigned char lvl;
|
||||
const std::string msg;
|
||||
const std::thread::id tid;
|
||||
const uint64_t pid;
|
||||
};
|
||||
|
||||
template <typename LT>
|
||||
class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{
|
||||
using StreamType = std::vector<std::ostream*>;
|
||||
@@ -74,7 +71,7 @@ public:
|
||||
|
||||
~DebugLogger() { type_ptr->stop(); }
|
||||
|
||||
void log(const std::string& msg, const uint8_t& lvl=0xFF) {
|
||||
void log(const std::string& msg, const uint8_t& lvl=0x00) {
|
||||
type_ptr->push(LogEvent{
|
||||
std::chrono::system_clock::now(),
|
||||
lvl,
|
||||
@@ -85,7 +82,7 @@ public:
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
void log(char const(&msg) [N], const uint8_t& lvl=0xFF) {
|
||||
void log(char const(&msg) [N], const uint8_t& lvl=0x00) {
|
||||
type_ptr->log(std::string(msg), lvl);
|
||||
}
|
||||
|
||||
@@ -104,7 +101,6 @@ protected:
|
||||
|
||||
private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
|
||||
};
|
||||
|
||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
@@ -127,17 +123,31 @@ public:
|
||||
protected:
|
||||
using Base::_ostream;
|
||||
|
||||
bool process(const LogEvent& msg) {
|
||||
for (const auto os : this->_ostream) {
|
||||
*os << msg.lvl << "\t|\t" << msg.msg << "\n";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool process(const LogEvent& msg);
|
||||
};
|
||||
|
||||
#ifndef _NB_NO_LOGGER
|
||||
extern DefaultDebugLogger logger;
|
||||
#endif // _NB_NO_LOGGER
|
||||
|
||||
// Taking Charge of Adult ADHD by Russell Barkley
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#ifndef CONSTRUCT_ERROR
|
||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||
#define CONSTRUCT_ERROR(type, ...) type(# __VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define CONSTRUCT_ERROR(type, ...) type(__VA_ARGS__)
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
#endif // CONSTRUCT_ERROR
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef _NB_AUTOLOG
|
||||
#define THROW(type, ...) {auto x = CONSTRUCT_ERROR(type, ## __VA_ARGS__); nb::logger.error(x); throw x;}
|
||||
#else
|
||||
#define THROW(type, ...) throw CONSTRUCT_ERROR(type, __VA_ARGS__)
|
||||
#endif // _NB_AUTOLOG
|
||||
#endif // THROW
|
||||
|
||||
#endif // _NB_LOGGER
|
||||
@@ -0,0 +1,142 @@
|
||||
#pragma once
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifdef _NB_TARGET_WINDOWS
|
||||
const std::string NEWLINE = "\n";
|
||||
const std::wstring WNEWLINE = L"\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>
|
||||
using StringConvertible = ValidConversion<std::string, T...>;
|
||||
|
||||
template <typename... T>
|
||||
using WStringConvertible = ValidConversion<std::wstring, T...>;
|
||||
|
||||
template <typename... T>
|
||||
constexpr bool StringConvertible_v = StringConvertible<T...>::value;
|
||||
|
||||
template <typename... T>
|
||||
constexpr bool WStringConvertible_v = WStringConvertible<T...>::value;
|
||||
|
||||
template <typename... T>
|
||||
using StringConvertible_to = typename StringConvertible<T...>::to;
|
||||
|
||||
template <typename... T>
|
||||
using WStringConvertible_to = typename WStringConvertible<T...>::to;
|
||||
|
||||
template <typename... T>
|
||||
using StringConvertible_from = typename StringConvertible<T...>::from;
|
||||
|
||||
template <typename... T>
|
||||
using WStringConvertible_from = typename WStringConvertible<T...>::from;
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<StringConvertible_v<T>, std::wstring> str_to_wstr(T in) {
|
||||
std::string str(in);
|
||||
std::wstring ret(str.begin(), str.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::enable_if_t<WStringConvertible_v<T>, std::string> wstr_to_str(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 Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(Stream& s, Args&&... args) {
|
||||
(s << ... << args);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void term(Args&&... args) { stream(std::cout, args..., nb::NEWLINE); }
|
||||
|
||||
template<typename... Args>
|
||||
void wterm(Args&&... args) { stream(std::wcout, args..., nb::WNEWLINE); }
|
||||
|
||||
template <typename T = std::string, typename A, typename B, typename C>
|
||||
ValidConversion_to<T, A, B, C> find_and_replace(
|
||||
A original,
|
||||
B find,
|
||||
C replace
|
||||
) {
|
||||
const T& find_t = T(find);
|
||||
const T& replace_t = T(replace);
|
||||
|
||||
T ret(original);
|
||||
|
||||
std::size_t find_len = find_t.length();
|
||||
std::size_t replace_len = replace_t.length();
|
||||
std::size_t currpos = 0;
|
||||
while(true) {
|
||||
currpos = ret.find(find_t, currpos);
|
||||
if (currpos == T::npos) {
|
||||
break;
|
||||
}
|
||||
ret = ret.erase(currpos, find_len);
|
||||
ret = ret.insert(currpos, replace_t);
|
||||
currpos += replace_len;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename A, typename B, typename C>
|
||||
StringConvertible_to<A, B, C> indent_strblock(
|
||||
A block,
|
||||
B prepend,
|
||||
C topIndent
|
||||
) {
|
||||
return std::string(topIndent) + find_and_replace<std::string>(
|
||||
std::string(block),
|
||||
nb::NEWLINE,
|
||||
nb::NEWLINE + std::string(prepend)
|
||||
);
|
||||
}
|
||||
|
||||
template <typename A, typename B>
|
||||
StringConvertible_to<A, B> indent_strblock(
|
||||
A block,
|
||||
B prepend
|
||||
) {
|
||||
return indent_strblock<A, B, B>(block, prepend, prepend);
|
||||
}
|
||||
|
||||
/* 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
|
||||
@@ -2,15 +2,78 @@
|
||||
#ifndef _NB_TYPE_TRAITS
|
||||
#define _NB_TYPE_TRAITS
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
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... Types>
|
||||
struct ValidConversion;
|
||||
|
||||
template <typename To, typename A, typename B, typename... Rest>
|
||||
struct ValidConversion<To, A, B, Rest...> {
|
||||
typedef std::conjunction<
|
||||
std::is_constructible<To, A>, std::is_constructible<To, A>, std::conjunction<std::is_constructible<To, Rest>...>
|
||||
> value_type;
|
||||
static constexpr bool value = value_type::value;
|
||||
typedef std::enable_if_t<value, To> to;
|
||||
|
||||
};
|
||||
|
||||
template <typename To, typename From>
|
||||
struct ValidConversion<To, From> {
|
||||
typedef std::is_constructible<To, From> value_type;
|
||||
static constexpr bool value = value_type::value;
|
||||
typedef std::enable_if_t<value, To> to;
|
||||
typedef std::enable_if_t<value, From> from;
|
||||
};
|
||||
|
||||
template<typename... Types>
|
||||
using ValidConversion_v = typename ValidConversion<Types...>::value;
|
||||
|
||||
template<typename... Types>
|
||||
using ValidConversion_to = typename ValidConversion<Types...>::to;
|
||||
|
||||
template<typename To, typename From>
|
||||
using ValidConversion_from = typename ValidConversion<To, From>::from;
|
||||
|
||||
template<typename Func, std::size_t N=0, typename... Pack>
|
||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>&, Func) {}
|
||||
|
||||
template<std::size_t N=0, typename Func, typename... Pack>
|
||||
template<typename Func, std::size_t N=0, typename... Pack>
|
||||
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>& tup, Func f) {
|
||||
f(N, std::get<N>(tup));
|
||||
|
||||
@@ -2,12 +2,10 @@
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename T>
|
||||
T swapEndian(const T& val) {
|
||||
/* template<typename T>
|
||||
T swap_endian(const T& val) {
|
||||
T ret;
|
||||
const int size = sizeof(T);
|
||||
auto retLoc = static_cast<void*>(&ret);
|
||||
@@ -17,9 +15,10 @@ T swapEndian(const T& val) {
|
||||
memcpy(retLoc+i, valLoc+(size-i-1), 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
} */
|
||||
|
||||
using ByteVector = std::vector<uint8_t>;
|
||||
|
||||
// using ByteVector = std::vector<uint8_t>;
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_CORE_TYPES
|
||||
@@ -1,12 +1,14 @@
|
||||
#include "Errors.hpp"
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
const std::string Error::type = "Error";
|
||||
const ErrorCodeMap Error::ErrorMessages = {
|
||||
{ErrorCodes::GENERAL, "General std::exception."},
|
||||
{ErrorCodes::UNDEFINED, "Undefined / general error."},
|
||||
{ErrorCodes::BADERRORCODE, "Unrecognized error code."}
|
||||
const std::string Error<nb::NoneType>::type = "nb::Error";
|
||||
|
||||
const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
|
||||
{Error::Codes::GENERAL, "General std::exception."},
|
||||
{Error::Codes::UNDEFINED, "Undefined / general error."},
|
||||
{Error::Codes::BADERRORCODE, "Unrecognized error code."}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,31 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "Logger.hpp"
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifndef _NB_NO_LOGGER
|
||||
nb::DefaultDebugLogger logger(std::cout);
|
||||
#endif // _NB_NO_LOGGER
|
||||
|
||||
bool nb::DefaultDebugLogger::process(const LogEvent& msg) {
|
||||
for (const auto os : this->_ostream) {
|
||||
*os << indent_strblock(
|
||||
msg.msg,
|
||||
std::string(10, ' '),
|
||||
"[ "+std::to_string(msg.lvl)+" ] : "
|
||||
) << nb::NEWLINE;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
||||
return log.run();
|
||||
}
|
||||
|
||||
|
||||
|
||||
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||
|
||||
} // namespace nb
|
||||
@@ -7,7 +7,6 @@
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
#include "Processes.hpp"
|
||||
#include "Types.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#include "StringUtils.hpp"
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -4,12 +4,21 @@ if (NB_BUILD_TESTS)
|
||||
enable_testing()
|
||||
include(GoogleTest)
|
||||
add_executable(TestCore
|
||||
testErrors.cpp
|
||||
testProcesses.cpp
|
||||
./testErrors.cpp
|
||||
#./testProcesses.cpp
|
||||
./testUtils.cpp
|
||||
)
|
||||
target_link_libraries(TestCore
|
||||
NBCore
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
add_executable(LoggerTest
|
||||
./testLogger.cpp
|
||||
)
|
||||
target_link_libraries(LoggerTest
|
||||
NBCore
|
||||
)
|
||||
|
||||
gtest_discover_tests(TestCore)
|
||||
endif()
|
||||
@@ -1,38 +1,39 @@
|
||||
#define CODE_ERROR_LOCATIONS
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "Logger.hpp"
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include <string>
|
||||
|
||||
using namespace nb;
|
||||
|
||||
class TestError : public ErrorBase<TestError> {
|
||||
class TestError : public Error<TestError> {
|
||||
using Base = Error<TestError>;
|
||||
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
|
||||
};
|
||||
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
static const nb::ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
const std::string TestError::type="TestError";
|
||||
const ErrorCodeMap TestError::ErrorMessages{
|
||||
{TestError::ErrorCodes::A, "Hey!"},
|
||||
{TestError::ErrorCodes::B, "How"},
|
||||
{TestError::ErrorCodes::C, "You"},
|
||||
{TestError::ErrorCodes::D, "Doin"}
|
||||
{TestError::Codes::A, "Hey!"},
|
||||
{TestError::Codes::B, "How"},
|
||||
{TestError::Codes::C, "You"},
|
||||
{TestError::Codes::D, "Doin"}
|
||||
};
|
||||
|
||||
|
||||
TEST(ErrorTest, Test) {
|
||||
EXPECT_EQ(1, 1);
|
||||
|
||||
std::stringstream sstream;
|
||||
|
||||
ASSERT_TRUE(nb::logger.isRunning());
|
||||
nb::logger.log("Hey!");
|
||||
auto err = TestError(0, TestError(1, TestError(2, TestError(3, TestError(2)))));
|
||||
ASSERT_STREQ(err.what(), "Hey!\n Trace: How\n Trace: You\n Trace: Doin\n Trace: You\n");
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <exception>
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include "Logger.hpp"
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
nb::logger.log("Whoop!");
|
||||
try {
|
||||
THROW(nb::Error, "Hiii!");
|
||||
} catch (nb::Error<nb::NoneType> e){
|
||||
nb::logger.log("Winner winner chicken dinner!");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
#define CODE_ERROR_LOCATIONS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <iostream>
|
||||
#include "Processes.hpp"
|
||||
#include <Windows.h>
|
||||
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
#define CODE_ERROR_LOCATIONS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
#include "TypeTraits.hpp"
|
||||
#include "StringUtils.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