Compare commits

..

11 Commits

Author SHA1 Message Date
NaifBanana
cff7164d27 Mild cleanup 2026-06-11 04:24:06 -05:00
NaifBanana
13ef940b2d Formatting tweaks 2026-06-11 04:24:06 -05:00
NaifBanana
58121bdf28 Got logging and errors basically all working the way I want 2026-06-11 04:24:06 -05:00
NaifBanana
0f62aecfa3 Error logging almost working! 2026-06-11 04:24:06 -05:00
NaifBanana
40cf079e9d Added type traits and string utils 2026-06-11 04:24:06 -05:00
NaifBanana
fb695798e7 nb::Error objects now working 2026-06-11 04:24:06 -05:00
NaifBanana
e1c456bfe4 I mean it compiled 2026-06-11 04:24:06 -05:00
NaifBanana
e23e062e58 Man im so tired This is wrong but I can taste it, im close 2026-06-11 04:24:06 -05:00
NaifBanana
fd7d36e4be Added more Util tests and str-wstr related changes to util functions 2026-06-11 04:24:06 -05:00
NaifBanana
0b473c0f8a Made errors mostly work as hoped 2026-06-11 04:24:06 -05:00
NaifBanana
d448b6724a README 2026-05-18 01:32:29 -05:00
9 changed files with 153 additions and 125 deletions

View File

@ -1,7 +1,5 @@
# NBEngine
My own GUI (and related stuff needed for GUI programs) engine. I suck at coding. Do not judge.
## Current Implemented Libraries
These are subject to (hopefully) grow in size as more stuff gets added.
@ -11,11 +9,12 @@ These are subject to (hopefully) grow in size as more stuff gets added.
## Dependencies and Foundations
While the engine is intended to be a close-to-ground-up project, there's no point in reinventing the spokes that
make up a wheel. This project utilizes and depends on (expected to change and grow):
For Code:
* GLFW
* Glad
Additionally, it's important to note that right now this project is on Windows only. I wanna work on this in Linux soon
("soon" whatever that means. Maybe in like 5 years).
For Repo:
* gTest
* doxygen

View File

@ -6,9 +6,11 @@
#include <memory>
#include <string>
#include <type_traits>
#include "TypeTraits.hpp"
#include <unordered_map>
#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

View File

@ -9,13 +9,15 @@
#include <vector>
#include "DataSink.hpp"
#include "Errors.hpp"
#include "Processes.hpp"
#include "ThreadSafeQueue.hpp"
#include "TypeTraits.hpp"
namespace nb {
template <typename T>
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<LoggerType*>(this)->log(val, 0xFF, file, line); }
) {
static_cast<LoggerType*>(this)->log(val, 0xFF, file, line);
static_cast<LoggerType*>(this)->flush();
}
protected:
std::vector<std::ostream*> _ostream;
@ -167,103 +172,6 @@ extern DefaultDebugLogger 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

View File

@ -7,6 +7,8 @@
namespace nb {
uint64_t GetPID();
uint64_t getTID();
} // namespace nb

View File

@ -1,6 +1,6 @@
#pragma once
#ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES
#ifndef _NB_STRING_UTILS
#define _NB_STRING_UTILS
#include <iostream>
#include <string>
@ -76,4 +76,4 @@ std::string indent_strblock(
);
} // namespace nb
#endif // _NB_CORE_TYPES
#endif // _NB_STRING_UTILS

View File

@ -2,23 +2,79 @@
#ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES
#include <mutex>
#include <vector>
#include "Errors.hpp"
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);
using ByteVector = std::vector<uint8_t>;
for (int i = 0; i < size; ++i) {
memcpy(retLoc+i, valLoc+(size-i-1), 1);
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>
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);
}
return ret;
} */
friend ThreadsafeObjectLock<T>;
// using ByteVector = std::vector<uint8_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

View File

@ -33,8 +33,6 @@ static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
return log.run();
}
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
} // namespace nb

View File

@ -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

View File

@ -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