Compare commits
1 Commits
error_stru
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 352a45bce0 |
@ -48,12 +48,13 @@ if(NB_BUILD_TESTS)
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
gtest
|
||||
URL https://github.com/google/googletest/archive/refs/heads/main.zip
|
||||
URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.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()
|
||||
@ -61,7 +62,6 @@ 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)
|
||||
|
||||
@ -4,6 +4,9 @@
|
||||
|
||||
These are subject to (hopefully) grow in size as more stuff gets added.
|
||||
|
||||
* NBCore: Core/foundational functionality for all other subpackages to be built upon.
|
||||
* Erroring and Logging
|
||||
* Basic data structures and declarations
|
||||
* NBWindow: Creates and manages GLFW windows
|
||||
* NBEvents: Multithreading event manager. Very unstable. Sucks a lot.
|
||||
|
||||
|
||||
@ -2,10 +2,16 @@
|
||||
#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,23 +1,20 @@
|
||||
include_directories(./.)
|
||||
|
||||
toAbsolutePath(NB_CORE_SOURCE
|
||||
./src/ErrorsImpl.cpp
|
||||
./src/Logger.cpp
|
||||
./src/Errors.cpp
|
||||
./src/Processes.cpp
|
||||
./src/StringUtils.cpp
|
||||
./src/Utils.cpp
|
||||
./src/Logger.cpp
|
||||
)
|
||||
|
||||
toAbsolutePath(NB_CORE_INCLUDE
|
||||
./ANSITerm.hpp
|
||||
./DataSink.hpp
|
||||
./Errors.hpp
|
||||
./ErrorsImpl.hpp
|
||||
./Logger.hpp
|
||||
./Processes.hpp
|
||||
./StringUtils.hpp
|
||||
./ThreadsafeQueue.hpp
|
||||
./Types.hpp
|
||||
./TypeTraits.hpp
|
||||
./Utils.hpp
|
||||
)
|
||||
|
||||
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
||||
@ -25,8 +22,6 @@ 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,7 +2,6 @@
|
||||
#ifndef _NB_DATASINK
|
||||
#define _NB_DATASINK
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
@ -35,9 +34,9 @@ class BufferedDataProcessor : public DataSink<DataType> {
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
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); }
|
||||
bool stop() noexcept { return type_ptr->stop(); }
|
||||
bool run() { return type_ptr->run(); }
|
||||
bool in(const DataType& val) { return type_ptr->in(val); }
|
||||
|
||||
protected:
|
||||
unsigned int count() const {
|
||||
@ -72,13 +71,13 @@ template<typename DataType, typename ProcessorType>
|
||||
class MultithreadedDataProcessor
|
||||
: public BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType> {
|
||||
using Base = BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType>;
|
||||
public:
|
||||
public:
|
||||
~MultithreadedDataProcessor() { type_ptr->stop(); }
|
||||
|
||||
bool isRunning() const noexcept override {
|
||||
return this->_running;
|
||||
return this->_running && (_runningThread!=nullptr);
|
||||
}
|
||||
bool run() override {
|
||||
bool run() {
|
||||
if (!type_ptr->isRunning()) {
|
||||
this->_running = true;
|
||||
_runningThread = std::make_shared<std::thread>([&]{
|
||||
@ -98,7 +97,7 @@ class MultithreadedDataProcessor
|
||||
return !type_ptr->isRunning();
|
||||
}
|
||||
|
||||
protected:
|
||||
protected:
|
||||
using Base::Base;
|
||||
|
||||
unsigned int count() const {
|
||||
|
||||
@ -2,42 +2,138 @@
|
||||
#ifndef _NB_ERROR
|
||||
#define _NB_ERROR
|
||||
|
||||
#include "ErrorsImpl.hpp"
|
||||
#include "Logger.hpp"
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#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
|
||||
|
||||
namespace nb {
|
||||
|
||||
#ifdef _NB_AUTOLOG
|
||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
||||
#ifndef LOG
|
||||
#define LOG(args...) nb::logger.log(args, __FILE__, __LINE__)
|
||||
#endif // LOG
|
||||
#ifndef WARN
|
||||
#define WARN(args...) nb::logger.warn(args, __FILE__, __LINE__)
|
||||
#endif // WARN
|
||||
#ifndef ERROR
|
||||
#define ERROR(args...) nb::logger.error(args, __FILE__, __LINE__)
|
||||
#endif // ERROR
|
||||
#else
|
||||
#ifndef LOG
|
||||
#define LOG(args...) nb::logger.log(args)
|
||||
#endif // LOG
|
||||
#ifndef WARN
|
||||
#define WARN(args...) nb::logger.warn(args)
|
||||
#endif // WARN
|
||||
#ifndef ERROR
|
||||
#define ERROR(args...) nb::logger.error(args)
|
||||
#endif // ERROR
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
#endif // _NB_AUTOLOG
|
||||
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
|
||||
) {}
|
||||
|
||||
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(); };
|
||||
|
||||
protected:
|
||||
ErrorBase() = default;
|
||||
ErrorBase(
|
||||
const unsigned int code,
|
||||
std::string msg,
|
||||
unsigned int line=0,
|
||||
std::string filename=""
|
||||
) noexcept : _code{code}, _msg{""} {
|
||||
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_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;
|
||||
};
|
||||
|
||||
class Error : public ErrorBase<Error> {
|
||||
public:
|
||||
enum ErrorCodes : unsigned int {
|
||||
GENERAL, UNDEFINED, BADERRORCODE
|
||||
};
|
||||
|
||||
using ErrorBase<Error>::ErrorBase;
|
||||
|
||||
friend ErrorBase<Error>;
|
||||
|
||||
protected:
|
||||
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);
|
||||
}
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef _NB_AUTOLOG
|
||||
#define THROW(args...) ERROR(args); nb::logger.stop(); throw args
|
||||
#else
|
||||
#define THROW(args...) throw args
|
||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
||||
#endif // THROW
|
||||
} // namespace nb
|
||||
|
||||
|
||||
#endif // _NB_ERROR
|
||||
@ -1,175 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_ERRORS_IMPL
|
||||
#define _NB_ERRORS_IMPL
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "StringUtils.hpp"
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
typedef std::unordered_map<unsigned int, std::string> ErrorCodeMap;
|
||||
|
||||
template <class ErrorType=NoneType>
|
||||
class Error;
|
||||
|
||||
class ErrorBase {
|
||||
protected:
|
||||
|
||||
public:
|
||||
const unsigned int code;
|
||||
const std::string msg;
|
||||
const std::string type;
|
||||
const std::shared_ptr<ErrorBase> trace;
|
||||
|
||||
ErrorBase(const ErrorBase&) = default;
|
||||
ErrorBase(const std::exception&) noexcept;
|
||||
virtual std::string what() const noexcept {
|
||||
std::string ret = msg;
|
||||
if (trace) {
|
||||
std::string trace_msg = msg;
|
||||
ret += nb::NEWLINE + indent_strblock(
|
||||
trace_msg,
|
||||
nb::TABOVER,
|
||||
nb::TABOVER+"Trace: "
|
||||
);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected:
|
||||
ErrorBase(
|
||||
unsigned int code_,
|
||||
std::string msg_,
|
||||
std::string type_,
|
||||
std::shared_ptr<ErrorBase> trace_
|
||||
) noexcept :
|
||||
code(code_),
|
||||
msg(msg_),
|
||||
type(type_),
|
||||
trace{trace_}
|
||||
{}
|
||||
};
|
||||
|
||||
template <class ErrorType>
|
||||
class Error : public ErrorBase {
|
||||
private:
|
||||
void inline check_asserts() {
|
||||
static_assert(std::is_same<const ErrorCodeMap, 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::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."
|
||||
);
|
||||
}
|
||||
|
||||
protected:
|
||||
using ErrorBase::ErrorBase;
|
||||
|
||||
public:
|
||||
Error(
|
||||
unsigned int code_,
|
||||
const ErrorBase& trace_
|
||||
) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages.at(code_),
|
||||
ErrorType::type,
|
||||
std::make_shared<ErrorBase>(trace_)
|
||||
) { check_asserts(); }
|
||||
|
||||
Error(
|
||||
std::string msg_,
|
||||
const ErrorBase& trace_
|
||||
) noexcept : ErrorBase(
|
||||
0,
|
||||
msg_,
|
||||
ErrorType::type,
|
||||
std::make_shared<ErrorBase>(trace_)
|
||||
) { check_asserts(); }
|
||||
|
||||
Error(unsigned int code_) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages.at(code_),
|
||||
ErrorType::type,
|
||||
nullptr
|
||||
) { check_asserts(); }
|
||||
|
||||
Error(std::string msg_) noexcept : ErrorBase(
|
||||
0,
|
||||
msg_,
|
||||
ErrorType::type,
|
||||
nullptr
|
||||
) { check_asserts(); }
|
||||
|
||||
Error(
|
||||
unsigned int code_,
|
||||
const std::string& note_,
|
||||
const ErrorBase& trace_
|
||||
) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
|
||||
ErrorType::type,
|
||||
std::make_shared<ErrorBase>(trace_)
|
||||
) { check_asserts(); }
|
||||
|
||||
Error(unsigned int code_, const std::string& note_) noexcept : ErrorBase(
|
||||
code_,
|
||||
ErrorType::ErrorMessages.at(code_) + " (" + note_ + ")",
|
||||
ErrorType::type,
|
||||
nullptr
|
||||
) { check_asserts(); }
|
||||
|
||||
using ErrorBase::what;
|
||||
using ErrorBase::code;
|
||||
using ErrorBase::msg;
|
||||
using ErrorBase::trace;
|
||||
using ErrorBase::type;
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
class Error<NoneType> : public Error<Error<NoneType>> {
|
||||
using Base = Error<Error<NoneType>>;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
Error(unsigned int code_=1) noexcept : Base(code_) {}
|
||||
|
||||
Error(const std::exception& err) : Base(
|
||||
Codes::STANDARD,
|
||||
std::string(err.what()),
|
||||
"std::exception",
|
||||
nullptr
|
||||
) {}
|
||||
|
||||
// fix
|
||||
Error(std::string msg_) noexcept : Base(
|
||||
Codes::UNDEFINED,
|
||||
msg_
|
||||
) {}
|
||||
|
||||
enum Codes : unsigned int {
|
||||
STANDARD, UNDEFINED
|
||||
};
|
||||
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_ERRORS_IMPL
|
||||
@ -2,27 +2,35 @@
|
||||
#ifndef _NB_LOGGER
|
||||
#define _NB_LOGGER
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <ostream>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "DataSink.hpp"
|
||||
#include "ErrorsImpl.hpp"
|
||||
#include "Processes.hpp"
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
class ErrorBase;
|
||||
|
||||
typedef std::chrono::time_point<
|
||||
std::chrono::system_clock,
|
||||
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;
|
||||
|
||||
@ -32,36 +40,26 @@ class LoggerBase
|
||||
using StreamType = ST;
|
||||
using LoggerType = Logger;
|
||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||
|
||||
public:
|
||||
bool run() override {
|
||||
if (!static_cast<LoggerType*>(this)->isRunning()) {
|
||||
public:
|
||||
bool run() {
|
||||
if (!type_ptr->isRunning()) {
|
||||
this->_running = true;
|
||||
this->_runningThread = std::make_shared<std::thread>([&]{
|
||||
while(static_cast<LoggerType*>(this)->isRunning()) {
|
||||
static_cast<LoggerType*>(this)->flush();
|
||||
while(type_ptr->isRunning()) {
|
||||
type_ptr->flush();
|
||||
}
|
||||
static_cast<LoggerType*>(this)->flush();
|
||||
type_ptr->flush();
|
||||
});
|
||||
}
|
||||
return static_cast<LoggerType*>(this)->isRunning();
|
||||
return type_ptr->isRunning();
|
||||
}
|
||||
using Base::flush;
|
||||
|
||||
protected:
|
||||
protected:
|
||||
LoggerBase() = default;
|
||||
|
||||
StreamType _ostream;
|
||||
};
|
||||
|
||||
struct LogEvent{
|
||||
const LoggerTimePoint time;
|
||||
const unsigned char lvl;
|
||||
const std::string msg;
|
||||
const std::thread::id tid;
|
||||
const uint64_t pid;
|
||||
const std::string file="";
|
||||
const unsigned int line=0;
|
||||
private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
};
|
||||
|
||||
template <typename LT>
|
||||
@ -74,70 +72,39 @@ public:
|
||||
template<typename... ST>
|
||||
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {}
|
||||
|
||||
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
||||
~DebugLogger() { type_ptr->stop(); }
|
||||
|
||||
template<typename U>
|
||||
void log(
|
||||
U val,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) { static_cast<LoggerType*>(this)->write_message(val, 0x00, file, line); }
|
||||
|
||||
template<typename U>
|
||||
void warn(
|
||||
U val,
|
||||
uint8_t lvl=0x01,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) { static_cast<LoggerType*>(this)->write_message(val, lvl, file, line); }
|
||||
|
||||
void error(
|
||||
const ErrorBase& val,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->write_message(val, 0xFF, file, line);
|
||||
static_cast<LoggerType*>(this)->flush();
|
||||
}
|
||||
|
||||
protected:
|
||||
std::vector<std::ostream*> _ostream;
|
||||
void write_message(
|
||||
std::string msg,
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->push(LogEvent{
|
||||
void log(const std::string& msg, const uint8_t& lvl=0xFF) {
|
||||
type_ptr->push(LogEvent{
|
||||
std::chrono::system_clock::now(),
|
||||
lvl,
|
||||
msg,
|
||||
std::this_thread::get_id(),
|
||||
GetPID(),
|
||||
file,
|
||||
line
|
||||
});
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
void write_message(
|
||||
char const(&msg) [N],
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->write_message(std::string(msg), lvl, file, line);
|
||||
void log(char const(&msg) [N], const uint8_t& lvl=0xFF) {
|
||||
type_ptr->log(std::string(msg), lvl);
|
||||
}
|
||||
|
||||
void write_message(
|
||||
const ErrorBase& err,
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->write_message(err.what(), lvl, file, line);
|
||||
void log(const std::exception& err, const uint8_t& lvl=0xFF) {
|
||||
type_ptr->log(err.what(), lvl);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void warn(const U& val, const uint8_t& lvl=0x01) { type_ptr->log(val, lvl); }
|
||||
|
||||
template<typename U>
|
||||
void error(const U& val) { type_ptr->log(val, 0xFF); }
|
||||
|
||||
protected:
|
||||
std::vector<std::ostream*> _ostream;
|
||||
|
||||
private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
|
||||
};
|
||||
|
||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
@ -147,7 +114,7 @@ class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
template <typename... Ts>
|
||||
struct LogRow;
|
||||
|
||||
public:
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
~DefaultDebugLogger() { stop(); }
|
||||
@ -160,17 +127,17 @@ class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
protected:
|
||||
using Base::_ostream;
|
||||
|
||||
bool process(const LogEvent& msg);
|
||||
bool process(const LogEvent& msg) {
|
||||
for (const auto os : this->_ostream) {
|
||||
*os << msg.lvl << "\t|\t" << msg.msg << "\n";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
extern const bool LOGGER_RUNNING;
|
||||
|
||||
#ifndef _NB_NO_LOGGER
|
||||
extern DefaultDebugLogger logger;
|
||||
#endif // _NB_NO_LOGGER
|
||||
|
||||
// Taking Charge of Adult ADHD by Russell Barkley
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_LOGGER
|
||||
@ -7,8 +7,6 @@
|
||||
namespace nb {
|
||||
|
||||
uint64_t GetPID();
|
||||
uint64_t getTID();
|
||||
|
||||
|
||||
} // namespace nb
|
||||
|
||||
|
||||
@ -1,79 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_STRING_UTILS
|
||||
#define _NB_STRING_UTILS
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#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
|
||||
|
||||
const std::string TABOVER = " ";
|
||||
|
||||
// std::wstring str_to_wstr(std::string in);
|
||||
|
||||
// std::string wstr_to_str(std::wstring in);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(const Stream& s, Args&&... args);
|
||||
|
||||
template <typename Stream, typename... Args>
|
||||
void stream(const 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 = char>
|
||||
std::basic_string<T> find_and_replace(
|
||||
ExplicitType_t<std::basic_string<T>> original,
|
||||
ExplicitType_t<std::basic_string_view<T>> find,
|
||||
ExplicitType_t<std::basic_string_view<T>> replace
|
||||
) {
|
||||
using StringType = std::basic_string<T>;
|
||||
|
||||
StringType ret(original);
|
||||
|
||||
std::size_t find_len = find.length();
|
||||
std::size_t replace_len = replace.length();
|
||||
std::size_t currpos = 0;
|
||||
while(true) {
|
||||
currpos = ret.find(find, currpos);
|
||||
if (currpos == StringType::npos) {
|
||||
break;
|
||||
}
|
||||
ret = ret.erase(currpos, find_len);
|
||||
ret = ret.insert(currpos, replace);
|
||||
currpos += replace_len;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string indent_strblock(
|
||||
std::string block,
|
||||
std::string prepend,
|
||||
std::string topIndent
|
||||
);
|
||||
|
||||
std::string indent_strblock(
|
||||
std::string block,
|
||||
std::string prepend
|
||||
);
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_STRING_UTILS
|
||||
@ -2,96 +2,15 @@
|
||||
#ifndef _NB_TYPE_TRAITS
|
||||
#define _NB_TYPE_TRAITS
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace nb {
|
||||
|
||||
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 T>
|
||||
struct ExplicitType { using type=T; };
|
||||
|
||||
template <typename T>
|
||||
using ExplicitType_t = typename ExplicitType<T>::type;
|
||||
|
||||
template <typename T>
|
||||
T* NULLPTR = static_cast<T*>(nullptr);
|
||||
|
||||
template <typename Func, typename T>
|
||||
struct RunAndOutput {
|
||||
using type = T;
|
||||
T value;
|
||||
RunAndOutput(Func func, T val) : value(val) {
|
||||
func();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Func, std::size_t N=0, typename... Pack>
|
||||
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) {}
|
||||
|
||||
template<typename Func, std::size_t N=0, typename... Pack>
|
||||
template<std::size_t N=0, typename Func, 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));
|
||||
|
||||
25
engine/NBCore/Types.hpp
Normal file
25
engine/NBCore/Types.hpp
Normal file
@ -0,0 +1,25 @@
|
||||
#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
|
||||
@ -1,118 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include "Errors.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename T>
|
||||
using SharedVector = std::vector<std::shared_ptr<T>>;
|
||||
|
||||
template<typename T>
|
||||
using RValueVector = std::vector<T&&>;
|
||||
|
||||
using ByteVector = std::vector<uint8_t>;
|
||||
|
||||
class ObjectManagerError : public Error<ObjectManagerError> {
|
||||
using Base = Error<ObjectManagerError>;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
enum Codes : unsigned int {
|
||||
UNDEFINED, NO_MANAGER, MANAGER_MISMATCH, LOCK_OVERFLOW, BAD_THREAD
|
||||
};
|
||||
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
ByteVector vectorToBytes(const std::vector<T>& vec) {
|
||||
unsigned int num_bytes = vec.size() * sizeof(T);
|
||||
ByteVector ret(num_bytes);
|
||||
memcpy(ret.data(), vec.data(), num_bytes);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T, typename S>
|
||||
ByteVector concatVectorBytes(const std::vector<T>& vec1, const std::vector<S>& vec2) {
|
||||
ByteVector vec1_raw = vectorToBytes<T>(vec1);
|
||||
ByteVector vec2_raw = vectorToBytes<S>(vec2);
|
||||
unsigned int vec1_raw_size = vec1_raw.size();
|
||||
unsigned int vec2_raw_size = vec2_raw.size();
|
||||
ByteVector ret(vec1_raw_size + vec2_raw_size);
|
||||
memcpy(ret.data(), vec1_raw.data(), vec1_raw_size);
|
||||
memcpy(ret.data() + vec1_raw_size, vec2_raw.data(), vec2_raw_size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::vector<T> bytesToVector(const ByteVector& vec) {
|
||||
if (vec.size() % sizeof(T) != 0) {
|
||||
THROW(std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">."));
|
||||
}
|
||||
unsigned int num_elmts = vec.size() / sizeof(T);
|
||||
std::vector<T> ret(num_elmts);
|
||||
memcpy(ret.data(), vec.data(), vec.size());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class ThreadsafeObjectLock;
|
||||
|
||||
template <typename T>
|
||||
class ThreadsafeObject {
|
||||
using Codes = ObjectManagerError::Codes;
|
||||
|
||||
public:
|
||||
ThreadsafeObject(T&& obj)
|
||||
: _obj(std::make_shared<T>(obj)) {}
|
||||
|
||||
ThreadsafeObjectLock<T> lock() {
|
||||
return ThreadsafeObjectLock<T>(this);
|
||||
}
|
||||
|
||||
friend ThreadsafeObjectLock<T>;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<T> _obj;
|
||||
mutable std::recursive_mutex _mutex;
|
||||
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class ThreadsafeObjectLock {
|
||||
public:
|
||||
ThreadsafeObjectLock(const ThreadsafeObjectLock&) = delete;
|
||||
ThreadsafeObjectLock operator=(const ThreadsafeObjectLock&) = delete;
|
||||
~ThreadsafeObjectLock() {
|
||||
_manager->_mutex.unlock();
|
||||
}
|
||||
|
||||
T* operator->() {
|
||||
return _manager->_obj.get();
|
||||
}
|
||||
|
||||
friend ThreadsafeObject<T>;
|
||||
|
||||
protected:
|
||||
ThreadsafeObjectLock(
|
||||
ThreadsafeObject<T>* const manager_
|
||||
) : _manager(manager_) {
|
||||
if (!_manager) {
|
||||
using Codes = ObjectManagerError::Codes;
|
||||
THROW(ObjectManagerError(Codes::NO_MANAGER));
|
||||
}
|
||||
_manager->_mutex.lock();
|
||||
}
|
||||
ThreadsafeObject<T>* const _manager;
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_CORE_TYPES
|
||||
12
engine/NBCore/src/Errors.cpp
Normal file
12
engine/NBCore/src/Errors.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
#include "Errors.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."}
|
||||
};
|
||||
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
#include "ErrorsImpl.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
const std::string Error<>::type = "nb::Error";
|
||||
const ErrorCodeMap Error<>::ErrorMessages = {
|
||||
{Error::Codes::STANDARD, "std::exception"},
|
||||
{Error::Codes::UNDEFINED, "Error"}
|
||||
};
|
||||
|
||||
//ErrorBase::
|
||||
|
||||
ErrorBase::ErrorBase(const std::exception& exception_) noexcept
|
||||
: ErrorBase(Error<NoneType>(exception_)) {}
|
||||
|
||||
}
|
||||
@ -1,38 +1,15 @@
|
||||
#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) {
|
||||
constexpr size_t level_field_width = 5;
|
||||
constexpr size_t msg_prompt_length = level_field_width+7;
|
||||
for (const auto os : this->_ostream) {
|
||||
*os << "[";
|
||||
os->width(level_field_width);
|
||||
*os << std::to_string(msg.lvl) << "] :: ";
|
||||
std::string fileloc="";
|
||||
if (!msg.file.empty()) {
|
||||
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
|
||||
}
|
||||
*os << indent_strblock(
|
||||
fileloc + msg.msg,
|
||||
std::string(20 - msg_prompt_length, ' '),
|
||||
""
|
||||
) << nb::NEWLINE;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
||||
return log.run();
|
||||
}
|
||||
|
||||
const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||
|
||||
} // namespace nb
|
||||
@ -7,6 +7,7 @@
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
#include "Processes.hpp"
|
||||
#include "Types.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
@ -15,14 +16,7 @@ uint64_t GetPID() {
|
||||
return GetCurrentProcessId();
|
||||
}
|
||||
#endif // _NB_TARGET_WINDOWS
|
||||
#ifdef _NB_TARGET_LINUX
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
#ifdef _NB_TARGET_WINDOWS
|
||||
uint64_t GetTID() {
|
||||
return GetCurrentThreadId();
|
||||
}
|
||||
#endif // _NB_TARGET_WINDOWS
|
||||
#ifdef _NB_TARGET_LINUX
|
||||
#endif // _NB_TARGET_LINUX
|
||||
|
||||
|
||||
@ -1,29 +0,0 @@
|
||||
#include "StringUtils.hpp"
|
||||
|
||||
/* std::string nb::wstr_to_str(std::wstring in) {
|
||||
std::size_t wstrlen = in.length();
|
||||
char* c_str= new char[wstrlen];
|
||||
std::wcstombs(c_str, in.c_str(), wstrlen);
|
||||
std::string ret(c_str, wstrlen);
|
||||
delete[] c_str;
|
||||
return ret;
|
||||
} */
|
||||
|
||||
std::string nb::indent_strblock(
|
||||
std::string block,
|
||||
std::string prepend,
|
||||
std::string topIndent
|
||||
) {
|
||||
return topIndent + nb::find_and_replace(
|
||||
block,
|
||||
nb::NEWLINE,
|
||||
nb::NEWLINE + prepend
|
||||
);
|
||||
}
|
||||
|
||||
std::string nb::indent_strblock(
|
||||
std::string block,
|
||||
std::string prepend
|
||||
) {
|
||||
return nb::indent_strblock(block, prepend, prepend);
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
#include "Utils.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
using ObjectManagerCodes = ObjectManagerError::Codes;
|
||||
const std::string ObjectManagerError::type = "nb::ObjectManagerError";
|
||||
const ErrorCodeMap ObjectManagerError::ErrorMessages({
|
||||
{ObjectManagerCodes::UNDEFINED, "Error"},
|
||||
{
|
||||
ObjectManagerCodes::NO_MANAGER,
|
||||
"Attempting to create object lock without lock manager"
|
||||
},
|
||||
{
|
||||
ObjectManagerCodes::MANAGER_MISMATCH,
|
||||
"Attempting to delete object lock from mismatched lock manager"
|
||||
},
|
||||
{
|
||||
ObjectManagerCodes::LOCK_OVERFLOW,
|
||||
"Too many object locks allocated"
|
||||
},
|
||||
{
|
||||
ObjectManagerCodes::BAD_THREAD,
|
||||
"Attempting operation from a bad thread"
|
||||
}
|
||||
});
|
||||
|
||||
}; // namespace nb
|
||||
@ -4,21 +4,12 @@ if (NB_BUILD_TESTS)
|
||||
enable_testing()
|
||||
include(GoogleTest)
|
||||
add_executable(TestCore
|
||||
./testErrors.cpp
|
||||
#./testProcesses.cpp
|
||||
./testUtils.cpp
|
||||
testErrors.cpp
|
||||
testProcesses.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,39 +1,38 @@
|
||||
#define CODE_ERROR_LOCATIONS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "Logger.hpp"
|
||||
|
||||
using namespace nb;
|
||||
|
||||
class TestError : public Error<TestError> {
|
||||
using Base = Error<TestError>;
|
||||
class TestError : public ErrorBase<TestError> {
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
using ErrorBase<TestError>::ErrorBase;
|
||||
|
||||
enum Codes : unsigned int {
|
||||
enum ErrorCodes : unsigned int {
|
||||
A, B, C, D
|
||||
};
|
||||
|
||||
static const std::string type;
|
||||
static const nb::ErrorCodeMap ErrorMessages;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
const std::string TestError::type="TestError";
|
||||
const ErrorCodeMap TestError::ErrorMessages{
|
||||
{TestError::Codes::A, "Hey!"},
|
||||
{TestError::Codes::B, "How"},
|
||||
{TestError::Codes::C, "You"},
|
||||
{TestError::Codes::D, "Doin"}
|
||||
{TestError::ErrorCodes::A, "Hey!"},
|
||||
{TestError::ErrorCodes::B, "How"},
|
||||
{TestError::ErrorCodes::C, "You"},
|
||||
{TestError::ErrorCodes::D, "Doin"}
|
||||
};
|
||||
|
||||
|
||||
TEST(ErrorTest, Test) {
|
||||
auto err = TestError(0, TestError(1, TestError(2, TestError(3, TestError(2)))));
|
||||
ASSERT_STREQ(err.what().c_str(), "Hey!\n Trace: How\n Trace: You\n Trace: Doin\n Trace: You");
|
||||
EXPECT_EQ(1, 1);
|
||||
|
||||
std::stringstream sstream;
|
||||
|
||||
ASSERT_TRUE(nb::logger.isRunning());
|
||||
nb::logger.log("Hey!");
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
#include <exception>
|
||||
|
||||
#include "Errors.hpp"
|
||||
#include "Logger.hpp"
|
||||
|
||||
class TestError : public nb::Error<TestError> {
|
||||
using Base = Error<TestError>;
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
|
||||
enum Codes : unsigned int {
|
||||
A, B, C, D
|
||||
};
|
||||
|
||||
static const std::string type;
|
||||
static const nb::ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
const std::string TestError::type="TestError";
|
||||
const nb::ErrorCodeMap TestError::ErrorMessages{
|
||||
{TestError::Codes::A, "Hey!"},
|
||||
{TestError::Codes::B, "How"},
|
||||
{TestError::Codes::C, "You"},
|
||||
{TestError::Codes::D, "Doin"}
|
||||
};
|
||||
|
||||
int main() {
|
||||
nb::logger.log("Whoop!");
|
||||
try {
|
||||
THROW(TestError(0, TestError(1, TestError(2, TestError(3, TestError(2))))));
|
||||
} catch (TestError e){
|
||||
nb::logger.log("nb::Error was thrown!");
|
||||
}
|
||||
|
||||
try {
|
||||
THROW(std::exception());
|
||||
} catch (std::exception e){
|
||||
nb::logger.log("std::exception was thrown!");
|
||||
}
|
||||
ERROR("hello there!");
|
||||
return 0;
|
||||
}
|
||||
@ -1,6 +1,8 @@
|
||||
#define CODE_ERROR_LOCATIONS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <iostream>
|
||||
#include "Processes.hpp"
|
||||
#include <Windows.h>
|
||||
|
||||
|
||||
@ -1,148 +0,0 @@
|
||||
#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<wchar_t>(L"Jeff", L"e", L"efe").c_str(),
|
||||
L"Jefeff"
|
||||
);
|
||||
|
||||
std::wstring tmp = find_and_replace<wchar_t>(L"Naif", L"a", L"afa");
|
||||
ASSERT_STREQ(
|
||||
find_and_replace<wchar_t>(tmp, L"i", L"ifi").c_str(),
|
||||
L"Nafaifif"
|
||||
);
|
||||
|
||||
tmp = find_and_replace<wchar_t>(L"aeiou", L"a", L"afa");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"e", L"efe");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"i", L"ifi");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"o", L"ofo");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"u", L"ufu");
|
||||
ASSERT_STREQ(
|
||||
tmp.c_str(),
|
||||
L"afaefeifiofoufu"
|
||||
);
|
||||
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"afa", L"a");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"efe", L"e");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"ifi", L"i");
|
||||
tmp = find_and_replace<wchar_t>(tmp, L"ofo", L"o");
|
||||
tmp = find_and_replace<wchar_t>(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
|
||||
Loading…
x
Reference in New Issue
Block a user