From cff7164d27b9a855bd36a6fc351051ade2a916ca Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Thu, 11 Jun 2026 04:23:29 -0500 Subject: [PATCH] Mild cleanup --- engine/NBCore/Errors.hpp | 38 +++++++++++- engine/NBCore/Logger.hpp | 106 +++----------------------------- engine/NBCore/Processes.hpp | 2 + engine/NBCore/StringUtils.hpp | 6 +- engine/NBCore/Utils.hpp | 78 +++++++++++++++++++---- engine/NBCore/src/Logger.cpp | 2 - engine/NBCore/src/Processes.cpp | 9 ++- engine/NBCore/src/Utils.cpp | 26 ++++++++ 8 files changed, 148 insertions(+), 119 deletions(-) diff --git a/engine/NBCore/Errors.hpp b/engine/NBCore/Errors.hpp index 2e38434..89cc13b 100644 --- a/engine/NBCore/Errors.hpp +++ b/engine/NBCore/Errors.hpp @@ -6,9 +6,11 @@ #include #include #include -#include "TypeTraits.hpp" #include + +#include "Logger.hpp" #include "StringUtils.hpp" +#include "TypeTraits.hpp" namespace nb { @@ -273,8 +275,38 @@ public: static const std::string type; static const ErrorCodeMap ErrorMessages; }; - - } // 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_ERROR \ No newline at end of file diff --git a/engine/NBCore/Logger.hpp b/engine/NBCore/Logger.hpp index e0f1f19..2d64198 100644 --- a/engine/NBCore/Logger.hpp +++ b/engine/NBCore/Logger.hpp @@ -9,13 +9,15 @@ #include #include "DataSink.hpp" -#include "Errors.hpp" #include "Processes.hpp" #include "ThreadSafeQueue.hpp" #include "TypeTraits.hpp" namespace nb { +template +class ErrorBase; + typedef std::chrono::time_point< std::chrono::system_clock, std::chrono::nanoseconds @@ -131,7 +133,10 @@ public: U val, std::string file="", unsigned int line=0 - ) { static_cast(this)->log(val, 0xFF, file, line); } + ) { + static_cast(this)->log(val, 0xFF, file, line); + static_cast(this)->flush(); + } protected: std::vector _ostream; @@ -167,103 +172,6 @@ extern DefaultDebugLogger logger; // Taking Charge of Adult ADHD by Russell Barkley -/* template -struct NB_DEFAULT_LOGGER_THROW; - -template<> -struct NB_DEFAULT_LOGGER_THROW { - template - NB_DEFAULT_LOGGER_THROW(T arg) { - #ifdef _NB_AUTOLOG - logger.error(arg); - #endif // _NB_AUTLOG - throw arg; - } -}; - -template -struct NB_DEFAULT_LOGGER_THROW { - template - NB_DEFAULT_LOGGER_THROW(Args&&... args) { - std::shared_ptr error_ptr; - #ifdef _NB_AUTOLOG - #ifdef _NB_CODE_ERROR_LOCATIONS - error_ptr = std::make_shared(args..., __LINE__, __FILE__); - #else - error_ptr = std::make_shared(args...); - #endif // _NB_CODE_ERROR_LOCATIONS - logger.error(*error_ptr); - #endif // _NB_AUTLOG - throw *error_ptr; - } -}; */ - -/* template -void NB_DEFAULT_LOGGER_THROW(Args&& ... args) { - std::shared_ptr error_ptr; - #ifdef _NB_AUTOLOG - #ifdef _NB_CODE_ERROR_LOCATIONS - error_ptr = std::make_shared(args..., __LINE__, __FILE__); - #else - error_ptr = std::make_shared(args...); - #endif // _NB_CODE_ERROR_LOCATIONS - logger.error(*error_ptr); - #endif // _NB_AUTLOG - throw *error_ptr; -} - -template -void NB_DEFAULT_LOGGER_THROW(const Arg& arg) { - #ifdef _NB_AUTOLOG - logger.error(arg); - #endif // _NB_AUTLOG - throw arg; -} */ - -template -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 \ No newline at end of file diff --git a/engine/NBCore/Processes.hpp b/engine/NBCore/Processes.hpp index 87ee029..87c16ee 100644 --- a/engine/NBCore/Processes.hpp +++ b/engine/NBCore/Processes.hpp @@ -7,6 +7,8 @@ namespace nb { uint64_t GetPID(); +uint64_t getTID(); + } // namespace nb diff --git a/engine/NBCore/StringUtils.hpp b/engine/NBCore/StringUtils.hpp index 9b709e1..b8d9778 100644 --- a/engine/NBCore/StringUtils.hpp +++ b/engine/NBCore/StringUtils.hpp @@ -1,6 +1,6 @@ #pragma once -#ifndef _NB_CORE_TYPES -#define _NB_CORE_TYPES +#ifndef _NB_STRING_UTILS +#define _NB_STRING_UTILS #include #include @@ -76,4 +76,4 @@ std::string indent_strblock( ); } // namespace nb -#endif // _NB_CORE_TYPES \ No newline at end of file +#endif // _NB_STRING_UTILS \ No newline at end of file diff --git a/engine/NBCore/Utils.hpp b/engine/NBCore/Utils.hpp index a8d47a5..b7a477d 100644 --- a/engine/NBCore/Utils.hpp +++ b/engine/NBCore/Utils.hpp @@ -2,23 +2,79 @@ #ifndef _NB_CORE_TYPES #define _NB_CORE_TYPES +#include +#include + +#include "Errors.hpp" + namespace nb { -/* template -T swap_endian(const T& val) { - T ret; - const int size = sizeof(T); - auto retLoc = static_cast(&ret); - auto valLoc = static_cast(&val); +using ByteVector = std::vector; - for (int i = 0; i < size; ++i) { - memcpy(retLoc+i, valLoc+(size-i-1), 1); +class ObjectManagerError : public Error { + using Base = Error; + + 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 +class ThreadsafeObjectLock; + +template +class ThreadsafeObject { + using Codes = ObjectManagerError::Codes; + + public: + ThreadsafeObject(T&& obj) + : _obj(std::make_shared(obj)) {} + + ThreadsafeObjectLock lock() { + return ThreadsafeObjectLock(this); } - return ret; -} */ + friend ThreadsafeObjectLock; -// using ByteVector = std::vector; + protected: + std::shared_ptr _obj; + mutable std::recursive_mutex _mutex; + +}; + +template +class ThreadsafeObjectLock { + public: + ThreadsafeObjectLock(const ThreadsafeObjectLock&) = delete; + ThreadsafeObjectLock operator=(const ThreadsafeObjectLock&) = delete; + ~ThreadsafeObjectLock() { + _manager->_mutex.unlock(); + } + + T* operator->() { + return _manager->_obj.get(); + } + + friend ThreadsafeObject; + + protected: + ThreadsafeObjectLock( + ThreadsafeObject* const manager_ + ) : _manager(manager_) { + if (!_manager) { + using Codes = ObjectManagerError::Codes; + THROW(ObjectManagerError(Codes::NO_MANAGER)); + } + _manager->_mutex.lock(); + } + ThreadsafeObject* const _manager; +}; } // namespace nb #endif // _NB_CORE_TYPES \ No newline at end of file diff --git a/engine/NBCore/src/Logger.cpp b/engine/NBCore/src/Logger.cpp index f2d0c8e..8af8796 100644 --- a/engine/NBCore/src/Logger.cpp +++ b/engine/NBCore/src/Logger.cpp @@ -33,8 +33,6 @@ static bool RUN_LOGGER(nb::DefaultDebugLogger& log) { return log.run(); } - - static const bool LOGGER_RUNNING = RUN_LOGGER(logger); } // namespace nb \ No newline at end of file diff --git a/engine/NBCore/src/Processes.cpp b/engine/NBCore/src/Processes.cpp index 744d96d..f0aec20 100644 --- a/engine/NBCore/src/Processes.cpp +++ b/engine/NBCore/src/Processes.cpp @@ -15,8 +15,15 @@ uint64_t GetPID() { return GetCurrentProcessId(); } #endif // _NB_TARGET_WINDOWS - #ifdef _NB_TARGET_LINUX #endif // _NB_TARGET_LINUX +#ifdef _NB_TARGET_WINDOWS +uint64_t GetTID() { + return GetCurrentThreadId(); +} +#endif // _NB_TARGET_WINDOWS +#ifdef _NB_TARGET_LINUX +#endif // _NB_TARGET_LINUX + } // namespace nb \ No newline at end of file diff --git a/engine/NBCore/src/Utils.cpp b/engine/NBCore/src/Utils.cpp index 8b13789..39fc680 100644 --- a/engine/NBCore/src/Utils.cpp +++ b/engine/NBCore/src/Utils.cpp @@ -1 +1,27 @@ +#include "Utils.hpp" +namespace nb { + +using ObjectManagerCodes = ObjectManagerError::Codes; +const std::string ObjectManagerError::type = "nb::ObjectManagerError"; +const ErrorCodeMap ObjectManagerError::ErrorMessages({ + {ObjectManagerCodes::UNDEFINED, "Error"}, + { + ObjectManagerCodes::NO_MANAGER, + "Attempting to create object lock without lock manager" + }, + { + ObjectManagerCodes::MANAGER_MISMATCH, + "Attempting to delete object lock from mismatched lock manager" + }, + { + ObjectManagerCodes::LOCK_OVERFLOW, + "Too many object locks allocated" + }, + { + ObjectManagerCodes::BAD_THREAD, + "Attempting operation from a bad thread" + } +}); + +}; // namespace nb