Revert struct lol
This commit is contained in:
@@ -1,220 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_ANSI_TERM
|
||||
#define _NB_ANSI_TERM
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
/*
|
||||
----------- TECH DEBT ------------
|
||||
Idk wtf to do here. This was originally to allow me to print
|
||||
to terminal easily, but it's becoming too much of an investment.
|
||||
For the time being, drop it.
|
||||
|
||||
*/
|
||||
|
||||
namespace nb {
|
||||
|
||||
template <typename ST, typename U>
|
||||
struct SmartText {
|
||||
using Type = ST;
|
||||
using ValType = U;
|
||||
static constexpr bool HasChild = Type::HasChild;
|
||||
|
||||
SmartText() = default;
|
||||
};
|
||||
|
||||
template <typename Type, typename U>
|
||||
struct StyleFormatter : SmartText<StyleFormatter<Type, U>, U> {
|
||||
using Base = SmartText<StyleFormatter<Type, U>, U>;
|
||||
using typename Base::ValType;
|
||||
static constexpr bool HasChild = true;
|
||||
|
||||
StyleFormatter(const std::string& _fmt, const ValType& _val, const std::string& _closing) :
|
||||
fmt{_fmt}, val{_val}, closing{_closing} {}
|
||||
|
||||
std::string fmt;
|
||||
ValType val;
|
||||
std::string closing;
|
||||
};
|
||||
|
||||
template <typename Child>
|
||||
struct StyleFormatter<Child, void> : SmartText<StyleFormatter<Child, void>, void>{
|
||||
using Base = SmartText<StyleFormatter<Child, void>, void>;
|
||||
using Base::ValType;
|
||||
static constexpr bool HasChild = false;
|
||||
|
||||
StyleFormatter(const std::string& _fmt) : fmt{_fmt} {}
|
||||
|
||||
std::string fmt;
|
||||
};
|
||||
|
||||
template <typename U=void>
|
||||
struct ANSITextColor;
|
||||
|
||||
template <typename U>
|
||||
struct ANSITextColor : StyleFormatter<ANSITextColor<U>, U> {
|
||||
using Base = StyleFormatter<ANSITextColor, U>;
|
||||
using typename Base::ValType;
|
||||
using Base::HasChild;
|
||||
|
||||
template <typename = typename std::enable_if<HasChild, U>::type>
|
||||
ANSITextColor(const U& _val) : Base{"\x1b[39m", _val, "\x1b[39m"} {}
|
||||
template <typename = typename std::enable_if<!HasChild, U>>
|
||||
ANSITextColor() : Base{"\x1b[92m"} {}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct ANSITextColor<std::string> : StyleFormatter<ANSITextColor<std::string>, std::string> {
|
||||
using Base = StyleFormatter<ANSITextColor<std::string>, std::string>;
|
||||
static constexpr bool HasChild = Base::HasChild;
|
||||
|
||||
template <std::size_t N>
|
||||
ANSITextColor(char const(&_val) [N]) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
||||
ANSITextColor(const char* _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
||||
ANSITextColor(const std::string& _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {}
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
|
||||
template<typename Type, typename U>
|
||||
std::ostream& operator<<(std::ostream& stream, const nb::StyleFormatter<Type, U>& msg) {
|
||||
if (&stream == &std::cout) {
|
||||
stream << msg.fmt;
|
||||
if (msg.HasChild) {
|
||||
stream << msg.val << msg.closing;
|
||||
}
|
||||
} else {
|
||||
if (msg.HasChild) { stream << msg.val; }
|
||||
}
|
||||
stream << std::flush;
|
||||
return stream;
|
||||
}
|
||||
|
||||
/* struct CellBase {
|
||||
uint8_t width;
|
||||
uint8_t height;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Cell : SmartText<Cell<T>, T> {
|
||||
using Base = SmartText<Cell<T>, T>;
|
||||
using typename Base::ValType;
|
||||
using Base::HasChild;
|
||||
|
||||
Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=1)
|
||||
: val(_val), width(w), height(h) {}
|
||||
|
||||
ValType val;
|
||||
uint8_t width;
|
||||
|
||||
uint8_t height;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Cell<std::string> : SmartText<Cell<std::string>, std::string> {
|
||||
using Base = SmartText<Cell<std::string>, std::string>;
|
||||
using typename Base::ValType;
|
||||
using Base::HasChild;
|
||||
|
||||
template <std::size_t N>
|
||||
Cell( const(&_val) [N], const uint8_t& w, const uint8_t& h=0) : Cell(std::string(_val), w, h) {}
|
||||
Cell(char* _val, const uint8_t& w, const uint8_t& h=0) : Cell(std::string(_val), w, h) {}
|
||||
Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=0)
|
||||
: val(_val), width(w) {
|
||||
unsigned int strsize = _val.size();
|
||||
height = strsize / width;
|
||||
height += (strsize % width) ? 1 : 0;
|
||||
}
|
||||
|
||||
ValType val;
|
||||
uint8_t width;
|
||||
|
||||
uint8_t height;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::ostream& operator<<(std::ostream& stream, const Cell<T>& cll) {
|
||||
if (&stream == &std::cout) {
|
||||
stream << "\x1b[s" << cll.val << "\x1b[u\x1b[" << 1+cll.width << "C\x1b[" << 1+cll.height;
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ChangeCellHeight(int i, Cell<T>& cll, const unsigned int& h) {
|
||||
if (h < cll.height) { cll.height=h; }
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void GetTallestCell(int i, Cell<T>& cll, unsigned int& h) {
|
||||
if (cll.height > h) { h = cll.height; }
|
||||
}
|
||||
|
||||
template <typename... Pack>
|
||||
struct TableRow {
|
||||
using RowCells = std::tuple<Cell<Pack>...>;
|
||||
using CellWidths = std::array<int, sizeof...(Pack)>;
|
||||
using DelimiterArray = std::array<std::string, sizeof...(Pack)+1>;
|
||||
|
||||
TableRow(CellWidths&& widths_, DelimiterArray&& delims_, const Pack&... vals_)
|
||||
: widths(widths_), delimiters(delims_), topBorder("") {
|
||||
nb::ForEach(_cells, GetTallestCell, _height);
|
||||
}
|
||||
|
||||
static constexpr int num_columns = sizeof...(Pack);
|
||||
|
||||
int height() const { return height; }
|
||||
inline int height(const unsigned int newh) {
|
||||
nb::ForEach(_cells, ChangeCellHeight, newh);
|
||||
return (_height=newh);
|
||||
}
|
||||
|
||||
const CellWidths widths;
|
||||
const DelimiterArray delimiters;
|
||||
const std::string topBorder;
|
||||
const std::string bottomBorder;
|
||||
|
||||
protected:
|
||||
RowCells _cells;
|
||||
int _height;
|
||||
|
||||
};
|
||||
|
||||
template <typename... Pack>
|
||||
std::ostream& operator<<(std::ostream& stream, const TableRow<Pack...>& row) {
|
||||
|
||||
}
|
||||
|
||||
template<typename... Pack>
|
||||
class Table {
|
||||
public:
|
||||
Table(const Pack&... _vals) : row(std::array<int, sizeof...(Pack)>(10),
|
||||
std::array<std::string, sizeof...(Pack)>(""), _vals...) {}
|
||||
private:
|
||||
TableRow<Pack...> row;
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#ifdef _NB_TARGET_WINDOWS
|
||||
template<typename... Types>
|
||||
std::ostream& operator<<(std::ostream& stream, const nb::TableRow<Types...>& row) {
|
||||
using PosType = decltype(stream.tellp());
|
||||
constexpr int num_cells = sizeof...(Types);
|
||||
|
||||
int col_num = 0;
|
||||
int col_start = 0;
|
||||
|
||||
return stream;
|
||||
}
|
||||
#endif // _NB_TARGET_WINDOWS */
|
||||
|
||||
#endif // _NB_ANSI_TERM
|
||||
@@ -1,133 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_DATASINK
|
||||
#define _NB_DATASINK
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename DataType>
|
||||
class DataSink {
|
||||
public:
|
||||
DataSink(const DataSink&) = delete;
|
||||
DataSink(DataSink&&) = delete;
|
||||
DataSink& operator=(const DataSink&) = delete;
|
||||
|
||||
virtual bool isRunning() const noexcept {
|
||||
return _running;
|
||||
}
|
||||
virtual bool stop() noexcept = 0;
|
||||
virtual bool run() = 0;
|
||||
virtual bool in(const DataType&) = 0;
|
||||
|
||||
protected:
|
||||
DataSink() = default;
|
||||
|
||||
std::atomic<bool> _running;
|
||||
};
|
||||
|
||||
template<typename DataType, typename BufferType, typename ProcessorType>
|
||||
class BufferedDataProcessor : public DataSink<DataType> {
|
||||
using Base = DataSink<DataType>;
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
bool stop() noexcept { return type_ptr->stop(); }
|
||||
bool run() { return type_ptr->run(); }
|
||||
bool in(const DataType& val) { return type_ptr->in(val); }
|
||||
|
||||
protected:
|
||||
unsigned int count() const {
|
||||
return type_ptr->count();
|
||||
}
|
||||
void push(const DataType& val) {
|
||||
type_ptr->push(val);
|
||||
}
|
||||
DataType pop() {
|
||||
return type_ptr->pop();
|
||||
}
|
||||
void flush() {
|
||||
type_ptr->flush();
|
||||
}
|
||||
void clear() {
|
||||
type_ptr->clear();
|
||||
}
|
||||
bool process(const DataType& val) {
|
||||
return type_ptr->process(val);
|
||||
}
|
||||
|
||||
using Base::_running;
|
||||
|
||||
BufferType _buffer;
|
||||
|
||||
private:
|
||||
ProcessorType* const type_ptr = static_cast<ProcessorType*>(this);
|
||||
|
||||
};
|
||||
|
||||
template<typename DataType, typename ProcessorType>
|
||||
class MultithreadedDataProcessor
|
||||
: public BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType> {
|
||||
using Base = BufferedDataProcessor<DataType, ThreadsafeQueue<DataType>, ProcessorType>;
|
||||
public:
|
||||
~MultithreadedDataProcessor() { type_ptr->stop(); }
|
||||
|
||||
bool isRunning() const noexcept override {
|
||||
return this->_running && (_runningThread!=nullptr);
|
||||
}
|
||||
bool run() {
|
||||
if (!type_ptr->isRunning()) {
|
||||
this->_running = true;
|
||||
_runningThread = std::make_shared<std::thread>([&]{
|
||||
while(type_ptr->isRunning()) {
|
||||
flush();
|
||||
}
|
||||
});
|
||||
}
|
||||
return this->isRunning();
|
||||
}
|
||||
bool stop() noexcept override {
|
||||
if (type_ptr->isRunning()) {
|
||||
this->_running = false;
|
||||
_runningThread->join();
|
||||
_runningThread = nullptr;
|
||||
}
|
||||
return !type_ptr->isRunning();
|
||||
}
|
||||
|
||||
protected:
|
||||
using Base::Base;
|
||||
|
||||
unsigned int count() const {
|
||||
return this->_buffer.size();
|
||||
}
|
||||
void push(const DataType& val) {
|
||||
this->_buffer.push(val);
|
||||
};
|
||||
DataType pop() {
|
||||
std::shared_ptr<DataType> event;
|
||||
this->_buffer.pop(event);
|
||||
this->process(*event);
|
||||
return *event;
|
||||
}
|
||||
void flush() {
|
||||
while(type_ptr->count()) {
|
||||
pop();
|
||||
}
|
||||
}
|
||||
void clear() {
|
||||
this->_buffer.empty();
|
||||
}
|
||||
|
||||
std::shared_ptr<std::thread> _runningThread;
|
||||
|
||||
private:
|
||||
ProcessorType* type_ptr = static_cast<ProcessorType*>(this);
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_DATASINK
|
||||
@@ -1,139 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_ERROR
|
||||
#define _NB_ERROR
|
||||
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
|
||||
#ifndef THROW_WITH_INFO
|
||||
#ifdef CODE_ERROR_LOCATIONS
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW_WITH_INFO(type, ...) throw type(__VA_ARGS__)
|
||||
#endif // CODE_ERROR_LOCATIONS
|
||||
#endif // THROW_WITH_INFO
|
||||
|
||||
#ifndef THROW
|
||||
#ifdef LOGGING
|
||||
#define THROW_WTIH_INFO(type, ...) throw type(__VA_ARGS__, __LINE__, __FILE__)
|
||||
#else
|
||||
#define THROW(...) THROW_WITH_INFO(__VA_ARGS__)
|
||||
#endif // LOGGING
|
||||
#endif // THROW
|
||||
|
||||
namespace nb {
|
||||
|
||||
typedef std::unordered_map<unsigned int, const char*> ErrorCodeMap;
|
||||
|
||||
template<class ErrorType>
|
||||
class ErrorBase : public std::exception {
|
||||
public:
|
||||
ErrorBase(
|
||||
const unsigned int code,
|
||||
unsigned int line=0,
|
||||
std::string filename=""
|
||||
) noexcept : ErrorBase(
|
||||
code,
|
||||
ErrorBase<ErrorType>::lookup(code),
|
||||
line,
|
||||
filename
|
||||
) {}
|
||||
ErrorBase(
|
||||
const unsigned int code,
|
||||
const std::exception& trace,
|
||||
unsigned int line=0,
|
||||
std::string filename=""
|
||||
) noexcept : ErrorBase(
|
||||
code,
|
||||
ErrorBase<ErrorType>::lookup(code),
|
||||
trace,
|
||||
line,
|
||||
filename
|
||||
) {}
|
||||
|
||||
static std::string lookup(unsigned int);
|
||||
unsigned int code() const noexcept {
|
||||
return static_cast<const ErrorType*>(this)->_code;
|
||||
};
|
||||
virtual const char* what() const noexcept override final { return _msg.c_str(); };
|
||||
|
||||
protected:
|
||||
ErrorBase() = default;
|
||||
ErrorBase(
|
||||
const unsigned int code,
|
||||
std::string msg,
|
||||
unsigned int line=0,
|
||||
std::string filename=""
|
||||
) noexcept : _code{code}, _msg{""} {
|
||||
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::ErrorCodes>, "enum ErrorCodes must be a class member.");
|
||||
static_assert(std::is_same<std::underlying_type_t<typename ErrorType::ErrorCodes>, unsigned int>::value,
|
||||
"enum ErrorCodes 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."
|
||||
);
|
||||
|
||||
_msg += std::string(ErrorType::type);
|
||||
_msg += "[" + std::to_string(_code) + "]";
|
||||
if (line && filename.size()) {
|
||||
_msg += " in \'" + filename + "\':" + std::to_string(line);
|
||||
}
|
||||
_msg += "\n " + msg;
|
||||
}
|
||||
ErrorBase(
|
||||
const unsigned int code,
|
||||
std::string msg,
|
||||
const std::exception& trace,
|
||||
unsigned int line=0,
|
||||
std::string filename=""
|
||||
) noexcept : ErrorBase(code, msg, line, filename) {
|
||||
std::string what_msg(trace.what());
|
||||
std::string::size_type newline_pos=-1;
|
||||
while((newline_pos=what_msg.find("\n", newline_pos+1))!= std::string::npos) {
|
||||
what_msg.replace(newline_pos, 1, "\n ");
|
||||
}
|
||||
_msg += "\n Trace - " + what_msg;
|
||||
}
|
||||
|
||||
unsigned int _code;
|
||||
std::string _msg;
|
||||
};
|
||||
|
||||
class Error : public ErrorBase<Error> {
|
||||
public:
|
||||
enum ErrorCodes : unsigned int {
|
||||
GENERAL, UNDEFINED, BADERRORCODE
|
||||
};
|
||||
|
||||
using ErrorBase<Error>::ErrorBase;
|
||||
|
||||
friend ErrorBase<Error>;
|
||||
|
||||
protected:
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
|
||||
};
|
||||
|
||||
template<class ErrorType>
|
||||
std::string ErrorBase<ErrorType>::lookup(unsigned int code) {
|
||||
for (auto kv : ErrorType::ErrorMessages) {
|
||||
if (kv.first==code) {
|
||||
return std::string(kv.second);
|
||||
}
|
||||
}
|
||||
|
||||
throw Error(Error::ErrorCodes::BADERRORCODE);
|
||||
}
|
||||
|
||||
} // namespace nb
|
||||
|
||||
|
||||
#endif // _NB_ERROR
|
||||
@@ -1,143 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_LOGGER
|
||||
#define _NB_LOGGER
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <ostream>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "DataSink.hpp"
|
||||
#include "Processes.hpp"
|
||||
#include "ThreadSafeQueue.hpp"
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
typedef std::chrono::time_point<
|
||||
std::chrono::system_clock,
|
||||
std::chrono::nanoseconds
|
||||
> LoggerTimePoint;
|
||||
|
||||
struct LogEvent{
|
||||
const LoggerTimePoint time;
|
||||
const unsigned char lvl;
|
||||
const std::string msg;
|
||||
const std::thread::id tid;
|
||||
const uint64_t pid;
|
||||
};
|
||||
|
||||
typedef std::string (*LogProcessFunction)(const LoggerTimePoint&, const std::string&);
|
||||
typedef std::unordered_map<uint8_t, LogProcessFunction> LogProcessFunctionMap;
|
||||
|
||||
template<typename LogType, typename Logger, typename ST=std::ostream*>
|
||||
class LoggerBase
|
||||
: public MultithreadedDataProcessor<LogType, Logger>{
|
||||
using StreamType = ST;
|
||||
using LoggerType = Logger;
|
||||
using Base = MultithreadedDataProcessor<LogType, LoggerType>;
|
||||
public:
|
||||
bool run() {
|
||||
if (!type_ptr->isRunning()) {
|
||||
this->_running = true;
|
||||
this->_runningThread = std::make_shared<std::thread>([&]{
|
||||
while(type_ptr->isRunning()) {
|
||||
type_ptr->flush();
|
||||
}
|
||||
type_ptr->flush();
|
||||
});
|
||||
}
|
||||
return type_ptr->isRunning();
|
||||
}
|
||||
protected:
|
||||
LoggerBase() = default;
|
||||
|
||||
StreamType _ostream;
|
||||
|
||||
private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
};
|
||||
|
||||
template <typename LT>
|
||||
class DebugLogger : public LoggerBase<LogEvent, LT, std::vector<std::ostream*>>{
|
||||
using StreamType = std::vector<std::ostream*>;
|
||||
using LoggerType = LT;
|
||||
using Base = LoggerBase<LogEvent, LoggerType, StreamType>;
|
||||
public:
|
||||
|
||||
template<typename... ST>
|
||||
DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec<std::ostream, ST...>(streams...).vec) {}
|
||||
|
||||
~DebugLogger() { type_ptr->stop(); }
|
||||
|
||||
void log(const std::string& msg, const uint8_t& lvl=0xFF) {
|
||||
type_ptr->push(LogEvent{
|
||||
std::chrono::system_clock::now(),
|
||||
lvl,
|
||||
msg,
|
||||
std::this_thread::get_id(),
|
||||
GetPID(),
|
||||
});
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
void log(char const(&msg) [N], const uint8_t& lvl=0xFF) {
|
||||
type_ptr->log(std::string(msg), lvl);
|
||||
}
|
||||
|
||||
void log(const std::exception& err, const uint8_t& lvl=0xFF) {
|
||||
type_ptr->log(err.what(), lvl);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void warn(const U& val, const uint8_t& lvl=0x01) { type_ptr->log(val, lvl); }
|
||||
|
||||
template<typename U>
|
||||
void error(const U& val) { type_ptr->log(val, 0xFF); }
|
||||
|
||||
protected:
|
||||
std::vector<std::ostream*> _ostream;
|
||||
|
||||
private:
|
||||
LoggerType* type_ptr = static_cast<LoggerType*>(this);
|
||||
|
||||
};
|
||||
|
||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
using LoggerType = DefaultDebugLogger;
|
||||
using Base = DebugLogger<DefaultDebugLogger>;
|
||||
|
||||
template <typename... Ts>
|
||||
struct LogRow;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
~DefaultDebugLogger() { stop(); }
|
||||
|
||||
friend class BufferedDataProcessor<LogEvent, ThreadsafeQueue<LogEvent>, DefaultDebugLogger>;
|
||||
|
||||
template <typename STR, typename... Ts>
|
||||
friend STR& operator<<(STR&, const DefaultDebugLogger::LogRow<Ts...>&);
|
||||
|
||||
protected:
|
||||
using Base::_ostream;
|
||||
|
||||
bool process(const LogEvent& msg) {
|
||||
for (const auto os : this->_ostream) {
|
||||
*os << msg.lvl << "\t|\t" << msg.msg << "\n";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
extern DefaultDebugLogger logger;
|
||||
|
||||
// Taking Charge of Adult ADHD by Russell Barkley
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_LOGGER
|
||||
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_PROCESSES
|
||||
#define _NB_PROCESSES
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace nb {
|
||||
|
||||
uint64_t GetPID();
|
||||
|
||||
} // namespace nb
|
||||
|
||||
|
||||
#endif // _NB_PROCESSES
|
||||
@@ -1,123 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_UTIL_THREADSAFE_QUEUE
|
||||
#define _NB_UTIL_THREADSAFE_QUEUE
|
||||
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <type_traits>
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename T, bool isPoly>
|
||||
class ThreadsafeQueue_impl;
|
||||
|
||||
// Non-polymorphic
|
||||
template<typename T>
|
||||
class ThreadsafeQueue_impl<T, false> {
|
||||
public:
|
||||
bool empty() const {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _queue.empty();
|
||||
}
|
||||
unsigned int size() const {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _queue.size();
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void push(U&& val) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_queue.push(std::forward<U>(val));
|
||||
_cond.notify_one();
|
||||
}
|
||||
bool pop(std::shared_ptr<T>& dest) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
if (_queue.empty()) { return false; }
|
||||
dest = std::make_shared<T>(std::move(_queue.front()));
|
||||
_queue.pop();
|
||||
return true;
|
||||
}
|
||||
std::shared_ptr<T> popBlock() {
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
_cond.wait(lock, [&]{ return !_queue.empty(); });
|
||||
auto ret = std::make_shared<T>(std::move(_queue.front()));
|
||||
_queue.pop();
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected:
|
||||
ThreadsafeQueue_impl() {}
|
||||
|
||||
std::queue<T> _queue;
|
||||
std::condition_variable _cond;
|
||||
mutable std::mutex _mutex;
|
||||
|
||||
};
|
||||
|
||||
// Polymorphic
|
||||
template<typename T>
|
||||
class ThreadsafeQueue_impl<T, true> {
|
||||
public:
|
||||
bool empty() const {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _queue.empty();
|
||||
}
|
||||
unsigned int size() const {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _queue.size();
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
void push(U&& val) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::shared_ptr<T> newval = std::make_shared<T>(std::forward<U>(val));
|
||||
_queue.push(newval);
|
||||
_cond.notify_one();
|
||||
}
|
||||
bool pop(std::shared_ptr<T>& dest) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
if (_queue.empty()) { return false; }
|
||||
dest = std::move(_queue.front());
|
||||
_queue.pop();
|
||||
return true;
|
||||
}
|
||||
std::shared_ptr<T> popBlock() {
|
||||
std::unique_lock<std::mutex> lock(_mutex);
|
||||
_cond.wait(lock, [&]{ return !_queue.empty(); });
|
||||
auto ret = std::move(_queue.front());
|
||||
_queue.pop();
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected:
|
||||
ThreadsafeQueue_impl() {}
|
||||
|
||||
std::queue<std::shared_ptr<T>> _queue;
|
||||
std::condition_variable _cond;
|
||||
mutable std::mutex _mutex;
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class ThreadsafeQueue : public ThreadsafeQueue_impl<T, std::is_polymorphic<T>::value> {
|
||||
public:
|
||||
using Implementation = ThreadsafeQueue_impl<T, std::is_polymorphic<T>::value>;
|
||||
|
||||
ThreadsafeQueue() = default;
|
||||
~ThreadsafeQueue() = default;
|
||||
|
||||
ThreadsafeQueue(ThreadsafeQueue& rhs) = delete;
|
||||
ThreadsafeQueue& operator=(ThreadsafeQueue& rhs) = delete;
|
||||
ThreadsafeQueue(ThreadsafeQueue&& rhs) = delete;
|
||||
ThreadsafeQueue& operator=(ThreadsafeQueue&& rhs) = delete;
|
||||
|
||||
operator bool() const { return !Implementation::empty(); }
|
||||
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
|
||||
|
||||
#endif // _NB_UTIL_THREADSAFE_QUEUE
|
||||
@@ -1,75 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_TYPE_TRAITS
|
||||
#define _NB_TYPE_TRAITS
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<std::size_t N=0, typename Func, typename... Pack>
|
||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>&, Func) {}
|
||||
|
||||
template<std::size_t N=0, typename Func, typename... Pack>
|
||||
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>& tup, Func f) {
|
||||
f(N, std::get<N>(tup));
|
||||
ForEach<N+1, Func, Pack...>(tup, f);
|
||||
}
|
||||
|
||||
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
||||
inline typename std::enable_if<N==sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>&, Func, const Args&) {}
|
||||
|
||||
template<std::size_t N=0, typename Func, typename Args, typename... Pack>
|
||||
inline typename std::enable_if<N < sizeof...(Pack), void>::type
|
||||
ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
|
||||
f(N, std::get<N>(tup), arg);
|
||||
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
|
||||
}
|
||||
|
||||
template <typename T, typename Pack1, typename... Pack>
|
||||
struct RefPackToPtrVec {
|
||||
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
|
||||
: vec(_vec) {
|
||||
static_assert(std::is_base_of<T, Pack1>::value);
|
||||
vec.push_back(&p1);
|
||||
vec = RefPackToPtrVec<T, Pack...>(vec, packs...).vec;
|
||||
}
|
||||
|
||||
RefPackToPtrVec(Pack1& p1, Pack&... packs)
|
||||
: RefPackToPtrVec({}, p1, packs...) {}
|
||||
|
||||
std::vector<T*> vec;
|
||||
};
|
||||
|
||||
template <typename T, typename Pack>
|
||||
struct RefPackToPtrVec<T, Pack> {
|
||||
RefPackToPtrVec(std::vector<T*> _vec, Pack& p1)
|
||||
: vec(_vec) {
|
||||
static_assert(std::is_base_of<T, Pack>::value);
|
||||
vec.push_back(&p1);
|
||||
}
|
||||
|
||||
RefPackToPtrVec(Pack& p1)
|
||||
: RefPackToPtrVec({}, p1) {}
|
||||
|
||||
std::vector<T*> vec;
|
||||
};
|
||||
|
||||
template <typename T, typename... Pack>
|
||||
struct PackIsSameType;
|
||||
|
||||
template <typename T, typename PackType>
|
||||
struct PackIsSameType<T, PackType> {
|
||||
const bool value = std::is_base_of<T, PackType>::value;
|
||||
};
|
||||
|
||||
template <typename T, typename FirstPack, typename... Pack>
|
||||
struct PackIsSameType<T, FirstPack, Pack...> {
|
||||
const bool value = std::is_base_of<T, FirstPack>::value && PackIsSameType<T, Pack...>::value;
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_TYPE_TRAITS
|
||||
@@ -1,25 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_CORE_TYPES
|
||||
#define _NB_CORE_TYPES
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace nb {
|
||||
|
||||
template<typename T>
|
||||
T swapEndian(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
|
||||
Reference in New Issue
Block a user