diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b40723..7e21f8c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,10 @@ project(NBEngine VERSION 0.1.0 LANGUAGES C CXX) cmake_policy(SET CMP0135 NEW) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + if(CMAKE_BUILD_TYPE STREQUAL "Release") message(STATUS "Targeting Release build") elseif(CMAKE_BUILD_TYPE STREQUAL "Debug") diff --git a/engine/NBCore/ANSITerm.hpp b/engine/NBCore/ANSITerm.hpp index 448a5e8..b74ee7b 100644 --- a/engine/NBCore/ANSITerm.hpp +++ b/engine/NBCore/ANSITerm.hpp @@ -12,6 +12,14 @@ #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 @@ -19,15 +27,19 @@ struct SmartText { using Type = ST; using ValType = U; static constexpr bool HasChild = Type::HasChild; + + SmartText() = default; }; template struct StyleFormatter : SmartText, U> { - using Base = SmartText>; - using Base::Type; - using Base::ValType; + using Base = SmartText, 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; @@ -35,11 +47,12 @@ struct StyleFormatter : SmartText, U> { template struct StyleFormatter : SmartText, void>{ - using Base = SmartText; - using Base::Type; + using Base = SmartText, void>; using Base::ValType; static constexpr bool HasChild = false; + StyleFormatter(const std::string& _fmt) : fmt{_fmt} {} + std::string fmt; }; @@ -49,7 +62,7 @@ struct ANSITextColor; template struct ANSITextColor : StyleFormatter, U> { using Base = StyleFormatter; - using Base::ValType + using typename Base::ValType; using Base::HasChild; template ::type> @@ -69,7 +82,23 @@ struct ANSITextColor : StyleFormatter, s ANSITextColor(const std::string& _val) : Base{"\x1b[92m", _val, "\x1b[39m"} {} }; -struct CellBase { +} // namespace nb + +template +std::ostream& operator<<(std::ostream& stream, const nb::StyleFormatter& 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; }; @@ -77,13 +106,13 @@ struct CellBase { template struct Cell : SmartText, T> { using Base = SmartText, T>; - using Base::ValType + using typename Base::ValType; using Base::HasChild; - Cell(const Type& _val, const uint8_t& w, const uint8_t& h=1) + Cell(const ValType& _val, const uint8_t& w, const uint8_t& h=1) : val(_val), width(w), height(h) {} - Type val; + ValType val; uint8_t width; uint8_t height; @@ -92,7 +121,7 @@ struct Cell : SmartText, T> { template <> struct Cell : SmartText, std::string> { using Base = SmartText, std::string>; - using Base::ValType + using typename Base::ValType; using Base::HasChild; template @@ -113,7 +142,10 @@ struct Cell : SmartText, std::string> { template std::ostream& operator<<(std::ostream& stream, const Cell& cll) { - + if (&stream == &std::cout) { + stream << "\x1b[s" << cll.val << "\x1b[u\x1b[" << 1+cll.width << "C\x1b[" << 1+cll.height; + } + return stream; } template @@ -133,7 +165,7 @@ struct TableRow { using DelimiterArray = std::array; TableRow(CellWidths&& widths_, DelimiterArray&& delims_, const Pack&... vals_) - : _widths(widths_), _delimiters(delims_) { + : widths(widths_), delimiters(delims_), topBorder("") { nb::ForEach(_cells, GetTallestCell, _height); } @@ -144,15 +176,23 @@ struct TableRow { nb::ForEach(_cells, ChangeCellHeight, newh); return (_height=newh); } + + const CellWidths widths; + const DelimiterArray delimiters; + const std::string topBorder; + const std::string bottomBorder; protected: - const CellWidths _widths; - const DelimiterArray _delimiters; RowCells _cells; int _height; }; +template +std::ostream& operator<<(std::ostream& stream, const TableRow& row) { + +} + template class Table { public: @@ -164,20 +204,6 @@ private: } // namespace nb -template -std::ostream& operator<<(std::ostream& stream, const nb::StyleFormatter& 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; -} - #ifdef _NB_TARGET_WINDOWS template std::ostream& operator<<(std::ostream& stream, const nb::TableRow& row) { @@ -189,6 +215,6 @@ std::ostream& operator<<(std::ostream& stream, const nb::TableRow& row return stream; } -#endif // _NB_TARGET_WINDOWS +#endif // _NB_TARGET_WINDOWS */ #endif // _NB_ANSI_TERM \ No newline at end of file diff --git a/engine/NBCore/CMakeLists.txt b/engine/NBCore/CMakeLists.txt index 5d329b2..d2d5e75 100644 --- a/engine/NBCore/CMakeLists.txt +++ b/engine/NBCore/CMakeLists.txt @@ -3,6 +3,7 @@ include_directories(./.) add_library(NBCore ./src/Errors.cpp ./src/Processes.cpp + ./src/Logger.cpp ) if (NB_BUILD_TESTS) diff --git a/engine/NBCore/Logger.hpp b/engine/NBCore/Logger.hpp index 8edef28..e767eea 100644 --- a/engine/NBCore/Logger.hpp +++ b/engine/NBCore/Logger.hpp @@ -8,13 +8,13 @@ #include #include #include +#include #include -#include "ANSITerm.hpp" #include "DataSink.hpp" #include "Processes.hpp" #include "ThreadSafeQueue.hpp" - +#include "TypeTraits.hpp" namespace nb { @@ -62,48 +62,6 @@ private: LoggerType* type_ptr = static_cast(this); }; -template -struct PackIsType; - -template -struct PackIsType { - const bool value = std::is_base_of::value; -}; - -template -struct PackIsType { - const bool value = std::is_base_of::value && PackIsType::value; -}; - -template -struct StreamRefToPtrVec { - StreamRefToPtrVec(std::vector _vec, Pack1& p1, Pack&... packs) - : vec(_vec) { - static_assert(std::is_base_of::value); - vec.push_back(&p1); - vec = StreamRefToPtrVec(vec, packs...).vec; - } - - StreamRefToPtrVec(Pack1& p1, Pack&... packs) - : StreamRefToPtrVec({}, p1, packs...) {} - - std::vector vec; -}; - -template -struct StreamRefToPtrVec { - StreamRefToPtrVec(std::vector _vec, Pack& p1) - : vec(_vec) { - static_assert(std::is_base_of::value); - vec.push_back(&p1); - } - - StreamRefToPtrVec(Pack& p1) - : StreamRefToPtrVec({}, p1) {} - - std::vector vec; -}; - template class DebugLogger : public LoggerBase>{ using StreamType = std::vector; @@ -112,7 +70,7 @@ class DebugLogger : public LoggerBase>{ public: template - DebugLogger(ST&... streams) : _ostream(StreamRefToPtrVec(streams...).vec) {} + DebugLogger(ST&... streams) : _ostream(nb::RefPackToPtrVec(streams...).vec) {} ~DebugLogger() { type_ptr->stop(); } @@ -152,12 +110,9 @@ private: class DefaultDebugLogger : public DebugLogger { using LoggerType = DefaultDebugLogger; using Base = DebugLogger; - template - struct Field { - Field(int w, U v) : width{w}, val(v) {} - int width; - U val; - }; + + template + struct LogRow; public: using Base::Base; @@ -166,36 +121,23 @@ public: friend class BufferedDataProcessor, DefaultDebugLogger>; - template - friend STR& operator<<(STR&, const DefaultDebugLogger::Field&); + template + friend STR& operator<<(STR&, const DefaultDebugLogger::LogRow&); protected: using Base::_ostream; bool process(const LogEvent& msg) { for (const auto os : this->_ostream) { - (*os) << "[ " << ANSITextColor("hey") << " | " - << Field(2, msg.tid) << " | " << Field(10, msg.msg) << " ]\n"; + *os << msg.lvl << "\t|\t" << msg.msg << "\n"; } return true; } -}; +}; -template -STR& operator<<(STR& stream, const DefaultDebugLogger::Field& field) { - stream << std::setw(field.width) << field.val; - return stream; -} +extern DefaultDebugLogger logger; -/* class BasicLogger : public LoggerBase { -public: - using Base = LoggerBase; - BasicLogger(std::ostream& stream) : Base(stream) {} - - virtual void process(const LogEvent& event) { - *_ostreams[0] << event.msg << "\n"; - } -}; */ +// Taking Charge of Adult ADHD by Russell Barkley } // namespace nb #endif // _NB_LOGGER \ No newline at end of file diff --git a/engine/NBCore/TypeTraits.hpp b/engine/NBCore/TypeTraits.hpp index a049183..5226e94 100644 --- a/engine/NBCore/TypeTraits.hpp +++ b/engine/NBCore/TypeTraits.hpp @@ -28,6 +28,48 @@ ForEach(std::tuple& tup, Func f, const Args& arg) { ForEach(tup, f, arg); } +template +struct RefPackToPtrVec { + RefPackToPtrVec(std::vector _vec, Pack1& p1, Pack&... packs) + : vec(_vec) { + static_assert(std::is_base_of::value); + vec.push_back(&p1); + vec = RefPackToPtrVec(vec, packs...).vec; + } + + RefPackToPtrVec(Pack1& p1, Pack&... packs) + : RefPackToPtrVec({}, p1, packs...) {} + + std::vector vec; +}; + +template +struct RefPackToPtrVec { + RefPackToPtrVec(std::vector _vec, Pack& p1) + : vec(_vec) { + static_assert(std::is_base_of::value); + vec.push_back(&p1); + } + + RefPackToPtrVec(Pack& p1) + : RefPackToPtrVec({}, p1) {} + + std::vector vec; +}; + +template +struct PackIsSameType; + +template +struct PackIsSameType { + const bool value = std::is_base_of::value; +}; + +template +struct PackIsSameType { + const bool value = std::is_base_of::value && PackIsSameType::value; +}; + } // namespace nb #endif // _NB_TYPE_TRAITS \ No newline at end of file diff --git a/engine/NBCore/src/Logger.cpp b/engine/NBCore/src/Logger.cpp new file mode 100644 index 0000000..dbeea5a --- /dev/null +++ b/engine/NBCore/src/Logger.cpp @@ -0,0 +1,8 @@ +#include "Logger.hpp" + +namespace nb { + +nb::DefaultDebugLogger logger; +//logger.run(); + +} // namespace nb \ No newline at end of file diff --git a/engine/NBCore/tests/testErrors.cpp b/engine/NBCore/tests/testErrors.cpp index 73a4987..9a68c74 100644 --- a/engine/NBCore/tests/testErrors.cpp +++ b/engine/NBCore/tests/testErrors.cpp @@ -32,13 +32,8 @@ TEST(ErrorTest, Test) { EXPECT_EQ(1, 1); std::stringstream sstream; - nb::DefaultDebugLogger logr(std::cout, sstream); - ASSERT_TRUE(logr.run()); - ASSERT_TRUE(logr.isRunning()); - while (!logr.isRunning()){ - 1+1; - } - logr.log("Hey!"); - logr.stop(); + + ASSERT_TRUE(nb::logger.isRunning()); + nb::logger.log("Hey!"); std::cout << "STRINGSTREAM:\n" << sstream.str(); } \ No newline at end of file