Minor cleanup and added some Type utils

This commit is contained in:
NaifBanana
2026-10-11 13:32:27 -05:00
parent e2ea8c5373
commit ab56a2cc95
6 changed files with 28 additions and 292 deletions
+2 -1
View File
@@ -5,4 +5,5 @@ CMakeCache.txt
docs/*
!docs/CMake*
!docs/Doxyfile.in
!.clangd
!.clangd
.glsld
+1 -1
View File
@@ -13,10 +13,10 @@ toAbsolutePath(NB_CORE_SOURCE
./src/Logger.cpp
./src/Processes.cpp
./src/StringUtils.cpp
./src/TypeTraits.cpp
./src/Utils.cpp
)
toAbsolutePath(NB_CORE_INCLUDE
./include/NBCore/ANSITerm.hpp
./include/NBCore/DataSink.hpp
./include/NBCore/Errors.hpp
./include/NBCore/ErrorsImpl.hpp
-214
View File
@@ -1,214 +0,0 @@
#pragma once
#ifndef _NB_ANSI_TERM
#define _NB_ANSI_TERM
#include <iostream>
#include <string>
#include <type_traits>
/*
----------- 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
+8 -75
View File
@@ -2,9 +2,7 @@
#ifndef _NB_TYPE_TRAITS
#define _NB_TYPE_TRAITS
#include <tuple>
#include <type_traits>
#include <vector>
namespace nb {
@@ -31,6 +29,14 @@ namespace detail {
};
} // namespace detail
enum TypeCodes {
UINT8, UINT16, UINT32, UINT64, INT8, INT16, INT32, INT64,
FLOAT16, FLOAT32, DOUBLE64, BOOL, NONE
};
template<typename T>
static constexpr TypeCodes GetTypeEnum = NONE;
struct NoneType{};
template<typename T, unsigned int Arg>
@@ -90,37 +96,6 @@ 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>
inline typename std::enable_if<N==sizeof...(Pack), void>::type
ForEach(std::tuple<Pack...>&, Func) {}
template<typename Func, std::size_t N=0, 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... Pack>
struct Find;
@@ -156,48 +131,6 @@ struct Find<T, U> {
>::value;
};
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
View File
@@ -2,7 +2,6 @@
#ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES
#include <exception>
#include <memory>
#include <unordered_map>
#include <vector>
+17
View File
@@ -0,0 +1,17 @@
#include <NBCore/TypeTraits.hpp>
namespace nb {
template<> constexpr TypeCodes GetTypeEnum<unsigned char> = TypeCodes::UINT8;
template<> constexpr TypeCodes GetTypeEnum<unsigned short> = TypeCodes::UINT16;
template<> constexpr TypeCodes GetTypeEnum<unsigned int> = TypeCodes::UINT32;
template<> constexpr TypeCodes GetTypeEnum<unsigned long int> = TypeCodes::UINT64;
template<> constexpr TypeCodes GetTypeEnum<char> = TypeCodes::INT8;
template<> constexpr TypeCodes GetTypeEnum<short> = TypeCodes::INT16;
template<> constexpr TypeCodes GetTypeEnum<int> = TypeCodes::INT32;
template<> constexpr TypeCodes GetTypeEnum<long> = TypeCodes::INT64;
template<> constexpr TypeCodes GetTypeEnum<float> = TypeCodes::FLOAT32;
template<> constexpr TypeCodes GetTypeEnum<double> = TypeCodes::DOUBLE64;
template<> constexpr TypeCodes GetTypeEnum<bool> = TypeCodes::BOOL;
} // namespace nb