Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9628a75710 | ||
|
|
5415a01885 | ||
|
|
abbae156a2 | ||
|
|
e4b164f29a | ||
|
|
cff7164d27 | ||
|
|
13ef940b2d | ||
|
|
58121bdf28 | ||
|
|
0f62aecfa3 | ||
|
|
40cf079e9d | ||
|
|
fb695798e7 | ||
|
|
e1c456bfe4 | ||
|
|
e23e062e58 | ||
|
|
fd7d36e4be | ||
|
|
0b473c0f8a |
+63
-12
@@ -7,6 +7,10 @@ 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 "")
|
||||||
@@ -28,12 +32,42 @@ 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_LOGGING ON)
|
set(NB_DEBUG_BUILD 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)
|
||||||
@@ -55,7 +89,6 @@ 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)
|
||||||
@@ -64,19 +97,16 @@ if (NB_LOGGING)
|
|||||||
add_compile_definitions(_NB_CODE_ERROR_LOCATIONS)
|
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)
|
||||||
elseif (NB_TARGET_LINUX)
|
endif()
|
||||||
|
if(NB_TARGET_LINUX)
|
||||||
add_compile_definitions(_NB_TARGET_LINUX)
|
add_compile_definitions(_NB_TARGET_LINUX)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# External Dep paths
|
get_filename_component(NBENGINE_ROOT ${CMAKE_CURRENT_LIST_DIR} ABSOLUTE)
|
||||||
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)
|
add_subdirectory(./engine ${PROJECT_BINARY_DIR}/${CMAKE_BUILD_TYPE})
|
||||||
|
|
||||||
# Toggle
|
# Toggle
|
||||||
set(NB_REBUILD_DOCS)
|
set(NB_REBUILD_DOCS)
|
||||||
@@ -84,3 +114,24 @@ 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()
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
@PACKAGE_INIT@
|
||||||
|
set(NBENGINE_PACKAGES
|
||||||
|
NBCore
|
||||||
|
NBGraphics
|
||||||
|
)
|
||||||
|
foreach(NBENGINE_LIB ${NBENGINE_PACKAGES})
|
||||||
|
include("${CMAKE_CURRENT_LIST_DIR}/${NBENGINE_LIB}Config.cmake")
|
||||||
|
endforeach()
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 58 KiB |
+21
-9
@@ -1,18 +1,30 @@
|
|||||||
include_directories(./.)
|
|
||||||
|
|
||||||
add_subdirectory(./NBCore)
|
add_subdirectory(./NBCore)
|
||||||
add_subdirectory(./NBEvents)
|
|
||||||
add_subdirectory(./NBGraphics)
|
|
||||||
|
|
||||||
if (NB_CORE_SOURCE OR NB_EVENTS_SOURCE OR NB_GRAPHICS_SOURCE)
|
get_target_property(NBCORE_INTERFACE_INCLUDES NBCore INTERFACE_INCLUDE_DIRECTORIES)
|
||||||
|
include_directories(${NBCORE_INTERFACE_INCLUDES})
|
||||||
|
|
||||||
|
#add_subdirectory(./NBEvents)
|
||||||
|
add_subdirectory(./NBGraphics)
|
||||||
|
add_subdirectory(./NBData)
|
||||||
|
|
||||||
|
if (NB_CORE_SOURCE OR NB_EVENTS_SOURCE OR NB_GRAPHICS_SOURCE OR NB_DATA_SOURCE)
|
||||||
set(NB_SOURCE_FILES "")
|
set(NB_SOURCE_FILES "")
|
||||||
list(APPEND NB_SOURCE_FILES ${NB_CORE_SOURCE} ${NB_GRAPHICS_SOURCE} ${NB_EVENTS_SOURCE})
|
list(APPEND NB_SOURCE_FILES
|
||||||
|
${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 ${NB_CORE_INCLUDE} ${NB_GRAPHICS_INCLUDE} ${NB_EVENTS_INCLUDE})
|
list(APPEND NB_INCLUDE_FILES
|
||||||
|
${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,31 +1,86 @@
|
|||||||
|
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/Errors.cpp
|
./src/ErrorsImpl.cpp
|
||||||
./src/Logger.cpp
|
./src/Logger.cpp
|
||||||
./src/Processes.cpp
|
./src/Processes.cpp
|
||||||
./src/StringUtils.cpp
|
./src/StringUtils.cpp
|
||||||
./src/Utils.cpp
|
./src/Utils.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
toAbsolutePath(NB_CORE_INCLUDE
|
toAbsolutePath(NB_CORE_INCLUDE
|
||||||
./ANSITerm.hpp
|
./include/NBCore/ANSITerm.hpp
|
||||||
./DataSink.hpp
|
./include/NBCore/DataSink.hpp
|
||||||
./Errors.hpp
|
./include/NBCore/Errors.hpp
|
||||||
./Logger.hpp
|
./include/NBCore/ErrorsImpl.hpp
|
||||||
./Processes.hpp
|
./include/NBCore/Logger.hpp
|
||||||
./StringUtils.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
|
||||||
|
)
|
||||||
|
|
||||||
target_include_directories(NBCore PUBLIC ./.)
|
if (NBENGINE_INSTALL)
|
||||||
|
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()
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_DATASINK
|
|
||||||
#define _NB_DATASINK
|
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#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;
|
|
||||||
|
|
||||||
virtual bool stop() noexcept override { return type_ptr->stop(); }
|
|
||||||
virtual bool run() override { return type_ptr->run(); }
|
|
||||||
virtual bool in(const DataType& val) override { return type_ptr->in(val); }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
unsigned int count() const {
|
|
||||||
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() override {
|
|
||||||
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
|
|
||||||
@@ -1,280 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_ERROR
|
|
||||||
#define _NB_ERROR
|
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
#include <type_traits>
|
|
||||||
#include "TypeTraits.hpp"
|
|
||||||
#include <unordered_map>
|
|
||||||
#include "StringUtils.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
constexpr bool IsValidException_v = std::is_base_of_v<std::exception, T>;
|
|
||||||
template <typename T>
|
|
||||||
using IsValidException = std::enable_if_t<IsValidException_v<T>, bool>;
|
|
||||||
|
|
||||||
template<class ErrorType=NoneType>
|
|
||||||
class ErrorBase;
|
|
||||||
|
|
||||||
template <class ErrorType=NoneType>
|
|
||||||
class Error;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
constexpr bool IsValidNBError_v = std::is_base_of_v<ErrorBase<NoneType>, T>;
|
|
||||||
template <typename T>
|
|
||||||
using IsValidNBError = std::enable_if_t<IsValidNBError_v<T>, bool>;
|
|
||||||
|
|
||||||
|
|
||||||
template<>
|
|
||||||
class ErrorBase<NoneType> {
|
|
||||||
public:
|
|
||||||
const unsigned int code;
|
|
||||||
const std::string msg;
|
|
||||||
const std::string type;
|
|
||||||
const std::shared_ptr<const void> trace;
|
|
||||||
const bool traceIsNBError;
|
|
||||||
|
|
||||||
virtual std::string what() const noexcept = 0;
|
|
||||||
virtual std::shared_ptr<ErrorBase> make_shared() const noexcept = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
template<typename T=NoneType>
|
|
||||||
ErrorBase(const ErrorBase<T>& err) : ErrorBase(std::move(err)) {}
|
|
||||||
|
|
||||||
template<typename T=NoneType>
|
|
||||||
ErrorBase(ErrorBase<T>&& err) : ErrorBase(
|
|
||||||
err.code,
|
|
||||||
err.msg,
|
|
||||||
err.type,
|
|
||||||
(err.traceIsNBError)
|
|
||||||
? static_cast<const ErrorBase*>(err.trace.get())
|
|
||||||
: static_cast<const std::exception*>(err.trace.get())
|
|
||||||
) {}
|
|
||||||
|
|
||||||
ErrorBase(
|
|
||||||
unsigned int code_,
|
|
||||||
std::string msg_,
|
|
||||||
std::string type_,
|
|
||||||
const std::exception* trace_
|
|
||||||
) noexcept :
|
|
||||||
code(code_),
|
|
||||||
msg(msg_),
|
|
||||||
type(type_),
|
|
||||||
trace{ trace_ ?
|
|
||||||
std::static_pointer_cast<const void>(
|
|
||||||
std::make_shared<Error<NoneType>>(*trace_))
|
|
||||||
: nullptr,
|
|
||||||
},
|
|
||||||
traceIsNBError(false)
|
|
||||||
{}
|
|
||||||
|
|
||||||
template<typename T, std::enable_if_t<IsValidNBError_v<T>, bool> = true>
|
|
||||||
ErrorBase(
|
|
||||||
unsigned int code_,
|
|
||||||
std::string msg_,
|
|
||||||
std::string type_,
|
|
||||||
const T* trace_
|
|
||||||
) noexcept :
|
|
||||||
code(code_),
|
|
||||||
msg(msg_),
|
|
||||||
type(type_),
|
|
||||||
trace{trace_ ? trace_->make_shared() : nullptr},
|
|
||||||
traceIsNBError(true)
|
|
||||||
{}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template <class ErrorType>
|
|
||||||
class ErrorBase : public ErrorBase<NoneType> {
|
|
||||||
private:
|
|
||||||
using Base = ErrorBase<NoneType>;
|
|
||||||
void inline check_asserts() {
|
|
||||||
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."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
template <typename T>
|
|
||||||
ErrorBase(const ErrorBase<T>& cpy) : Base(cpy) { check_asserts(); }
|
|
||||||
|
|
||||||
virtual std::shared_ptr<Base> make_shared() const noexcept override {
|
|
||||||
return std::static_pointer_cast<Base>(
|
|
||||||
std::make_shared<ErrorBase>(*this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual std::string what() const noexcept override {
|
|
||||||
std::string ret = msg;
|
|
||||||
if (trace) {
|
|
||||||
std::string trace_msg;
|
|
||||||
if (traceIsNBError) {
|
|
||||||
trace_msg = std::static_pointer_cast<const Base>(trace)->what();
|
|
||||||
} else {
|
|
||||||
trace_msg = std::string(std::static_pointer_cast<const Base>(trace)->what());
|
|
||||||
}
|
|
||||||
ret += nb::NEWLINE + indent_strblock(
|
|
||||||
trace_msg,
|
|
||||||
nb::TABOVER,
|
|
||||||
nb::TABOVER+"Trace: "
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const std::string type;
|
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
|
|
||||||
using Base::code;
|
|
||||||
using Base::msg;
|
|
||||||
using Base::trace;
|
|
||||||
using Base::traceIsNBError;
|
|
||||||
|
|
||||||
friend ErrorType;
|
|
||||||
friend Error<ErrorType>;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
ErrorBase(
|
|
||||||
unsigned int code_,
|
|
||||||
std::string msg_,
|
|
||||||
std::string type_,
|
|
||||||
const T* trace_
|
|
||||||
) : Base(
|
|
||||||
code_,
|
|
||||||
msg_,
|
|
||||||
type_,
|
|
||||||
trace_
|
|
||||||
) { check_asserts(); }
|
|
||||||
};
|
|
||||||
template <typename ErrorType>
|
|
||||||
const std::string ErrorBase<ErrorType>::type = ErrorType::type;
|
|
||||||
template <typename ErrorType>
|
|
||||||
const ErrorCodeMap ErrorBase<ErrorType>::ErrorMessages = ErrorType::ErrorMessages;
|
|
||||||
|
|
||||||
|
|
||||||
template <class ErrorType>
|
|
||||||
class Error : public ErrorBase<ErrorType> {
|
|
||||||
using Base = ErrorBase<ErrorType>;
|
|
||||||
public:
|
|
||||||
template <typename T>
|
|
||||||
Error(
|
|
||||||
unsigned int code_,
|
|
||||||
const T& trace_
|
|
||||||
) noexcept : Base(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages.at(code_),
|
|
||||||
ErrorType::type,
|
|
||||||
&trace_
|
|
||||||
) {}
|
|
||||||
|
|
||||||
template<typename ET>
|
|
||||||
Error(
|
|
||||||
std::string msg_,
|
|
||||||
const ErrorBase<ET>& trace_
|
|
||||||
) noexcept : Base(
|
|
||||||
0,
|
|
||||||
msg_,
|
|
||||||
ErrorType::type,
|
|
||||||
&trace_
|
|
||||||
) {}
|
|
||||||
|
|
||||||
Error(
|
|
||||||
std::string msg_,
|
|
||||||
const std::exception& trace_
|
|
||||||
) noexcept : Base(
|
|
||||||
0,
|
|
||||||
msg_,
|
|
||||||
ErrorType::type,
|
|
||||||
&trace_
|
|
||||||
) {}
|
|
||||||
|
|
||||||
Error(unsigned int code_) noexcept : Base(
|
|
||||||
code_,
|
|
||||||
ErrorType::ErrorMessages.at(code_),
|
|
||||||
ErrorType::type,
|
|
||||||
NULLPTR<std::exception>
|
|
||||||
) {}
|
|
||||||
|
|
||||||
Error(std::string msg_) noexcept : Base(
|
|
||||||
0,
|
|
||||||
msg_,
|
|
||||||
ErrorType::type,
|
|
||||||
NULLPTR<std::exception>
|
|
||||||
) {}
|
|
||||||
|
|
||||||
using Base::what;
|
|
||||||
using Base::code;
|
|
||||||
using Base::msg;
|
|
||||||
using Base::trace;
|
|
||||||
using Base::traceIsNBError;
|
|
||||||
using Base::type;
|
|
||||||
using Base::ErrorMessages;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<>
|
|
||||||
class Error<NoneType> : public ErrorBase<Error<NoneType>> {
|
|
||||||
using Base = ErrorBase<Error<NoneType>>;
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
Error(unsigned int code_=1) noexcept : Base(
|
|
||||||
code_,
|
|
||||||
ErrorMessages.at(code_),
|
|
||||||
type,
|
|
||||||
NULLPTR<std::exception>
|
|
||||||
) {}
|
|
||||||
|
|
||||||
Error(const std::exception& err) : Base(
|
|
||||||
Codes::STANDARD,
|
|
||||||
std::string(err.what()),
|
|
||||||
"std::exception",
|
|
||||||
nullptr
|
|
||||||
) {}
|
|
||||||
|
|
||||||
Error(std::string msg_) noexcept : Base(
|
|
||||||
Codes::UNDEFINED,
|
|
||||||
msg_,
|
|
||||||
type,
|
|
||||||
NULLPTR<std::exception>
|
|
||||||
) {}
|
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
STANDARD, UNDEFINED
|
|
||||||
};
|
|
||||||
|
|
||||||
using Base::what;
|
|
||||||
|
|
||||||
using Base::code;
|
|
||||||
using Base::msg;
|
|
||||||
using Base::trace;
|
|
||||||
using Base::traceIsNBError;
|
|
||||||
static const std::string type;
|
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
#endif // _NB_ERROR
|
|
||||||
@@ -1,269 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_LOGGER
|
|
||||||
#define _NB_LOGGER
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <ostream>
|
|
||||||
#include <thread>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "DataSink.hpp"
|
|
||||||
#include "Errors.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;
|
|
||||||
|
|
||||||
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() override {
|
|
||||||
if (!static_cast<LoggerType*>(this)->isRunning()) {
|
|
||||||
this->_running = true;
|
|
||||||
this->_runningThread = std::make_shared<std::thread>([&]{
|
|
||||||
while(static_cast<LoggerType*>(this)->isRunning()) {
|
|
||||||
static_cast<LoggerType*>(this)->flush();
|
|
||||||
}
|
|
||||||
static_cast<LoggerType*>(this)->flush();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return static_cast<LoggerType*>(this)->isRunning();
|
|
||||||
}
|
|
||||||
protected:
|
|
||||||
LoggerBase() = default;
|
|
||||||
|
|
||||||
StreamType _ostream;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct LogEvent{
|
|
||||||
const LoggerTimePoint time;
|
|
||||||
const unsigned char lvl;
|
|
||||||
const std::string msg;
|
|
||||||
const std::thread::id tid;
|
|
||||||
const uint64_t pid;
|
|
||||||
const std::string file="";
|
|
||||||
const unsigned int line=0;
|
|
||||||
};
|
|
||||||
|
|
||||||
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() { static_cast<LoggerType*>(this)->stop(); }
|
|
||||||
|
|
||||||
void log(
|
|
||||||
std::string msg,
|
|
||||||
uint8_t lvl=0x00,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
static_cast<LoggerType*>(this)->push(LogEvent{
|
|
||||||
std::chrono::system_clock::now(),
|
|
||||||
lvl,
|
|
||||||
msg,
|
|
||||||
std::this_thread::get_id(),
|
|
||||||
GetPID(),
|
|
||||||
file,
|
|
||||||
line
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t N>
|
|
||||||
void log(
|
|
||||||
char const(&msg) [N],
|
|
||||||
uint8_t lvl=0x00,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
static_cast<LoggerType*>(this)->log(std::string(msg), lvl, file, line);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void log(
|
|
||||||
const ErrorBase<T>& err,
|
|
||||||
uint8_t lvl=0xFF,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
static_cast<LoggerType*>(this)->log(err.what(), lvl, file, line);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
std::enable_if_t<std::is_base_of_v<std::exception, T>, void> log(
|
|
||||||
const T& err,
|
|
||||||
uint8_t lvl=0xFF,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) {
|
|
||||||
static_cast<LoggerType*>(this)->log(std::string(err.what()), lvl, file, line);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename U>
|
|
||||||
void warn(
|
|
||||||
U val,
|
|
||||||
uint8_t lvl=0x01,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) { static_cast<LoggerType*>(this)->log(val, lvl, file, line); }
|
|
||||||
|
|
||||||
template<typename U>
|
|
||||||
void error(
|
|
||||||
U val,
|
|
||||||
std::string file="",
|
|
||||||
unsigned int line=0
|
|
||||||
) { static_cast<LoggerType*>(this)->log(val, 0xFF, file, line); }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::vector<std::ostream*> _ostream;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifndef _NB_NO_LOGGER
|
|
||||||
extern DefaultDebugLogger logger;
|
|
||||||
#endif // _NB_NO_LOGGER
|
|
||||||
|
|
||||||
// Taking Charge of Adult ADHD by Russell Barkley
|
|
||||||
|
|
||||||
/* template <typename T=NoneType>
|
|
||||||
struct NB_DEFAULT_LOGGER_THROW;
|
|
||||||
|
|
||||||
template<>
|
|
||||||
struct NB_DEFAULT_LOGGER_THROW<NoneType> {
|
|
||||||
template <typename T>
|
|
||||||
NB_DEFAULT_LOGGER_THROW(T arg) {
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
logger.error(arg);
|
|
||||||
#endif // _NB_AUTLOG
|
|
||||||
throw arg;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
struct NB_DEFAULT_LOGGER_THROW {
|
|
||||||
template <typename... Args>
|
|
||||||
NB_DEFAULT_LOGGER_THROW(Args&&... args) {
|
|
||||||
std::shared_ptr<T> error_ptr;
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
|
||||||
error_ptr = std::make_shared<T>(args..., __LINE__, __FILE__);
|
|
||||||
#else
|
|
||||||
error_ptr = std::make_shared<T>(args...);
|
|
||||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
|
||||||
logger.error(*error_ptr);
|
|
||||||
#endif // _NB_AUTLOG
|
|
||||||
throw *error_ptr;
|
|
||||||
}
|
|
||||||
}; */
|
|
||||||
|
|
||||||
/* template <typename Arg1=NoneType, typename...Args>
|
|
||||||
void NB_DEFAULT_LOGGER_THROW(Args&& ... args) {
|
|
||||||
std::shared_ptr<Arg1> error_ptr;
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
|
||||||
error_ptr = std::make_shared<Arg1>(args..., __LINE__, __FILE__);
|
|
||||||
#else
|
|
||||||
error_ptr = std::make_shared<Arg1>(args...);
|
|
||||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
|
||||||
logger.error(*error_ptr);
|
|
||||||
#endif // _NB_AUTLOG
|
|
||||||
throw *error_ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Arg>
|
|
||||||
void NB_DEFAULT_LOGGER_THROW<NoneType>(const Arg& arg) {
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
logger.error(arg);
|
|
||||||
#endif // _NB_AUTLOG
|
|
||||||
throw arg;
|
|
||||||
} */
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void NB_DEFAULT_LOGGER_THROW(const T& err, std::string file="", unsigned int line = 0) {
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
if (file.empty()) {
|
|
||||||
logger.error(err);
|
|
||||||
} else {
|
|
||||||
logger.error(err, file, line);
|
|
||||||
}
|
|
||||||
#endif // _NB_AUTLOG
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
#ifdef _NB_CODE_ERROR_LOCATIONS
|
|
||||||
#ifndef LOG
|
|
||||||
#define LOG(args...) nb::logger.log(args, __FILE__, __LINE__)
|
|
||||||
#endif // LOG
|
|
||||||
#ifndef WARN
|
|
||||||
#define WARN(args...) nb::logger.warn(args, __FILE__, __LINE__)
|
|
||||||
#endif // WARN
|
|
||||||
#ifndef ERROR
|
|
||||||
#define ERROR(args...) nb::logger.error(args, __FILE__, __LINE__)
|
|
||||||
#endif // ERROR
|
|
||||||
#else
|
|
||||||
#ifndef LOG
|
|
||||||
#define LOG(args...) nb::logger.log(args)
|
|
||||||
#endif // LOG
|
|
||||||
#ifndef WARN
|
|
||||||
#define WARN(args...) nb::logger.warn(args)
|
|
||||||
#endif // WARN
|
|
||||||
#ifndef ERROR
|
|
||||||
#define ERROR(args...) nb::logger.error(args)
|
|
||||||
#endif // ERROR
|
|
||||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
|
||||||
#endif // _NB_AUTOLOG
|
|
||||||
|
|
||||||
#ifndef THROW
|
|
||||||
#ifdef _NB_AUTOLOG
|
|
||||||
#define THROW(args...) ERROR(args); throw args
|
|
||||||
#else
|
|
||||||
#define THROW(args...) throw args
|
|
||||||
#endif // _NB_CODE_ERROR_LOCATIONS
|
|
||||||
#endif // THROW
|
|
||||||
|
|
||||||
#endif // _NB_LOGGER
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
@PACKAGE_INIT@
|
||||||
|
include("${CMAKE_CURRENT_LIST_DIR}/NBCoreTargets.cmake")
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_CORE_TYPES
|
|
||||||
#define _NB_CORE_TYPES
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
/* template<typename T>
|
|
||||||
T swap_endian(const T& val) {
|
|
||||||
T ret;
|
|
||||||
const int size = sizeof(T);
|
|
||||||
auto retLoc = static_cast<void*>(&ret);
|
|
||||||
auto valLoc = static_cast<const void*>(&val);
|
|
||||||
|
|
||||||
for (int i = 0; i < size; ++i) {
|
|
||||||
memcpy(retLoc+i, valLoc+(size-i-1), 1);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
} */
|
|
||||||
|
|
||||||
|
|
||||||
// using ByteVector = std::vector<uint8_t>;
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
#endif // _NB_CORE_TYPES
|
|
||||||
@@ -0,0 +1,184 @@
|
|||||||
|
#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
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
#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
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
#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
|
||||||
@@ -0,0 +1,250 @@
|
|||||||
|
#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
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#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
|
||||||
@@ -7,6 +7,8 @@
|
|||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
uint64_t GetPID();
|
uint64_t GetPID();
|
||||||
|
uint64_t getTID();
|
||||||
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|
||||||
@@ -1,44 +1,25 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#ifndef _NB_CORE_TYPES
|
#ifndef _NB_STRING_UTILS
|
||||||
#define _NB_CORE_TYPES
|
#define _NB_STRING_UTILS
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#include "TypeTraits.hpp"
|
#include <NBCore/TypeTraits.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
#ifdef _NB_TARGET_WINDOWS
|
#ifdef _NB_TARGET_WINDOWS
|
||||||
const std::string NEWLINE = "\n";
|
inline static const std::string NEWLINE = "\n";
|
||||||
const std::wstring WNEWLINE = L"\n";
|
inline static const std::wstring WNEWLINE = L"\n";
|
||||||
#endif // _NB_TARGET_WINDOWS
|
#endif // _NB_TARGET_WINDOWS
|
||||||
#ifdef _NB_TARGET_LINUX
|
#ifdef _NB_TARGET_LINUX
|
||||||
const std::string NEWLINE = "\n";
|
inline const std::string NEWLINE = "\n";
|
||||||
const std::wstring WNEWLINE = L"\n";
|
inline const std::wstring WNEWLINE = L"\n";
|
||||||
#endif // _NB_TARGET_LINUX
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
const std::string TABOVER = " ";
|
const std::string TABOVER = " ";
|
||||||
|
|
||||||
// std::wstring str_to_wstr(std::string in);
|
|
||||||
|
|
||||||
// std::string wstr_to_str(std::wstring in);
|
|
||||||
|
|
||||||
template <typename Stream, typename... Args>
|
|
||||||
void stream(const Stream& s, Args&&... args);
|
|
||||||
|
|
||||||
template <typename Stream, typename... Args>
|
|
||||||
void stream(const Stream& s, Args&&... args) {
|
|
||||||
(s << ... << args);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void term(Args&&... args) { stream(std::cout, args..., nb::NEWLINE); }
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void wterm(Args&&... args) { stream(std::wcout, args..., nb::WNEWLINE); }
|
|
||||||
|
|
||||||
template <typename T = char>
|
template <typename T = char>
|
||||||
std::basic_string<T> find_and_replace(
|
std::basic_string<T> find_and_replace(
|
||||||
ExplicitType_t<std::basic_string<T>> original,
|
ExplicitType_t<std::basic_string<T>> original,
|
||||||
@@ -76,4 +57,4 @@ std::string indent_strblock(
|
|||||||
);
|
);
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
#endif // _NB_CORE_TYPES
|
#endif // _NB_STRING_UTILS
|
||||||
@@ -33,6 +33,9 @@ namespace detail {
|
|||||||
|
|
||||||
struct NoneType{};
|
struct NoneType{};
|
||||||
|
|
||||||
|
template<typename T, unsigned int Arg>
|
||||||
|
using Const_t = std::integral_constant<T, Arg>;
|
||||||
|
|
||||||
template <
|
template <
|
||||||
template <class...> class Op,
|
template <class...> class Op,
|
||||||
class... Args
|
class... Args
|
||||||
@@ -87,6 +90,15 @@ struct RunAndOutput {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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>
|
template<typename Func, std::size_t N=0, typename... Pack>
|
||||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||||
ForEach(std::tuple<Pack...>&, Func) {}
|
ForEach(std::tuple<Pack...>&, Func) {}
|
||||||
@@ -109,6 +121,41 @@ ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
|
|||||||
ForEach<N+1, Func, Args, Pack...>(tup, f, 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>
|
template <typename T, typename Pack1, typename... Pack>
|
||||||
struct RefPackToPtrVec {
|
struct RefPackToPtrVec {
|
||||||
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
|
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
#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
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
#include "Errors.hpp"
|
|
||||||
#include "TypeTraits.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
const std::string Error<>::type = "nb::Error";
|
|
||||||
|
|
||||||
const ErrorCodeMap Error<nb::NoneType>::ErrorMessages = {
|
|
||||||
{Error::Codes::STANDARD, "std::exception"},
|
|
||||||
{Error::Codes::UNDEFINED, "Error"}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#include <NBCore/ErrorsImpl.hpp>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
//ErrorBase::
|
||||||
|
ErrorBase::ErrorBase(const std::exception& exception_) noexcept
|
||||||
|
: ErrorBase(Error(exception_)) {}
|
||||||
|
|
||||||
|
} // namespace nb
|
||||||
|
|
||||||
|
|
||||||
@@ -1,40 +1,82 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "Logger.hpp"
|
#include <NBCore/Logger.hpp>
|
||||||
#include "StringUtils.hpp"
|
#include <NBCore/StringUtils.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
#ifndef _NB_NO_LOGGER
|
static bool RUN_LOGGER(nb::DefaultDebugLogger& logger_) {
|
||||||
nb::DefaultDebugLogger logger(std::cout);
|
logger_.run();
|
||||||
#endif // _NB_NO_LOGGER
|
return logger_.isRunning();
|
||||||
|
}
|
||||||
|
|
||||||
bool nb::DefaultDebugLogger::process(const LogEvent& msg) {
|
#ifdef _NB_AUTOLOG
|
||||||
|
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 level_field_width = 5;
|
||||||
constexpr size_t msg_prompt_length = level_field_width+7;
|
constexpr size_t msg_prompt_length = level_field_width+7;
|
||||||
for (const auto os : this->_ostream) {
|
std::stringstream formatted_stream;
|
||||||
*os << "[";
|
formatted_stream << "[";
|
||||||
os->width(level_field_width);
|
formatted_stream.width(level_field_width);
|
||||||
*os << std::to_string(msg.lvl) << "] :: ";
|
formatted_stream << std::to_string(msg.lvl) << "] :: ";
|
||||||
std::string fileloc="";
|
std::string fileloc="";
|
||||||
if (!msg.file.empty()) {
|
if (!msg.file.empty()) {
|
||||||
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
|
fileloc += "(" + msg.file + ":" + std::to_string(msg.line) + ") - ";
|
||||||
}
|
|
||||||
*os << indent_strblock(
|
|
||||||
fileloc + msg.msg,
|
|
||||||
std::string(20 - msg_prompt_length, ' '),
|
|
||||||
""
|
|
||||||
) << nb::NEWLINE;
|
|
||||||
}
|
}
|
||||||
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
// class DefaultDebugLogger
|
||||||
return log.run();
|
|
||||||
|
bool DefaultDebugLogger::process(const LogEvent& msg) {
|
||||||
|
return _logsink->in(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif // _NB_TARGET_LINUX
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
#include "Processes.hpp"
|
#include <NBCore/Processes.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
@@ -15,8 +15,15 @@ uint64_t GetPID() {
|
|||||||
return GetCurrentProcessId();
|
return GetCurrentProcessId();
|
||||||
}
|
}
|
||||||
#endif // _NB_TARGET_WINDOWS
|
#endif // _NB_TARGET_WINDOWS
|
||||||
|
|
||||||
#ifdef _NB_TARGET_LINUX
|
#ifdef _NB_TARGET_LINUX
|
||||||
#endif // _NB_TARGET_LINUX
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
|
#ifdef _NB_TARGET_WINDOWS
|
||||||
|
uint64_t GetTID() {
|
||||||
|
return GetCurrentThreadId();
|
||||||
|
}
|
||||||
|
#endif // _NB_TARGET_WINDOWS
|
||||||
|
#ifdef _NB_TARGET_LINUX
|
||||||
|
#endif // _NB_TARGET_LINUX
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "StringUtils.hpp"
|
#include <NBCore/StringUtils.hpp>
|
||||||
|
|
||||||
/* std::string nb::wstr_to_str(std::wstring in) {
|
/* std::string nb::wstr_to_str(std::wstring in) {
|
||||||
std::size_t wstrlen = in.length();
|
std::size_t wstrlen = in.length();
|
||||||
|
|||||||
@@ -1 +1,27 @@
|
|||||||
|
#include <NBCore/Utils.hpp>
|
||||||
|
|
||||||
|
namespace nb {
|
||||||
|
|
||||||
|
/* using ObjectManagerCodes = ObjectManagerError::Codes;
|
||||||
|
const std::string ObjectManagerError::type = "nb::ObjectManagerError";
|
||||||
|
const ErrorCodeMap ObjectManagerError::ErrorMessages({
|
||||||
|
{ObjectManagerCodes::UNDEFINED, "Error"},
|
||||||
|
{
|
||||||
|
ObjectManagerCodes::NO_MANAGER,
|
||||||
|
"Attempting to create object lock without lock manager"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ObjectManagerCodes::MANAGER_MISMATCH,
|
||||||
|
"Attempting to delete object lock from mismatched lock manager"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ObjectManagerCodes::LOCK_OVERFLOW,
|
||||||
|
"Too many object locks allocated"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ObjectManagerCodes::BAD_THREAD,
|
||||||
|
"Attempting operation from a bad thread"
|
||||||
|
}
|
||||||
|
}); */
|
||||||
|
|
||||||
|
}; // namespace nb
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#define CODE_ERROR_LOCATIONS
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include "Errors.hpp"
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <NBCore/Errors.hpp>
|
||||||
|
|
||||||
using namespace nb;
|
using namespace nb;
|
||||||
|
|
||||||
class TestError : public Error<TestError> {
|
class TestError : public Error<TestError> {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
|
#define _NB_AUTOLOG
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
#include "Errors.hpp"
|
#include <NBCore/Errors.hpp>
|
||||||
#include "Logger.hpp"
|
#include <NBCore/Logger.hpp>
|
||||||
|
|
||||||
class TestError : public nb::Error<TestError> {
|
class TestError : public nb::Error<TestError> {
|
||||||
using Base = Error<TestError>;
|
using Base = Error<TestError>;
|
||||||
@@ -29,17 +30,19 @@ const nb::ErrorCodeMap TestError::ErrorMessages{
|
|||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
nb::logger.log("Whoop!");
|
while(!nb::LOGGER_RUNNING) {}
|
||||||
|
nb::logger.minimalLogLevel(0x0);
|
||||||
|
nb::logger.msg("Whoop!");
|
||||||
try {
|
try {
|
||||||
THROW(TestError(0, TestError(1, TestError(2, TestError(3, TestError(2))))));
|
THROW(TestError(0, TestError(1, TestError(2, TestError(3, TestError(2))))));
|
||||||
} catch (TestError e){
|
} catch (TestError e){
|
||||||
nb::logger.log("nb::Error was thrown!");
|
nb::logger.msg("nb::Error was thrown!");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
THROW(std::exception());
|
THROW(std::exception());
|
||||||
} catch (std::exception e){
|
} catch (std::exception e){
|
||||||
nb::logger.log("std::exception was thrown!");
|
nb::logger.msg("std::exception was thrown!");
|
||||||
}
|
}
|
||||||
ERROR("hello there!");
|
ERROR("hello there!");
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#define CODE_ERROR_LOCATIONS
|
#define CODE_ERROR_LOCATIONS
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include "Processes.hpp"
|
#include <NBCore/Processes.hpp>
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
TEST(ProcessesTest, GetPID) {
|
TEST(ProcessesTest, GetPID) {
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "TypeTraits.hpp"
|
#include <NBCore/TypeTraits.hpp>
|
||||||
#include "StringUtils.hpp"
|
#include <NBCore/StringUtils.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "Events.hpp"
|
#include <NBCore/Events.hpp>
|
||||||
|
|
||||||
namespace NB {
|
namespace NB {
|
||||||
|
|
||||||
|
|||||||
+284
-193
@@ -4,17 +4,15 @@
|
|||||||
|
|
||||||
#include "GLLoad.hpp"
|
#include "GLLoad.hpp"
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
#include <NBCore/Errors.hpp>
|
||||||
#include <NBCore/Logger.hpp>
|
#include <NBCore/Types.hpp>
|
||||||
#include <NBCore/Utils.hpp>
|
|
||||||
|
|
||||||
#include "OGLObjects.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
typedef std::vector<unsigned char> RawVec;
|
||||||
|
|
||||||
static uint8_t GLSLTypeSize(GLenum type) {
|
static uint8_t GLSLTypeSize(GLenum type) {
|
||||||
switch(type) {
|
switch(type) {
|
||||||
@@ -37,30 +35,51 @@ static uint8_t GLSLTypeSize(GLenum type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct VertexAttributePointer {
|
||||||
|
GLuint buffer = 0;
|
||||||
|
int32_t offset = 0;
|
||||||
|
GLsizei stride = -1;
|
||||||
|
GLuint divisor = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexAttribute {
|
||||||
|
GLint GLSLSize;
|
||||||
|
GLenum GLSLType;
|
||||||
|
GLboolean GLSLNormalization;
|
||||||
|
VertexAttributePointer ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool isIndexInVertexAttribute(int i, const VertexAttribute& va) {
|
||||||
|
if ((i -= va.ptr.offset) < 0) { return false; }
|
||||||
|
return (i%va.ptr.stride) < GLSLTypeSize(va.GLSLType) * va.GLSLSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef std::vector<VertexAttribute> VertexAttributeList;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ByteVector vectorToRaw(const std::vector<T>& vec) {
|
RawVec vectorToRaw(const std::vector<T>& vec) {
|
||||||
unsigned int num_bytes = vec.size() * sizeof(T);
|
unsigned int num_bytes = vec.size() * sizeof(T);
|
||||||
ByteVector ret(num_bytes);
|
RawVec ret(num_bytes);
|
||||||
memcpy(ret.data(), vec.data(), num_bytes);
|
memcpy(ret.data(), vec.data(), num_bytes);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename S>
|
template<typename T, typename S>
|
||||||
ByteVector concatVectorsToRaw(const std::vector<T>& vec1, const std::vector<S>& vec2) {
|
RawVec concatVectorsToRaw(const std::vector<T>& vec1, const std::vector<S>& vec2) {
|
||||||
ByteVector vec1_raw = vectorToRaw<T>(vec1);
|
RawVec vec1_raw = vectorToRaw<T>(vec1);
|
||||||
ByteVector vec2_raw = vectorToRaw<S>(vec2);
|
RawVec vec2_raw = vectorToRaw<S>(vec2);
|
||||||
unsigned int vec1_raw_size = vec1_raw.size();
|
unsigned int vec1_raw_size = vec1_raw.size();
|
||||||
unsigned int vec2_raw_size = vec2_raw.size();
|
unsigned int vec2_raw_size = vec2_raw.size();
|
||||||
ByteVector ret(vec1_raw_size + vec2_raw_size);
|
RawVec ret(vec1_raw_size + vec2_raw_size);
|
||||||
memcpy(ret.data(), vec1_raw.data(), vec1_raw_size);
|
memcpy(ret.data(), vec1_raw.data(), vec1_raw_size);
|
||||||
memcpy(ret.data() + vec1_raw_size, vec2_raw.data(), vec2_raw_size);
|
memcpy(ret.data() + vec1_raw_size, vec2_raw.data(), vec2_raw_size);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::vector<T> rawToVector(const ByteVector& vec) {
|
std::vector<T> rawToVector(const RawVec& vec) {
|
||||||
if (vec.size() % sizeof(T) != 0) {
|
if (vec.size() % sizeof(T) != 0) {
|
||||||
THROW(std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">."));
|
throw std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">.");
|
||||||
}
|
}
|
||||||
unsigned int num_elmts = vec.size() / sizeof(T);
|
unsigned int num_elmts = vec.size() / sizeof(T);
|
||||||
std::vector<T> ret(num_elmts);
|
std::vector<T> ret(num_elmts);
|
||||||
@@ -69,206 +88,278 @@ std::vector<T> rawToVector(const ByteVector& vec) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
class BufferError : public Error<BufferError> {
|
class BufferError : public std::runtime_error {
|
||||||
using Base = Error<BufferError>;
|
public:
|
||||||
|
const bool error;
|
||||||
|
BufferError(const std::string& msg, const std::string& file="", int line=-1)
|
||||||
|
: std::runtime_error(NB::formatDebugString(msg, file, line)), error(true) {}
|
||||||
|
BufferError(bool isError=true) : std::runtime_error(""), error(isError) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <typename ObjectType>
|
||||||
|
class OpenGLObject {
|
||||||
|
public:
|
||||||
|
OpenGLObject(OpenGLObject&&) = default;
|
||||||
|
OpenGLObject& operator=(OpenGLObject&&) = default;
|
||||||
|
~OpenGLObject() { remove(); }
|
||||||
|
|
||||||
public:
|
virtual void bind() const = 0;
|
||||||
|
virtual void unbind() const = 0;
|
||||||
|
virtual bool isInitialized() const = 0;
|
||||||
|
GLuint id() const { return _id; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
OpenGLObject() = default;
|
||||||
|
OpenGLObject(const OpenGLObject&) = delete;
|
||||||
|
OpenGLObject& operator=(const OpenGLObject&) = delete;
|
||||||
|
|
||||||
|
virtual void remove() const = 0;
|
||||||
|
|
||||||
|
GLuint _id;
|
||||||
|
};
|
||||||
|
|
||||||
|
class VAO : public virtual OpenGLObject<VAO> {
|
||||||
|
public:
|
||||||
|
using Base = OpenGLObject<VAO>;
|
||||||
using Base::Base;
|
using Base::Base;
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
UNDEFINED, DATA_OVERFLOW, INVALID_BUFFER, HANDLE_OVERWRITE
|
|
||||||
};
|
|
||||||
|
|
||||||
static const std::string type;
|
VAO() { glGenVertexArrays(1, &_id); }
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename BufferType>
|
void bind() const { glBindVertexArray(_id); }
|
||||||
class Buffer : public OpenGLObject {
|
void unbind() const { glBindVertexArray(0); }
|
||||||
using Codes = BufferError::Codes;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using OpenGLObject::OpenGLObject;
|
|
||||||
|
|
||||||
Buffer() = default;
|
|
||||||
Buffer(Buffer&& other) {
|
|
||||||
*this = std::move(other);
|
|
||||||
}
|
|
||||||
Buffer& operator=(Buffer&& rhs) {
|
|
||||||
if (_id) { THROW(BufferError(Codes::HANDLE_OVERWRITE)); }
|
|
||||||
_id = rhs._id;
|
|
||||||
_usage = rhs._usage;
|
|
||||||
_size = rhs._size;
|
|
||||||
|
|
||||||
rhs._id = 0;
|
|
||||||
rhs._usage = GL_STATIC_DRAW;
|
|
||||||
rhs._size = 0;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void bind() const override {
|
|
||||||
if (_id) {
|
|
||||||
glBindBuffer(Target, _id);
|
|
||||||
}
|
|
||||||
THROW(BufferError(Codes::INVALID_BUFFER));
|
|
||||||
}
|
|
||||||
virtual void unbind() const override { glBindBuffer(Target, 0); }
|
|
||||||
virtual GLenum usage() const { return _usage; }
|
|
||||||
virtual GLenum usage(GLenum usage_) {
|
|
||||||
_usage = usage_;
|
|
||||||
data(data(), _usage);
|
|
||||||
return _usage;
|
|
||||||
}
|
|
||||||
virtual size_t size() const { return _size; }
|
|
||||||
virtual ByteVector data() const {
|
|
||||||
bind();
|
|
||||||
ByteVector ret(_size);
|
|
||||||
glGetBufferSubData(Target, 0, _size, ret.data());
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
virtual void data(
|
|
||||||
const void* data_,
|
|
||||||
GLsizei size_,
|
|
||||||
GLenum usage_=GL_STATIC_DRAW
|
|
||||||
) {
|
|
||||||
declare();
|
|
||||||
bind();
|
|
||||||
glBufferData(Target, size_, data_, usage_);
|
|
||||||
_size = size_;
|
|
||||||
_usage = usage_;
|
|
||||||
}
|
|
||||||
virtual void data(const ByteVector& data_, GLenum usage_=GL_STATIC_DRAW) {
|
|
||||||
data(data_.data(), data_.size(), usage_);
|
|
||||||
|
|
||||||
}
|
|
||||||
virtual void subdata(void* data_, GLsizeiptr size_, GLintptr offset_=0) {
|
|
||||||
bind();
|
|
||||||
if (offset_+size_ <= _size) {
|
|
||||||
THROW(BufferError(BufferError::Codes::DATA_OVERFLOW));
|
|
||||||
}
|
|
||||||
glBufferSubData(Target, offset_, size_, data_);
|
|
||||||
}
|
|
||||||
virtual void subdata(ByteVector& data_, GLintptr offset_=0) {
|
|
||||||
bind();
|
|
||||||
size_t size_ = data_.size();
|
|
||||||
if (offset_+size_ <= _size) {
|
|
||||||
THROW(BufferError(BufferError::Codes::DATA_OVERFLOW));
|
|
||||||
}
|
|
||||||
glBufferSubData(Target, offset_, data_.size(), data_.data());
|
|
||||||
}
|
|
||||||
// virtual void clear() const { glClearBufferData(Target, ) }
|
|
||||||
static const GLenum Target;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using OpenGLObject::_id;
|
|
||||||
GLenum _usage = GL_STATIC_DRAW;
|
|
||||||
size_t _size;
|
|
||||||
virtual GLuint declare() override {
|
|
||||||
if (!_id) {
|
|
||||||
glGenBuffers(1, &_id);
|
|
||||||
}
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
virtual void remove() override {
|
|
||||||
if (_id) {
|
|
||||||
glDeleteBuffers(1, &_id);
|
|
||||||
_id = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename BufferType>
|
|
||||||
const GLenum Buffer<BufferType>::Target = BufferType::Target;
|
|
||||||
|
|
||||||
template <typename BufferType>
|
|
||||||
class ImmutableBuffer : public virtual Buffer<BufferType> {
|
|
||||||
public:
|
|
||||||
using Base = Buffer<BufferType>;
|
|
||||||
using Base::Target;
|
|
||||||
using Base::bind;
|
|
||||||
|
|
||||||
ImmutableBuffer() = default;
|
|
||||||
ImmutableBuffer(size_t size_, GLenum usage_ = GL_STATIC_DRAW, GLbitfield flags_=0x0) : size(size_), Base::usage(usage_) {
|
|
||||||
glBufferStorage(Target, size, nullptr, flags_);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::weak_ptr<void> map(GLbitfield access_) {
|
|
||||||
if (_map && _mapAccess != access_) { unmap(); }
|
|
||||||
_mapAccess = access_;
|
|
||||||
if (!_map) {
|
|
||||||
bind();
|
|
||||||
glMapBuffer(Target, _mapAccess);
|
|
||||||
}
|
|
||||||
return _map;
|
|
||||||
}
|
|
||||||
void unmap() {
|
|
||||||
bind();
|
|
||||||
glUnmapBuffer(Target);
|
|
||||||
_map.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t size;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using Base::_id;
|
using Base::_id;
|
||||||
std::shared_ptr<void> _map = nullptr;
|
const
|
||||||
GLbitfield _mapAccess = 0;
|
|
||||||
|
void remove() { glDeleteVertexArrays(1, &_id); }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<bool Immutable=false>
|
template <typename BufferType>
|
||||||
class ArrayBuffer : public Buffer<ArrayBuffer<false>> {
|
class Buffer : public virtual OpenGLObject<Buffer<BufferType>> {
|
||||||
using Base = Buffer<ArrayBuffer<false>>;
|
public:
|
||||||
using BufferType = Base;
|
using Base = OpenGLObject<Buffer>;
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
using Base::Base;
|
||||||
using Base::data;
|
|
||||||
ArrayBuffer(const ByteVector&, GLenum usage_=GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
static const GLenum Target = GL_ARRAY_BUFFER;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base::_usage;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
class ArrayBuffer<true>
|
|
||||||
: public virtual ArrayBuffer<false>, public virtual ImmutableBuffer<ArrayBuffer<true>> {
|
|
||||||
using Base = ArrayBuffer<false>;
|
|
||||||
using BufferType = ImmutableBuffer<ArrayBuffer<true>>;
|
|
||||||
using BufferType::BufferType;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Target;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<bool Immutable=false>
|
|
||||||
class ElementBuffer : public virtual Buffer<ElementBuffer<false>> {
|
|
||||||
using Base = Buffer<ElementBuffer<false>>;
|
|
||||||
using BufferType = Base;
|
|
||||||
|
|
||||||
public:
|
Buffer(GLenum usage_ = GL_STATIC_DRAW) : usage(usage_) { glGenBuffers(1, &_id); }
|
||||||
using Base::Base;
|
|
||||||
static const GLenum Target = GL_ELEMENT_ARRAY_BUFFER;
|
|
||||||
|
|
||||||
protected:
|
void bind() const { glBindBuffer(GLTarget, _id); }
|
||||||
|
void unbind() const { glBindBuffer(GLTarget, 0); }
|
||||||
|
void data(void* data_, size_t size) const { glBufferData(GLTarget, size, data_, usage); }
|
||||||
|
void data(nb::ByteVector& data_) const { glBufferData(GLTarget, data_.size(), data_.data(), usage); }
|
||||||
|
void invalidate() const { glInvalidateBufferData(_id); }
|
||||||
|
|
||||||
|
|
||||||
|
const GLenum GLTarget = BufferType::GLTarget;
|
||||||
|
GLenum usage;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using Base::_id;
|
||||||
|
void remove() { glDeleteBuffers(1, &_id); }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
class VertexBuffer : public virtual Buffer<VertexBuffer> {
|
||||||
class ElementBuffer<true>
|
public:
|
||||||
: public virtual ElementBuffer<false>, public virtual ImmutableBuffer<ElementBuffer<true>> {
|
|
||||||
using Base = ElementBuffer<false>;
|
|
||||||
using BufferType = ImmutableBuffer<ElementBuffer<true>>;
|
|
||||||
|
|
||||||
public:
|
static const GLenum GLTarget = GL_ARRAY_BUFFER;
|
||||||
using BufferType::BufferType;
|
protected:
|
||||||
using Base::Target;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ElementBuffer : public virtual Buffer<ElementBuffer> {
|
||||||
|
public:
|
||||||
|
|
||||||
|
static const GLenum GLTarget = GL_ELEMENT_ARRAY_BUFFER;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
class Buffer : public OpenGLObject {
|
||||||
|
public:
|
||||||
|
Buffer(
|
||||||
|
GLenum buffer_type,
|
||||||
|
GLenum usage=GL_STATIC_DRAW
|
||||||
|
) : _type(buffer_type), _usage(usage) {
|
||||||
|
glGenBuffers(1, &_id);
|
||||||
|
}
|
||||||
|
Buffer(
|
||||||
|
const RawVec& init_data,
|
||||||
|
GLenum buffer_type,
|
||||||
|
GLenum usage=GL_STATIC_DRAW
|
||||||
|
) : _type{buffer_type}, _usage{usage} { data(init_data); }
|
||||||
|
Buffer(
|
||||||
|
unsigned int size,
|
||||||
|
GLenum buffer_type,
|
||||||
|
GLenum usage=GL_STATIC_DRAW
|
||||||
|
) : Buffer(RawVec(size), buffer_type, usage) {}
|
||||||
|
|
||||||
|
Buffer(Buffer&& rhs) { *this = std::move(rhs); }
|
||||||
|
Buffer& operator=(Buffer&& rhs) {
|
||||||
|
remove();
|
||||||
|
_usage = rhs._usage;
|
||||||
|
_id = rhs._id;
|
||||||
|
rhs._id = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLenum usage() const { return _usage; }
|
||||||
|
unsigned int id() const { return _id; }
|
||||||
|
GLuint size() const { return _size; }
|
||||||
|
bool isInitialized() const override { return _id && glIsBuffer(_id) && bool(_size); }
|
||||||
|
RawVec data() const {
|
||||||
|
RawVec ret(_size);
|
||||||
|
bind();
|
||||||
|
glGetBufferSubData(_type, 0, _size, ret.data());
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
void bind() const override { glBindBuffer(_type, _id); }
|
||||||
|
void unbind() const override { glBindBuffer(_type, 0); }
|
||||||
|
|
||||||
|
GLenum usage(GLenum usage) {
|
||||||
|
_usage=usage;
|
||||||
|
data(data());
|
||||||
|
return _usage;
|
||||||
|
}
|
||||||
|
void data(const RawVec& set_data) {
|
||||||
|
if (!glfwGetCurrentContext()) {
|
||||||
|
THROW_BUFFER_ERROR("No OpenGL context.");
|
||||||
|
}
|
||||||
|
bind();
|
||||||
|
glBufferData(_type, set_data.size(), set_data.data(), _usage);
|
||||||
|
_size = set_data.size();
|
||||||
|
}
|
||||||
|
void data(const RawVec& newData, unsigned int offset) {
|
||||||
|
if (newData.size()+offset > _size) {
|
||||||
|
THROW_BUFFER_ERROR("Attempting to overflow buffer of capacity " + std::to_string(_size) + ".");
|
||||||
|
}
|
||||||
|
bind();
|
||||||
|
glBufferSubData(_type, offset, newData.size(), newData.data());
|
||||||
|
}
|
||||||
|
void data(void* src, unsigned int offset, unsigned int size) {
|
||||||
|
if (size+offset > _size) {
|
||||||
|
THROW_BUFFER_ERROR("Attempting to overflow buffer of capacity " + std::to_string(_size) + ".");
|
||||||
|
}
|
||||||
|
bind();
|
||||||
|
glBufferSubData(_type, offset, size, src);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void remove() const override {
|
||||||
|
unbind();
|
||||||
|
if (_id) { glDeleteBuffers(1, &_id); }
|
||||||
|
}
|
||||||
|
|
||||||
|
using OpenGLObject::_id;
|
||||||
|
GLenum _type;
|
||||||
|
GLenum _usage;
|
||||||
|
unsigned int _size = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class Texture : public OpenGLObject {
|
||||||
|
public:
|
||||||
|
Texture(GLenum tar=GL_TEXTURE_2D) : _target(tar) { glGenTextures(1, &_id); }
|
||||||
|
|
||||||
|
Texture(Texture&& rhs) { *this=std::move(rhs); }
|
||||||
|
Texture& operator=(Texture&& rhs) {
|
||||||
|
_target = rhs._target;
|
||||||
|
_id = rhs._id;
|
||||||
|
rhs._id = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bind() const override { glBindTexture(_target, _id); }
|
||||||
|
void unbind() const override { glBindTexture(_target, 0); }
|
||||||
|
bool isInitialized() const override { return _id && glIsTexture(_id); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using OpenGLObject::_id;
|
||||||
|
GLenum _target;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Framebuffer : public OpenGLObject {
|
||||||
|
public:
|
||||||
|
|
||||||
|
Framebuffer(GLenum tar=GL_FRAMEBUFFER) : _target(tar) { glGenFramebuffers(1, &_id); }
|
||||||
|
|
||||||
|
Framebuffer(Framebuffer&& rhs) { *this=std::move(rhs); }
|
||||||
|
Framebuffer& operator=(Framebuffer&& rhs) {
|
||||||
|
remove();
|
||||||
|
_target = rhs._target;
|
||||||
|
_id = rhs._id;
|
||||||
|
rhs._id = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bind() const override { glBindFramebuffer(_target, _id); }
|
||||||
|
void unbind() const override { glBindFramebuffer(_target, 0); }
|
||||||
|
bool isInitialized() const override { return _id && glIsFramebuffer(_id); }
|
||||||
|
GLenum status() const {
|
||||||
|
bind();
|
||||||
|
return glCheckFramebufferStatus(_target);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void remove() const override {
|
||||||
|
unbind();
|
||||||
|
if (_id) { glDeleteFramebuffers(1, &_id); }
|
||||||
|
}
|
||||||
|
using OpenGLObject::_id;
|
||||||
|
GLenum _target;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Renderbuffer : public OpenGLObject {
|
||||||
|
public:
|
||||||
|
Renderbuffer(unsigned int x, unsigned int y, GLenum format=GL_RGBA32F, unsigned int multi=0) : Renderbuffer() {
|
||||||
|
storage(x, y, format, multi);
|
||||||
|
}
|
||||||
|
Renderbuffer() { glGenRenderbuffers(1, &_id); }
|
||||||
|
|
||||||
|
|
||||||
|
Renderbuffer(Renderbuffer&& rhs) { *this = std::move(rhs); }
|
||||||
|
Renderbuffer& operator=(Renderbuffer&& rhs) {
|
||||||
|
_format = rhs._format;
|
||||||
|
_id = rhs._id;
|
||||||
|
rhs._id = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void bind() const override { glBindRenderbuffer(GL_RENDERBUFFER, _id); }
|
||||||
|
virtual void unbind() const override { glBindRenderbuffer(GL_RENDERBUFFER, _id); }
|
||||||
|
virtual bool isInitialized() const override { return _id && glIsRenderbuffer(_id); }
|
||||||
|
|
||||||
|
void storage(unsigned int x, unsigned int y, GLenum format=GL_RGBA32F, unsigned int multi=0) {
|
||||||
|
_sizex = x;
|
||||||
|
_sizey = y;
|
||||||
|
_format = format;
|
||||||
|
_multisample = multi;
|
||||||
|
bind();
|
||||||
|
glRenderbufferStorageMultisample(GL_RENDERBUFFER, _multisample, _format, _sizex, _sizey);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void remove() const override {
|
||||||
|
unbind();
|
||||||
|
if (_id) { glDeleteRenderbuffers(1, &_id); }
|
||||||
|
}
|
||||||
|
|
||||||
|
using OpenGLObject::_id;
|
||||||
|
GLenum _format;
|
||||||
|
unsigned int _sizex{0};
|
||||||
|
unsigned int _sizey{0};
|
||||||
|
unsigned int _multisample{0};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
} // namespace NB
|
} // namespace NB
|
||||||
#endif // _NB_BUFFER
|
#endif // _NB_BUFFER
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
include_directories(./.)
|
||||||
|
|
||||||
find_package(OpenGL)
|
find_package(OpenGL)
|
||||||
add_subdirectory(${GLFW_PATH} ${GLFW_PATH}/build)
|
add_subdirectory(${GLFW_PATH} ${GLFW_PATH}/build)
|
||||||
|
|
||||||
@@ -8,11 +10,8 @@ set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
|||||||
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
||||||
|
|
||||||
toAbsolutePath(NB_GRAPHICS_SOURCE
|
toAbsolutePath(NB_GRAPHICS_SOURCE
|
||||||
./src/Buffers.cpp
|
|
||||||
./src/OGLObjects.cpp
|
|
||||||
./src/Textures.cpp
|
|
||||||
./src/VertexArray.cpp
|
|
||||||
./src/Window.cpp
|
./src/Window.cpp
|
||||||
|
./src/Buffers.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
toAbsolutePath(NB_GRAPHICS_INCLUDE
|
toAbsolutePath(NB_GRAPHICS_INCLUDE
|
||||||
@@ -20,10 +19,8 @@ toAbsolutePath(NB_GRAPHICS_INCLUDE
|
|||||||
./Camera.hpp
|
./Camera.hpp
|
||||||
./Draw.hpp
|
./Draw.hpp
|
||||||
./GLLoad.hpp
|
./GLLoad.hpp
|
||||||
./OGLObjects.hpp
|
|
||||||
./shader.hpp
|
./shader.hpp
|
||||||
./Textures.hpp
|
./VAOManager.hpp
|
||||||
./VertexArray.hpp
|
|
||||||
./Window.hpp
|
./Window.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,11 +31,4 @@ add_library(NBGraphics
|
|||||||
${NB_GRAPHICS_SOURCE}
|
${NB_GRAPHICS_SOURCE}
|
||||||
${GLAD_PATH}/src/glad.c
|
${GLAD_PATH}/src/glad.c
|
||||||
)
|
)
|
||||||
target_link_libraries(NBGraphics glfw)
|
target_link_libraries(NBGraphics glfw3)
|
||||||
|
|
||||||
target_include_directories(NBGraphics PUBLIC ./.)
|
|
||||||
|
|
||||||
if (NB_BUILD_TESTS)
|
|
||||||
add_subdirectory(./tests)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,13 @@
|
|||||||
#ifndef _NB_CAMERA
|
#ifndef _NB_CAMERA
|
||||||
#define _NB_CAMERA
|
#define _NB_CAMERA
|
||||||
|
|
||||||
#include "GLLoad.hpp"
|
#include <GLLoad.h>
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_inverse.hpp>
|
#include <glm/gtc/matrix_inverse.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
namespace nb {
|
namespace NB {
|
||||||
|
|
||||||
class Camera {
|
class Camera {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace NB {
|
namespace NB {
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_OGL_OBJECTS
|
|
||||||
#define _NB_OGL_OBJECTS
|
|
||||||
|
|
||||||
#include "GLLoad.hpp"
|
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
class OGLError : public Error<OGLError> {
|
|
||||||
using Base = Error<OGLError>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
UNDEFINED, HANGING_OBJECT
|
|
||||||
};
|
|
||||||
|
|
||||||
static const std::string type;
|
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
};
|
|
||||||
|
|
||||||
class OpenGLObject {
|
|
||||||
public:
|
|
||||||
OpenGLObject(const OpenGLObject&) = delete;
|
|
||||||
OpenGLObject& operator=(const OpenGLObject&) = delete;
|
|
||||||
~OpenGLObject() { remove(); }
|
|
||||||
|
|
||||||
virtual void bind() const = 0;
|
|
||||||
virtual void unbind() const = 0;
|
|
||||||
GLuint id() const { return _id; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
OpenGLObject() = default;
|
|
||||||
|
|
||||||
virtual GLuint declare() = 0;
|
|
||||||
virtual void remove() { this->remove(); };
|
|
||||||
|
|
||||||
GLuint _id;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
#endif // _NB_OGL_OBJECTS
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_TEXTURES
|
|
||||||
#define _NB_TEXTURES
|
|
||||||
|
|
||||||
#include "Buffers.hpp"
|
|
||||||
#include "OGLObjects.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
template<bool Immutable=false>
|
|
||||||
class TextureBuffer : public virtual Buffer<TextureBuffer<false>> {
|
|
||||||
using Base = Buffer<TextureBuffer<false>>;
|
|
||||||
using BufferType = Base;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
static const GLenum Target = GL_TEXTURE_BUFFER;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
class TextureBuffer<true>
|
|
||||||
: public virtual TextureBuffer<false>, public virtual ImmutableBuffer<TextureBuffer<true>> {
|
|
||||||
using Base = TextureBuffer<false>;
|
|
||||||
using BufferType = ImmutableBuffer<TextureBuffer<true>>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using BufferType::BufferType;
|
|
||||||
using Base::Target;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Texture : public OpenGLObject {
|
|
||||||
using Base = OpenGLObject;
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
using Base::id;
|
|
||||||
|
|
||||||
Texture(GLenum);
|
|
||||||
|
|
||||||
virtual void bind() const override { glBindTexture(target, _id); }
|
|
||||||
virtual void unbind() const override { glBindTexture(target, 0); }
|
|
||||||
|
|
||||||
const GLenum target;
|
|
||||||
|
|
||||||
friend Base;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base::_id;
|
|
||||||
|
|
||||||
virtual void remove() override {
|
|
||||||
if (_id) {
|
|
||||||
glDeleteTextures(0, &_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
#endif // _NB_TEXTURES
|
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef _NB_VAO_MANAGER
|
||||||
|
#define _NB_VAO_MANAGER
|
||||||
|
|
||||||
|
#include <GLLoad.h>
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "Buffers.h"
|
||||||
|
|
||||||
|
#define THROW_VAO_ERROR(msg) throw VAOError(msg, __FILE__, __LINE__);
|
||||||
|
|
||||||
|
namespace NB {
|
||||||
|
|
||||||
|
class VAOError : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
const bool error;
|
||||||
|
VAOError(const std::string& msg, const std::string& file="", int line=-1)
|
||||||
|
: std::runtime_error(NB::formatDebugString(msg, file, line)), error(true) {}
|
||||||
|
VAOError(bool isError=true) : std::runtime_error(""), error(isError) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class VAOManager : public OpenGLObject {
|
||||||
|
public:
|
||||||
|
typedef std::shared_ptr<Buffer> BufferManagerPointer;
|
||||||
|
//typedef BufferManager BufferManagerPointer;
|
||||||
|
|
||||||
|
VAOManager();
|
||||||
|
VAOManager(
|
||||||
|
std::vector<BufferManagerPointer>,
|
||||||
|
BufferManagerPointer elmt_buf = nullptr,
|
||||||
|
const VertexAttributeList& vert_attrs = {}
|
||||||
|
);
|
||||||
|
VAOManager(
|
||||||
|
BufferManagerPointer vert_bufs,
|
||||||
|
BufferManagerPointer elmt_buf = nullptr,
|
||||||
|
const VertexAttributeList& vert_attrs = {}
|
||||||
|
) : VAOManager(std::vector<BufferManagerPointer>(1, vert_bufs), elmt_buf, vert_attrs) {}
|
||||||
|
|
||||||
|
VAOManager(VAOManager&& rhs);
|
||||||
|
VAOManager& operator=(VAOManager&& rhs);
|
||||||
|
|
||||||
|
VertexAttributeList getLayout() const;
|
||||||
|
GLuint id() const;
|
||||||
|
unsigned int vertSize(GLuint) const;
|
||||||
|
std::vector<BufferManagerPointer> getVertexBuffers() const;
|
||||||
|
bool isInitialized() const override;
|
||||||
|
RawVec attributeData(unsigned int);
|
||||||
|
|
||||||
|
RawVec attributeData(unsigned int, const RawVec&);
|
||||||
|
void bind() const;
|
||||||
|
void unbind() const;
|
||||||
|
void addVBO(BufferManagerPointer);
|
||||||
|
void changeEBO(BufferManagerPointer);
|
||||||
|
VertexAttributeList addVertexAttributes(const VertexAttributeList&);
|
||||||
|
VertexAttributeList addVertexAttributes(VertexAttribute);
|
||||||
|
VertexAttributeList generate();
|
||||||
|
VertexAttributeList generate(VertexAttributeList);
|
||||||
|
VertexAttributeList changeLayout(unsigned int, VertexAttributePointer);
|
||||||
|
|
||||||
|
VAOError checkValid(const VertexAttributeList&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void remove() const override;
|
||||||
|
|
||||||
|
using OpenGLObject::_id;
|
||||||
|
std::map<GLuint, BufferManagerPointer> _vert_buffers;
|
||||||
|
BufferManagerPointer _elmt_buffer = nullptr;
|
||||||
|
VertexAttributeList _vert_attrs;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace NB
|
||||||
|
#endif // _NB_VAO_MANAGER
|
||||||
@@ -1,179 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#ifndef _NB_VERTEX_ARRAY
|
|
||||||
#define _NB_VERTEX_ARRAY
|
|
||||||
|
|
||||||
#include "GLLoad.hpp"
|
|
||||||
|
|
||||||
#include <NBCore/Errors.hpp>
|
|
||||||
|
|
||||||
#include "Buffers.hpp"
|
|
||||||
#include "OGLObjects.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
struct VertexAttributeLayout {
|
|
||||||
int32_t offset = 0;
|
|
||||||
GLsizei stride = 0;
|
|
||||||
GLuint divisor = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VertexAttribute {
|
|
||||||
GLint GLSLSize;
|
|
||||||
GLenum GLSLType;
|
|
||||||
GLboolean GLSLNormalization;
|
|
||||||
VertexAttributeLayout layout;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VertexAttributePointer;
|
|
||||||
|
|
||||||
static bool isIndexInVertexAttribute(int i, const VertexAttribute& va) {
|
|
||||||
if ((i -= va.layout.offset) < 0) { return false; }
|
|
||||||
return (i%va.layout.stride) < GLSLTypeSize(va.GLSLType) * va.GLSLSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef std::vector<VertexAttribute> VertexAttributeList;
|
|
||||||
typedef std::vector<VertexAttributePointer> VertexAttributePointerList;
|
|
||||||
|
|
||||||
class VAOError : public Error<VAOError> {
|
|
||||||
using Base = Error<VAOError>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
|
|
||||||
enum Codes : unsigned int {
|
|
||||||
UNDEFINED, INVALID_ATTRIBUTE, INVALID_VAO
|
|
||||||
};
|
|
||||||
|
|
||||||
static const std::string type;
|
|
||||||
static const ErrorCodeMap ErrorMessages;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <bool Immutable=false>
|
|
||||||
class VertexBuffer : public ArrayBuffer<false>{
|
|
||||||
using Base = ArrayBuffer<false>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
template<typename... Args>
|
|
||||||
VertexBuffer(const VertexAttributeList& vas={}, Args... args)
|
|
||||||
: attributes(vas) {
|
|
||||||
if (sizeof...(Args)) {
|
|
||||||
data(args...);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
VertexBuffer(VertexBuffer&& rhs)
|
|
||||||
: attributes(rhs.attributes), Base(std::move(rhs)){}
|
|
||||||
|
|
||||||
|
|
||||||
using Base::Target;
|
|
||||||
const VertexAttributeList attributes;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base::_usage;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
class VertexBuffer<true>
|
|
||||||
: public virtual VertexBuffer<false>, public virtual ImmutableBuffer<ArrayBuffer<true>> {
|
|
||||||
using Base = ArrayBuffer<false>;
|
|
||||||
using BufferType = ImmutableBuffer<ArrayBuffer<true>>;
|
|
||||||
using BufferType::BufferType;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Target;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VertexAttributePointer {
|
|
||||||
VertexAttribute attribute;
|
|
||||||
VertexBuffer<false>* buffer;
|
|
||||||
GLuint index;
|
|
||||||
};
|
|
||||||
|
|
||||||
class VAO : public OpenGLObject {
|
|
||||||
using Base = OpenGLObject;
|
|
||||||
using Codes = VAOError::Codes;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using Base::Base;
|
|
||||||
VAO() = default;
|
|
||||||
VAO(const VertexAttributePointerList&);
|
|
||||||
VAO(VAO&&);
|
|
||||||
|
|
||||||
VAO& operator=(VAO&&);
|
|
||||||
|
|
||||||
virtual void bind() const override {
|
|
||||||
if(_id) {
|
|
||||||
glBindVertexArray(_id);
|
|
||||||
} else {
|
|
||||||
THROW(VAOError(Codes::INVALID_VAO));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
virtual void unbind() const override { glBindVertexArray(0); }
|
|
||||||
|
|
||||||
VertexAttributePointerList attributes() const;
|
|
||||||
VertexAttributePointerList attributes(const VertexAttributePointerList&);
|
|
||||||
VertexAttributePointer attr(GLuint) const;
|
|
||||||
void enable() const;
|
|
||||||
void enable(GLuint) const;
|
|
||||||
void disable() const;
|
|
||||||
void disable(GLuint) const;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
using Base::_id;
|
|
||||||
VertexAttributePointerList _attrs;
|
|
||||||
|
|
||||||
virtual void remove() override {
|
|
||||||
if(_id) {
|
|
||||||
disable();
|
|
||||||
bind();
|
|
||||||
glDeleteVertexArrays(1, &_id);
|
|
||||||
_id = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
virtual GLuint declare() override {
|
|
||||||
if (!_id) {
|
|
||||||
glGenVertexArrays(1, &_id);
|
|
||||||
}
|
|
||||||
return _id;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
class VertexGroup {
|
|
||||||
public:
|
|
||||||
VertexGroup(VertexGroup&&);
|
|
||||||
VertexGroup& operator=(VertexGroup&&);
|
|
||||||
|
|
||||||
void bind() const {
|
|
||||||
_elmt_buf.bind();
|
|
||||||
_vao.bind();
|
|
||||||
}
|
|
||||||
void unbind() const {
|
|
||||||
_vao.bind();
|
|
||||||
_elmt_buf.unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexBuffer<false>& buffer(size_t);
|
|
||||||
size_t buffer(VertexBuffer<false>&&);
|
|
||||||
std::vector<std::shared_ptr<VertexBuffer<false>>> buffers();
|
|
||||||
size_t buffers(std::vector<VertexBuffer<false>>&&);
|
|
||||||
VertexBuffer<false>&& dropBuffer(size_t);
|
|
||||||
|
|
||||||
VertexAttributePointer attribute(size_t) const;
|
|
||||||
void enableAttr(GLuint);
|
|
||||||
void disableAttr(GLuint);
|
|
||||||
void enableBuffer(size_t);
|
|
||||||
void disableBuffer(size_t);
|
|
||||||
void enable();
|
|
||||||
void disable();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::vector<std::shared_ptr<VertexBuffer<false>>> _vertex_data;
|
|
||||||
ElementBuffer<false> _elmt_buf;
|
|
||||||
VAO _vao;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
#endif // _NB_VERTEX_ARRAY
|
|
||||||
@@ -4,13 +4,12 @@
|
|||||||
|
|
||||||
#include "GLLoad.hpp"
|
#include "GLLoad.hpp"
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <NBCore/Utils.hpp>
|
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
class GLError : public std::runtime_error {
|
class GLError : public std::runtime_error {
|
||||||
|
|||||||
@@ -2,20 +2,4 @@
|
|||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
using BufferErrorCodes = BufferError::Codes;
|
|
||||||
const std::string BufferError::type = "nb::BufferError";
|
|
||||||
const ErrorCodeMap BufferError::ErrorMessages = {
|
|
||||||
{ BufferErrorCodes::UNDEFINED, "Error" },
|
|
||||||
{ BufferErrorCodes::DATA_OVERFLOW, "Attempting buffer overflow" },
|
|
||||||
{ BufferErrorCodes::INVALID_BUFFER, "Attempting operation on invalid buffer" },
|
|
||||||
{BufferErrorCodes::HANDLE_OVERWRITE, "Attempting to overwrite active buffer"}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template<>
|
|
||||||
ArrayBuffer<false>::ArrayBuffer(const ByteVector& data_, GLenum usage_)
|
|
||||||
: BufferType() {
|
|
||||||
data(data_, usage_);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
#include "Camera.hpp"
|
#include "Camera.h"
|
||||||
|
namespace NB {
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
// Camera class
|
// Camera class
|
||||||
Camera::Camera(const Vec3& pos, const Vec3& tar, const Vec3& up) {
|
Camera::Camera(const Vec3& pos, const Vec3& tar, const Vec3& up) {
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
#include "OGLObjects.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
const std::string OGLError::type = "nb::OGLError";
|
|
||||||
const ErrorCodeMap OGLError::ErrorMessages = {
|
|
||||||
{ OGLError::Codes::UNDEFINED, "Error" },
|
|
||||||
{OGLError::Codes::HANGING_OBJECT, "Attempting to leave a hanging OpenGL object"}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#include "Textures.hpp"
|
|
||||||
|
|
||||||
namespace nb{
|
|
||||||
|
|
||||||
Texture::Texture(GLenum target_) : target(target_) {
|
|
||||||
glGenTextures(1, &_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
@@ -0,0 +1,225 @@
|
|||||||
|
#include "VAOManager.hpp"
|
||||||
|
|
||||||
|
namespace NB {
|
||||||
|
|
||||||
|
using BufferManagerPointer = std::shared_ptr<Buffer>;
|
||||||
|
|
||||||
|
VAOManager::VAOManager() { glGenVertexArrays(1, &_id); }
|
||||||
|
|
||||||
|
VAOManager::VAOManager(
|
||||||
|
std::vector<BufferManagerPointer> vert_bufs,
|
||||||
|
BufferManagerPointer elmt_buf,
|
||||||
|
const VertexAttributeList& vert_attrs
|
||||||
|
) : VAOManager() {
|
||||||
|
_elmt_buffer = elmt_buf;
|
||||||
|
for (BufferManagerPointer vb : vert_bufs) {
|
||||||
|
_vert_buffers[vb->id()] = vb;
|
||||||
|
}
|
||||||
|
if (vert_attrs.size()!=0) {
|
||||||
|
generate(vert_attrs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VAOManager::VAOManager(VAOManager&& rhs) { *this = std::move(rhs); }
|
||||||
|
|
||||||
|
VAOManager& VAOManager::operator=(VAOManager&& rhs) {
|
||||||
|
remove();
|
||||||
|
_vert_buffers = rhs._vert_buffers;
|
||||||
|
_elmt_buffer = rhs._elmt_buffer;
|
||||||
|
_vert_attrs = rhs._vert_attrs;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributeList VAOManager::getLayout() const { return _vert_attrs; }
|
||||||
|
|
||||||
|
GLuint VAOManager::id() const { return _id; }
|
||||||
|
|
||||||
|
bool VAOManager::isInitialized() const {
|
||||||
|
return _id && glIsVertexArray(_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAOManager::remove() const {
|
||||||
|
bind();
|
||||||
|
for (int i{0}; i < _vert_attrs.size(); ++i) {
|
||||||
|
glDisableVertexAttribArray(i);
|
||||||
|
}
|
||||||
|
unbind();
|
||||||
|
glDisableVertexAttribArray(_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int VAOManager::vertSize(GLuint id) const {
|
||||||
|
unsigned int size = 0;
|
||||||
|
for (const VertexAttribute& va : _vert_attrs) {
|
||||||
|
if (va.ptr.buffer == id) {
|
||||||
|
size += va.GLSLSize * GLSLTypeSize(va.GLSLType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<BufferManagerPointer> VAOManager::getVertexBuffers() const {
|
||||||
|
std::vector<BufferManagerPointer> ret;
|
||||||
|
for (const auto& i : _vert_buffers) {
|
||||||
|
ret.emplace_back(i.second);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
RawVec VAOManager::attributeData(unsigned int i) {
|
||||||
|
if (i <= _vert_attrs.size()) {
|
||||||
|
throw std::out_of_range("No vertex attribute exists for specified index");
|
||||||
|
}
|
||||||
|
VertexAttribute attr = _vert_attrs[i];
|
||||||
|
BufferManagerPointer buffer = _vert_buffers[attr.ptr.buffer];
|
||||||
|
RawVec data = buffer->data();
|
||||||
|
unsigned int attr_size = attr.GLSLSize*GLSLTypeSize(attr.GLSLType);
|
||||||
|
RawVec ret((buffer->size()-attr.ptr.offset-attr_size)*attr_size/attr.ptr.stride);
|
||||||
|
int data_size = data.size();
|
||||||
|
for (int i{0}; i < data_size; ++i) {
|
||||||
|
if (isIndexInVertexAttribute(i, attr)) {
|
||||||
|
ret[i] = data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
RawVec VAOManager::attributeData(unsigned int i, const RawVec& new_data) {
|
||||||
|
if (i >= _vert_attrs.size()) {
|
||||||
|
throw std::out_of_range("No vertex attribute exists for specified index.");
|
||||||
|
}
|
||||||
|
VertexAttribute attr = _vert_attrs[i];
|
||||||
|
BufferManagerPointer buffer = _vert_buffers[attr.ptr.buffer];
|
||||||
|
unsigned int attr_size = attr.GLSLSize*GLSLTypeSize(attr.GLSLType);
|
||||||
|
unsigned int offset = attr.ptr.offset;
|
||||||
|
unsigned int num_verts = buffer->size() / vertSize(buffer->id());
|
||||||
|
if (num_verts*attr_size != new_data.size()) {
|
||||||
|
// WHY HERE????
|
||||||
|
THROW_VAO_ERROR(
|
||||||
|
"Input data size of " + std::to_string(new_data.size())
|
||||||
|
+ " does not match data size for requested vertex attribute of total size " + std::to_string(num_verts*attr_size)
|
||||||
|
+ "."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
uint64_t pos = uint64_t(new_data.data());
|
||||||
|
for (int i{0}; i<num_verts; i++) {
|
||||||
|
buffer->data((void*)(pos+i*attr_size), offset+i*attr.ptr.stride, attr_size);
|
||||||
|
}
|
||||||
|
return RawVec(new_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAOManager::bind() const {
|
||||||
|
glBindVertexArray(_id);
|
||||||
|
if (_elmt_buffer) { _elmt_buffer->bind(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAOManager::unbind() const {
|
||||||
|
glBindVertexArray(0);
|
||||||
|
if (_elmt_buffer) { _elmt_buffer->unbind(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAOManager::addVBO(BufferManagerPointer vert_buf) {
|
||||||
|
GLuint vert_id = vert_buf->id();
|
||||||
|
if (_vert_buffers.find(vert_id) == _vert_buffers.end()) {
|
||||||
|
_vert_buffers[vert_buf->id()] = vert_buf;
|
||||||
|
} else {
|
||||||
|
THROW_VAO_ERROR("Attempting to add identical VBO id of " + std::to_string(vert_id) + ".");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAOManager::changeEBO(BufferManagerPointer ebo) {
|
||||||
|
_elmt_buffer = ebo;
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributeList VAOManager::addVertexAttributes(const VertexAttributeList& vert_attrs) {
|
||||||
|
VertexAttributeList curr_vas = _vert_attrs;
|
||||||
|
curr_vas.insert(curr_vas.end(), vert_attrs.begin(), vert_attrs.end());
|
||||||
|
try {
|
||||||
|
throw checkValid(curr_vas);
|
||||||
|
} catch (VAOError vaoe) {
|
||||||
|
if (vaoe.error) {
|
||||||
|
THROW_VAO_ERROR(vaoe.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_vert_attrs = curr_vas;
|
||||||
|
generate();
|
||||||
|
return _vert_attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributeList VAOManager::addVertexAttributes(VertexAttribute vert_attr) {
|
||||||
|
return addVertexAttributes({vert_attr});
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributeList VAOManager::generate() {
|
||||||
|
bind();
|
||||||
|
unsigned int num_attrs = _vert_attrs.size();
|
||||||
|
VertexAttribute* va;
|
||||||
|
for (int i = 0; i < num_attrs; ++i) {
|
||||||
|
va = &(_vert_attrs[i]);
|
||||||
|
_vert_buffers[va->ptr.buffer]->bind();
|
||||||
|
glVertexAttribPointer(
|
||||||
|
i,
|
||||||
|
va->GLSLSize,
|
||||||
|
va->GLSLType,
|
||||||
|
va->GLSLNormalization,
|
||||||
|
va->ptr.stride,
|
||||||
|
(void*)va->ptr.offset
|
||||||
|
);
|
||||||
|
glVertexAttribDivisor(i, va->ptr.divisor);
|
||||||
|
glEnableVertexAttribArray(i);
|
||||||
|
}
|
||||||
|
if (_elmt_buffer != nullptr) {
|
||||||
|
_elmt_buffer->bind();
|
||||||
|
}
|
||||||
|
unbind();
|
||||||
|
return _vert_attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributeList VAOManager::generate(VertexAttributeList vert_attrs) {
|
||||||
|
try {
|
||||||
|
throw checkValid(vert_attrs);
|
||||||
|
} catch (VAOError vaoe) {
|
||||||
|
if (vaoe.error) {
|
||||||
|
THROW_VAO_ERROR(vaoe.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_vert_attrs.swap(vert_attrs);
|
||||||
|
return generate();
|
||||||
|
}
|
||||||
|
|
||||||
|
VertexAttributeList VAOManager::changeLayout(unsigned int i, VertexAttributePointer vap) {
|
||||||
|
_vert_attrs[i].ptr = vap;
|
||||||
|
bind();
|
||||||
|
const VertexAttribute* va = &(_vert_attrs[i]);
|
||||||
|
glDisableVertexAttribArray(i);
|
||||||
|
_vert_buffers[va->ptr.buffer]->bind();
|
||||||
|
glVertexAttribPointer(
|
||||||
|
_id,
|
||||||
|
va->GLSLSize,
|
||||||
|
va->GLSLType,
|
||||||
|
va->GLSLNormalization,
|
||||||
|
va->ptr.stride,
|
||||||
|
(void*)va->ptr.offset
|
||||||
|
);
|
||||||
|
glVertexAttribDivisor(i, va->ptr.divisor);
|
||||||
|
glEnableVertexAttribArray(i);
|
||||||
|
unbind();
|
||||||
|
return _vert_attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
VAOError VAOManager::checkValid(const VertexAttributeList& vert_attrs) {
|
||||||
|
GLuint va_id;
|
||||||
|
unsigned int num_attrs = vert_attrs.size();
|
||||||
|
for (int i = 0; i < num_attrs; ++i) {
|
||||||
|
va_id = vert_attrs[i].ptr.buffer;
|
||||||
|
if (_vert_buffers.find(va_id) == _vert_buffers.cend()) {
|
||||||
|
return VAOError("Attempting to point to unknown VBO of id " + std::to_string(va_id) +
|
||||||
|
" at Vertex Attribute " + std::to_string(i) + ".");
|
||||||
|
}
|
||||||
|
if (vert_attrs[i].ptr.stride<0) {
|
||||||
|
return VAOError("Invalid stride value at Vertex Attribute " + std::to_string(i) + ".");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return VAOError(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace NB
|
||||||
@@ -1,225 +0,0 @@
|
|||||||
#include "VertexArray.hpp"
|
|
||||||
|
|
||||||
namespace nb {
|
|
||||||
|
|
||||||
using VAOErrorCodes = VAOError::Codes;
|
|
||||||
const std::string VAOError::type = "nb::VAOError";
|
|
||||||
const ErrorCodeMap VAOError::ErrorMessages = {
|
|
||||||
{ VAOErrorCodes::UNDEFINED, "Error" },
|
|
||||||
{ VAOErrorCodes::INVALID_ATTRIBUTE, "Targeting invalid attribute"},
|
|
||||||
{ VAOErrorCodes::INVALID_VAO, "Targeting invalid VAO" }
|
|
||||||
};
|
|
||||||
|
|
||||||
VAO::VAO(const VertexAttributePointerList& attr_ptrs) {
|
|
||||||
attributes(attr_ptrs);
|
|
||||||
}
|
|
||||||
|
|
||||||
VAO::VAO(VAO&& other) {
|
|
||||||
*this = std::move(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
VAO& VAO::operator=(VAO&& rhs) {
|
|
||||||
_id = rhs._id;
|
|
||||||
_attrs = rhs._attrs;
|
|
||||||
|
|
||||||
rhs._id = 0;
|
|
||||||
rhs._attrs = {};
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributePointerList VAO::attributes() const {
|
|
||||||
return _attrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributePointerList VAO::attributes(const VertexAttributePointerList& attrs_) {
|
|
||||||
declare();
|
|
||||||
disable();
|
|
||||||
_attrs = attrs_;
|
|
||||||
bind();
|
|
||||||
for (auto attr_ptr : attrs_) {
|
|
||||||
attr_ptr.buffer->bind();
|
|
||||||
GLuint idx = attr_ptr.index;
|
|
||||||
glVertexAttribPointer(
|
|
||||||
idx,
|
|
||||||
attr_ptr.attribute.GLSLSize,
|
|
||||||
attr_ptr.attribute.GLSLType,
|
|
||||||
attr_ptr.attribute.GLSLNormalization,
|
|
||||||
attr_ptr.attribute.layout.stride,
|
|
||||||
(void*)attr_ptr.attribute.layout.offset
|
|
||||||
);
|
|
||||||
glEnableVertexAttribArray(idx);
|
|
||||||
}
|
|
||||||
unbind();
|
|
||||||
return _attrs;
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributePointer VAO::attr(GLuint idx) const {
|
|
||||||
for (auto attr_ptr : _attrs) {
|
|
||||||
if (idx == attr_ptr.index) {
|
|
||||||
return attr_ptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
THROW(BufferError(Codes::INVALID_ATTRIBUTE));
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAO::enable() const {
|
|
||||||
if (_id) {
|
|
||||||
bind();
|
|
||||||
for(auto attr_ptr : _attrs) {
|
|
||||||
glEnableVertexAttribArray(attr_ptr.index);
|
|
||||||
}
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAO::enable(GLuint idx) const {
|
|
||||||
if(_id) {
|
|
||||||
bind();
|
|
||||||
glEnableVertexAttribArray(attr(idx).index);
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAO::disable() const {
|
|
||||||
if(_id) {
|
|
||||||
bind();
|
|
||||||
for(auto attr_ptr : _attrs) {
|
|
||||||
glDisableVertexAttribArray(attr_ptr.index);
|
|
||||||
}
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAO::disable(GLuint idx) const {
|
|
||||||
if(_id) {
|
|
||||||
bind();
|
|
||||||
glDisableVertexAttribArray(attr(idx).index);
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexGroup::VertexGroup(VertexGroup&& other) {
|
|
||||||
*this = std::move(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexGroup& VertexGroup::operator=(VertexGroup&& rhs) {
|
|
||||||
_vertex_data = rhs._vertex_data;
|
|
||||||
_elmt_buf = std::move(rhs._elmt_buf);
|
|
||||||
_vao = std::move(rhs._vao);
|
|
||||||
|
|
||||||
rhs._vertex_data = {};
|
|
||||||
rhs._elmt_buf = ElementBuffer<false>();
|
|
||||||
rhs._vao = VAO();
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t VertexGroup::buffer(VertexBuffer<false>&& buf_) {
|
|
||||||
_vertex_data.emplace_back(
|
|
||||||
std::make_shared<VertexBuffer<false>>(std::move(buf_))
|
|
||||||
);
|
|
||||||
VertexBuffer<false>* buf_ptr = _vertex_data.back().get();
|
|
||||||
VertexAttributePointerList attrs = _vao.attributes();
|
|
||||||
GLuint idx = attrs.size();
|
|
||||||
for (auto attr : buf_ptr->attributes) {
|
|
||||||
attrs.emplace_back(
|
|
||||||
VertexAttributePointer{attr, buf_ptr,idx}
|
|
||||||
);
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
_vao.attributes(attrs);
|
|
||||||
return _vertex_data.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexBuffer<false>& VertexGroup::buffer(size_t idx) {
|
|
||||||
return *_vertex_data[idx];
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::shared_ptr<VertexBuffer<false>>> VertexGroup::buffers() {
|
|
||||||
return _vertex_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t VertexGroup::buffers(std::vector<VertexBuffer<false>>&& bufs_) {
|
|
||||||
_vertex_data.clear();
|
|
||||||
for (auto& buf : bufs_) {
|
|
||||||
_vertex_data.emplace_back(
|
|
||||||
std::make_shared<VertexBuffer<false>>(std::move(buf))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
VertexAttributePointerList attr_ptrs;
|
|
||||||
GLuint idx = 0;
|
|
||||||
for (auto buf : _vertex_data) {
|
|
||||||
for (auto attr : buf->attributes) {
|
|
||||||
attr_ptrs.emplace_back(
|
|
||||||
VertexAttributePointer{attr, buf.get(), idx}
|
|
||||||
);
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_vao.attributes(attr_ptrs);
|
|
||||||
return _vertex_data.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexBuffer<false>&& VertexGroup::dropBuffer(size_t idx) {
|
|
||||||
if (idx >= _vertex_data.size()) {
|
|
||||||
THROW(BufferError(BufferError::Codes::INVALID_BUFFER));
|
|
||||||
}
|
|
||||||
std::vector<VertexBuffer<false>> tmp;
|
|
||||||
std::shared_ptr<VertexBuffer<false>> ret_ptr;
|
|
||||||
size_t i = 0;
|
|
||||||
for (auto buf : _vertex_data) {
|
|
||||||
if (i == idx) {
|
|
||||||
ret_ptr = std::make_shared<VertexBuffer<false>>(std::move(tmp[idx]));
|
|
||||||
} else {
|
|
||||||
tmp.emplace_back(std::move(*buf));
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
buffers(std::move(tmp));
|
|
||||||
return std::move(*ret_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexAttributePointer VertexGroup::attribute(size_t idx_) const {
|
|
||||||
return _vao.attr(idx_);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::enableAttr(GLuint idx) {
|
|
||||||
bind();
|
|
||||||
_vao.enable(idx);
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::disableAttr(GLuint idx) {
|
|
||||||
bind();
|
|
||||||
_vao.disable(idx);
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::disableBuffer(size_t idx) {
|
|
||||||
auto buf_ptr = _vertex_data[idx].get();
|
|
||||||
bind();
|
|
||||||
for (auto attr : _vao.attributes()) {
|
|
||||||
if (attr.buffer == buf_ptr) {
|
|
||||||
_vao.disable(attr.index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::enableBuffer(size_t idx) {
|
|
||||||
auto buf_ptr = _vertex_data[idx].get();
|
|
||||||
bind();
|
|
||||||
for (auto attr : _vao.attributes()) {
|
|
||||||
if (attr.buffer == buf_ptr) {
|
|
||||||
_vao.enable(attr.index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unbind();
|
|
||||||
}
|
|
||||||
|
|
||||||
void VertexGroup::enable() { _vao.enable(); }
|
|
||||||
|
|
||||||
void VertexGroup::disable() { _vao.disable(); }
|
|
||||||
|
|
||||||
} // namespace nb
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
cmake_minimum_required(VERSION 3.26.0)
|
|
||||||
|
|
||||||
if (NB_BUILD_TESTS)
|
|
||||||
enable_testing()
|
|
||||||
include(GoogleTest)
|
|
||||||
add_executable(TestWindow
|
|
||||||
./TestWindow.cpp
|
|
||||||
)
|
|
||||||
target_link_libraries(TestWindow
|
|
||||||
NBCore
|
|
||||||
NBGraphics
|
|
||||||
GTest::gtest_main
|
|
||||||
)
|
|
||||||
gtest_discover_tests(TestWindow)
|
|
||||||
|
|
||||||
endif()
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
#include <Buffers.hpp>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user