WIP: Graphics Overhaul #2
@ -6,9 +6,11 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include "TypeTraits.hpp"
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#include "Logger.hpp"
|
||||||
#include "StringUtils.hpp"
|
#include "StringUtils.hpp"
|
||||||
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
@ -273,8 +275,38 @@ public:
|
|||||||
static const std::string type;
|
static const std::string type;
|
||||||
static const ErrorCodeMap ErrorMessages;
|
static const ErrorCodeMap ErrorMessages;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
} // namespace nb
|
} // 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
|
#endif // _NB_ERROR
|
||||||
@ -9,13 +9,15 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "DataSink.hpp"
|
#include "DataSink.hpp"
|
||||||
#include "Errors.hpp"
|
|
||||||
#include "Processes.hpp"
|
#include "Processes.hpp"
|
||||||
#include "ThreadSafeQueue.hpp"
|
#include "ThreadSafeQueue.hpp"
|
||||||
#include "TypeTraits.hpp"
|
#include "TypeTraits.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class ErrorBase;
|
||||||
|
|
||||||
typedef std::chrono::time_point<
|
typedef std::chrono::time_point<
|
||||||
std::chrono::system_clock,
|
std::chrono::system_clock,
|
||||||
std::chrono::nanoseconds
|
std::chrono::nanoseconds
|
||||||
@ -131,7 +133,10 @@ public:
|
|||||||
U val,
|
U val,
|
||||||
std::string file="",
|
std::string file="",
|
||||||
unsigned int line=0
|
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:
|
protected:
|
||||||
std::vector<std::ostream*> _ostream;
|
std::vector<std::ostream*> _ostream;
|
||||||
@ -167,103 +172,6 @@ extern DefaultDebugLogger logger;
|
|||||||
|
|
||||||
// Taking Charge of Adult ADHD by Russell Barkley
|
// 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
|
} // 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
|
#endif // _NB_LOGGER
|
||||||
@ -7,6 +7,8 @@
|
|||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
uint64_t GetPID();
|
uint64_t GetPID();
|
||||||
|
uint64_t getTID();
|
||||||
|
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#ifndef _NB_CORE_TYPES
|
#ifndef _NB_STRING_UTILS
|
||||||
#define _NB_CORE_TYPES
|
#define _NB_STRING_UTILS
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -76,4 +76,4 @@ std::string indent_strblock(
|
|||||||
);
|
);
|
||||||
|
|
||||||
} // namespace nb
|
} // namespace nb
|
||||||
#endif // _NB_CORE_TYPES
|
#endif // _NB_STRING_UTILS
|
||||||
@ -2,23 +2,79 @@
|
|||||||
#ifndef _NB_CORE_TYPES
|
#ifndef _NB_CORE_TYPES
|
||||||
#define _NB_CORE_TYPES
|
#define _NB_CORE_TYPES
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "Errors.hpp"
|
||||||
|
|
||||||
namespace nb {
|
namespace nb {
|
||||||
|
|
||||||
/* template<typename T>
|
using ByteVector = std::vector<uint8_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) {
|
class ObjectManagerError : public Error<ObjectManagerError> {
|
||||||
memcpy(retLoc+i, valLoc+(size-i-1), 1);
|
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
|
} // namespace nb
|
||||||
#endif // _NB_CORE_TYPES
|
#endif // _NB_CORE_TYPES
|
||||||
@ -33,8 +33,6 @@ static bool RUN_LOGGER(nb::DefaultDebugLogger& log) {
|
|||||||
return log.run();
|
return log.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
static const bool LOGGER_RUNNING = RUN_LOGGER(logger);
|
||||||
|
|
||||||
} // 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 +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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user