Compare commits
1
Commits
nbdata
..
126862748e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
126862748e |
+12
-64
@@ -7,10 +7,6 @@ set(CMAKE_CXX_STANDARD 17)
|
|||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
|
||||||
include(CMakePackageConfigHelpers)
|
|
||||||
include(CMakeDependentOption)
|
|
||||||
|
|
||||||
function(toAbsolutePath SETVAR PATHS)
|
function(toAbsolutePath SETVAR PATHS)
|
||||||
set(PATHS ${PATHS} ${ARGN})
|
set(PATHS ${PATHS} ${ARGN})
|
||||||
set(RET_VAL "")
|
set(RET_VAL "")
|
||||||
@@ -32,42 +28,12 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
|||||||
message(STATUS "Targeting Release build")
|
message(STATUS "Targeting Release build")
|
||||||
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
message(STATUS "Targeting Debug build")
|
message(STATUS "Targeting Debug build")
|
||||||
set(NB_DEBUG_BUILD ON)
|
set(NB_LOGGING ON)
|
||||||
|
set(NB_BUILD_TESTS ON)
|
||||||
|
set(NB_BUILD_DOCS ON)
|
||||||
add_compile_definitions(_NB_BUILD_DEBUG)
|
add_compile_definitions(_NB_BUILD_DEBUG)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
cmake_dependent_option(NB_LOGGING
|
|
||||||
"Creates a default logger and automatically logs with code locations."
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NB_BUILD_TESTS
|
|
||||||
"Build unit tests"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NB_BUILD_DOCS
|
|
||||||
"Build documentation"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NBENGINE_INSTALL
|
|
||||||
"Install NBEngine"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
cmake_dependent_option(NB_MONITOR_OPENGL_CALLS
|
|
||||||
"Monitors every OpenGL call for errors"
|
|
||||||
ON
|
|
||||||
NB_DEBUG_BUILD
|
|
||||||
OFF
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||||
message(STATUS "Building for Windows")
|
message(STATUS "Building for Windows")
|
||||||
set(NB_TARGET_WINDOWS ON)
|
set(NB_TARGET_WINDOWS ON)
|
||||||
@@ -89,24 +55,27 @@ if(NB_BUILD_TESTS)
|
|||||||
FetchContent_MakeAvailable(gtest)
|
FetchContent_MakeAvailable(gtest)
|
||||||
endif()
|
endif()
|
||||||
set(GTEST_COLOR ON)
|
set(GTEST_COLOR ON)
|
||||||
|
set(GLFW_BUILD_TESTS ON CACHE BOOL "" FORCE)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (NB_LOGGING)
|
if (NB_LOGGING)
|
||||||
message(STATUS "Building with automatic logging")
|
message(STATUS "Building with automatic logging")
|
||||||
add_compile_definitions(_NB_AUTOLOG)
|
add_compile_definitions(_NB_AUTOLOG)
|
||||||
add_compile_definitions(_NB_CODE_ERROR_LOCATIONS)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(NB_TARGET_WINDOWS)
|
if (NB_TARGET_WINDOWS)
|
||||||
add_compile_definitions(_NB_TARGET_WINDOWS)
|
add_compile_definitions(_NB_TARGET_WINDOWS)
|
||||||
endif()
|
elseif (NB_TARGET_LINUX)
|
||||||
if(NB_TARGET_LINUX)
|
|
||||||
add_compile_definitions(_NB_TARGET_LINUX)
|
add_compile_definitions(_NB_TARGET_LINUX)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
get_filename_component(NBENGINE_ROOT ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE)
|
# External Dep paths
|
||||||
|
set(GLFW_PATH ../glfw/)
|
||||||
|
set(GLAD_PATH ../glad/)
|
||||||
|
get_filename_component(GLFW_PATH ${GLFW_PATH} ABSOLUTE)
|
||||||
|
get_filename_component(GLAD_PATH ${GLAD_PATH} ABSOLUTE)
|
||||||
|
|
||||||
add_subdirectory(./engine ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE})
|
add_subdirectory(./engine)
|
||||||
|
|
||||||
# Toggle
|
# Toggle
|
||||||
set(NB_REBUILD_DOCS)
|
set(NB_REBUILD_DOCS)
|
||||||
@@ -114,24 +83,3 @@ set(NB_REBUILD_DOCS)
|
|||||||
if (NB_BUILD_DOCS)
|
if (NB_BUILD_DOCS)
|
||||||
add_subdirectory(./docs)
|
add_subdirectory(./docs)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
include(CMakePackageConfigHelpers)
|
|
||||||
configure_package_config_file(
|
|
||||||
"NBEngineConfig.cmake.in"
|
|
||||||
"${PROJECT_BINARY_DIR}/cmake/NBEngineConfig.cmake"
|
|
||||||
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/cmake
|
|
||||||
PATH_VARS CMAKE_INSTALL_LIBDIR
|
|
||||||
)
|
|
||||||
write_basic_package_version_file(
|
|
||||||
"${PROJECT_BINARY_DIR}/cmake/NBEngineConfigVersion.cmake"
|
|
||||||
COMPATIBILITY AnyNewerVersion
|
|
||||||
)
|
|
||||||
|
|
||||||
if (NBENGINE_INSTALL)
|
|
||||||
include(GNUInstallDirs)
|
|
||||||
install(FILES
|
|
||||||
"${PROJECT_BINARY_DIR}/cmake/NBEngineConfig.cmake"
|
|
||||||
"${PROJECT_BINARY_DIR}/cmake/NBEngineConfigVersion.cmake"
|
|
||||||
DESTINATION "${CMAKE_INSTALL_PREFIX}/cmake"
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
@PACKAGE_INIT@
|
|
||||||
set(NBENGINE_PACKAGES
|
|
||||||
NBCore
|
|
||||||
NBGraphics
|
|
||||||
)
|
|
||||||
foreach(NBENGINE_LIB ${NBENGINE_PACKAGES})
|
|
||||||
include("${CMAKE_CURRENT_LIST_DIR}/${NBENGINE_LIB}Config.cmake")
|
|
||||||
endforeach()
|
|
||||||
@@ -1,23 +1,21 @@
|
|||||||
# NBEngine
|
# NBEngine
|
||||||
|
|
||||||
|
My own GUI (and related stuff needed for GUI programs) engine. I suck at coding. Do not judge.
|
||||||
|
|
||||||
## Current Implemented Libraries
|
## Current Implemented Libraries
|
||||||
|
|
||||||
These are subject to (hopefully) grow in size as more stuff gets added.
|
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
|
* NBWindow: Creates and manages GLFW windows
|
||||||
* NBEvents: Multithreading event manager. Very unstable. Sucks a lot.
|
* NBEvents: Multithreading event manager. Very unstable. Sucks a lot.
|
||||||
|
|
||||||
## Dependencies and Foundations
|
## Dependencies and Foundations
|
||||||
|
|
||||||
For Code:
|
While the engine is intended to be a close-to-ground-up project, there's no point in reinventing the spokes that
|
||||||
|
make up a wheel. This project utilizes and depends on (expected to change and grow):
|
||||||
|
|
||||||
* GLFW
|
* GLFW
|
||||||
* Glad
|
* Glad
|
||||||
|
|
||||||
For Repo:
|
Additionally, it's important to note that right now this project is on Windows only. I wanna work on this in Linux soon
|
||||||
|
("soon" whatever that means. Maybe in like 5 years).
|
||||||
* gTest
|
|
||||||
* doxygen
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 58 KiB |
+8
-20
@@ -1,30 +1,18 @@
|
|||||||
|
include_directories(./.)
|
||||||
|
|
||||||
add_subdirectory(./NBCore)
|
add_subdirectory(./NBCore)
|
||||||
|
add_subdirectory(./NBEvents)
|
||||||
get_target_property(NBCORE_INTERFACE_INCLUDES NBCore INTERFACE_INCLUDE_DIRECTORIES)
|
|
||||||
include_directories(${NBCORE_INTERFACE_INCLUDES})
|
|
||||||
|
|
||||||
#add_subdirectory(./NBEvents)
|
|
||||||
add_subdirectory(./NBGraphics)
|
add_subdirectory(./NBGraphics)
|
||||||
add_subdirectory(./NBData)
|
|
||||||
|
|
||||||
if (NB_CORE_SOURCE OR NB_EVENTS_SOURCE OR NB_GRAPHICS_SOURCE OR NB_DATA_SOURCE)
|
if (NB_CORE_SOURCE OR NB_EVENTS_SOURCE OR NB_GRAPHICS_SOURCE)
|
||||||
set(NB_SOURCE_FILES "")
|
set(NB_SOURCE_FILES "")
|
||||||
list(APPEND NB_SOURCE_FILES
|
list(APPEND NB_SOURCE_FILES ${NB_CORE_SOURCE} ${NB_GRAPHICS_SOURCE} ${NB_EVENTS_SOURCE})
|
||||||
${NB_CORE_SOURCE}
|
|
||||||
${NB_GRAPHICS_SOURCE}
|
|
||||||
${NB_EVENTS_SOURCE}
|
|
||||||
${NB_DATA_SOURCE}
|
|
||||||
)
|
|
||||||
set(NB_SOURCE_FILES ${NB_SOURCE_FILES} PARENT_SCOPE)
|
set(NB_SOURCE_FILES ${NB_SOURCE_FILES} PARENT_SCOPE)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (NB_CORE_INCLUDE OR NB_EVENTS_INCLUDE OR NB_GRAPHICS_INCLUDE OR NB_DATA_INCLUDE)
|
|
||||||
|
if (NB_CORE_INCLUDE OR NB_EVENTS_INCLUDE OR NB_GRAPHICS_INCLUDE)
|
||||||
set(NB_INCLUDE_FILES "")
|
set(NB_INCLUDE_FILES "")
|
||||||
list(APPEND NB_INCLUDE_FILES
|
list(APPEND NB_INCLUDE_FILES ${NB_CORE_INCLUDE} ${NB_GRAPHICS_INCLUDE} ${NB_EVENTS_INCLUDE})
|
||||||
${NB_CORE_INCLUDE}
|
|
||||||
${NB_GRAPHICS_INCLUDE}
|
|
||||||
${NB_EVENTS_INCLUDE}
|
|
||||||
${NB_DATA_INCLUDE}
|
|
||||||
)
|
|
||||||
set(NB_INCLUDE_FILES ${NB_INCLUDE_FILES} PARENT_SCOPE)
|
set(NB_INCLUDE_FILES ${NB_INCLUDE_FILES} PARENT_SCOPE)
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
@@ -1,86 +1,29 @@
|
|||||||
cmake_minimum_required(VERSION 3.10)
|
|
||||||
project(NBCore VERSION 0.1)
|
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
|
||||||
include(CMakePackageConfigHelpers)
|
|
||||||
|
|
||||||
toAbsolutePath(NB_CORE_SOURCE
|
toAbsolutePath(NB_CORE_SOURCE
|
||||||
./src/ErrorsImpl.cpp
|
./src/Errors.cpp
|
||||||
./src/Logger.cpp
|
./src/Logger.cpp
|
||||||
./src/Processes.cpp
|
./src/Processes.cpp
|
||||||
./src/StringUtils.cpp
|
|
||||||
./src/Utils.cpp
|
./src/Utils.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
toAbsolutePath(NB_CORE_INCLUDE
|
toAbsolutePath(NB_CORE_INCLUDE
|
||||||
./include/NBCore/ANSITerm.hpp
|
./ANSITerm.hpp
|
||||||
./include/NBCore/DataSink.hpp
|
./DataSink.hpp
|
||||||
./include/NBCore/Errors.hpp
|
./Errors.hpp
|
||||||
./include/NBCore/ErrorsImpl.hpp
|
./Logger.hpp
|
||||||
./include/NBCore/Logger.hpp
|
./Processes.hpp
|
||||||
./include/NBCore/Printer.hpp
|
./ThreadsafeQueue.hpp
|
||||||
./include/NBCore/Processes.hpp
|
./Types.hpp
|
||||||
./include/NBCore/StringUtils.hpp
|
./TypeTraits.hpp
|
||||||
./include/NBCore/ThreadsafeQueue.hpp
|
./Utils.hpp
|
||||||
./include/NBCore/Types.hpp
|
|
||||||
./include/NBCore/TypeTraits.hpp
|
|
||||||
./include/NBCore/Utils.hpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
set(NB_CORE_SOURCE ${NB_CORE_SOURCE} PARENT_SCOPE)
|
||||||
set(NB_CORE_INCLUDE ${NB_CORE_INCLUDE} PARENT_SCOPE)
|
set(NB_CORE_INCLUDE ${NB_CORE_INCLUDE} PARENT_SCOPE)
|
||||||
|
|
||||||
add_library(NBCore ${NB_CORE_SOURCE})
|
add_library(NBCore ${NB_CORE_SOURCE})
|
||||||
add_library(NBEngine::Core ALIAS NBCore)
|
|
||||||
target_include_directories(NBCore
|
|
||||||
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
|
||||||
PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
|
||||||
)
|
|
||||||
export(
|
|
||||||
TARGETS NBCore
|
|
||||||
FILE "${CMAKE_BINARY_DIR}/cmake/NBCoreTargets.cmake"
|
|
||||||
NAMESPACE NBEngine::
|
|
||||||
)
|
|
||||||
configure_package_config_file(
|
|
||||||
"NBCoreConfig.cmake.in"
|
|
||||||
"${CMAKE_BINARY_DIR}/cmake/NBCoreConfig.cmake"
|
|
||||||
INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}/cmake"
|
|
||||||
PATH_VARS CMAKE_INSTALL_LIBDIR
|
|
||||||
)
|
|
||||||
write_basic_package_version_file(
|
|
||||||
"${CMAKE_BINARY_DIR}/cmake/NBCoreConfigVersion.cmake"
|
|
||||||
COMPATIBILITY AnyNewerVersion
|
|
||||||
)
|
|
||||||
|
|
||||||
if (NBENGINE_INSTALL)
|
target_include_directories(NBCore PUBLIC ./.)
|
||||||
message("Installing NBCore to ${CMAKE_INSTALL_PREFIX}")
|
|
||||||
install(
|
|
||||||
TARGETS NBCore
|
|
||||||
EXPORT NBCoreTargets
|
|
||||||
LIBRARY DESTINATION lib
|
|
||||||
ARCHIVE DESTINATION lib
|
|
||||||
INCLUDES DESTINATION include
|
|
||||||
)
|
|
||||||
install(
|
|
||||||
DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/NBCore"
|
|
||||||
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
|
|
||||||
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
|
|
||||||
)
|
|
||||||
install(
|
|
||||||
EXPORT NBCoreTargets
|
|
||||||
DESTINATION "${CMAKE_INSTALL_PREFIX}/cmake"
|
|
||||||
NAMESPACE NBEngine::
|
|
||||||
)
|
|
||||||
install(FILES
|
|
||||||
"${CMAKE_BINARY_DIR}/cmake/NBCoreConfig.cmake"
|
|
||||||
"${CMAKE_BINARY_DIR}/cmake/NBCoreConfigVersion.cmake"
|
|
||||||
DESTINATION "${CMAKE_INSTALL_PREFIX}/CMake"
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (NB_BUILD_TESTS)
|
if (NB_BUILD_TESTS)
|
||||||
add_subdirectory(./tests )
|
add_subdirectory(./tests)
|
||||||
endif()
|
endif()
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_DATASINK
|
||||||
|
#define _NB_DATASINK
|
||||||
|
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "ThreadSafeQueue.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
template<typename DataType>
|
||||||
|
class DataSink {
|
||||||
|
public:
|
||||||
|
DataSink(const DataSink&) = delete;
|
||||||
|
DataSink(DataSink&&) = delete;
|
||||||
|
DataSink& operator=(const DataSink&) = delete;
|
||||||
|
|
||||||
|
virtual bool isRunning() const noexcept {
|
||||||
|
return _running;
|
||||||
|
}
|
||||||
|
virtual bool stop() noexcept = 0;
|
||||||
|
virtual bool run() = 0;
|
||||||
|
virtual bool in(const DataType&) = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
DataSink() = default;
|
||||||
|
|
||||||
|
std::atomic<bool> _running;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename DataType, typename BufferType, typename ProcessorType>
|
||||||
|
class BufferedDataProcessor : public DataSink<DataType> {
|
||||||
|
using Base = 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); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
unsigned int count() const {
|
||||||
|
return type_ptr->count();
|
||||||
|
}
|
||||||
|
void push(const DataType& val) {
|
||||||
|
type_ptr->push(val);
|
||||||
|
}
|
||||||
|
DataType pop() {
|
||||||
|
return type_ptr->pop();
|
||||||
|
}
|
||||||
|
void flush() {
|
||||||
|
type_ptr->flush();
|
||||||
|
}
|
||||||
|
void clear() {
|
||||||
|
type_ptr->clear();
|
||||||
|
}
|
||||||
|
bool process(const DataType& val) {
|
||||||
|
return type_ptr->process(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
using Base::_running;
|
||||||
|
|
||||||
|
BufferType _buffer;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ProcessorType* const type_ptr = static_cast<ProcessorType*>(this);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename DataType, typename ProcessorType>
|
||||||
|
class MultithreadedDataProcessor
|
||||||
|
: public BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType> {
|
||||||
|
using Base = BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType>;
|
||||||
|
public:
|
||||||
|
~MultithreadedDataProcessor() { type_ptr->stop(); }
|
||||||
|
|
||||||
|
bool isRunning() const noexcept override {
|
||||||
|
return this->_running && (_runningThread!=nullptr);
|
||||||
|
}
|
||||||
|
bool run() {
|
||||||
|
if (!type_ptr->isRunning()) {
|
||||||
|
this->_running = true;
|
||||||
|
_runningThread = std::make_shared<std::thread>([&]{
|
||||||
|
while(type_ptr->isRunning()) {
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return this->isRunning();
|
||||||
|
}
|
||||||
|
bool stop() noexcept override {
|
||||||
|
if (type_ptr->isRunning()) {
|
||||||
|
this->_running = false;
|
||||||
|
_runningThread->join();
|
||||||
|
_runningThread = nullptr;
|
||||||
|
}
|
||||||
|
return !type_ptr->isRunning();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
unsigned int count() const {
|
||||||
|
return this->_buffer.size();
|
||||||
|
}
|
||||||
|
void push(const DataType& val) {
|
||||||
|
this->_buffer.push(val);
|
||||||
|
};
|
||||||
|
DataType pop() {
|
||||||
|
std::shared_ptr<DataType> event;
|
||||||
|
this->_buffer.pop(event);
|
||||||
|
this->process(*event);
|
||||||
|
return *event;
|
||||||
|
}
|
||||||
|
void flush() {
|
||||||
|
while(type_ptr->count()) {
|
||||||
|
pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void clear() {
|
||||||
|
this->_buffer.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<std::thread> _runningThread;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ProcessorType* type_ptr = static_cast<ProcessorType*>(this);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_DATASINK
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_ERROR
|
||||||
|
#define _NB_ERROR
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include "Utils.hpp"
|
||||||
|
|
||||||
|
#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 {
|
||||||
|
|
||||||
|
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
||||||
|
|
||||||
|
template<class ErrorType>
|
||||||
|
class ErrorBase : public std::exception {
|
||||||
|
public:
|
||||||
|
ErrorBase(const ErrorBase&) = default;
|
||||||
|
ErrorBase& operator=(const ErrorBase&) = delete;
|
||||||
|
|
||||||
|
virtual const char* what() const noexcept override final {
|
||||||
|
std::string ret = this->msg;
|
||||||
|
const std::string replace = nb::NEWLINE + "\t";
|
||||||
|
if (trace) {
|
||||||
|
ret += replace + find_and_replace(
|
||||||
|
std::string(trace->what()),
|
||||||
|
nb::NEWLINE,
|
||||||
|
replace
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const std::string& ret_ref = ret;
|
||||||
|
return ret_ref.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
static const std::string type;
|
||||||
|
static const ErrorCodeMap ErrorMessages;
|
||||||
|
|
||||||
|
const unsigned int code;
|
||||||
|
const std::string msg;
|
||||||
|
const std::exception* trace;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ErrorBase() = delete;
|
||||||
|
|
||||||
|
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_=""
|
||||||
|
) noexcept :
|
||||||
|
code(code_),
|
||||||
|
msg(msg_),
|
||||||
|
trace{static_cast<std::exception*>(trace_ ? new TraceType(*trace_) : nullptr) }
|
||||||
|
{
|
||||||
|
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::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."
|
||||||
|
);
|
||||||
|
static_assert(std::is_base_of<std::exception, TraceType>::value,
|
||||||
|
"`trace_` must be a pointer to a child object of std::exception."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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_,
|
||||||
|
nullptr,
|
||||||
|
line_,
|
||||||
|
filename_
|
||||||
|
) {}
|
||||||
|
|
||||||
|
};
|
||||||
|
template <typename ErrorType>
|
||||||
|
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
|
||||||
|
template <typename ErrorType>
|
||||||
|
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
|
||||||
|
|
||||||
|
class DefaultError {DefaultError() = default;};
|
||||||
|
|
||||||
|
template <class ErrorType = DefaultError>
|
||||||
|
class Error;
|
||||||
|
|
||||||
|
template<>
|
||||||
|
class Error<DefaultError>;
|
||||||
|
|
||||||
|
template <class ErrorType>
|
||||||
|
class Error : public ErrorBase<ErrorType> {
|
||||||
|
using Base = ErrorBase<ErrorType>;
|
||||||
|
public:
|
||||||
|
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<DefaultError> : public ErrorBase<Error<DefaultError>> {
|
||||||
|
using Base = ErrorBase<Error<DefaultError>>;
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
Error() : Base(0) {}
|
||||||
|
|
||||||
|
enum Codes : unsigned int {
|
||||||
|
GENERAL, UNDEFINED, BADERRORCODE
|
||||||
|
};
|
||||||
|
|
||||||
|
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_ERROR
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
#pragma once
|
||||||
|
#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 "Processes.hpp"
|
||||||
|
#include "ThreadSafeQueue.hpp"
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
template<typename LogType, typename Logger, typename ST=std::ostream*>
|
||||||
|
class LoggerBase
|
||||||
|
: public MultithreadedDataProcessor<LogType, Logger>{
|
||||||
|
using StreamType = ST;
|
||||||
|
using LoggerType = Logger;
|
||||||
|
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||||
|
public:
|
||||||
|
bool run() {
|
||||||
|
if (!type_ptr->isRunning()) {
|
||||||
|
this->_running = true;
|
||||||
|
this->_runningThread = std::make_shared<std::thread>([&]{
|
||||||
|
while(type_ptr->isRunning()) {
|
||||||
|
type_ptr->flush();
|
||||||
|
}
|
||||||
|
type_ptr->flush();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return type_ptr->isRunning();
|
||||||
|
}
|
||||||
|
protected:
|
||||||
|
LoggerBase() = default;
|
||||||
|
|
||||||
|
StreamType _ostream;
|
||||||
|
|
||||||
|
private:
|
||||||
|
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename LT>
|
||||||
|
class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{
|
||||||
|
using StreamType = std::vector<std::ostream*>;
|
||||||
|
using LoggerType = LT;
|
||||||
|
using Base = LoggerBase<LogEvent, LoggerType, StreamType>;
|
||||||
|
public:
|
||||||
|
|
||||||
|
template<typename... ST>
|
||||||
|
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {}
|
||||||
|
|
||||||
|
~DebugLogger() { type_ptr->stop(); }
|
||||||
|
|
||||||
|
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(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t N>
|
||||||
|
void log(char const(&msg) [N], const uint8_t& lvl=0xFF) {
|
||||||
|
type_ptr->log(std::string(msg), lvl);
|
||||||
|
}
|
||||||
|
|
||||||
|
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> {
|
||||||
|
using LoggerType = DefaultDebugLogger;
|
||||||
|
using Base = DebugLogger<DefaultDebugLogger>;
|
||||||
|
|
||||||
|
template <typename... Ts>
|
||||||
|
struct LogRow;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using Base::Base;
|
||||||
|
|
||||||
|
~DefaultDebugLogger() { stop(); }
|
||||||
|
|
||||||
|
friend class BufferedDataProcessor<LogEvent, ThreadsafeQueue<LogEvent>, DefaultDebugLogger>;
|
||||||
|
|
||||||
|
template <typename STR, typename... Ts>
|
||||||
|
friend STR& operator<<(STR&, const DefaultDebugLogger::LogRow<Ts...>&);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
extern DefaultDebugLogger logger;
|
||||||
|
|
||||||
|
// Taking Charge of Adult ADHD by Russell Barkley
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_LOGGER
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
@PACKAGE_INIT@
|
|
||||||
include("${CMAKE_CURRENT_LIST_DIR}/NBCoreTargets.cmake")
|
|
||||||
@@ -7,8 +7,6 @@
|
|||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
uint64_t GetPID();
|
uint64_t GetPID();
|
||||||
uint64_t getTID();
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_TYPE_TRAITS
|
||||||
|
#define _NB_TYPE_TRAITS
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
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<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));
|
||||||
|
ForEach<N+1, Func, Pack...>(tup, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
||||||
|
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||||
|
ForEach(std::tuple<Pack...>&, Func, const Args&) {}
|
||||||
|
|
||||||
|
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
||||||
|
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
||||||
|
ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
|
||||||
|
f(N, std::get<N>(tup), arg);
|
||||||
|
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename Pack1, typename... Pack>
|
||||||
|
struct RefPackToPtrVec {
|
||||||
|
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
|
||||||
|
: vec(_vec) {
|
||||||
|
static_assert(std::is_base_of<T, Pack1>::value);
|
||||||
|
vec.push_back(&p1);
|
||||||
|
vec = RefPackToPtrVec<T, Pack...>(vec, packs...).vec;
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPackToPtrVec(Pack1& p1, Pack&... packs)
|
||||||
|
: RefPackToPtrVec({}, p1, packs...) {}
|
||||||
|
|
||||||
|
std::vector<T*> vec;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename Pack>
|
||||||
|
struct RefPackToPtrVec<T, Pack> {
|
||||||
|
RefPackToPtrVec(std::vector<T*> _vec, Pack& p1)
|
||||||
|
: vec(_vec) {
|
||||||
|
static_assert(std::is_base_of<T, Pack>::value);
|
||||||
|
vec.push_back(&p1);
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPackToPtrVec(Pack& p1)
|
||||||
|
: RefPackToPtrVec({}, p1) {}
|
||||||
|
|
||||||
|
std::vector<T*> vec;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename... Pack>
|
||||||
|
struct PackIsSameType;
|
||||||
|
|
||||||
|
template <typename T, typename PackType>
|
||||||
|
struct PackIsSameType<T, PackType> {
|
||||||
|
const bool value = std::is_base_of<T, PackType>::value;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename FirstPack, typename... Pack>
|
||||||
|
struct PackIsSameType<T, FirstPack, Pack...> {
|
||||||
|
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
|
||||||
|
#endif // _NB_TYPE_TRAITS
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_CORE_TYPES
|
||||||
|
#define _NB_CORE_TYPES
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <utility>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
#ifdef _NB_TARGET_WINDOWS
|
||||||
|
const std::string NEWLINE("\r\n");
|
||||||
|
#endif // _NB_TARGET_WINDOWS
|
||||||
|
#ifdef _NB_TARGET_LINUX
|
||||||
|
const std::string NEWLINE("\n");
|
||||||
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
|
template<typename K, typename V, size_t N>
|
||||||
|
struct ConstexprMap {
|
||||||
|
template<typename Input>
|
||||||
|
constexpr ConstexprMap(Input inp) : _data{inp} {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
const std::array<std::pair<K, V>, N> _data;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
} */
|
||||||
|
|
||||||
|
std::string find_and_replace(const std::string& original, const std::string& find, const std::string& replace);
|
||||||
|
|
||||||
|
// using ByteVector = std::vector<uint8_t>;
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
#endif // _NB_CORE_TYPES
|
||||||
@@ -1,184 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_DATASINK
|
|
||||||
#define _NB_DATASINK
|
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <thread>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include <NBCore/ThreadSafeQueue.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
template<typename DataType>
|
|
||||||
class DataSink {
|
|
||||||
public:
|
|
||||||
DataSink(const DataSink&) = delete;
|
|
||||||
DataSink(DataSink&&) = delete;
|
|
||||||
DataSink& operator=(const DataSink&) = delete;
|
|
||||||
|
|
||||||
virtual bool isRunning() const noexcept {
|
|
||||||
std::atomic_thread_fence(std::memory_order_acquire);
|
|
||||||
return _running.load(std::memory_order_acquire);
|
|
||||||
}
|
|
||||||
virtual bool stop() noexcept = 0;
|
|
||||||
virtual bool run() = 0;
|
|
||||||
virtual bool in(const DataType&) = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
DataSink() {
|
|
||||||
_running.store(false, std::memory_order_release);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::atomic<bool> _running;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename DataType, typename SinkTypes=DataSink<DataType>>
|
|
||||||
class MultiSink : public DataSink<DataType> {
|
|
||||||
protected:
|
|
||||||
using Base = DataSink<DataType>;
|
|
||||||
using SinkPtr = std::shared_ptr<SinkTypes>;
|
|
||||||
using Base::_running;
|
|
||||||
std::vector<SinkPtr> _sinks;
|
|
||||||
|
|
||||||
public:
|
|
||||||
MultiSink(std::vector<SinkPtr> sinks={}) : _sinks(sinks) {}
|
|
||||||
virtual void addSink(SinkPtr sink) {
|
|
||||||
_sinks.push_back(sink);
|
|
||||||
}
|
|
||||||
virtual std::vector<SinkPtr>& getSinks() { return _sinks; }
|
|
||||||
bool isRunning() const noexcept override {
|
|
||||||
return Base::isRunning();
|
|
||||||
}
|
|
||||||
bool stop() noexcept override {
|
|
||||||
_running.store(false, std::memory_order_release);
|
|
||||||
for (auto& sink : _sinks) {
|
|
||||||
sink->stop();
|
|
||||||
}
|
|
||||||
return isRunning();
|
|
||||||
}
|
|
||||||
bool run() override {
|
|
||||||
_running.store(true, std::memory_order_release);
|
|
||||||
for (auto& sink : _sinks) {
|
|
||||||
sink->run();
|
|
||||||
}
|
|
||||||
return isRunning();
|
|
||||||
}
|
|
||||||
bool in(const DataType& data) override {
|
|
||||||
if (isRunning()) {
|
|
||||||
bool success = true;
|
|
||||||
for (auto& sink : _sinks) {
|
|
||||||
success &= sink->in(data);
|
|
||||||
}
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename DataType, typename BufferType, typename ProcessorType>
|
|
||||||
class BufferedDataProcessor : public DataSink<DataType> {
|
|
||||||
private:
|
|
||||||
ProcessorType* const type_ptr = static_cast<ProcessorType*>(this);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base = DataSink<DataType>;
|
|
||||||
using Base::_running;
|
|
||||||
BufferType _buffer;
|
|
||||||
virtual unsigned int count() const {
|
|
||||||
return type_ptr->count();
|
|
||||||
}
|
|
||||||
virtual bool pop(std::shared_ptr<DataType> ret) {
|
|
||||||
return type_ptr->pop(ret);
|
|
||||||
}
|
|
||||||
virtual void flush() {
|
|
||||||
type_ptr->flush();
|
|
||||||
}
|
|
||||||
virtual void clear() {
|
|
||||||
type_ptr->clear();
|
|
||||||
}
|
|
||||||
virtual bool process(const DataType& val) = 0;
|
|
||||||
|
|
||||||
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); }
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename DataType, typename ProcessorType>
|
|
||||||
class MultithreadedDataProcessor
|
|
||||||
: public BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType> {
|
|
||||||
private:
|
|
||||||
ProcessorType* const type_ptr = static_cast<ProcessorType*>(this);
|
|
||||||
std::mutex _pause;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base = BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType>;
|
|
||||||
using Base::Base;
|
|
||||||
using Base::process;
|
|
||||||
using Base::_running;
|
|
||||||
std::shared_ptr<std::thread> _runningThread;
|
|
||||||
virtual unsigned int count() const override {
|
|
||||||
return type_ptr->_buffer.size();
|
|
||||||
}
|
|
||||||
virtual bool pop(std::shared_ptr<DataType> ret=nullptr) override {
|
|
||||||
type_ptr->_buffer.pop(ret);
|
|
||||||
return this->process(*ret);
|
|
||||||
}
|
|
||||||
virtual bool popBlock(std::shared_ptr<DataType> ret=nullptr) {
|
|
||||||
ret = type_ptr->_buffer.popBlock();
|
|
||||||
return this->process(*ret);
|
|
||||||
}
|
|
||||||
virtual void flush() override {
|
|
||||||
while(type_ptr->count()) {
|
|
||||||
type_ptr->pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
virtual void clear() override {
|
|
||||||
type_ptr->_buffer.empty();
|
|
||||||
}
|
|
||||||
virtual std::unique_lock<std::mutex> pause() {
|
|
||||||
return std::move(std::unique_lock<std::mutex>(_pause));
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::isRunning;
|
|
||||||
virtual ~MultithreadedDataProcessor() { type_ptr->stop(); }
|
|
||||||
|
|
||||||
bool run() override {
|
|
||||||
if (!type_ptr->isRunning()) {
|
|
||||||
_running.store(true, std::memory_order_release);
|
|
||||||
_runningThread = std::make_shared<std::thread>([&]{
|
|
||||||
while(type_ptr->isRunning()) {
|
|
||||||
std::lock_guard<std::mutex> lock(_pause);
|
|
||||||
type_ptr->flush();
|
|
||||||
}
|
|
||||||
type_ptr->flush();
|
|
||||||
type_ptr->stop();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return type_ptr->isRunning();
|
|
||||||
}
|
|
||||||
bool stop() noexcept override {
|
|
||||||
if (type_ptr->isRunning()) {
|
|
||||||
_running.store(false, std::memory_order_release);
|
|
||||||
if (_runningThread) {
|
|
||||||
_runningThread->join();
|
|
||||||
_runningThread = nullptr;
|
|
||||||
}
|
|
||||||
type_ptr->flush();
|
|
||||||
}
|
|
||||||
return !type_ptr->isRunning();
|
|
||||||
}
|
|
||||||
bool in(const DataType& val) override {
|
|
||||||
type_ptr->_buffer.push(val);
|
|
||||||
return type_ptr->isRunning();
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
#endif // _NB_DATASINK
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_ERROR
|
|
||||||
#define _NB_ERROR
|
|
||||||
|
|
||||||
#include <NBCore/ErrorsImpl.hpp>
|
|
||||||
#include <NBCore/Logger.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
#ifndef LOG_W_CODE_LOC
|
|
||||||
#define LOG_W_CODE_LOC(file, line, args...) nb::logger.msg(args, file, line)
|
|
||||||
#endif // LOG_W_CODE_LOC
|
|
||||||
#ifndef WARN_W_CODE_LOC
|
|
||||||
#define WARN_W_CODE_LOC(file, line, args...) nb::logger.warn(args, file, line)
|
|
||||||
#endif // WARN_W_CODE_LOC
|
|
||||||
#ifndef ERROR_W_CODE_LOC
|
|
||||||
#define ERROR_W_CODE_LOC(file, line, args...) nb::logger.error(args, file, line)
|
|
||||||
#endif // ERROR_W_CODE_LOC
|
|
||||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
|
||||||
#ifndef LOG
|
|
||||||
#define LOG(arg) LOG_W_CODE_LOC(__FILE__, __LINE__, arg)
|
|
||||||
#endif // LOG
|
|
||||||
#ifndef WARN
|
|
||||||
#define WARN(args...) WARN_W_CODE_LOC(__FILE__, __LINE__, args)
|
|
||||||
#endif // WARN
|
|
||||||
#ifndef ERROR
|
|
||||||
#define ERROR(arg) ERROR_W_CODE_LOC(__FILE__, __LINE__, arg)
|
|
||||||
#endif // ERROR
|
|
||||||
#else
|
|
||||||
#ifndef LOG
|
|
||||||
#define LOG(args) nb::logger.msg(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
|
|
||||||
#ifndef THROW_W_CODE_LOC
|
|
||||||
#define THROW_W_CODE_LOC(file, line, args...) ERROR_W_CODE_LOC(file, line, args); throw args
|
|
||||||
#endif // THROW_W_CODE_LOC
|
|
||||||
#else
|
|
||||||
#ifndef LOG
|
|
||||||
#define LOG(args)
|
|
||||||
#endif // LOG
|
|
||||||
#ifndef WARN
|
|
||||||
#define WARN(args...)
|
|
||||||
#endif // WARN
|
|
||||||
#ifndef ERROR
|
|
||||||
#define ERROR(args)
|
|
||||||
#endif // ERROR
|
|
||||||
#endif // _NB_AUTOLOG
|
|
||||||
|
|
||||||
#ifndef THROW
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
#define THROW(args...) THROW_W_CODE_LOC(__FILE__, __LINE__, args)
|
|
||||||
#else
|
|
||||||
#define THROW(args...) throw args
|
|
||||||
#endif // _NB_AUTOLOG
|
|
||||||
#endif // THROW
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
#endif // _NB_ERROR
|
|
||||||
@@ -1,171 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_ERRORS_IMPL
|
|
||||||
#define _NB_ERRORS_IMPL
|
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
#include <NBCore/StringUtils.hpp>
|
|
||||||
#include <NBCore/Utils.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
using ErrorCodeMap = ConstantMap<unsigned int, std::string>;
|
|
||||||
|
|
||||||
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 = trace->what();
|
|
||||||
ret += NEWLINE + indent_strblock(
|
|
||||||
trace_msg,
|
|
||||||
TABOVER,
|
|
||||||
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=NoneType>
|
|
||||||
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."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
using ErrorBase::ErrorBase;
|
|
||||||
|
|
||||||
Error(
|
|
||||||
unsigned int code_,
|
|
||||||
const ErrorBase& trace_
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages[code_],
|
|
||||||
ErrorType::type,
|
|
||||||
std::make_shared<ErrorBase>(trace_)
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(
|
|
||||||
std::string msg_,
|
|
||||||
const ErrorBase& trace_
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
ErrorType::Codes::UNDEFINED,
|
|
||||||
msg_,
|
|
||||||
ErrorType::type,
|
|
||||||
std::make_shared<ErrorBase>(trace_)
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(unsigned int code_) noexcept : ErrorBase(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages[code_],
|
|
||||||
ErrorType::type,
|
|
||||||
nullptr
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(std::string msg_) noexcept : ErrorBase(
|
|
||||||
ErrorType::Codes::UNDEFINED,
|
|
||||||
msg_,
|
|
||||||
ErrorType::type,
|
|
||||||
nullptr
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(
|
|
||||||
unsigned int code_,
|
|
||||||
const std::string& note_,
|
|
||||||
const ErrorBase& trace_
|
|
||||||
) noexcept : ErrorBase(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages[code_] + " (" + note_ + ")",
|
|
||||||
ErrorType::type,
|
|
||||||
std::make_shared<ErrorBase>(trace_)
|
|
||||||
) { check_asserts(); }
|
|
||||||
|
|
||||||
Error(unsigned int code_, const std::string& note_) noexcept : ErrorBase(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages[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>> {
|
|
||||||
protected:
|
|
||||||
using Base = Error<Error<NoneType>>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::what;
|
|
||||||
using Base::code;
|
|
||||||
using Base::trace;
|
|
||||||
using Base::Base;
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
STANDARD, UNDEFINED, OUT_OF_RANGE, VALUE_ERROR, OVERWRITE_ERROR
|
|
||||||
};
|
|
||||||
inline static const std::string type = "nb::Error";
|
|
||||||
inline static const ErrorCodeMap ErrorMessages = {
|
|
||||||
{STANDARD, "std::exception"},
|
|
||||||
{UNDEFINED, "Error"},
|
|
||||||
{OUT_OF_RANGE, "Out-of-range error"},
|
|
||||||
{VALUE_ERROR, "Invalid value"},
|
|
||||||
{OVERWRITE_ERROR, "Attempting to overwrite managed data"}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
Error(unsigned int code_=1) noexcept : Base(code_) {}
|
|
||||||
Error(const std::exception& err) : Base(
|
|
||||||
STANDARD,
|
|
||||||
std::string(err.what()),
|
|
||||||
"std::exception",
|
|
||||||
nullptr
|
|
||||||
) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
Error(Args...) -> Error<NoneType>;
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
#endif // _NB_ERRORS_IMPL
|
|
||||||
@@ -1,250 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_LOGGER
|
|
||||||
#define _NB_LOGGER
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <thread>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <NBCore/DataSink.hpp>
|
|
||||||
#include <NBCore/ErrorsImpl.hpp>
|
|
||||||
#include <NBCore/Printer.hpp>
|
|
||||||
#include <NBCore/Processes.hpp>
|
|
||||||
#include <NBCore/ThreadSafeQueue.hpp>
|
|
||||||
#include <NBCore/TypeTraits.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
class ErrorBase;
|
|
||||||
|
|
||||||
typedef std::chrono::time_point<
|
|
||||||
std::chrono::system_clock,
|
|
||||||
std::chrono::nanoseconds
|
|
||||||
> LoggerTimePoint;
|
|
||||||
|
|
||||||
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
|
|
||||||
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
|
|
||||||
|
|
||||||
template<typename LogType, typename Logger>
|
|
||||||
class LoggerBase : public MultithreadedDataProcessor<LogType, Logger>{
|
|
||||||
private:
|
|
||||||
Logger* const type_ptr = static_cast<LoggerType*>(this);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using LoggerType = Logger;
|
|
||||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
|
||||||
using SinkType = DataSink<LogType>;
|
|
||||||
using Base::process;
|
|
||||||
std::shared_ptr<SinkType> _logsink;
|
|
||||||
LoggerBase(std::shared_ptr<SinkType> sink) : _logsink(sink) {}
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::flush;
|
|
||||||
bool run() override {
|
|
||||||
_logsink->run();
|
|
||||||
return Base::run();
|
|
||||||
}
|
|
||||||
using Base::pause;
|
|
||||||
bool stop() noexcept override {
|
|
||||||
auto ret = Base::stop();
|
|
||||||
_logsink->stop();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
class LogEventHandler : public DataSink<LogEvent> {
|
|
||||||
protected:
|
|
||||||
using Base = DataSink<LogEvent>;
|
|
||||||
using Base::_running;
|
|
||||||
std::atomic<uint8_t> _logLevel;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::isRunning;
|
|
||||||
LogEventHandler(uint8_t logging_level=0x01);
|
|
||||||
virtual uint8_t logLevel(uint8_t);
|
|
||||||
virtual uint8_t logLevel() const;
|
|
||||||
bool stop() noexcept override;
|
|
||||||
bool run() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename LT>
|
|
||||||
class DebugLogger : public LoggerBase<LogEvent, LT>{
|
|
||||||
private:
|
|
||||||
LT* const type_ptr = static_cast<LT*>(this);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using LoggerType = LT;
|
|
||||||
using Base = LoggerBase<LogEvent, LoggerType>;
|
|
||||||
using SinkType = LogEventHandler;
|
|
||||||
using SinkPtr = std::shared_ptr<SinkType>;
|
|
||||||
using Distributor = MultiSink<LogEvent, LogEventHandler>;
|
|
||||||
using Base::_logsink;
|
|
||||||
using Base::process;
|
|
||||||
std::atomic<uint8_t> _loglvl;
|
|
||||||
virtual void write_message(
|
|
||||||
std::string msg,
|
|
||||||
uint8_t lvl=0x00,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
type_ptr->in(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
|
|
||||||
) {
|
|
||||||
this->write_message(std::string(msg), lvl, file, line);
|
|
||||||
}
|
|
||||||
void write_message(
|
|
||||||
const ErrorBase& err,
|
|
||||||
uint8_t lvl=0x00,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
this->write_message(err.what(), lvl, file, line);
|
|
||||||
}
|
|
||||||
template<typename U>
|
|
||||||
std::enable_if_t<std::is_integral_v<U>, void> write_message(
|
|
||||||
const U& val,
|
|
||||||
uint8_t lvl=0x00,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
this->write_message(std::to_string(val), lvl, file, line);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
DebugLogger(std::vector<SinkPtr> sinks={}) : Base(
|
|
||||||
std::static_pointer_cast<DataSink<LogEvent>>(std::make_shared<Distributor>())
|
|
||||||
) {
|
|
||||||
for (auto sink : sinks) {
|
|
||||||
addLogHandler(sink);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
~DebugLogger() { static_cast<LoggerType*>(this)->stop(); }
|
|
||||||
void addLogHandler(SinkPtr sink) {
|
|
||||||
auto multsink = std::static_pointer_cast<Distributor>(_logsink);
|
|
||||||
multsink->addSink(sink);
|
|
||||||
}
|
|
||||||
uint8_t minimalLogLevel() const {
|
|
||||||
return _loglvl.load(std::memory_order_acquire);
|
|
||||||
}
|
|
||||||
uint8_t minimalLogLevel(uint8_t level) {
|
|
||||||
_loglvl.store(level, std::memory_order_release);
|
|
||||||
auto multsink = std::static_pointer_cast<Distributor>(_logsink);
|
|
||||||
for (auto& sink : multsink->getSinks()) {
|
|
||||||
if (sink->logLevel() < level) {
|
|
||||||
sink->logLevel(level);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return minimalLogLevel();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename U>
|
|
||||||
void msg(
|
|
||||||
U val,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) { 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
|
|
||||||
) { this->write_message(val, lvl, file, line); }
|
|
||||||
|
|
||||||
void error(
|
|
||||||
const ErrorBase& val,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
this->write_message(val, 0xFF, file, line);
|
|
||||||
auto lock = this->pause();
|
|
||||||
this->flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
void error(
|
|
||||||
const std::string& val,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
this->write_message(Error<>(val), 0xFF, file, line);
|
|
||||||
auto lock = this->pause();
|
|
||||||
this->flush();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class DefaultTerminalLogEventPrinter : public LogEventHandler {
|
|
||||||
protected:
|
|
||||||
using Base = LogEventHandler;
|
|
||||||
using Base::_running;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::isRunning;
|
|
||||||
using Base::logLevel;
|
|
||||||
using Base::stop;
|
|
||||||
using Base::run;
|
|
||||||
using Base::Base;
|
|
||||||
bool in(const LogEvent&) override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
|
||||||
protected:
|
|
||||||
using LoggerType = DefaultDebugLogger;
|
|
||||||
using Base = DebugLogger<DefaultDebugLogger>;
|
|
||||||
using MultiLogSink = MultiSink<LogEvent>;
|
|
||||||
using SinkPtr = std::shared_ptr<LogEventHandler>;
|
|
||||||
using Base::_logsink;
|
|
||||||
using Base::write_message;
|
|
||||||
virtual bool process(const LogEvent& msg) override;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
using Base::msg;
|
|
||||||
using Base::warn;
|
|
||||||
using Base::error;
|
|
||||||
using Base::addLogHandler;
|
|
||||||
using Base::minimalLogLevel;
|
|
||||||
DefaultDebugLogger(std::vector<SinkPtr> sinks={}) : Base(sinks) {}
|
|
||||||
~DefaultDebugLogger() { stop(); }
|
|
||||||
|
|
||||||
friend class BufferedDataProcessor<LogEvent, ThreadsafeQueue<LogEvent>, DefaultDebugLogger>;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern const bool LOGGER_RUNNING;
|
|
||||||
|
|
||||||
extern const bool LOGGER_RUNNING;
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
extern DefaultDebugLogger logger;
|
|
||||||
#endif // _NB_AUTOLOG
|
|
||||||
|
|
||||||
// Taking Charge of Adult ADHD by Russell Barkley
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
#endif // _NB_LOGGER
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_PRINTER
|
|
||||||
#define _NB_PRINTER
|
|
||||||
|
|
||||||
#include <condition_variable>
|
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
|
||||||
#include <mutex>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
template<typename STREAM, typename PrinterType>
|
|
||||||
class Printer {
|
|
||||||
private:
|
|
||||||
PrinterType* const type_ptr = static_cast<PrinterType*>(this);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
STREAM& _stream;
|
|
||||||
Printer(STREAM* const stream_) : _stream(stream_) {}
|
|
||||||
|
|
||||||
public:
|
|
||||||
template<typename T>
|
|
||||||
PrinterType& operator<<(const T& val_) {
|
|
||||||
(*type_ptr) << val_;
|
|
||||||
return type_ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
class Terminal : public Printer<decltype(std::cout), Terminal> {
|
|
||||||
private:
|
|
||||||
Terminal* const type_ptr = static_cast<Terminal*>(this);
|
|
||||||
using MutexLock = std::lock_guard<std::mutex>;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base = Printer<decltype(std::cout), Terminal>;
|
|
||||||
using Base::_stream;
|
|
||||||
std::mutex _mutex;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
Terminal& operator<<(const T& val_) {
|
|
||||||
MutexLock lock(_mutex);
|
|
||||||
_stream << val_;
|
|
||||||
return *type_ptr;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
#endif // _NB_PRINTER
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_STRING_UTILS
|
|
||||||
#define _NB_STRING_UTILS
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
#include <NBCore/TypeTraits.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
#ifdef _NB_TARGET_WINDOWS
|
|
||||||
inline static const std::string NEWLINE = "\n";
|
|
||||||
inline static const std::wstring WNEWLINE = L"\n";
|
|
||||||
#endif // _NB_TARGET_WINDOWS
|
|
||||||
#ifdef _NB_TARGET_LINUX
|
|
||||||
inline const std::string NEWLINE = "\n";
|
|
||||||
inline const std::wstring WNEWLINE = L"\n";
|
|
||||||
#endif // _NB_TARGET_LINUX
|
|
||||||
|
|
||||||
const std::string TABOVER = " ";
|
|
||||||
|
|
||||||
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
|
|
||||||
@@ -1,203 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#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<typename T, unsigned int Arg>
|
|
||||||
using Const_t = std::integral_constant<T, Arg>;
|
|
||||||
|
|
||||||
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 A, typename B>
|
|
||||||
using SubtractType = decltype(std::declval<A>() - std::declval<B>());
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
using MultiplyType = decltype(std::declval<A>() * std::declval<B>());
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
using AdditionType = decltype(std::declval<A>() + std::declval<B>());
|
|
||||||
|
|
||||||
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<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));
|
|
||||||
ForEach<N+1, Func, Pack...>(tup, f);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
|
||||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
|
||||||
ForEach(std::tuple<Pack...>&, Func, const Args&) {}
|
|
||||||
|
|
||||||
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
|
||||||
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
|
||||||
ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
|
|
||||||
f(N, std::get<N>(tup), arg);
|
|
||||||
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, typename... Pack>
|
|
||||||
struct Find;
|
|
||||||
|
|
||||||
template<typename T, typename U, typename... Pack>
|
|
||||||
struct Find<T, U, Pack...> {
|
|
||||||
protected:
|
|
||||||
using here = Find<T, U>;
|
|
||||||
static constexpr unsigned int downstream = std::conditional_t<
|
|
||||||
here::value,
|
|
||||||
Const_t<unsigned int, 0>,
|
|
||||||
Find<T, Pack...>
|
|
||||||
>::value;
|
|
||||||
|
|
||||||
public:
|
|
||||||
static constexpr unsigned int value = std::conditional_t<
|
|
||||||
here::value,
|
|
||||||
Const_t<unsigned int, 1>,
|
|
||||||
std::conditional_t<
|
|
||||||
downstream,
|
|
||||||
Const_t<unsigned int, 1+downstream>,
|
|
||||||
Const_t<unsigned int, 0>
|
|
||||||
>
|
|
||||||
>::value;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T, typename U>
|
|
||||||
struct Find<T, U> {
|
|
||||||
public:
|
|
||||||
static constexpr unsigned int value = std::conditional_t<
|
|
||||||
std::is_base_of<T, U>::value,
|
|
||||||
std::integral_constant<unsigned int, 1>,
|
|
||||||
std::integral_constant<unsigned int, 0>
|
|
||||||
>::value;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, typename Pack1, typename... Pack>
|
|
||||||
struct RefPackToPtrVec {
|
|
||||||
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
|
|
||||||
: vec(_vec) {
|
|
||||||
static_assert(std::is_base_of<T, Pack1>::value);
|
|
||||||
vec.push_back(&p1);
|
|
||||||
vec = RefPackToPtrVec<T, Pack...>(vec, packs...).vec;
|
|
||||||
}
|
|
||||||
|
|
||||||
RefPackToPtrVec(Pack1& p1, Pack&... packs)
|
|
||||||
: RefPackToPtrVec({}, p1, packs...) {}
|
|
||||||
|
|
||||||
std::vector<T*> vec;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, typename Pack>
|
|
||||||
struct RefPackToPtrVec<T, Pack> {
|
|
||||||
RefPackToPtrVec(std::vector<T*> _vec, Pack& p1)
|
|
||||||
: vec(_vec) {
|
|
||||||
static_assert(std::is_base_of<T, Pack>::value);
|
|
||||||
vec.push_back(&p1);
|
|
||||||
}
|
|
||||||
|
|
||||||
RefPackToPtrVec(Pack& p1)
|
|
||||||
: RefPackToPtrVec({}, p1) {}
|
|
||||||
|
|
||||||
std::vector<T*> vec;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, typename... Pack>
|
|
||||||
struct PackIsSameType;
|
|
||||||
|
|
||||||
template <typename T, typename PackType>
|
|
||||||
struct PackIsSameType<T, PackType> {
|
|
||||||
const bool value = std::is_base_of<T, PackType>::value;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, typename FirstPack, typename... Pack>
|
|
||||||
struct PackIsSameType<T, FirstPack, Pack...> {
|
|
||||||
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
#endif // _NB_TYPE_TRAITS
|
|
||||||
@@ -1,135 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_CORE_TYPES
|
|
||||||
#define _NB_CORE_TYPES
|
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <memory>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
// #include <NBCore/Errors.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
class ConstantMap : public std::unordered_map<A, B> {
|
|
||||||
using Base = std::unordered_map<A, B>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
using Base::at;
|
|
||||||
const B& operator[](const A& key) const {
|
|
||||||
return at(key);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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(Error<NoneType>(
|
|
||||||
std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">.")
|
|
||||||
)); */
|
|
||||||
throw "byyyyeeee";
|
|
||||||
}
|
|
||||||
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
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#include "Errors.hpp"
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
const std::string Error<DefaultError>::type = "nb::Error";
|
||||||
|
|
||||||
|
const ErrorCodeMap Error<DefaultError>::ErrorMessages = {
|
||||||
|
{Error::Codes::GENERAL, "General std::exception."},
|
||||||
|
{Error::Codes::UNDEFINED, "Undefined / general error."},
|
||||||
|
{Error::Codes::BADERRORCODE, "Unrecognized error code."}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#include <NBCore/ErrorsImpl.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
//ErrorBase::
|
|
||||||
ErrorBase::ErrorBase(const std::exception& exception_) noexcept
|
|
||||||
: ErrorBase(Error(exception_)) {}
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,82 +1,15 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include <NBCore/Logger.hpp>
|
#include "Logger.hpp"
|
||||||
#include <NBCore/StringUtils.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
static bool RUN_LOGGER(nb::DefaultDebugLogger& logger_) {
|
nb::DefaultDebugLogger logger(std::cout);
|
||||||
logger_.run();
|
|
||||||
return logger_.isRunning();
|
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
||||||
|
return log.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _NB_AUTOLOG
|
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||||
DefaultDebugLogger logger({std::static_pointer_cast<LogEventHandler>(
|
|
||||||
std::make_shared<DefaultTerminalLogEventPrinter>(0x0)
|
|
||||||
)});
|
|
||||||
const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
|
||||||
#else
|
|
||||||
const bool LOGGER_RUNNING = false;
|
|
||||||
#endif // _NB_AUTOLOG
|
|
||||||
|
|
||||||
// class LogEventHandler
|
|
||||||
|
|
||||||
LogEventHandler::LogEventHandler(uint8_t logging_level) {
|
|
||||||
_logLevel.store(logging_level, std::memory_order_release);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LogEventHandler::stop() noexcept {
|
|
||||||
_running.store(false, std::memory_order_release);
|
|
||||||
return isRunning();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LogEventHandler::run() {
|
|
||||||
_running.store(true, std::memory_order_release);
|
|
||||||
return isRunning();
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t LogEventHandler::logLevel(uint8_t val) {
|
|
||||||
_logLevel.store(val, std::memory_order_release);
|
|
||||||
return logLevel();
|
|
||||||
}
|
|
||||||
uint8_t LogEventHandler::logLevel() const {
|
|
||||||
return _logLevel.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
// class DefaultTerminalLogEventPrinter
|
|
||||||
|
|
||||||
bool DefaultTerminalLogEventPrinter::in(const LogEvent& msg) {
|
|
||||||
if (!isRunning()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (msg.lvl < logLevel()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
constexpr size_t level_field_width = 5;
|
|
||||||
constexpr size_t msg_prompt_length = level_field_width+7;
|
|
||||||
std::stringstream formatted_stream;
|
|
||||||
formatted_stream << "[";
|
|
||||||
formatted_stream.width(level_field_width);
|
|
||||||
formatted_stream << std::to_string(msg.lvl) << "] :: ";
|
|
||||||
std::string fileloc="";
|
|
||||||
if (!msg.file.empty()) {
|
|
||||||
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
|
|
||||||
}
|
|
||||||
formatted_stream << indent_strblock(
|
|
||||||
fileloc + msg.msg,
|
|
||||||
std::string(20 - msg_prompt_length, ' '),
|
|
||||||
""
|
|
||||||
) << nb::NEWLINE;
|
|
||||||
std::string formatted_string = formatted_stream.str();
|
|
||||||
std::cout << formatted_string << std::flush;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// class DefaultDebugLogger
|
|
||||||
|
|
||||||
bool DefaultDebugLogger::process(const LogEvent& msg) {
|
|
||||||
return _logsink->in(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif // _NB_TARGET_LINUX
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
#include <NBCore/Processes.hpp>
|
#include "Processes.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
@@ -15,14 +15,7 @@ uint64_t GetPID() {
|
|||||||
return GetCurrentProcessId();
|
return GetCurrentProcessId();
|
||||||
}
|
}
|
||||||
#endif // _NB_TARGET_WINDOWS
|
#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
|
#ifdef _NB_TARGET_LINUX
|
||||||
#endif // _NB_TARGET_LINUX
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
#include <NBCore/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);
|
|
||||||
}
|
|
||||||
+20
-24
@@ -1,27 +1,23 @@
|
|||||||
#include <NBCore/Utils.hpp>
|
#include <string>
|
||||||
|
#include "Utils.hpp"
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
/* using ObjectManagerCodes = ObjectManagerError::Codes;
|
std::string nb::find_and_replace(const std::string& original, const std::string& find, const std::string& replace) {
|
||||||
const std::string ObjectManagerError::type = "nb::ObjectManagerError";
|
std::string ret = original;
|
||||||
const ErrorCodeMap ObjectManagerError::ErrorMessages({
|
|
||||||
{ObjectManagerCodes::UNDEFINED, "Error"},
|
std::size_t find_len = find.length();
|
||||||
{
|
std::size_t replace_len = replace.length();
|
||||||
ObjectManagerCodes::NO_MANAGER,
|
std::size_t currpos = 0;
|
||||||
"Attempting to create object lock without lock manager"
|
std::size_t lastpos = 0;
|
||||||
},
|
while(true) {
|
||||||
{
|
lastpos = currpos;
|
||||||
ObjectManagerCodes::MANAGER_MISMATCH,
|
currpos = original.find(find, lastpos);
|
||||||
"Attempting to delete object lock from mismatched lock manager"
|
if (currpos == std::string::npos) {
|
||||||
},
|
break;
|
||||||
{
|
|
||||||
ObjectManagerCodes::LOCK_OVERFLOW,
|
|
||||||
"Too many object locks allocated"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ObjectManagerCodes::BAD_THREAD,
|
|
||||||
"Attempting operation from a bad thread"
|
|
||||||
}
|
}
|
||||||
}); */
|
ret = ret.erase(currpos, find_len);
|
||||||
|
ret = ret.insert(currpos, replace);
|
||||||
}; // namespace nb
|
currpos += replace_len;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
@@ -12,13 +12,5 @@ if (NB_BUILD_TESTS)
|
|||||||
NBCore
|
NBCore
|
||||||
GTest::gtest_main
|
GTest::gtest_main
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(LoggerTest
|
|
||||||
./testLogger.cpp
|
|
||||||
)
|
|
||||||
target_link_libraries(LoggerTest
|
|
||||||
NBCore
|
|
||||||
)
|
|
||||||
|
|
||||||
gtest_discover_tests(TestCore)
|
gtest_discover_tests(TestCore)
|
||||||
endif()
|
endif()
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
#define CODE_ERROR_LOCATIONS
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
|
#include "Errors.hpp"
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <string>
|
#include <iostream>
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
|
||||||
|
|
||||||
using namespace nb;
|
using namespace nb;
|
||||||
|
|
||||||
@@ -34,6 +33,5 @@ const ErrorCodeMap TestError::ErrorMessages{
|
|||||||
|
|
||||||
|
|
||||||
TEST(ErrorTest, Test) {
|
TEST(ErrorTest, Test) {
|
||||||
auto err = TestError(0, TestError(1, TestError(2, TestError(3, TestError(2)))));
|
std::cout << TestError(0, TestError(1, TestError(2))).what();
|
||||||
ASSERT_STREQ(err.what().c_str(), "Hey!\n Trace: How\n Trace: You\n Trace: Doin\n Trace: You");
|
|
||||||
}
|
}
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
#define _NB_AUTOLOG
|
|
||||||
#include <exception>
|
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
|
||||||
#include <NBCore/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() {
|
|
||||||
while(!nb::LOGGER_RUNNING) {}
|
|
||||||
nb::logger.minimalLogLevel(0x0);
|
|
||||||
nb::logger.msg("Whoop!");
|
|
||||||
try {
|
|
||||||
THROW(TestError(0, TestError(1, TestError(2, TestError(3, TestError(2))))));
|
|
||||||
} catch (TestError e){
|
|
||||||
nb::logger.msg("nb::Error was thrown!");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
THROW(std::exception());
|
|
||||||
} catch (std::exception e){
|
|
||||||
nb::logger.msg("std::exception was thrown!");
|
|
||||||
}
|
|
||||||
ERROR("hello there!");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#define CODE_ERROR_LOCATIONS
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <NBCore/Processes.hpp>
|
#include "Processes.hpp"
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
TEST(ProcessesTest, GetPID) {
|
TEST(ProcessesTest, GetPID) {
|
||||||
|
|||||||
@@ -1,20 +1,17 @@
|
|||||||
#define CODE_ERROR_LOCATIONS
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
|
#include "Utils.hpp"
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <NBCore/TypeTraits.hpp>
|
|
||||||
#include <NBCore/StringUtils.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
TEST(UtilsTest, TestFindAndReplace) {
|
TEST(UtilsTest, Test) {
|
||||||
ASSERT_STREQ(
|
ASSERT_STREQ(
|
||||||
find_and_replace("Jeff", "e", "efe").c_str(),
|
find_and_replace("Jeff", "e", "efe").c_str(),
|
||||||
"Jefeff"
|
"Jefeff"
|
||||||
);
|
);
|
||||||
|
|
||||||
std::string tmp = find_and_replace("Naif", "a", "afa");
|
auto tmp = find_and_replace("Naif", "a", "afa");
|
||||||
ASSERT_STREQ(
|
ASSERT_STREQ(
|
||||||
find_and_replace(tmp, "i", "ifi").c_str(),
|
find_and_replace(tmp, "i", "ifi").c_str(),
|
||||||
"Nafaifif"
|
"Nafaifif"
|
||||||
@@ -41,108 +38,4 @@ TEST(UtilsTest, TestFindAndReplace) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
} // namespace nb
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
cmake_minimum_required(VERSION 3.10)
|
|
||||||
project(NBData VERSION 0.1)
|
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
|
||||||
include(CMakePackageConfigHelpers)
|
|
||||||
|
|
||||||
toAbsolutePath(NB_DATA_SOURCE
|
|
||||||
./src/FFT.cpp
|
|
||||||
)
|
|
||||||
toAbsolutePath(NB_DATA_INCLUDE
|
|
||||||
./include/NBData/FFT.hpp
|
|
||||||
)
|
|
||||||
|
|
||||||
set(NB_DATA_SOURCE ${NB_DATA_SOURCE} PARENT_SCOPE)
|
|
||||||
set(NB_DATA_INCLUDE ${NB_DATA_INCLUDE} PARENT_SCOPE)
|
|
||||||
|
|
||||||
add_library(NBData ${NB_DATA_SOURCE})
|
|
||||||
add_library(NBEngine::Data ALIAS NBData)
|
|
||||||
target_link_libraries(NBData
|
|
||||||
NBCore
|
|
||||||
)
|
|
||||||
target_include_directories(NBData
|
|
||||||
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
|
||||||
PUBLIC "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
|
||||||
)
|
|
||||||
export(
|
|
||||||
TARGETS NBData
|
|
||||||
FILE "${CMAKE_BINARY_DIR}/cmake/NBDataTargets.cmake"
|
|
||||||
NAMESPACE NBEngine::
|
|
||||||
)
|
|
||||||
configure_package_config_file(
|
|
||||||
"NBDataConfig.cmake.in"
|
|
||||||
"${CMAKE_BINARY_DIR}/cmake/NBDataConfig.cmake"
|
|
||||||
INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}/cmake"
|
|
||||||
PATH_VARS CMAKE_INSTALL_LIBDIR
|
|
||||||
)
|
|
||||||
write_basic_package_version_file(
|
|
||||||
"${CMAKE_BINARY_DIR}/cmake/NBDataConfigVersion.cmake"
|
|
||||||
COMPATIBILITY AnyNewerVersion
|
|
||||||
)
|
|
||||||
|
|
||||||
if (NBENGINE_INSTALL)
|
|
||||||
message("Installing NBData to ${CMAKE_INSTALL_PREFIX}")
|
|
||||||
install(
|
|
||||||
TARGETS NBData
|
|
||||||
EXPORT NBDataTargets
|
|
||||||
LIBRARY DESTINATION lib
|
|
||||||
ARCHIVE DESTINATION lib
|
|
||||||
INCLUDES DESTINATION include
|
|
||||||
)
|
|
||||||
install(
|
|
||||||
DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/NBData"
|
|
||||||
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
|
|
||||||
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
|
|
||||||
)
|
|
||||||
install(
|
|
||||||
EXPORT NBDataTargets
|
|
||||||
DESTINATION "${CMAKE_INSTALL_PREFIX}/cmake"
|
|
||||||
NAMESPACE NBEngine::
|
|
||||||
)
|
|
||||||
install(FILES
|
|
||||||
"${CMAKE_BINARY_DIR}/cmake/NBDataConfig.cmake"
|
|
||||||
"${CMAKE_BINARY_DIR}/cmake/NBDataConfigVersion.cmake"
|
|
||||||
DESTINATION "${CMAKE_INSTALL_PREFIX}/CMake"
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (NB_BUILD_TESTS)
|
|
||||||
add_subdirectory(./tests )
|
|
||||||
endif()
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
@PACKAGE_INIT@
|
|
||||||
include("${CMAKE_CURRENT_LIST_DIR}/NBDataTargets.cmake")
|
|
||||||
@@ -1,252 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_DATA_FFT
|
|
||||||
#define _NB_DATA_FFT
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
#include <complex>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
|
||||||
|
|
||||||
#ifndef PI_VALUE
|
|
||||||
#define PI_VALUE 3.1415926535
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*! @file FFT.hpp */
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
using namespace std::complex_literals;
|
|
||||||
|
|
||||||
constexpr double operator""_pi(long double val) {
|
|
||||||
return val*PI_VALUE;
|
|
||||||
}
|
|
||||||
constexpr double operator""_pi(unsigned long long val) {
|
|
||||||
return val*PI_VALUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr float operator""_pif(long double val) {
|
|
||||||
return val*PI_VALUE;
|
|
||||||
}
|
|
||||||
constexpr float operator""_pif(unsigned long long val) {
|
|
||||||
return val*PI_VALUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
bool isPowerOf2(const T& x) {
|
|
||||||
bool ret = false;
|
|
||||||
unsigned int mask=1;
|
|
||||||
for (int i=0; i<(8*sizeof(std::declval<T>())); ++i) {ret ^= x&mask; mask<<=1;}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief Calculates the complex-valued twiddle factor given the `phase`
|
|
||||||
@param phase A double representing the phase, or `(k*n)/N`, of a DFT,
|
|
||||||
in units of 2*Pi.
|
|
||||||
@return A std::complex<T> representing the complex twiddle factor of
|
|
||||||
the DFT at that `phase`, calculated via `exp(-2*Pi*i*phase)`.
|
|
||||||
|
|
||||||
Calculates the complex-valued twiddle factor given the `phase` in units
|
|
||||||
of the ratio around the circle (i.e. in units of 2*Pi), calculated via
|
|
||||||
`exp(-2*Pi*i*phase)`. Note the minus sign, as this function is for the
|
|
||||||
*forward* DFT.
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
std::complex<T> calculateTwiddleFactor(double phase) {
|
|
||||||
int sign = -1;
|
|
||||||
while(phase>0.5) { phase-=0.5; sign*=-1; }
|
|
||||||
return static_cast<std::complex<T>>(std::exp(2_pi*(sign)*(1i)*phase));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Stores the twiddle factors for Radix-2 DFT matrices for a given data type
|
|
||||||
(i.e. stores std::complex<T> values), which can be retrieved using `TwiddleFactor<T>::get`.
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
class TwiddleFactors2 {
|
|
||||||
protected:
|
|
||||||
static std::unordered_map<unsigned int, std::complex<T>> factors;
|
|
||||||
|
|
||||||
public:
|
|
||||||
/*!
|
|
||||||
@brief Retrieves the twiddle factor for a Radix-2 DFT
|
|
||||||
@param numerator The numerator (i.e. `k*n` of a DFT matrix) part of the phase
|
|
||||||
when defined in units of 2*Pi.
|
|
||||||
@param log2N The `log2(denominator)` (i.e. `/N` of a DFT matrix) part of the phase
|
|
||||||
when defined in units of 2*Pi.
|
|
||||||
@return A std::complex<T> for the value of the `numerator / (2^log2N)` twiddle factor
|
|
||||||
*/
|
|
||||||
static std::complex<T> get(unsigned int numerator, unsigned int log2N);
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::complex<T>* fft2_impl(
|
|
||||||
const std::complex<T>* source,
|
|
||||||
std::complex<T>* dest,
|
|
||||||
unsigned int log2N,
|
|
||||||
unsigned int stride=1
|
|
||||||
) {
|
|
||||||
constexpr std::complex<T> J(0, 1);
|
|
||||||
switch(log2N) {
|
|
||||||
case 0:
|
|
||||||
dest[0] = source[0];
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
dest[0] = source[0] + source[stride];
|
|
||||||
dest[1] = source[0] - source[stride];
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
dest[0] = source[0] + source[stride] + source[2*stride] + source[3*stride];
|
|
||||||
dest[1] = source[0] - J*source[stride] - source[2*stride] + J*source[3*stride];
|
|
||||||
dest[2] = source[0] - source[stride] + source[2*stride] - source[3*stride];
|
|
||||||
dest[3] = source[0] + J*source[stride] - source[2*stride] - J*source[3*stride];
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
const unsigned int halfway = 1<<(log2N-1);
|
|
||||||
fft2_impl(source, dest, log2N-1, 2*stride);
|
|
||||||
fft2_impl(&(source[stride]), &(dest[halfway]), log2N-1, 2*stride);
|
|
||||||
std::complex<T> twiddle;
|
|
||||||
std::complex<T> p, q;
|
|
||||||
for (unsigned int i=0; i < halfway; ++i) {
|
|
||||||
twiddle = TwiddleFactors2<T>::get(i, log2N);
|
|
||||||
p = dest[i];
|
|
||||||
q = dest[halfway+i]*twiddle;
|
|
||||||
dest[i] = p+q;
|
|
||||||
dest[halfway+i] = p-q;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return dest;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::complex<T>* dft_impl(
|
|
||||||
const std::complex<T>* source,
|
|
||||||
std::complex<T>* dest,
|
|
||||||
unsigned int N
|
|
||||||
) {
|
|
||||||
for (int k=0; k < N; ++k) {
|
|
||||||
dest[k] = 0;
|
|
||||||
for (int n = 0; n < N; ++n) {
|
|
||||||
dest[k] += source[n] * calculateTwiddleFactor<T>(double(n*k)/double(N));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dest;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief Computes the minimal Radix-2 DFT on a std::vector<std::complex<T>>
|
|
||||||
@param values A std::vector<std::complex<T>> representing the time domain values
|
|
||||||
@return A std::vector<std::complex<T>>, representing the minimal Radix-2 DFT of `values`
|
|
||||||
|
|
||||||
Computes the Radix-2 DFT on a std::vector<std::complex<T>>. If the input vector
|
|
||||||
`values` is not a power of 2, then the number of points in the DFT (and thus the
|
|
||||||
size of the returned frequency domain vector) is determined by
|
|
||||||
`2^ceil(log2(values.size()))`
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
std::vector<std::complex<T>> fft2(const std::vector<std::complex<T>>& values) {
|
|
||||||
const auto N = values.size();
|
|
||||||
unsigned int log2N=0;
|
|
||||||
while(1<<log2N < N) {log2N++;}
|
|
||||||
std::vector<std::complex<T>> temp(1<<log2N, 0);
|
|
||||||
std::vector<std::complex<T>> ret = temp;
|
|
||||||
for (int i=0; i < N; ++i) { temp[i] = values[i]; }
|
|
||||||
std::complex<T>* fft_results = fft2_impl(temp.data(), ret.data(), log2N);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief Computes the minimal Radix-2 DFT on a `std::vector<T>`
|
|
||||||
@param values A std::vector<T> representing the time domain values with packed real and
|
|
||||||
imaginary parts
|
|
||||||
@return A `std::vector<T>`, representing the minimal Radix-2 DFT of `values` with packed
|
|
||||||
real and imaginary parts
|
|
||||||
@throw std::string Thrown when the size of the vector is not divisible by 2 (i.e.
|
|
||||||
there are unmatched real/imaginary pairs).
|
|
||||||
|
|
||||||
Computes the Radix-2 DFT on a std::vector<std::complex<T>>. Equivalent to
|
|
||||||
`fft2(const std::vector<std::complex<T>>)` if `values[2*i]` and `values[2*i+1]`
|
|
||||||
were mapped to the real and imaginary parts of the complex vector.
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
std::vector<T> fft2(const std::vector<T>& values) {
|
|
||||||
if (values.size()&1) {
|
|
||||||
THROW(Error<>(Error<>::VALUE_ERROR,
|
|
||||||
"Vector must be of even length to represent complex values."
|
|
||||||
));
|
|
||||||
}
|
|
||||||
std::complex<T> complex_input(values.data(), values.size()/2);
|
|
||||||
std::complex<T> complex_ret = fft2(complex_input);
|
|
||||||
return std::vector<T>(complex_ret.data(), complex_ret.size()*2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief Computes the DFT on a std::vector<std::complex<T>>
|
|
||||||
@param values A std::vector<std::complex<T>> representing the time domain values
|
|
||||||
@return A std::vector<std::complex<T>> of the DFT of `values` in the frequency domain
|
|
||||||
|
|
||||||
Computes the DFT of the std::vector<std::complex<T>> `values`. Does so using the
|
|
||||||
naive O(N^2) matrix-multiplication method.
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
std::vector<std::complex<T>> dft(const std::vector<std::complex<T>>& x) {
|
|
||||||
std::vector<std::complex<T>> ret = x;
|
|
||||||
std::complex<T>* fft_results = dft_impl(x.data(), ret.data(), x.size());
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief Computes the DFT on a `std::vector<T>`
|
|
||||||
@param values A `std::vector<T>` representing the time domain values with packed real and
|
|
||||||
imaginary parts
|
|
||||||
@return A `std::vector<T>` of the DFT of `values` in the frequency domain with
|
|
||||||
packed real and imaginary parts
|
|
||||||
@throw std::string Thrown when the size of the vector is not divisible by 2 (i.e.
|
|
||||||
there are unmatched real/imaginary pairs).
|
|
||||||
|
|
||||||
Computes the DFT on a `std::vector<T>`. Equivalent to
|
|
||||||
`dft(const std::vector<std::complex<T>>)` if `values[2*i]` and `values[2*i+1]`
|
|
||||||
were mapped to the real and imaginary parts of the complex vector.
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
std::vector<T> dft(const std::vector<T>& values) {
|
|
||||||
if (values.size()&1) {
|
|
||||||
THROW(Error<>(Error<>::VALUE_ERROR,
|
|
||||||
"Vector must be of even length to represent complex values."
|
|
||||||
));
|
|
||||||
}
|
|
||||||
std::complex<T> complex_input(values.data(), values.size()/2);
|
|
||||||
std::complex<T> complex_ret = dft(complex_input);
|
|
||||||
return std::vector<T>(complex_ret.data(), complex_ret.size()*2);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::unordered_map<unsigned int, std::complex<T>> TwiddleFactors2<T>::factors = {};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::complex<T> TwiddleFactors2<T>::get(unsigned int a, unsigned int log2N) {
|
|
||||||
while(a && !(a&1)) { a>>=1; log2N--; }
|
|
||||||
const unsigned int N = 1<<log2N;
|
|
||||||
const double phase = double(a)/double(N);
|
|
||||||
const unsigned int key = a*N;
|
|
||||||
try {
|
|
||||||
return factors.at(key);
|
|
||||||
} catch (const std::out_of_range& e) {
|
|
||||||
if (a > N>>1 ) {
|
|
||||||
return (factors[key] = std::conj(TwiddleFactors2<T>::get(N-a, log2N)));
|
|
||||||
} else {
|
|
||||||
return (factors[key] = calculateTwiddleFactor<T>(phase));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace n
|
|
||||||
|
|
||||||
#endif // _NB_DATA_FFT
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#include <NBCore/Errors.hpp>
|
|
||||||
#include <NBData/FFT.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
cmake_minimum_required(VERSION 3.26.0)
|
|
||||||
|
|
||||||
if (NB_BUILD_TESTS)
|
|
||||||
enable_testing()
|
|
||||||
include(GoogleTest)
|
|
||||||
|
|
||||||
add_executable(TestData
|
|
||||||
./testFFT.cpp
|
|
||||||
)
|
|
||||||
target_link_libraries(TestData
|
|
||||||
NBData
|
|
||||||
GTest::gtest_main
|
|
||||||
)
|
|
||||||
gtest_discover_tests(TestData)
|
|
||||||
|
|
||||||
add_executable(FFTSpeedTest
|
|
||||||
./fftspeedtest.cpp
|
|
||||||
)
|
|
||||||
target_link_libraries(FFTSpeedTest
|
|
||||||
NBData
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
#include <NBData/FFT.hpp>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
#include <gtest/gtest.h>
|
|
||||||
|
|
||||||
#include <NBData/ComplexNumbers.hpp>
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
void COMPLEX_EQ(const nb::Complex<A>& a, const nb::Complex<B>& b) {
|
|
||||||
ASSERT_EQ(a.r, b.r);
|
|
||||||
ASSERT_EQ(a.i, b.i);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
void COMPLEX_ALMOST_EQ(const nb::Complex<A>& a, const nb::Complex<B>& b) {
|
|
||||||
ASSERT_FLOAT_EQ(a.r, b.r);
|
|
||||||
ASSERT_FLOAT_EQ(a.i, b.i);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(ComplexNumberTests, TestConstruction) {
|
|
||||||
COMPLEX_ALMOST_EQ(nb::Complex(), nb::Complex(0,0));
|
|
||||||
COMPLEX_ALMOST_EQ(nb::Complex(5.0), nb::Complex(5, 0));
|
|
||||||
COMPLEX_ALMOST_EQ(1.5_j, nb::Complex(0.0, 1.5));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(ComplexNumberTests, TestAdd) {
|
|
||||||
auto x = nb::Complex(1.0) - nb::Complex(2.0, 1.0);
|
|
||||||
ASSERT_FLOAT_EQ(x.r, -1);
|
|
||||||
ASSERT_FLOAT_EQ(x.i, -1);
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
#include <gtest/gtest.h>
|
|
||||||
|
|
||||||
#include <NBData/FFT.hpp>
|
|
||||||
|
|
||||||
using namespace nb;
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
void COMPLEX_EQ(const std::complex<A>& a, const std::complex<B>& b) {
|
|
||||||
ASSERT_EQ(a.real(), b.real());
|
|
||||||
ASSERT_EQ(a.imag(), b.imag());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
void COMPLEX_FLOAT_EQ(
|
|
||||||
const std::complex<A>& a,
|
|
||||||
const std::complex<B>& b,
|
|
||||||
const long double epsilon=0
|
|
||||||
) {
|
|
||||||
if (epsilon) {
|
|
||||||
ASSERT_NEAR(a.real(), b.real(), epsilon);
|
|
||||||
ASSERT_NEAR(a.imag(), b.imag(), epsilon);
|
|
||||||
} else {
|
|
||||||
ASSERT_FLOAT_EQ(a.real(), b.real());
|
|
||||||
ASSERT_FLOAT_EQ(a.imag(), b.imag());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
void COMPLEX_EQ_VEC(
|
|
||||||
const std::vector<std::complex<A>>& a,
|
|
||||||
const std::vector<std::complex<A>>& b
|
|
||||||
) {
|
|
||||||
if (a.size() != b.size()) { FAIL(); }
|
|
||||||
for (int i = 0; i < a.size(); ++i) {
|
|
||||||
COMPLEX_EQ(a[i], b[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename A, typename B>
|
|
||||||
void COMPLEX_FLOAT_EQ_VEC(
|
|
||||||
const std::vector<std::complex<A>>& a,
|
|
||||||
const std::vector<std::complex<B>>& b,
|
|
||||||
const long double epsilon
|
|
||||||
) {
|
|
||||||
if (a.size() != b.size()) { FAIL(); }
|
|
||||||
for (int i = 0; i < a.size(); ++i) {
|
|
||||||
COMPLEX_FLOAT_EQ(a[i], b[i], epsilon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(FFTTest, TestFFT) {
|
|
||||||
std::vector<std::vector<std::complex<double>>> input = {
|
|
||||||
{1.0, 0.5, 0},
|
|
||||||
{1,1,1,1,1,1,1,1},
|
|
||||||
|
|
||||||
};
|
|
||||||
std::vector<std::vector<std::complex<double>>> expected = {
|
|
||||||
{1.5,1.f-0.5if,0.5,1.f+0.5if},
|
|
||||||
{8, 0, 0, 0, 0, 0, 0, 0}
|
|
||||||
};
|
|
||||||
for (int i = 0; i < input.size(); ++i) {
|
|
||||||
COMPLEX_FLOAT_EQ_VEC(
|
|
||||||
nb::fft2(input[i]),
|
|
||||||
expected[i],
|
|
||||||
1e-10
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include <NBCore/Events.hpp>
|
#include "Events.hpp"
|
||||||
|
|
||||||
namespace NB {
|
namespace NB {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user